msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkImageGeneratorCG.h" |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 9 | #include "SkPixmapPriv.h" |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 10 | |
| 11 | #ifdef SK_BUILD_FOR_MAC |
| 12 | #include <ApplicationServices/ApplicationServices.h> |
| 13 | #endif |
| 14 | |
| 15 | #ifdef SK_BUILD_FOR_IOS |
| 16 | #include <CoreGraphics/CoreGraphics.h> |
| 17 | #include <ImageIO/ImageIO.h> |
| 18 | #include <MobileCoreServices/MobileCoreServices.h> |
| 19 | #endif |
| 20 | |
| 21 | static CGImageSourceRef data_to_CGImageSrc(SkData* data) { |
| 22 | CGDataProviderRef cgData = CGDataProviderCreateWithData(data, data->data(), data->size(), |
| 23 | nullptr); |
| 24 | if (!cgData) { |
| 25 | return nullptr; |
| 26 | } |
| 27 | CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(cgData, 0); |
| 28 | CGDataProviderRelease(cgData); |
| 29 | return imageSrc; |
| 30 | } |
| 31 | |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 32 | #ifdef SK_LEGACY_NEW_FROM_ENCODED_CG |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 33 | SkImageGenerator* SkImageGeneratorCG::NewFromEncodedCG(SkData* data) { |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 34 | return MakeFromEncodedCG(sk_ref_sp(data)).release(); |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | std::unique_ptr<SkImageGenerator> SkImageGeneratorCG::MakeFromEncodedCG(sk_sp<SkData> data) { |
| 39 | CGImageSourceRef imageSrc = data_to_CGImageSrc(data.get()); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 40 | if (!imageSrc) { |
| 41 | return nullptr; |
| 42 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 43 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 44 | // Make sure we call CFRelease to free the imageSrc. Since CFRelease actually takes |
| 45 | // a const void*, we must cast the imageSrc to a const void*. |
| 46 | SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc); |
| 47 | |
| 48 | CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0, nullptr); |
| 49 | if (!properties) { |
| 50 | return nullptr; |
| 51 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 52 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 53 | CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
| 54 | kCGImagePropertyPixelWidth)); |
| 55 | CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
| 56 | kCGImagePropertyPixelHeight)); |
| 57 | if (nullptr == widthRef || nullptr == heightRef) { |
| 58 | return nullptr; |
| 59 | } |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 60 | |
| 61 | int width, height; |
| 62 | if (!CFNumberGetValue(widthRef, kCFNumberIntType, &width) || |
| 63 | !CFNumberGetValue(heightRef, kCFNumberIntType, &height)) { |
| 64 | return nullptr; |
| 65 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 66 | |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 67 | bool hasAlpha = (bool) (CFDictionaryGetValue(properties, |
| 68 | kCGImagePropertyHasAlpha)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 69 | SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType; |
Matt Sarett | 1e86044d | 2016-12-16 09:02:18 -0500 | [diff] [blame] | 70 | SkImageInfo info = SkImageInfo::MakeS32(width, height, alphaType); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 71 | |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 72 | auto origin = kDefault_SkEncodedOrigin; |
| 73 | auto orientationRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
| 74 | kCGImagePropertyOrientation)); |
| 75 | int originInt; |
| 76 | if (orientationRef && CFNumberGetValue(orientationRef, kCFNumberIntType, &originInt)) { |
| 77 | origin = (SkEncodedOrigin) originInt; |
| 78 | } |
| 79 | |
| 80 | if (SkPixmapPriv::ShouldSwapWidthHeight(origin)) { |
| 81 | info = SkPixmapPriv::SwapWidthHeight(info); |
| 82 | } |
| 83 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 84 | // FIXME: We have the opportunity to extract color space information here, |
| 85 | // though I think it makes sense to wait until we understand how |
| 86 | // we want to communicate it to the generator. |
| 87 | |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 88 | return std::unique_ptr<SkImageGenerator>(new SkImageGeneratorCG(info, autoImageSrc.release(), |
| 89 | std::move(data), origin)); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 92 | SkImageGeneratorCG::SkImageGeneratorCG(const SkImageInfo& info, const void* imageSrc, |
| 93 | sk_sp<SkData> data, SkEncodedOrigin origin) |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 94 | : INHERITED(info) |
| 95 | , fImageSrc(imageSrc) |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 96 | , fData(std::move(data)) |
| 97 | , fOrigin(origin) |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 98 | {} |
| 99 | |
Brian Osman | 2feb796 | 2017-04-25 16:41:47 -0400 | [diff] [blame] | 100 | SkData* SkImageGeneratorCG::onRefEncodedData() { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 101 | return SkRef(fData.get()); |
| 102 | } |
| 103 | |
| 104 | bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 105 | const Options&) { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 106 | if (kN32_SkColorType != info.colorType()) { |
| 107 | // FIXME: Support other colorTypes. |
| 108 | return false; |
| 109 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 110 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 111 | switch (info.alphaType()) { |
| 112 | case kOpaque_SkAlphaType: |
| 113 | if (kOpaque_SkAlphaType != this->getInfo().alphaType()) { |
| 114 | return false; |
| 115 | } |
| 116 | break; |
| 117 | case kPremul_SkAlphaType: |
| 118 | break; |
| 119 | default: |
| 120 | return false; |
| 121 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 122 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 123 | CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImageSrc.get(), 0, |
| 124 | nullptr); |
| 125 | if (!image) { |
| 126 | return false; |
| 127 | } |
| 128 | SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image); |
| 129 | |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame^] | 130 | SkPixmap dst(info, pixels, rowBytes); |
| 131 | auto decode = [&image](const SkPixmap& pm) { |
| 132 | // FIXME: Using SkCopyPixelsFromCGImage (as opposed to swizzling |
| 133 | // ourselves) greatly restricts the color and alpha types that we |
| 134 | // support. If we swizzle ourselves, we can add support for: |
| 135 | // kUnpremul_SkAlphaType |
| 136 | // 16-bit per component RGBA |
| 137 | // kGray_8_SkColorType |
| 138 | // Additionally, it would be interesting to compare the performance |
| 139 | // of SkSwizzler with CG's built in swizzler. |
| 140 | return SkCopyPixelsFromCGImage(pm, image); |
| 141 | }; |
| 142 | return SkPixmapPriv::Orient(dst, fOrigin, decode); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 143 | } |