1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "skia/ext/bitmap_platform_device_mac.h"
7 #import <ApplicationServices/ApplicationServices.h>
10 #include "base/mac/mac_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "skia/ext/bitmap_platform_device.h"
13 #include "skia/ext/platform_canvas.h"
14 #include "skia/ext/skia_utils_mac.h"
15 #include "third_party/skia/include/core/SkMatrix.h"
16 #include "third_party/skia/include/core/SkRegion.h"
17 #include "third_party/skia/include/core/SkTypes.h"
18 #include "third_party/skia/include/core/SkUtils.h"
24 static CGContextRef
CGContextForData(void* data
, int width
, int height
) {
25 #define HAS_ARGB_SHIFTS(a, r, g, b) \
26 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
27 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
28 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
29 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple
30 // recommends these flags for improved CG performance.
32 // CGBitmapContextCreate returns NULL if width/height are 0. However, our
33 // callers expect to get a canvas back (which they later resize/reallocate)
34 // so we pin the dimensions here.
35 width
= SkMax32(1, width
);
36 height
= SkMax32(1, height
);
37 CGContextRef context
=
38 CGBitmapContextCreate(data
, width
, height
, 8, width
* 4,
39 base::mac::GetSystemColorSpace(),
40 kCGImageAlphaPremultipliedFirst
|
41 kCGBitmapByteOrder32Host
);
43 #error We require that Skia's and CoreGraphics's recommended \
44 image memory layout match.
46 #undef HAS_ARGB_SHIFTS
51 // Change the coordinate system to match WebCore's
52 CGContextTranslateCTM(context
, 0, height
);
53 CGContextScaleCTM(context
, 1.0, -1.0);
60 void BitmapPlatformDevice::ReleaseBitmapContext() {
61 SkASSERT(bitmap_context_
);
62 CGContextRelease(bitmap_context_
);
63 bitmap_context_
= NULL
;
66 void BitmapPlatformDevice::SetMatrixClip(
67 const SkMatrix
& transform
,
68 const SkRegion
& region
) {
69 transform_
= transform
;
70 clip_region_
= region
;
74 void BitmapPlatformDevice::LoadConfig() {
75 if (!config_dirty_
|| !bitmap_context_
)
76 return; // Nothing to do.
77 config_dirty_
= false;
79 // We must restore and then save the state of the graphics context since the
80 // calls to Load the clipping region to the context are strictly cummulative,
81 // i.e., you can't replace a clip rect, other than with a save/restore.
82 // But this implies that no other changes to the state are done elsewhere.
83 // If we ever get to need to change this, then we must replace the clip rect
84 // calls in LoadClippingRegionToCGContext() with an image mask instead.
85 CGContextRestoreGState(bitmap_context_
);
86 CGContextSaveGState(bitmap_context_
);
87 LoadTransformToCGContext(bitmap_context_
, transform_
);
88 LoadClippingRegionToCGContext(bitmap_context_
, clip_region_
, transform_
);
92 // We use this static factory function instead of the regular constructor so
93 // that we can create the pixel data before calling the constructor. This is
94 // required so that we can call the base class' constructor with the pixel
96 BitmapPlatformDevice
* BitmapPlatformDevice::Create(CGContextRef context
,
100 if (RasterDeviceTooBigToAllocate(width
, height
))
104 // TODO: verify that the CG Context's pixels will have tight rowbytes or pass in the correct
105 // rowbytes for the case when context != NULL.
106 bitmap
.setInfo(SkImageInfo::MakeN32(width
, height
, is_opaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
));
110 data
= CGBitmapContextGetData(context
);
111 bitmap
.setPixels(data
);
113 if (!bitmap
.tryAllocPixels())
115 data
= bitmap
.getPixels();
118 // If we were given data, then don't clobber it!
120 if (!context
&& is_opaque
) {
121 // To aid in finding bugs, we set the background color to something
122 // obviously wrong so it will be noticable when it is not cleared
123 bitmap
.eraseARGB(255, 0, 255, 128); // bright bluish green
128 context
= CGContextForData(data
, width
, height
);
132 CGContextRetain(context
);
134 BitmapPlatformDevice
* rv
= new BitmapPlatformDevice(context
, bitmap
);
136 // The device object took ownership of the graphics context with its own
137 // CGContextRetain call.
138 CGContextRelease(context
);
143 BitmapPlatformDevice
* BitmapPlatformDevice::CreateAndClear(int width
,
146 BitmapPlatformDevice
* device
= Create(NULL
, width
, height
, is_opaque
);
152 BitmapPlatformDevice
* BitmapPlatformDevice::CreateWithData(uint8_t* data
,
156 CGContextRef context
= NULL
;
158 context
= CGContextForData(data
, width
, height
);
160 BitmapPlatformDevice
* rv
= Create(context
, width
, height
, is_opaque
);
162 // The device object took ownership of the graphics context with its own
163 // CGContextRetain call.
165 CGContextRelease(context
);
170 // The device will own the bitmap, which corresponds to also owning the pixel
171 // data. Therefore, we do not transfer ownership to the SkBitmapDevice's bitmap.
172 BitmapPlatformDevice::BitmapPlatformDevice(
173 CGContextRef context
, const SkBitmap
& bitmap
)
174 : SkBitmapDevice(bitmap
),
175 bitmap_context_(context
),
176 config_dirty_(true), // Want to load the config next time.
177 transform_(SkMatrix::I()) {
178 SetPlatformDevice(this, this);
179 SkASSERT(bitmap_context_
);
180 // Initialize the clip region to the entire bitmap.
184 CGBitmapContextGetWidth(bitmap_context_
),
185 CGBitmapContextGetHeight(bitmap_context_
));
186 clip_region_
= SkRegion(rect
);
187 CGContextRetain(bitmap_context_
);
188 // We must save the state once so that we can use the restore/save trick
190 CGContextSaveGState(bitmap_context_
);
193 BitmapPlatformDevice::~BitmapPlatformDevice() {
195 CGContextRelease(bitmap_context_
);
198 CGContextRef
BitmapPlatformDevice::GetBitmapContext() {
200 return bitmap_context_
;
203 void BitmapPlatformDevice::setMatrixClip(const SkMatrix
& transform
,
204 const SkRegion
& region
,
205 const SkClipStack
&) {
206 SetMatrixClip(transform
, region
);
209 void BitmapPlatformDevice::DrawToNativeContext(CGContextRef context
, int x
,
210 int y
, const CGRect
* src_rect
) {
211 bool created_dc
= false;
212 if (!bitmap_context_
) {
217 // this should not make a copy of the bits, since we're not doing
218 // anything to trigger copy on write
219 CGImageRef image
= CGBitmapContextCreateImage(bitmap_context_
);
224 bounds
.size
.width
= src_rect
->size
.width
;
225 bounds
.size
.height
= src_rect
->size
.height
;
226 CGImageRef sub_image
= CGImageCreateWithImageInRect(image
, *src_rect
);
227 CGContextDrawImage(context
, bounds
, sub_image
);
228 CGImageRelease(sub_image
);
230 bounds
.size
.width
= width();
231 bounds
.size
.height
= height();
232 CGContextDrawImage(context
, bounds
, image
);
234 CGImageRelease(image
);
237 ReleaseBitmapContext();
240 SkBaseDevice
* BitmapPlatformDevice::onCreateDevice(const SkImageInfo
& info
,
242 SkASSERT(info
.colorType() == kN32_SkColorType
);
243 return BitmapPlatformDevice::CreateAndClear(info
.width(), info
.height(),
247 // PlatformCanvas impl
249 SkCanvas
* CreatePlatformCanvas(CGContextRef ctx
, int width
, int height
,
250 bool is_opaque
, OnFailureType failureType
) {
251 skia::RefPtr
<SkBaseDevice
> dev
= skia::AdoptRef(
252 BitmapPlatformDevice::Create(ctx
, width
, height
, is_opaque
));
253 return CreateCanvas(dev
, failureType
);
256 SkCanvas
* CreatePlatformCanvas(int width
, int height
, bool is_opaque
,
257 uint8_t* data
, OnFailureType failureType
) {
258 skia::RefPtr
<SkBaseDevice
> dev
= skia::AdoptRef(
259 BitmapPlatformDevice::CreateWithData(data
, width
, height
, is_opaque
));
260 return CreateCanvas(dev
, failureType
);
263 // Port of PlatformBitmap to mac
265 PlatformBitmap::~PlatformBitmap() {
267 CGContextRelease(surface_
);
270 bool PlatformBitmap::Allocate(int width
, int height
, bool is_opaque
) {
271 if (RasterDeviceTooBigToAllocate(width
, height
))
274 if (!bitmap_
.tryAllocN32Pixels(width
, height
, is_opaque
))
278 bitmap_
.eraseColor(0);
280 surface_
= CGContextForData(bitmap_
.getPixels(), bitmap_
.width(),