initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / PlatformCanvas.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "PlatformCanvas.h"
29
30 #include "GraphicsContext.h"
31
32 #if USE(SKIA)
33 #include "NativeImageSkia.h"
34 #include "PlatformContextSkia.h"
35 #include "SkColorPriv.h"
36 #include "skia/ext/platform_canvas.h"
37 #elif USE(CG)
38 #include <CoreGraphics/CGBitmapContext.h>
39 #endif
40
41 namespace WebCore {
42
43 PlatformCanvas::PlatformCanvas()
44 {
45 }
46
47 PlatformCanvas::~PlatformCanvas()
48 {
49 }
50
51 void PlatformCanvas::resize(const IntSize& size)
52 {
53     if (m_size == size)
54         return;
55     m_size = size;
56 #if USE(SKIA)
57     m_skiaCanvas = adoptPtr(skia::CreateBitmapCanvas(size.width(), size.height(), false));
58 #elif USE(CG)
59     size_t bufferSize = size.width() * size.height() * 4;
60     m_pixelData = adoptArrayPtr(new uint8_t[bufferSize]);
61     memset(m_pixelData.get(), 0, bufferSize);
62 #endif
63 }
64
65 PlatformCanvas::AutoLocker::AutoLocker(PlatformCanvas* canvas)
66     : m_canvas(canvas)
67     , m_pixels(0)
68 {
69 #if USE(SKIA)
70     if (m_canvas->m_skiaCanvas) {
71         m_bitmap = &m_canvas->m_skiaCanvas->getDevice()->accessBitmap(false);
72         m_bitmap->lockPixels();
73
74         if (m_bitmap->config() == SkBitmap::kARGB_8888_Config)
75             m_pixels = static_cast<uint8_t*>(m_bitmap->getPixels());
76     } else
77         m_bitmap = 0;
78 #elif USE(CG)
79     if (canvas->m_pixelData)
80         m_pixels = &canvas->m_pixelData[0];
81 #endif
82 }
83
84 PlatformCanvas::AutoLocker::~AutoLocker()
85 {
86 #if USE(SKIA)
87     if (m_bitmap)
88         m_bitmap->unlockPixels();
89 #endif
90 }
91
92 PlatformCanvas::Painter::Painter(PlatformCanvas* canvas, PlatformCanvas::Painter::TextOption option)
93 {
94 #if USE(SKIA)
95     m_skiaContext = adoptPtr(new PlatformContextSkia(canvas->m_skiaCanvas.get()));
96
97     m_skiaContext->setDrawingToImageBuffer(option == GrayscaleText);
98
99     m_context = adoptPtr(new GraphicsContext(reinterpret_cast<PlatformGraphicsContext*>(m_skiaContext.get())));
100 #elif USE(CG)
101
102     m_colorSpace = CGColorSpaceCreateDeviceRGB();
103     size_t rowBytes = canvas->size().width() * 4;
104     m_contextCG = CGBitmapContextCreate(canvas->m_pixelData.get(),
105                                         canvas->size().width(), canvas->size().height(), 8, rowBytes,
106                                         m_colorSpace.get(),
107                                         kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
108     CGContextTranslateCTM(m_contextCG.get(), 0, canvas->size().height());
109     CGContextScaleCTM(m_contextCG.get(), 1, -1);
110     m_context = adoptPtr(new GraphicsContext(m_contextCG.get()));
111 #endif
112     context()->save();
113 }
114
115 PlatformCanvas::Painter::~Painter()
116 {
117     context()->restore();
118 }
119
120 } // namespace WebCore