OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "skia/ext/bitmap_platform_device_mac.h" | 5 #include "skia/ext/bitmap_platform_device_mac.h" |
6 | 6 |
7 #import <ApplicationServices/ApplicationServices.h> | 7 #import <ApplicationServices/ApplicationServices.h> |
8 #include <time.h> | 8 #include <time.h> |
9 | 9 |
10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 static CGContextRef CGContextForData(void* data, int width, int height) { | 24 static CGContextRef CGContextForData(void* data, int width, int height) { |
25 #define HAS_ARGB_SHIFTS(a, r, g, b) \ | 25 #define HAS_ARGB_SHIFTS(a, r, g, b) \ |
26 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ | 26 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ |
27 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) | 27 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) |
28 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) | 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 | 29 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple |
30 // recommends these flags for improved CG performance. | 30 // recommends these flags for improved CG performance. |
| 31 |
| 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); |
31 CGContextRef context = | 37 CGContextRef context = |
32 CGBitmapContextCreate(data, width, height, 8, width * 4, | 38 CGBitmapContextCreate(data, width, height, 8, width * 4, |
33 base::mac::GetSystemColorSpace(), | 39 base::mac::GetSystemColorSpace(), |
34 kCGImageAlphaPremultipliedFirst | | 40 kCGImageAlphaPremultipliedFirst | |
35 kCGBitmapByteOrder32Host); | 41 kCGBitmapByteOrder32Host); |
36 #else | 42 #else |
37 #error We require that Skia's and CoreGraphics's recommended \ | 43 #error We require that Skia's and CoreGraphics's recommended \ |
38 image memory layout match. | 44 image memory layout match. |
39 #endif | 45 #endif |
40 #undef HAS_ARGB_SHIFTS | 46 #undef HAS_ARGB_SHIFTS |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 | 254 |
249 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( | 255 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( |
250 SkBitmap::Config config, int width, int height, bool isOpaque, | 256 SkBitmap::Config config, int width, int height, bool isOpaque, |
251 Usage /*usage*/) { | 257 Usage /*usage*/) { |
252 SkASSERT(config == SkBitmap::kARGB_8888_Config); | 258 SkASSERT(config == SkBitmap::kARGB_8888_Config); |
253 SkDevice* bitmap_device = BitmapPlatformDevice::CreateAndClear(width, height, | 259 SkDevice* bitmap_device = BitmapPlatformDevice::CreateAndClear(width, height, |
254 isOpaque); | 260 isOpaque); |
255 return bitmap_device; | 261 return bitmap_device; |
256 } | 262 } |
257 | 263 |
| 264 // PlatformCanvas impl |
| 265 |
| 266 SkCanvas* CreatePlatformCanvas(CGContextRef ctx, int width, int height, |
| 267 bool is_opaque, OnFailureType failureType) { |
| 268 SkDevice* dev = BitmapPlatformDevice::Create(ctx, width, height, is_opaque); |
| 269 return CreateCanvas(dev, failureType); |
| 270 } |
| 271 |
| 272 SkCanvas* CreatePlatformCanvas(int width, int height, bool is_opaque, |
| 273 uint8_t* data, OnFailureType failureType) { |
| 274 SkDevice* dev = BitmapPlatformDevice::CreateWithData(data, width, height, |
| 275 is_opaque); |
| 276 return CreateCanvas(dev, failureType); |
| 277 } |
| 278 |
258 // Port of PlatformBitmap to mac | 279 // Port of PlatformBitmap to mac |
259 | 280 |
260 PlatformBitmap::~PlatformBitmap() { | 281 PlatformBitmap::~PlatformBitmap() { |
261 if (surface_) | 282 if (surface_) |
262 CGContextRelease(surface_); | 283 CGContextRelease(surface_); |
263 } | 284 } |
264 | 285 |
265 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) { | 286 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) { |
266 if (RasterDeviceTooBigToAllocate(width, height)) | 287 if (RasterDeviceTooBigToAllocate(width, height)) |
267 return false; | 288 return false; |
268 | 289 |
269 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, width, height, width * 4); | 290 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, width, height, width * 4); |
270 if (!bitmap_.allocPixels()) | 291 if (!bitmap_.allocPixels()) |
271 return false; | 292 return false; |
272 | 293 |
273 if (!is_opaque) | 294 if (!is_opaque) |
274 bitmap_.eraseColor(0); | 295 bitmap_.eraseColor(0); |
275 bitmap_.setIsOpaque(is_opaque); | 296 bitmap_.setIsOpaque(is_opaque); |
276 | 297 |
277 surface_ = CGContextForData(bitmap_.getPixels(), bitmap_.width(), | 298 surface_ = CGContextForData(bitmap_.getPixels(), bitmap_.width(), |
278 bitmap_.height()); | 299 bitmap_.height()); |
279 return true; | 300 return true; |
280 } | 301 } |
281 | 302 |
282 } // namespace skia | 303 } // namespace skia |
OLD | NEW |