Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1469)

Unified Diff: skia/ext/skia_utils_ios.mm

Issue 10928093: Adds an iOS implementation of gfx::Image. (Closed) Base URL: http://git.chromium.org/chromium/src.git@skia
Patch Set: Cleanup. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: skia/ext/skia_utils_ios.mm
diff --git a/skia/ext/skia_utils_ios.mm b/skia/ext/skia_utils_ios.mm
new file mode 100644
index 0000000000000000000000000000000000000000..477fe31e2b1b4549dff3b8e8ce54b152335ff807
--- /dev/null
+++ b/skia/ext/skia_utils_ios.mm
@@ -0,0 +1,84 @@
+// 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,
Robert Sesek 2012/09/13 16:14:24 nit: continuations are indented 4 from the previou
rohitrao (ping after 24h) 2012/09/13 17:57:57 Done.
rohitrao (ping after 24h) 2012/09/13 17:57:57 Done.
+ 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
Robert Sesek 2012/09/13 16:14:24 There was a discussion about the threaded usage of
rohitrao (ping after 24h) 2012/09/13 17:57:57 There was an assertion that ImageSkia is currently
+ // calls setCurrentContext, so it's similarly limited to the main thread.
+ DCHECK([NSThread isMainThread]);
+ UIGraphicsPushContext(context);
Robert Sesek 2012/09/13 16:14:24 I'm surprised you guys don't have a scoper for thi
rohitrao (ping after 24h) 2012/09/13 17:57:57 I didn't see one in any of the obvious places. I'
stuartmorgan 2012/09/14 11:01:37 Did you check in ui/gfx/scoped_ui_graphics_push_co
+ [image drawInRect:CGRectMake(0, 0, size.width, size.height)
+ blendMode:kCGBlendModeCopy
+ alpha:1.0];
+ UIGraphicsPopContext();
+
+ return bitmap;
+}
+
+UIImage* SkBitmapToUIImageWithColorSpace(const SkBitmap& skiaBitmap,
+ CGColorSpaceRef colorSpace) {
Robert Sesek 2012/09/13 16:14:24 naming: color_space and throughout
rohitrao (ping after 24h) 2012/09/13 17:57:57 Done.
+ if (skiaBitmap.isNull())
+ return nil;
+
+ // First convert SkBitmap to CGImageRef.
+ base::mac::ScopedCFTypeRef<CGImageRef> cgimage(
+ SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace));
+
+ // Now convert to NSImage.
+ // TODO(rohitrao): Gotta incorporate the scale factor somewhere!
+ return [UIImage imageWithCGImage:cgimage.get()];
+}
+
+UIImage* SkBitmapToUIImage(const SkBitmap& skiaBitmap) {
+ base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
+ CGColorSpaceCreateDeviceRGB());
+ return SkBitmapToUIImageWithColorSpace(skiaBitmap, colorSpace.get());
+}
+
+
+} // namespace gfx

Powered by Google App Engine
This is Rietveld 408576698