Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
readpixels.cpp
Go to the documentation of this file.
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"
13#include "include/core/SkData.h"
19#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
27#include "modules/skcms/skcms.h"
28#include "tools/Resources.h"
29
30#include <string.h>
31#include <memory>
32#include <utility>
33
34static const int kWidth = 64;
35static const int kHeight = 64;
36
38 std::unique_ptr<SkStream> stream(GetResourceAsStream("images/google_chrome.ico"));
39 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromStream(std::move(stream));
40 if (!codec) {
41 return nullptr;
42 }
43
44 SkImageInfo info = codec->getInfo().makeWH(kWidth, kHeight)
47 return std::get<0>(codec->getImage(info));
48}
49
51 sk_sp<SkData> encoded = GetResourceAsData("images/randPixels.png");
53}
54
55static void draw_contents(SkCanvas* canvas) {
58 paint.setStrokeWidth(20);
59 paint.setColor(0xFF800000);
60 canvas->drawCircle(40, 40, 35, paint);
61 paint.setColor(0xFF008000);
62 canvas->drawCircle(50, 50, 35, paint);
63 paint.setColor(0xFF000080);
64 canvas->drawCircle(60, 60, 35, paint);
65}
66
77
79 skcms_Matrix3x3 toXYZD50;
80 SkAssertResult(primaries.toXYZD50(&toXYZD50));
81 skcms_TransferFunction fn = { 1.8f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f };
82 return SkColorSpace::MakeRGB(fn, toXYZD50);
83}
84
86 // ProPhoto
87 SkColorSpacePrimaries primaries;
88 primaries.fRX = 0.7347f;
89 primaries.fRY = 0.2653f;
90 primaries.fGX = 0.1596f;
91 primaries.fGY = 0.8404f;
92 primaries.fBX = 0.0366f;
93 primaries.fBY = 0.0001f;
94 primaries.fWX = 0.34567f;
95 primaries.fWY = 0.35850f;
96 return make_parametric_transfer_fn(primaries);
97}
98
100 SkColorSpacePrimaries primaries;
101 primaries.fRX = 0.50f;
102 primaries.fRY = 0.33f;
103 primaries.fGX = 0.30f;
104 primaries.fGY = 0.50f;
105 primaries.fBX = 0.25f;
106 primaries.fBY = 0.16f;
107 primaries.fWX = 0.3127f;
108 primaries.fWY = 0.3290f;
109 return make_parametric_transfer_fn(primaries);
110}
111
112static void draw_image(GrDirectContext* dContext, SkCanvas* canvas, SkImage* image,
113 SkColorType dstColorType, SkAlphaType dstAlphaType,
114 sk_sp<SkColorSpace> dstColorSpace, SkImage::CachingHint hint) {
115 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(dstColorType);
117 SkImageInfo dstInfo = SkImageInfo::Make(image->width(), image->height(), dstColorType,
118 dstAlphaType, dstColorSpace);
119 if (!image->readPixels(dContext, dstInfo, data->writable_data(), rowBytes, 0, 0, hint)) {
120 memset(data->writable_data(), 0, rowBytes * image->height());
121 }
122
123 // Now that we have called readPixels(), dump the raw pixels into an srgb image.
125 sk_sp<SkImage> raw = SkImages::RasterFromData(dstInfo.makeColorSpace(srgb), data, rowBytes);
126 canvas->drawImage(raw.get(), 0.0f, 0.0f);
127}
128
129class ReadPixelsGM : public skiagm::GM {
130public:
132
133protected:
134 SkString getName() const override { return SkString("readpixels"); }
135
136 SkISize getISize() override { return SkISize::Make(6 * kWidth, 9 * kHeight); }
137
138 void onDraw(SkCanvas* canvas) override {
139 const SkAlphaType alphaTypes[] = {
142 };
143 const SkColorType colorTypes[] = {
147 };
148 const sk_sp<SkColorSpace> colorSpaces[] = {
152 };
153
154 for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
155 for (SkColorType srcColorType : colorTypes) {
156 canvas->save();
157 sk_sp<SkImage> image = make_raster_image(srcColorType);
158 if (!image) {
159 continue;
160 }
161 auto dContext = GrAsDirectContext(canvas->recordingContext());
162 if (dContext) {
164 }
165 if (image) {
166 for (SkColorType dstColorType : colorTypes) {
167 for (SkAlphaType dstAlphaType : alphaTypes) {
168 draw_image(dContext, canvas, image.get(), dstColorType, dstAlphaType,
169 dstColorSpace, SkImage::kAllow_CachingHint);
170 canvas->translate((float)kWidth, 0.0f);
171 }
172 }
173 }
174 canvas->restore();
175 canvas->translate(0.0f, (float) kHeight);
176 }
177 }
178 }
179
180private:
181 using INHERITED = skiagm::GM;
182};
183DEF_GM( return new ReadPixelsGM; )
184
185class ReadPixelsCodecGM : public skiagm::GM {
186public:
188
189protected:
190 SkString getName() const override { return SkString("readpixelscodec"); }
191
192 SkISize getISize() override {
193 return SkISize::Make(3 * (kEncodedWidth + 1), 12 * (kEncodedHeight + 1));
194 }
195
196 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
197 if (!canvas->imageInfo().colorSpace()) {
198 *errorMsg = "This gm is only interesting in color correct modes.";
199 return DrawResult::kSkip;
200 }
201
202 const SkAlphaType alphaTypes[] = {
205 };
206 const SkColorType colorTypes[] = {
210 };
211 const sk_sp<SkColorSpace> colorSpaces[] = {
215 };
216 const SkImage::CachingHint hints[] = {
219 };
220
222 for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
223 canvas->save();
224 for (SkColorType dstColorType : colorTypes) {
225 for (SkAlphaType dstAlphaType : alphaTypes) {
226 for (SkImage::CachingHint hint : hints) {
227 draw_image(nullptr, canvas, image.get(), dstColorType, dstAlphaType,
228 dstColorSpace, hint);
229 canvas->translate(0.0f, (float) kEncodedHeight + 1);
230 }
231 }
232 }
233 canvas->restore();
234 canvas->translate((float) kEncodedWidth + 1, 0.0f);
235 }
236 return DrawResult::kOk;
237 }
238
239private:
240 static const int kEncodedWidth = 8;
241 static const int kEncodedHeight = 8;
242
243 using INHERITED = skiagm::GM;
244};
245DEF_GM( return new ReadPixelsCodecGM; )
246
247class ReadPixelsPictureGM : public skiagm::GM {
248public:
250
251protected:
252 SkString getName() const override { return SkString("readpixelspicture"); }
253
254 SkISize getISize() override { return SkISize::Make(3 * kWidth, 12 * kHeight); }
255
256 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
257 if (!canvas->imageInfo().colorSpace()) {
258 *errorMsg = "This gm is only interesting in color correct modes.";
259 return DrawResult::kSkip;
260 }
261
262 const sk_sp<SkImage> images[] = {
264 };
265 const SkAlphaType alphaTypes[] = {
268 };
269 const SkColorType colorTypes[] = {
273 };
274 const sk_sp<SkColorSpace> colorSpaces[] = {
278 };
279 const SkImage::CachingHint hints[] = {
282 };
283
284 for (const sk_sp<SkImage>& image : images) {
285 for (const sk_sp<SkColorSpace>& dstColorSpace : colorSpaces) {
286 canvas->save();
287 for (SkColorType dstColorType : colorTypes) {
288 for (SkAlphaType dstAlphaType : alphaTypes) {
289 for (SkImage::CachingHint hint : hints) {
290 draw_image(nullptr, canvas, image.get(), dstColorType, dstAlphaType,
291 dstColorSpace, hint);
292 canvas->translate(0.0f, (float) kHeight);
293 }
294 }
295 }
296 canvas->restore();
297 canvas->translate((float) kWidth, 0.0f);
298 }
299 }
300 return DrawResult::kOk;
301 }
302
303private:
304
305 using INHERITED = skiagm::GM;
306};
307DEF_GM( return new ReadPixelsPictureGM; )
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
kUnpremul_SkAlphaType
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition Resources.cpp:31
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition Resources.cpp:42
SkAlphaType
Definition SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkAssertResult(cond)
Definition SkAssert.h:123
SkColorType
Definition SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
#define INHERITED(method,...)
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
SkString getName() const override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
SkISize getISize() override
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
SkISize getISize() override
SkString getName() const override
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
virtual GrRecordingContext * recordingContext() const
int save()
Definition SkCanvas.cpp:451
SkImageInfo imageInfo() const
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
static std::unique_ptr< SkCodec > MakeFromStream(std::unique_ptr< SkStream >, SkSpan< const SkCodecs::Decoder > decoders, Result *=nullptr, SkPngChunkReader *=nullptr, SelectionPolicy selectionPolicy=SelectionPolicy::kPreferStillImage)
Definition SkCodec.cpp:163
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkColorSpace > MakeRGB(const skcms_TransferFunction &transferFn, const skcms_Matrix3x3 &toXYZ)
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Definition SkImage.cpp:42
int width() const
Definition SkImage.h:285
int height() const
Definition SkImage.h:291
CachingHint
Definition SkImage.h:463
@ kDisallow_CachingHint
disallows internally caching decoded and copied pixels
Definition SkImage.h:465
@ kAllow_CachingHint
allows internally caching decoded and copied pixels
Definition SkImage.h:464
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
#define DEF_GM(CODE)
Definition gm.h:40
std::array< MockImage, 3 > images
SK_API sk_sp< SkImage > DeferredFromPicture(sk_sp< SkPicture > picture, const SkISize &dimensions, const SkMatrix *matrix, const SkPaint *paint, BitDepth bitDepth, sk_sp< SkColorSpace > colorSpace, SkSurfaceProps props)
SK_API sk_sp< SkImage > RasterFromData(const SkImageInfo &info, sk_sp< SkData > pixels, size_t rowBytes)
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
@ kU8
uses 8-bit unsigned int per color component
DrawResult
Definition gm.h:104
static void draw_image(GrDirectContext *dContext, SkCanvas *canvas, SkImage *image, SkColorType dstColorType, SkAlphaType dstAlphaType, sk_sp< SkColorSpace > dstColorSpace, SkImage::CachingHint hint)
static sk_sp< SkColorSpace > make_wide_gamut()
static const int kHeight
static const int kWidth
static sk_sp< SkImage > make_picture_image()
static sk_sp< SkImage > make_raster_image(SkColorType colorType)
static sk_sp< SkColorSpace > make_parametric_transfer_fn(const SkColorSpacePrimaries &primaries)
static sk_sp< SkImage > make_codec_image()
static sk_sp< SkColorSpace > make_small_gamut()
static void draw_contents(SkCanvas *canvas)
bool toXYZD50(skcms_Matrix3x3 *toXYZD50) const
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
SkImageInfo makeWH(int newWidth, int newHeight) const
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
SkColorSpace * colorSpace() const
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkImageInfo makeColorType(SkColorType newColorType) const
static SkRect MakeIWH(int w, int h)
Definition SkRect.h:623