2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "PlatformCanvas.h"
30 #include "GraphicsContext.h"
33 #include "NativeImageSkia.h"
34 #include "PlatformContextSkia.h"
35 #include "SkColorPriv.h"
36 #include "skia/ext/platform_canvas.h"
38 #include <CoreGraphics/CGBitmapContext.h>
43 PlatformCanvas::PlatformCanvas()
47 PlatformCanvas::~PlatformCanvas()
51 void PlatformCanvas::resize(const IntSize& size)
57 m_skiaCanvas = adoptPtr(skia::CreateBitmapCanvas(size.width(), size.height(), false));
59 size_t bufferSize = size.width() * size.height() * 4;
60 m_pixelData = adoptArrayPtr(new uint8_t[bufferSize]);
61 memset(m_pixelData.get(), 0, bufferSize);
65 PlatformCanvas::AutoLocker::AutoLocker(PlatformCanvas* canvas)
70 if (m_canvas->m_skiaCanvas) {
71 m_bitmap = &m_canvas->m_skiaCanvas->getDevice()->accessBitmap(false);
72 m_bitmap->lockPixels();
74 if (m_bitmap->config() == SkBitmap::kARGB_8888_Config)
75 m_pixels = static_cast<uint8_t*>(m_bitmap->getPixels());
79 if (canvas->m_pixelData)
80 m_pixels = &canvas->m_pixelData[0];
84 PlatformCanvas::AutoLocker::~AutoLocker()
88 m_bitmap->unlockPixels();
92 PlatformCanvas::Painter::Painter(PlatformCanvas* canvas, PlatformCanvas::Painter::TextOption option)
95 m_skiaContext = adoptPtr(new PlatformContextSkia(canvas->m_skiaCanvas.get()));
97 m_skiaContext->setDrawingToImageBuffer(option == GrayscaleText);
99 m_context = adoptPtr(new GraphicsContext(reinterpret_cast<PlatformGraphicsContext*>(m_skiaContext.get())));
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,
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()));
115 PlatformCanvas::Painter::~Painter()
117 context()->restore();
120 } // namespace WebCore