Index: src/ports/SkImageGeneratorCG.cpp |
diff --git a/src/ports/SkImageGeneratorCG.cpp b/src/ports/SkImageGeneratorCG.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..86c419d7205a31600677cac3ee18a7025ac2c8b3 |
--- /dev/null |
+++ b/src/ports/SkImageGeneratorCG.cpp |
@@ -0,0 +1,112 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkImageGeneratorCG.h" |
+#include "SkUnpremultiply.h" |
+ |
+#ifdef SK_BUILD_FOR_MAC |
+#include <ApplicationServices/ApplicationServices.h> |
+#endif |
+ |
+#ifdef SK_BUILD_FOR_IOS |
+#include <CoreGraphics/CoreGraphics.h> |
+#include <ImageIO/ImageIO.h> |
+#include <MobileCoreServices/MobileCoreServices.h> |
+#endif |
+ |
+static CGImageSourceRef data_to_CGImageSrc(SkData* data) { |
+ CGDataProviderRef cgData = CGDataProviderCreateWithData(data, data->data(), data->size(), |
+ nullptr); |
+ if (!cgData) { |
+ return nullptr; |
+ } |
+ CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(cgData, 0); |
+ CGDataProviderRelease(cgData); |
+ return imageSrc; |
+} |
+ |
+SkImageGenerator* SkImageGeneratorCG::NewFromEncodedCG(SkData* data) { |
+ CGImageSourceRef imageSrc = data_to_CGImageSrc(data); |
+ if (!imageSrc) { |
+ return nullptr; |
+ } |
+ SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc); |
scroggo
2016/03/07 13:04:30
Why do you treat this as a const void instead of a
msarett
2016/03/08 23:20:18
CFRelease must be used to free the CGImageSrcRef,
|
+ |
+ CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0, nullptr); |
+ if (!properties) { |
+ return nullptr; |
+ } |
+ |
+ CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
+ kCGImagePropertyPixelWidth)); |
+ CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
+ kCGImagePropertyPixelHeight)); |
+ bool hasAlpha = (bool) (CFDictionaryGetValue(properties, |
+ kCGImagePropertyHasAlpha)); |
+ |
+ int width, height; |
+ CFNumberGetValue(widthRef, kCFNumberIntType, &width); |
scroggo
2016/03/07 13:04:30
It looks like these return whether or not they suc
msarett
2016/03/08 23:20:17
Done.
|
+ CFNumberGetValue(heightRef, kCFNumberIntType, &height); |
+ |
+ SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType; |
+ SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, alphaType); |
+ |
+ // FIXME: We have the opportunity to extract color space information here, |
+ // though I think it makes sense to wait until we understand how |
+ // we want to communicate it to the generator. |
+ |
+ return new SkImageGeneratorCG(info, autoImageSrc.detach(), data); |
+} |
+ |
+SkImageGeneratorCG::SkImageGeneratorCG(const SkImageInfo& info, const void* imageSrc, SkData* data) |
+ : INHERITED(info) |
+ , fImageSrc(imageSrc) |
+ , fData(SkRef(data)) |
+{} |
+ |
+SkData* SkImageGeneratorCG::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { |
+ return SkRef(fData.get()); |
+} |
+ |
+bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
+ SkPMColor ctable[], int* ctableCount) { |
+ if (kN32_SkColorType != info.colorType()) { |
scroggo
2016/03/07 13:04:30
FIXME: Support other types?
msarett
2016/03/08 23:20:17
Done.
|
+ return false; |
+ } |
+ |
+ switch (info.alphaType()) { |
+ case kOpaque_SkAlphaType: |
+ if (kOpaque_SkAlphaType != this->getInfo().alphaType()) { |
+ return false; |
+ } |
+ break; |
+ case kPremul_SkAlphaType: |
+ break; |
+ default: |
+ return false; |
+ } |
+ |
+ CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImageSrc.get(), 0, |
+ nullptr); |
+ if (!image) { |
+ return false; |
+ } |
+ SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image); |
+ |
+ // FIXME: Using this function (as opposed to swizzling ourselves) greatly |
+ // restricts the color and alpha types that we support. If we |
scroggo
2016/03/07 13:04:30
I'd be curious about the performance difference, t
msarett
2016/03/08 23:20:18
Me too :). Adding to the FIXME.
|
+ // swizzle ourselves, we can add support for: |
+ // kUnpremul_SkAlphaType |
+ // 16-bit per component RGBA |
+ // kGray_8_SkColorType |
+ // kIndex_8_SkColorType |
+ if (!SkCopyPixelsFromCGImage(info, rowBytes, pixels, image)) { |
+ return false; |
+ } |
+ |
+ return true; |
+} |