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

Unified Diff: src/ports/SkImageGeneratorCG.cpp

Issue 1718273004: Add an SkImageGeneratorCG (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Use CGCopyProperties to perform a deferred decode Created 4 years, 10 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
« gyp/ports.gyp ('K') | « src/ports/SkImageGeneratorCG.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/ports/SkImageGeneratorCG.cpp
diff --git a/src/ports/SkImageGeneratorCG.cpp b/src/ports/SkImageGeneratorCG.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..850bb3abcc7007c8453def38c791b05e11167fe5
--- /dev/null
+++ b/src/ports/SkImageGeneratorCG.cpp
@@ -0,0 +1,95 @@
+/*
+ * 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"
+#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
+#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 (nullptr == imageSrc) {
+ return nullptr;
+ }
+ SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc);
+
+ CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0, nullptr);
+ CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties,
+ kCGImagePropertyPixelWidth));
+ CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties,
+ kCGImagePropertyPixelHeight));
+ bool hasAlpha = (bool) (CFDictionaryGetValue(properties,
+ kCGImagePropertyHasAlpha));
reed1 2016/03/02 01:45:42 Do we know that hasalpha means its not opaque? Do
msarett 2016/03/04 23:48:33 I tested this experimentally. We can trust hasAlp
+
+ int width, height;
+ CFNumberGetValue(widthRef, kCFNumberIntType, &width);
+ CFNumberGetValue(heightRef, kCFNumberIntType, &height);
+
+ SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType;
+ SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, alphaType);
reed1 2016/03/02 01:45:42 It appears that we can query properties to discove
msarett 2016/03/04 23:48:33 Strangely, we can't query for unpremul/premul unti
+ 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) {
+ CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImageSrc.get(), 0,
+ nullptr);
+ if (nullptr == image) {
+ return false;
+ }
+ SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image);
+
+ if (!SkCopyPixelsFromCGImage(info, rowBytes, pixels, image)) {
+ return false;
+ }
+
+ // CG cannot produce unpremultiplied outputs, so if the client requests
+ // kUnpremul, we must unpremultiply.
scroggo 2016/03/02 13:08:14 I think this is the same as the old one, but this
msarett 2016/03/04 23:48:33 Removed this code.
+ if (kUnpremul_SkAlphaType == info.alphaType()) {
+ SkPMColor* dstRow = (SkPMColor*) pixels;
+ for (int y = 0; y < info.height(); y++) {
+ for (int x = 0; x < info.width(); x++) {
+ dstRow[x] = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(dstRow[x]);
+ }
+ dstRow = SkTAddOffset<SkPMColor>(dstRow, rowBytes);
+ }
+ }
+
+ return true;
+}
+
+#endif //defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
« gyp/ports.gyp ('K') | « src/ports/SkImageGeneratorCG.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698