• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
PlatformCanvas()43 PlatformCanvas::PlatformCanvas()
44 {
45 }
46 
~PlatformCanvas()47 PlatformCanvas::~PlatformCanvas()
48 {
49 }
50 
resize(const IntSize & size)51 void PlatformCanvas::resize(const IntSize& size)
52 {
53     m_size = size;
54 #if USE(SKIA)
55     m_skiaCanvas = skia::CreateBitmapCanvas(size.width(), size.height(), false);
56 #elif USE(CG)
57     size_t bufferSize = size.width() * size.height() * 4;
58     m_pixelData = adoptArrayPtr(new uint8_t[bufferSize]);
59     memset(m_pixelData.get(), 0, bufferSize);
60 #endif
61 }
62 
AutoLocker(PlatformCanvas * canvas)63 PlatformCanvas::AutoLocker::AutoLocker(PlatformCanvas* canvas)
64     : m_canvas(canvas)
65     , m_pixels(0)
66 {
67 #if USE(SKIA)
68     if (m_canvas->m_skiaCanvas) {
69         m_bitmap = &m_canvas->m_skiaCanvas->getDevice()->accessBitmap(false);
70         m_bitmap->lockPixels();
71 
72         if (m_bitmap->config() == SkBitmap::kARGB_8888_Config)
73             m_pixels = static_cast<uint8_t*>(m_bitmap->getPixels());
74     } else
75         m_bitmap = 0;
76 #elif USE(CG)
77     if (canvas->m_pixelData)
78         m_pixels = &canvas->m_pixelData[0];
79 #endif
80 }
81 
~AutoLocker()82 PlatformCanvas::AutoLocker::~AutoLocker()
83 {
84 #if USE(SKIA)
85     if (m_bitmap)
86         m_bitmap->unlockPixels();
87 #endif
88 }
89 
Painter(PlatformCanvas * canvas,PlatformCanvas::Painter::TextOption option)90 PlatformCanvas::Painter::Painter(PlatformCanvas* canvas, PlatformCanvas::Painter::TextOption option)
91 {
92 #if USE(SKIA)
93     m_skiaContext = adoptPtr(new PlatformContextSkia(canvas->m_skiaCanvas.get()));
94 
95     m_skiaContext->setDrawingToImageBuffer(option == GrayscaleText);
96 
97     m_context = adoptPtr(new GraphicsContext(reinterpret_cast<PlatformGraphicsContext*>(m_skiaContext.get())));
98 #elif USE(CG)
99 
100     m_colorSpace = CGColorSpaceCreateDeviceRGB();
101     size_t rowBytes = canvas->size().width() * 4;
102     m_contextCG = CGBitmapContextCreate(canvas->m_pixelData.get(),
103                                         canvas->size().width(), canvas->size().height(), 8, rowBytes,
104                                         m_colorSpace.get(),
105                                         kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
106     CGContextTranslateCTM(m_contextCG.get(), 0, canvas->size().height());
107     CGContextScaleCTM(m_contextCG.get(), 1, -1);
108     m_context = adoptPtr(new GraphicsContext(m_contextCG.get()));
109 #endif
110 }
111 
~Painter()112 PlatformCanvas::Painter::~Painter()
113 {
114 }
115 
116 } // namespace WebCore
117