Roll leveldb from r73 to r75.
[chromium-blink-merge.git] / skia / ext / skia_utils_ios.mm
blob72c69a02b592b8b9db08d7a0c5e598d33cad0192
1 // Copyright 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/skia_utils_ios.h"
7 #import <ImageIO/ImageIO.h>
8 #import <UIKit/UIKit.h>
10 #include "base/logging.h"
11 #include "base/mac/scoped_cftyperef.h"
12 #include "third_party/skia/include/utils/mac/SkCGUtils.h"
14 namespace gfx {
16 SkBitmap CGImageToSkBitmap(CGImageRef image, CGSize size, bool is_opaque) {
17   SkBitmap bitmap;
18   if (!image)
19     return bitmap;
21   bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height);
22   if (!bitmap.allocPixels())
23     return bitmap;
25   bitmap.setIsOpaque(is_opaque);
26   void* data = bitmap.getPixels();
28   // Allocate a bitmap context with 4 components per pixel (BGRA). Apple
29   // recommends these flags for improved CG performance.
30 #define HAS_ARGB_SHIFTS(a, r, g, b) \
31             (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
32              && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
33 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
34   base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space(
35       CGColorSpaceCreateDeviceRGB());
36   base::mac::ScopedCFTypeRef<CGContextRef> context(
37       CGBitmapContextCreate(data, size.width, size.height, 8, size.width*4,
38                             color_space,
39                             kCGImageAlphaPremultipliedFirst |
40                                 kCGBitmapByteOrder32Host));
41 #else
42 #error We require that Skia's and CoreGraphics's recommended \
43        image memory layout match.
44 #endif
45 #undef HAS_ARGB_SHIFTS
47   DCHECK(context);
48   if (!context)
49     return bitmap;
51   CGRect imageRect = CGRectMake(0.0, 0.0, size.width, size.height);
52   CGContextSetBlendMode(context, kCGBlendModeCopy);
53   CGContextDrawImage(context, imageRect, image);
55   return bitmap;
58 UIImage* SkBitmapToUIImageWithColorSpace(const SkBitmap& skia_bitmap,
59                                          CGFloat scale,
60                                          CGColorSpaceRef color_space) {
61   if (skia_bitmap.isNull())
62     return nil;
64   // First convert SkBitmap to CGImageRef.
65   base::mac::ScopedCFTypeRef<CGImageRef> cg_image(
66       SkCreateCGImageRefWithColorspace(skia_bitmap, color_space));
68   // Now convert to UIImage.
69   return [UIImage imageWithCGImage:cg_image.get()
70                              scale:scale
71                        orientation:UIImageOrientationUp];
74 std::vector<SkBitmap> ImageDataToSkBitmaps(NSData* image_data) {
75   DCHECK(image_data);
76   base::mac::ScopedCFTypeRef<CFDictionaryRef> empty_dictionary(
77       CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL));
78   std::vector<SkBitmap> frames;
80   base::mac::ScopedCFTypeRef<CGImageSourceRef> source(
81       CGImageSourceCreateWithData((CFDataRef)image_data, empty_dictionary));
83   size_t count = CGImageSourceGetCount(source);
84   for (size_t index = 0; index < count; ++index) {
85     base::mac::ScopedCFTypeRef<CGImageRef> cg_image(
86         CGImageSourceCreateImageAtIndex(source, index, empty_dictionary));
88     CGSize size = CGSizeMake(CGImageGetWidth(cg_image),
89                              CGImageGetHeight(cg_image));
90     const SkBitmap bitmap = CGImageToSkBitmap(cg_image, size, false);
91     if (!bitmap.empty())
92       frames.push_back(bitmap);
93   }
95   DLOG_IF(WARNING, frames.size() != count) << "Only decoded " << frames.size()
96       << " frames for " << count << " expected.";
97   return frames;
100 }  // namespace gfx