Debian Codegraph @

gm/readpixels.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
8 #include "gm/gm.h"
9 #include "include/codec/SkCodec.h"
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPicture.h"
18 #include "include/core/SkPictureRecorder.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkStream.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkTypes.h"
25 #include "modules/skcms/skcms.h"
26 #include "tools/Resources.h"
27
28 #if defined(SK_GANESH)
29 #include "include/gpu/ganesh/GrDirectContext.h"
30 #include "include/gpu/ganesh/SkImageGanesh.h"
31 #endif
32
33 #include <string.h>
34 #include <memory>
35 #include <utility>
36
37 static const int kWidth = 64;
38 static const int kHeight = 64;
39
40 static sk_sp<SkImage> make_raster_image(SkColorType colorType) {
41 std::unique_ptr<SkStream> stream(GetResourceAsStream("images/google_chrome.ico"));
42 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromStream(std::move(stream));
43 if (!codec) {
44 return nullptr;
45 }
46
47 SkImageInfo info = codec->getInfo().makeWH(kWidth, kHeight)
48 .makeColorType(colorType)
49 .makeAlphaType(kPremul_SkAlphaType);
50 return std::get<0>(codec->getImage(info));
51 }
52
53 static sk_sp<SkImage> make_codec_image() {
54 sk_sp<SkData> encoded = GetResourceAsData("images/randPixels.png");
55 return SkImages::DeferredFromEncodedData(encoded);
56 }
57
58 static void draw_contents(SkCanvas* canvas) {
59 SkPaint paint;
60 paint.setStyle(SkPaint::kStroke_Style);
61 paint.setStrokeWidth(20);
62 paint.setColor(0xFF800000);
63 canvas->drawCircle(40, 40, 35, paint);
64 paint.setColor(0xFF008000);
65 canvas->drawCircle(50, 50, 35, paint);
66 paint.setColor(0xFF000080);
67 canvas->drawCircle(60, 60, 35, paint);
68 }
69
70 static sk_sp<SkImage> make_picture_image() {
71 SkPictureRecorder recorder;
72 draw_contents(recorder.beginRecording(SkRect::MakeIWH(kWidth, kHeight)));
73 return SkImages::DeferredFromPicture(recorder.finishRecordingAsPicture(),
74 SkISize::Make(kWidth, kHeight),
75 nullptr,
76 nullptr,
77 SkImages::BitDepth::kU8,
78 SkColorSpace::MakeSRGB());
79 }
80
81 static sk_sp<SkColorSpace> make_parametric_transfer_fn(const SkColorSpacePrimaries& primaries) {
82 skcms_Matrix3x3 toXYZD50;
83 SkAssertResult(primaries.toXYZD50(&toXYZD50));
84 skcms_TransferFunction fn = { 1.8f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f };
85 return SkColorSpace::MakeRGB(fn, toXYZD50);
86 }
87
88 static sk_sp<SkColorSpace> make_wide_gamut() {
89 // ProPhoto
90 SkColorSpacePrimaries primaries;
91 primaries.fRX = 0.7347f;
92 primaries.fRY = 0.2653f;
93 primaries.fGX = 0.1596f;
94 primaries.fGY = 0.8404f;
95 primaries.fBX = 0.0366f;
96 primaries.fBY = 0.0001f;
97 primaries.fWX = 0.34567f;
98 primaries.fWY = 0.35850f;
99 return make_parametric_transfer_fn(primaries);
100 }
101
102 static sk_sp<SkColorSpace> make_small_gamut() {
103 SkColorSpacePrimaries primaries;
104 primaries.fRX = 0.50f;
105 primaries.fRY = 0.33f;
106 primaries.fGX = 0.30f;
107 primaries.fGY = 0.50f;
108 primaries.fBX = 0.25f;
109 primaries.fBY = 0.16f;
110 primaries.fWX = 0.3127f;
111 primaries.fWY = 0.3290f;
112 return make_parametric_transfer_fn(primaries);
113 }
114
115 static void draw_image(GrDirectContext* dContext, SkCanvas* canvas, SkImage* image,
116 SkColorType dstColorType, SkAlphaType dstAlphaType,
117 sk_sp<SkColorSpace> dstColorSpace, SkImage::CachingHint hint) {
118 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(dstColorType);
119 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
120 SkImageInfo dstInfo = SkImageInfo::Make(image->width(), image->height(), dstColorType,
121 dstAlphaType, dstColorSpace);
122 if (!image->readPixels(dContext, dstInfo, data->writable_data(), rowBytes, 0, 0, hint)) {
123 memset(data->writable_data(), 0, rowBytes * image->height());
124 }
125
126 // Now that we have called readPixels(), dump the raw pixels into an srgb image.
127 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
128 sk_sp<SkImage> raw = SkImages::RasterFromData(dstInfo.makeColorSpace(srgb), data, rowBytes);
129 canvas->drawImage(raw.get(), 0.0f, 0.0f);
130 }
131
132 class ReadPixelsGM : public skiagm::GM {
133 public:
134 ReadPixelsGM() {}
135
136 protected:
137 SkString getName() const override { return SkString("readpixels"); }
138
139 SkISize getISize() override { return SkISize::Make(6 * kWidth, 9 * kHeight); }
140
141 void onDraw(SkCanvas* canvas) override {
142 const SkAlphaType alphaTypes[] = {
143 kUnpremul_SkAlphaType,
144 kPremul_SkAlphaType,
145 };
146 const SkColorType colorTypes[] = {
147 kRGBA_8888_SkColorType,
148 kBGRA_8888_SkColorType,
149 kRGBA_F16_SkColorType,
150 };
151 const sk_sp<SkColorSpace> colorSpaces[] = {
152 make_wide_gamut(),
153 SkColorSpace::MakeSRGB(),
154 make_small_gamut(),
155 };
156
157 for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
158 for (SkColorType srcColorType : colorTypes) {
159 canvas->save();
160 sk_sp<SkImage> image = make_raster_image(srcColorType);
161 if (!image) {
162 continue;
163 }
164 GrDirectContext* dContext = nullptr;
165 #if defined(SK_GANESH)
166 dContext = GrAsDirectContext(canvas->recordingContext());
167 if (dContext) {
168 image = SkImages::TextureFromImage(dContext, image);
169 }
170 #endif
171 if (image) {
172 for (SkColorType dstColorType : colorTypes) {
173 for (SkAlphaType dstAlphaType : alphaTypes) {
174 draw_image(dContext, canvas, image.get(), dstColorType, dstAlphaType,
175 dstColorSpace, SkImage::kAllow_CachingHint);
176 canvas->translate((float)kWidth, 0.0f);
177 }
178 }
179 }
180 canvas->restore();
181 canvas->translate(0.0f, (float) kHeight);
182 }
183 }
184 }
185
186 private:
187 using INHERITED = skiagm::GM;
188 };
189 DEF_GM( return new ReadPixelsGM; )
190
191 class ReadPixelsCodecGM : public skiagm::GM {
192 public:
193 ReadPixelsCodecGM() {}
194
195 protected:
196 SkString getName() const override { return SkString("readpixelscodec"); }
197
198 SkISize getISize() override {
199 return SkISize::Make(3 * (kEncodedWidth + 1), 12 * (kEncodedHeight + 1));
200 }
201
202 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
203 if (!canvas->imageInfo().colorSpace()) {
204 *errorMsg = "This gm is only interesting in color correct modes.";
205 return DrawResult::kSkip;
206 }
207
208 const SkAlphaType alphaTypes[] = {
209 kUnpremul_SkAlphaType,
210 kPremul_SkAlphaType,
211 };
212 const SkColorType colorTypes[] = {
213 kRGBA_8888_SkColorType,
214 kBGRA_8888_SkColorType,
215 kRGBA_F16_SkColorType,
216 };
217 const sk_sp<SkColorSpace> colorSpaces[] = {
218 make_wide_gamut(),
219 SkColorSpace::MakeSRGB(),
220 make_small_gamut(),
221 };
222 const SkImage::CachingHint hints[] = {
223 SkImage::kAllow_CachingHint,
224 SkImage::kDisallow_CachingHint,
225 };
226
227 sk_sp<SkImage> image = make_codec_image();
228 for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
229 canvas->save();
230 for (SkColorType dstColorType : colorTypes) {
231 for (SkAlphaType dstAlphaType : alphaTypes) {
232 for (SkImage::CachingHint hint : hints) {
233 draw_image(nullptr, canvas, image.get(), dstColorType, dstAlphaType,
234 dstColorSpace, hint);
235 canvas->translate(0.0f, (float) kEncodedHeight + 1);
236 }
237 }
238 }
239 canvas->restore();
240 canvas->translate((float) kEncodedWidth + 1, 0.0f);
241 }
242 return DrawResult::kOk;
243 }
244
245 private:
246 static const int kEncodedWidth = 8;
247 static const int kEncodedHeight = 8;
248
249 using INHERITED = skiagm::GM;
250 };
251 DEF_GM( return new ReadPixelsCodecGM; )
252
253 class ReadPixelsPictureGM : public skiagm::GM {
254 public:
255 ReadPixelsPictureGM() {}
256
257 protected:
258 SkString getName() const override { return SkString("readpixelspicture"); }
259
260 SkISize getISize() override { return SkISize::Make(3 * kWidth, 12 * kHeight); }
261
262 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
263 if (!canvas->imageInfo().colorSpace()) {
264 *errorMsg = "This gm is only interesting in color correct modes.";
265 return DrawResult::kSkip;
266 }
267
268 const sk_sp<SkImage> images[] = {
269 make_picture_image(),
270 };
271 const SkAlphaType alphaTypes[] = {
272 kUnpremul_SkAlphaType,
273 kPremul_SkAlphaType,
274 };
275 const SkColorType colorTypes[] = {
276 kRGBA_8888_SkColorType,
277 kBGRA_8888_SkColorType,
278 kRGBA_F16_SkColorType,
279 };
280 const sk_sp<SkColorSpace> colorSpaces[] = {
281 make_wide_gamut(),
282 SkColorSpace::MakeSRGB(),
283 make_small_gamut(),
284 };
285 const SkImage::CachingHint hints[] = {
286 SkImage::kAllow_CachingHint,
287 SkImage::kDisallow_CachingHint,
288 };
289
290 for (const sk_sp<SkImage>& image : images) {
291 for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
292 canvas->save();
293 for (SkColorType dstColorType : colorTypes) {
294 for (SkAlphaType dstAlphaType : alphaTypes) {
295 for (SkImage::CachingHint hint : hints) {
296 draw_image(nullptr, canvas, image.get(), dstColorType, dstAlphaType,
297 dstColorSpace, hint);
298 canvas->translate(0.0f, (float) kHeight);
299 }
300 }
301 }
302 canvas->restore();
303 canvas->translate((float) kWidth, 0.0f);
304 }
305 }
306 return DrawResult::kOk;
307 }
308
309 private:
310
311 using INHERITED = skiagm::GM;
312 };
313 DEF_GM( return new ReadPixelsPictureGM; )