diff options
author | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-14 12:55:23 +0000 |
---|---|---|
committer | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-14 12:55:23 +0000 |
commit | d5f5c2a08c3130d4836551f713dab341f4c06db3 (patch) | |
tree | 6210951e7f43d3bf8aaf2377e2855d5b2b2b2b03 /skia/ext/skia_utils_ios.mm | |
parent | 9a9f8bd9220215ae97cab7cb43bba8eb0b0d52bf (diff) | |
download | chromium_src-d5f5c2a08c3130d4836551f713dab341f4c06db3.zip chromium_src-d5f5c2a08c3130d4836551f713dab341f4c06db3.tar.gz chromium_src-d5f5c2a08c3130d4836551f713dab341f4c06db3.tar.bz2 |
Adds an iOS implementation of gfx::Image.
BUG=None
TEST=None
Review URL: https://chromiumcodereview.appspot.com/10928093
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@156787 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/skia_utils_ios.mm')
-rw-r--r-- | skia/ext/skia_utils_ios.mm | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/skia/ext/skia_utils_ios.mm b/skia/ext/skia_utils_ios.mm new file mode 100644 index 0000000..9d4c14a --- /dev/null +++ b/skia/ext/skia_utils_ios.mm @@ -0,0 +1,77 @@ +// Copyright 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "skia/ext/skia_utils_ios.h" + +#import <UIKit/UIKit.h> + +#include "base/logging.h" +#include "base/mac/scoped_cftyperef.h" +#include "third_party/skia/include/utils/mac/SkCGUtils.h" + +namespace gfx { + +SkBitmap UIImageToSkBitmap(UIImage* image, CGSize size, bool is_opaque) { + SkBitmap bitmap; + if (!image) + return bitmap; + + bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height); + if (!bitmap.allocPixels()) + return bitmap; + + bitmap.setIsOpaque(is_opaque); + void* data = bitmap.getPixels(); + + // Allocate a bitmap context with 4 components per pixel (BGRA). Apple + // recommends these flags for improved CG performance. +#define HAS_ARGB_SHIFTS(a, r, g, b) \ + (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ + && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) +#if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) + base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( + CGColorSpaceCreateDeviceRGB()); + base::mac::ScopedCFTypeRef<CGContextRef> context( + CGBitmapContextCreate(data, size.width, size.height, 8, size.width*4, + color_space, + kCGImageAlphaPremultipliedFirst | + kCGBitmapByteOrder32Host)); +#else +#error We require that Skia's and CoreGraphics's recommended \ + image memory layout match. +#endif +#undef HAS_ARGB_SHIFTS + + DCHECK(context); + if (!context) + return bitmap; + + // UIGraphicsPushContext be called from the main thread. + // TODO(rohitrao): We can use CG to make this thread safe, but the mac code + // calls setCurrentContext, so it's similarly limited to the main thread. + DCHECK([NSThread isMainThread]); + UIGraphicsPushContext(context); + [image drawInRect:CGRectMake(0, 0, size.width, size.height) + blendMode:kCGBlendModeCopy + alpha:1.0]; + UIGraphicsPopContext(); + + return bitmap; +} + +UIImage* SkBitmapToUIImageWithColorSpace(const SkBitmap& skia_bitmap, + CGColorSpaceRef color_space) { + if (skia_bitmap.isNull()) + return nil; + + // First convert SkBitmap to CGImageRef. + base::mac::ScopedCFTypeRef<CGImageRef> cg_image( + SkCreateCGImageRefWithColorspace(skia_bitmap, color_space)); + + // Now convert to UIImage. + // TODO(rohitrao): Gotta incorporate the scale factor somewhere! + return [UIImage imageWithCGImage:cg_image.get()]; +} + +} // namespace gfx |