Debian Codegraph @

src/ports/SkImageGeneratorCG.cpp

raw
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 #include "include/ports/SkImageGeneratorCG.h"
8
9 #include "include/codec/SkEncodedOrigin.h"
10 #include "include/private/base/SkTemplates.h"
11 #include "include/utils/mac/SkCGUtils.h"
12 #include "src/codec/SkPixmapUtilsPriv.h"
13 #include "src/utils/mac/SkUniqueCFRef.h"
14
15 #ifdef SK_BUILD_FOR_MAC
16 #include <ApplicationServices/ApplicationServices.h>
17 #endif
18
19 #ifdef SK_BUILD_FOR_IOS
20 #include <CoreGraphics/CoreGraphics.h>
21 #include <ImageIO/ImageIO.h>
22 #include <MobileCoreServices/MobileCoreServices.h>
23 #endif
24
25 namespace {
26 class ImageGeneratorCG : public SkImageGenerator {
27 public:
28 ImageGeneratorCG(const SkImageInfo&,
29 SkUniqueCFRef<CGImageSourceRef> imageSrc,
30 sk_sp<const SkData> data,
31 SkEncodedOrigin);
32
33 protected:
34 sk_sp<const SkData> onRefEncodedData() override;
35 bool onGetPixels(const SkImageInfo&, void* pixels, size_t rowBytes, const Options&) override;
36
37 private:
38 const SkUniqueCFRef<CGImageSourceRef> fImageSrc;
39 const sk_sp<const SkData> fData;
40 const SkEncodedOrigin fOrigin;
41
42 using INHERITED = SkImageGenerator;
43 };
44
45 static SkUniqueCFRef<CGImageSourceRef> data_to_CGImageSrc(const SkData* data) {
46 // We disable cgData from freeing the memory because data's lifetime will
47 // be tracked with the fData field.
48 SkUniqueCFRef<CGDataProviderRef> cgData(
49 CGDataProviderCreateWithData(nullptr, data->data(), data->size(), nullptr));
50 if (!cgData) {
51 return nullptr;
52 }
53 return SkUniqueCFRef<CGImageSourceRef>(
54 CGImageSourceCreateWithDataProvider(cgData.get(), nullptr));
55 }
56
57 } // namespace
58
59 std::unique_ptr<SkImageGenerator> SkImageGeneratorCG::MakeFromEncodedCG(sk_sp<const SkData> data) {
60 SkUniqueCFRef<CGImageSourceRef> imageSrc = data_to_CGImageSrc(data.get());
61 if (!imageSrc) {
62 return nullptr;
63 }
64
65 SkUniqueCFRef<CFDictionaryRef> properties(
66 CGImageSourceCopyPropertiesAtIndex(imageSrc.get(), 0, nullptr));
67 if (!properties) {
68 return nullptr;
69 }
70
71 CFNumberRef widthRef = static_cast<CFNumberRef>(
72 CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelWidth));
73 CFNumberRef heightRef = static_cast<CFNumberRef>(
74 CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelHeight));
75 if (nullptr == widthRef || nullptr == heightRef) {
76 return nullptr;
77 }
78
79 int width, height;
80 if (!CFNumberGetValue(widthRef , kCFNumberIntType, &width ) ||
81 !CFNumberGetValue(heightRef, kCFNumberIntType, &height))
82 {
83 return nullptr;
84 }
85
86 bool hasAlpha = bool(CFDictionaryGetValue(properties.get(), kCGImagePropertyHasAlpha));
87 SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType;
88 SkImageInfo info = SkImageInfo::MakeS32(width, height, alphaType);
89
90 SkEncodedOrigin origin = kDefault_SkEncodedOrigin;
91 CFNumberRef orientationRef = static_cast<CFNumberRef>(
92 CFDictionaryGetValue(properties.get(), kCGImagePropertyOrientation));
93 int originInt;
94 if (orientationRef && CFNumberGetValue(orientationRef, kCFNumberIntType, &originInt)) {
95 origin = (SkEncodedOrigin) originInt;
96 }
97
98 if (SkEncodedOriginSwapsWidthHeight(origin)) {
99 info = SkPixmapUtils::SwapWidthHeight(info);
100 }
101
102 // FIXME: We have the opportunity to extract color space information here,
103 // though I think it makes sense to wait until we understand how
104 // we want to communicate it to the generator.
105
106 return std::unique_ptr<SkImageGenerator>(new ImageGeneratorCG(info, std::move(imageSrc),
107 std::move(data), origin));
108 }
109
110 ImageGeneratorCG::ImageGeneratorCG(const SkImageInfo& info,
111 SkUniqueCFRef<CGImageSourceRef> src,
112 sk_sp<const SkData> data,
113 SkEncodedOrigin origin)
114 : INHERITED(info), fImageSrc(std::move(src)), fData(std::move(data)), fOrigin(origin) {}
115
116 sk_sp<const SkData> ImageGeneratorCG::onRefEncodedData() { return fData; }
117
118 bool ImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
119 const Options&)
120 {
121 if (kN32_SkColorType != info.colorType()) {
122 // FIXME: Support other colorTypes.
123 return false;
124 }
125
126 switch (info.alphaType()) {
127 case kOpaque_SkAlphaType:
128 if (kOpaque_SkAlphaType != this->getInfo().alphaType()) {
129 return false;
130 }
131 break;
132 case kPremul_SkAlphaType:
133 break;
134 default:
135 return false;
136 }
137
138 SkUniqueCFRef<CGImageRef> image(CGImageSourceCreateImageAtIndex(fImageSrc.get(), 0, nullptr));
139 if (!image) {
140 return false;
141 }
142
143 SkPixmap dst(info, pixels, rowBytes);
144 auto decode = [&image](const SkPixmap& pm) {
145 // FIXME: Using SkCopyPixelsFromCGImage (as opposed to swizzling
146 // ourselves) greatly restricts the color and alpha types that we
147 // support. If we swizzle ourselves, we can add support for:
148 // kUnpremul_SkAlphaType
149 // 16-bit per component RGBA
150 // kGray_8_SkColorType
151 // Additionally, it would be interesting to compare the performance
152 // of SkSwizzler with CG's built in swizzler.
153 return SkCopyPixelsFromCGImage(pm, image.get());
154 };
155 return SkPixmapUtils::Orient(dst, fOrigin, decode);
156 }