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 | |
Ben Wagner | bdf5433 | 2018-05-15 14:12:14 -0400 | [diff] [blame^] | 100 | #if SK_IGNORE_SKIMAGE_ONREFENCODED_CHANGE |
Brian Osman | 2feb796 | 2017-04-25 16:41:47 -0400 | [diff] [blame] | 101 | SkData* SkImageGeneratorCG::onRefEncodedData() { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 102 | return SkRef(fData.get()); |
| 103 | } |
Ben Wagner | bdf5433 | 2018-05-15 14:12:14 -0400 | [diff] [blame^] | 104 | #else |
| 105 | sk_sp<SkData> SkImageGeneratorCG::onRefEncodedData() { |
| 106 | return fData; |
| 107 | } |
| 108 | #endif |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 109 | |
| 110 | bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 111 | const Options&) { |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 112 | if (kN32_SkColorType != info.colorType()) { |
| 113 | // FIXME: Support other colorTypes. |
| 114 | return false; |
| 115 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 116 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 117 | switch (info.alphaType()) { |
| 118 | case kOpaque_SkAlphaType: |
| 119 | if (kOpaque_SkAlphaType != this->getInfo().alphaType()) { |
| 120 | return false; |
| 121 | } |
| 122 | break; |
| 123 | case kPremul_SkAlphaType: |
| 124 | break; |
| 125 | default: |
| 126 | return false; |
| 127 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 128 | |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 129 | CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImageSrc.get(), 0, |
| 130 | nullptr); |
| 131 | if (!image) { |
| 132 | return false; |
| 133 | } |
| 134 | SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image); |
| 135 | |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame] | 136 | SkPixmap dst(info, pixels, rowBytes); |
| 137 | auto decode = [&image](const SkPixmap& pm) { |
| 138 | // FIXME: Using SkCopyPixelsFromCGImage (as opposed to swizzling |
| 139 | // ourselves) greatly restricts the color and alpha types that we |
| 140 | // support. If we swizzle ourselves, we can add support for: |
| 141 | // kUnpremul_SkAlphaType |
| 142 | // 16-bit per component RGBA |
| 143 | // kGray_8_SkColorType |
| 144 | // Additionally, it would be interesting to compare the performance |
| 145 | // of SkSwizzler with CG's built in swizzler. |
| 146 | return SkCopyPixelsFromCGImage(pm, image); |
| 147 | }; |
| 148 | return SkPixmapPriv::Orient(dst, fOrigin, decode); |
msarett | 1897631 | 2016-03-09 14:20:58 -0800 | [diff] [blame] | 149 | } |