reed@android.com | 0d55f1e | 2008-12-18 19:26:11 +0000 | [diff] [blame] | 1 | #include "SkCGUtils.h" |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 2 | #include "SkBitmap.h" |
reed@android.com | a545a55 | 2009-06-29 17:07:19 +0000 | [diff] [blame] | 3 | #include "SkColorPriv.h" |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 4 | |
| 5 | extern CGImageRef SkCreateCGImageRef(const SkBitmap&); |
| 6 | |
reed@android.com | 2b26cac | 2008-12-22 02:33:11 +0000 | [diff] [blame] | 7 | static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) { |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 8 | SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info); |
| 9 | delete bitmap; |
| 10 | } |
| 11 | |
reed@android.com | a545a55 | 2009-06-29 17:07:19 +0000 | [diff] [blame] | 12 | #define HAS_ARGB_SHIFTS(a, r, g, b) \ |
| 13 | (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ |
| 14 | && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) |
| 15 | |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 16 | static SkBitmap* prepareForImageRef(const SkBitmap& bm, |
| 17 | size_t* bitsPerComponent, |
| 18 | CGBitmapInfo* info) { |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 19 | bool upscaleTo32 = false; |
reed@android.com | a545a55 | 2009-06-29 17:07:19 +0000 | [diff] [blame] | 20 | |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 21 | switch (bm.config()) { |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 22 | case SkBitmap::kRGB_565_Config: |
| 23 | upscaleTo32 = true; |
| 24 | // fall through |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 25 | case SkBitmap::kARGB_8888_Config: |
| 26 | *bitsPerComponent = 8; |
reed@android.com | 8ede492 | 2009-06-22 20:04:33 +0000 | [diff] [blame] | 27 | #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 0, 8, 16) \ |
| 28 | || defined(SK_CPU_BENDIAN) && HAS_ARGB_SHIFTS(0, 24, 16, 8) |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame^] | 29 | *info = kCGBitmapByteOrder32Big; |
reed@android.com | 8ede492 | 2009-06-22 20:04:33 +0000 | [diff] [blame] | 30 | #elif defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) \ |
| 31 | || defined(SK_CPU_BENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) |
| 32 | // Matches the CGBitmapInfo that Apple recommends for best |
| 33 | // performance, used by google chrome. |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame^] | 34 | *info = kCGBitmapByteOrder32Little; |
reed@android.com | 8ede492 | 2009-06-22 20:04:33 +0000 | [diff] [blame] | 35 | #else |
| 36 | // ...add more formats as required... |
| 37 | #warning Cannot convert SkBitmap to CGImageRef with these shiftmasks. \ |
| 38 | This will probably not work. |
| 39 | // Legacy behavior. Perhaps turn this into an error at some |
| 40 | // point. |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame^] | 41 | *info = kCGBitmapByteOrder32Big; |
reed@android.com | 8ede492 | 2009-06-22 20:04:33 +0000 | [diff] [blame] | 42 | #endif |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 43 | break; |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 44 | #if 0 |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 45 | case SkBitmap::kRGB_565_Config: |
| 46 | // doesn't see quite right. Are they thinking 1555? |
| 47 | *bitsPerComponent = 5; |
| 48 | *info = kCGBitmapByteOrder16Little; |
| 49 | break; |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 50 | #endif |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 51 | case SkBitmap::kARGB_4444_Config: |
| 52 | *bitsPerComponent = 4; |
| 53 | *info = kCGBitmapByteOrder16Little | kCGImageAlphaPremultipliedLast; |
| 54 | break; |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 55 | default: |
| 56 | return NULL; |
| 57 | } |
| 58 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame^] | 59 | if (!bm.isOpaque()) { |
| 60 | *info |= kCGImageAlphaPremultipliedLast; |
| 61 | } |
| 62 | |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 63 | SkBitmap* copy; |
| 64 | if (upscaleTo32) { |
| 65 | copy = new SkBitmap; |
| 66 | // here we make a ceep copy of the pixels, since CG won't take our |
| 67 | // 565 directly |
| 68 | bm.copyTo(copy, SkBitmap::kARGB_8888_Config); |
| 69 | } else { |
| 70 | copy = new SkBitmap(bm); |
| 71 | } |
| 72 | return copy; |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 73 | } |
| 74 | |
reed@android.com | a545a55 | 2009-06-29 17:07:19 +0000 | [diff] [blame] | 75 | #undef HAS_ARGB_SHIFTS |
| 76 | |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 77 | CGImageRef SkCreateCGImageRef(const SkBitmap& bm) { |
| 78 | size_t bitsPerComponent; |
| 79 | CGBitmapInfo info; |
| 80 | |
| 81 | SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info); |
| 82 | if (NULL == bitmap) { |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | const int w = bitmap->width(); |
| 87 | const int h = bitmap->height(); |
| 88 | const size_t s = bitmap->getSize(); |
| 89 | |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 90 | // our provider "owns" the bitmap*, and will take care of deleting it |
reed@android.com | 2b26cac | 2008-12-22 02:33:11 +0000 | [diff] [blame] | 91 | // we initially lock it, so we can access the pixels. The bitmap will be deleted in the release |
| 92 | // proc, which will in turn unlock the pixels |
| 93 | bitmap->lockPixels(); |
| 94 | CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s, |
| 95 | SkBitmap_ReleaseInfo); |
| 96 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame^] | 97 | CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 98 | CGImageRef ref = CGImageCreate(w, h, bitsPerComponent, |
| 99 | bitmap->bytesPerPixel() * 8, |
| 100 | bitmap->rowBytes(), space, info, dataRef, |
| 101 | NULL, false, kCGRenderingIntentDefault); |
| 102 | CGColorSpaceRelease(space); |
| 103 | CGDataProviderRelease(dataRef); |
| 104 | return ref; |
| 105 | } |
| 106 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame^] | 107 | void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) { |
| 108 | CGImageRef img = SkCreateCGImageRef(bm); |
| 109 | |
| 110 | if (img) { |
| 111 | CGRect r = CGRectMake(0, 0, bm.width(), bm.height()); |
| 112 | |
| 113 | CGContextSaveGState(cg); |
| 114 | CGContextTranslateCTM(cg, x, r.size.height + y); |
| 115 | CGContextScaleCTM(cg, 1, -1); |
| 116 | |
| 117 | CGContextDrawImage(cg, r, img); |
| 118 | |
| 119 | CGContextRestoreGState(cg); |
| 120 | |
| 121 | CGImageRelease(img); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 126 | |