Flutter Engine
The Flutter Engine
imagefromyuvtextures.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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// This test only works with the GPU backend.
9
10#include "gm/gm.h"
22#include "include/core/SkSize.h"
29#include "include/gpu/GrTypes.h"
32#include "src/base/SkMathPriv.h"
33#include "src/core/SkYUVMath.h"
35#include "tools/DecodeUtils.h"
36#include "tools/Resources.h"
37#include "tools/gpu/YUVUtils.h"
38
39#if defined(SK_GRAPHITE)
41#endif
42
43namespace skiagm {
44class ImageFromYUV : public GM {
45public:
46 enum class Source {
47 kTextures,
48 kImages,
49 };
50
52 this->setBGColor(0xFFFFFFFF);
53 }
54
55protected:
56 SkString getName() const override {
57 switch (fSource) {
58 case Source::kTextures: return SkString("image_from_yuv_textures");
59 case Source::kImages: return SkString("image_from_yuv_images");
60 }
62 }
63
64 SkISize getISize() override { return {1950, 800}; }
65
66 static std::unique_ptr<sk_gpu_test::LazyYUVImage> CreatePlanes(const char* name) {
67 SkBitmap bmp;
69 return {};
70 }
71 if (bmp.colorType() != kRGBA_8888_SkColorType) {
74 copy.allocPixels(info);
75 SkAssertResult(bmp.readPixels(copy.pixmap()));
76 bmp = copy;
77 }
78 SkYUVAPixmapInfo pixmapInfo({bmp.dimensions(),
83 nullptr);
84 auto pixmaps = SkYUVAPixmaps::Allocate(pixmapInfo);
85
86 unsigned char* yuvPixels[] = {
87 static_cast<unsigned char*>(pixmaps.planes()[0].writable_addr()),
88 static_cast<unsigned char*>(pixmaps.planes()[1].writable_addr()),
89 static_cast<unsigned char*>(pixmaps.planes()[2].writable_addr()),
90 static_cast<unsigned char*>(pixmaps.planes()[3].writable_addr()),
91 };
92
93 float m[20];
94 SkColorMatrix_RGB2YUV(pixmaps.yuvaInfo().yuvColorSpace(), m);
95 // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
96 // we will draw it with all the supported yuv color spaces when converted back to RGB
97 for (int j = 0; j < pixmaps.planes()[0].height(); ++j) {
98 for (int i = 0; i < pixmaps.planes()[0].width(); ++i) {
99 auto rgba = *bmp.getAddr32(i, j);
100 auto r = (rgba & 0x000000ff) >> 0;
101 auto g = (rgba & 0x0000ff00) >> 8;
102 auto b = (rgba & 0x00ff0000) >> 16;
103 auto a = (rgba & 0xff000000) >> 24;
104 yuvPixels[0][j*pixmaps.planes()[0].width() + i] = SkToU8(
105 sk_float_round2int(m[0]*r + m[1]*g + m[2]*b + m[3]*a + 255*m[4]));
106 yuvPixels[3][j*pixmaps.planes()[0].width() + i] = SkToU8(sk_float_round2int(
107 m[15]*r + m[16]*g + m[17]*b + m[18]*a + 255*m[19]));
108 }
109 }
110 for (int j = 0; j < pixmaps.planes()[1].height(); ++j) {
111 for (int i = 0; i < pixmaps.planes()[1].width(); ++i) {
112 // Average together 4 pixels of RGB.
113 int rgba[] = {0, 0, 0, 0};
114 int denom = 0;
115 int ylimit = std::min(2*j + 2, pixmaps.planes()[0].height());
116 int xlimit = std::min(2*i + 2, pixmaps.planes()[0].width());
117 for (int y = 2*j; y < ylimit; ++y) {
118 for (int x = 2*i; x < xlimit; ++x) {
119 auto src = *bmp.getAddr32(x, y);
120 rgba[0] += (src & 0x000000ff) >> 0;
121 rgba[1] += (src & 0x0000ff00) >> 8;
122 rgba[2] += (src & 0x00ff0000) >> 16;
123 rgba[3] += (src & 0xff000000) >> 24;
124 ++denom;
125 }
126 }
127 for (int c = 0; c < 4; ++c) {
128 rgba[c] /= denom;
129 }
130 int uvIndex = j*pixmaps.planes()[1].width() + i;
131 yuvPixels[1][uvIndex] = SkToU8(sk_float_round2int(
132 m[5]*rgba[0] + m[6]*rgba[1] + m[7]*rgba[2] + m[8]*rgba[3] + 255*m[9]));
133 yuvPixels[2][uvIndex] = SkToU8(sk_float_round2int(
134 m[10]*rgba[0] + m[11]*rgba[1] + m[12]*rgba[2] + m[13]*rgba[3] + 255*m[14]));
135 }
136 }
138 }
139
141 SkASSERT(SkToBool(context) != SkToBool(recorder));
143 switch (fSource) {
146 }
147 if (context) {
148 return fLazyYUVImage->refImage(context, type);
149 }
150#if defined(SK_GRAPHITE)
151 return fLazyYUVImage->refImage(recorder, type);
152#endif
153 return nullptr;
154 }
155
157 skgpu::graphite::Recorder* recorder) {
158 auto planarImage = this->makeYUVAImage(dContext, recorder);
159 if (!planarImage) {
160 return nullptr;
161 }
162
163 auto resultInfo = SkImageInfo::Make(fLazyYUVImage->dimensions(),
166 sk_sp<SkSurface> resultSurface;
167 if (dContext) {
168 resultSurface = SkSurfaces::RenderTarget(dContext,
170 resultInfo,
171 1,
173 nullptr,
174 /*shouldCreateWithMips=*/true);
175 }
176#if defined(SK_GRAPHITE)
177 if (recorder) {
178 resultSurface = SkSurfaces::RenderTarget(recorder, resultInfo, skgpu::Mipmapped::kYes);
179 }
180#endif
181 if (!resultSurface) {
182 return nullptr;
183 }
184
185 resultSurface->getCanvas()->drawImage(std::move(planarImage), 0, 0);
186 return resultSurface->makeImageSnapshot();
187 }
188
190 auto dContext = GrAsDirectContext(canvas->recordingContext());
191 auto* recorder = canvas->recorder();
192
193 if (!recorder && (!dContext || dContext->abandoned())) {
194 *errorMsg = "DirectContext or graphite::Recorder required to create YUV images";
195 return DrawResult::kSkip;
196 }
197
198 if (dContext && !dContext->priv().caps()->mipmapSupport()) {
199 return DrawResult::kSkip;
200 }
201
202 if (fSource == Source::kImages && dContext) {
203 *errorMsg = "YUV Image from SkImage planes not supported with Ganesh.";
204 return DrawResult::kSkip;
205 }
206
207 if (!fLazyYUVImage) {
208 fLazyYUVImage = CreatePlanes("images/mandrill_128.png");
209 }
210
211 // We make a version of this image for each draw because, if any draw flattens it to
212 // RGBA, then all subsequent draws would use the RGBA texture.
213 for (int i = 0; i < kNumImages; ++i) {
214 fYUVAImages[i] = this->makeYUVAImage(dContext, recorder);
215 if (!fYUVAImages[i]) {
216 *errorMsg = "Couldn't create src YUVA image.";
217 return DrawResult::kFail;
218 }
219 }
220
221 fReferenceImage = this->createReferenceImage(dContext, recorder);
222 if (!fReferenceImage) {
223 *errorMsg = "Couldn't create reference YUVA image.";
224 return DrawResult::kFail;
225 }
226
227 if (dContext) {
228 // Some backends (e.g., Vulkan) require all work be completed for backend textures
229 // before they are deleted. Since we don't know when we'll next have access to a
230 // direct context, flush all the work now.
231 dContext->flush();
232 dContext->submit(GrSyncCpu::kYes);
233 }
234
235 return DrawResult::kOk;
236 }
237
238 void onGpuTeardown() override {
239 fLazyYUVImage.reset();
240 for (sk_sp<SkImage>& image : fYUVAImages) {
241 image.reset();
242 }
243 fReferenceImage.reset();
244 }
245
246 SkImage* getYUVAImage(int index) {
247 SkASSERT(index >= 0 && index < kNumImages);
248 return fYUVAImages[index].get();
249 }
250
251 void onDraw(SkCanvas* canvas) override {
252 auto draw_image = [canvas](SkImage* image, const SkSamplingOptions& sampling) -> SkSize {
253 if (!image) {
254 return {0, 0};
255 }
256 canvas->drawImage(image, 0, 0, sampling, nullptr);
258 };
259
260 auto draw_image_rect = [canvas](SkImage* image,
262 if (!image) {
263 return {0, 0};
264 }
265 auto subset = SkRect::Make(image->dimensions());
266 subset.inset(subset.width() * .05f, subset.height() * .1f);
267 auto dst = SkRect::MakeWH(subset.width(), subset.height());
268 canvas->drawImageRect(image, subset, dst, sampling, nullptr,
270 return {dst.width(), dst.height()};
271 };
272
273 auto draw_image_shader = [canvas](SkImage* image,
275 if (!image) {
276 return {0, 0};
277 }
278 SkMatrix m;
279 m.setRotate(45, image->width()/2.f, image->height()/2.f);
282 sampling, m));
283 auto rect = SkRect::MakeWH(image->width() * 1.3f, image->height());
284 canvas->drawRect(rect, paint);
285 return {rect.width(), rect.height()};
286 };
287
288 canvas->translate(kPad, kPad);
289 int imageIndex = 0;
290 using DrawSig = SkSize(SkImage* image, const SkSamplingOptions&);
291 using DF = std::function<DrawSig>;
292 for (const auto& draw : {DF(draw_image), DF(draw_image_rect), DF(draw_image_shader)}) {
293 float wForDrawFunc = 0;
294 canvas->save();
295 for (auto scale : {1.f, 1.5f, 0.3f}) {
296 float hForScale = 0;
297 float wForScale = 0;
298 canvas->save();
299 // We exercise either bicubic or mipmaps depending on the scale.
300 SkSamplingOptions samplings[] = {
303 scale > 1.f
306
307 for (const auto& sampling : samplings) {
308 float yuvAndRefH;
309 canvas->save();
310 canvas->scale(scale, scale);
311 auto s1 = draw(this->getYUVAImage(imageIndex++), sampling);
312 yuvAndRefH = kPad + std::ceil(scale * s1.height());
313 canvas->restore();
314 canvas->save();
315 canvas->translate(0, yuvAndRefH);
316 canvas->scale(scale, scale);
317 auto s2 = draw(fReferenceImage.get(), sampling);
318 yuvAndRefH += std::ceil(scale * s2.height());
319 canvas->restore();
320
321 float thisW = std::ceil(scale * std::max(s1.width(), s2.width()));
322
323 SkPaint outline;
324 outline.setColor(SK_ColorBLACK);
325 outline.setStroke(true);
326 outline.setAntiAlias(false);
327 canvas->drawRect(SkRect::MakeXYWH(-1, -1, thisW + 1, yuvAndRefH + 1), outline);
328
329 thisW += kPad;
330 yuvAndRefH += kPad;
331
332 canvas->translate(thisW, 0);
333
334 wForScale += thisW;
335 hForScale = std::max(hForScale, yuvAndRefH);
336 }
337 canvas->restore();
338 canvas->translate(0, hForScale);
339 wForDrawFunc = std::max(wForScale, wForDrawFunc);
340 }
341 canvas->restore();
342 canvas->translate(wForDrawFunc, 0);
343 }
344 }
345
346private:
347 Source fSource;
348
349 std::unique_ptr<sk_gpu_test::LazyYUVImage> fLazyYUVImage;
350
351 // 3 draws x 3 scales x 4 filter qualities
352 inline static constexpr int kNumImages = 3 * 3 * 4;
353 sk_sp<SkImage> fYUVAImages[kNumImages];
354 sk_sp<SkImage> fReferenceImage;
355
356 inline static constexpr SkScalar kPad = 10.0f;
357
358 using INHERITED = GM;
359};
360
361DEF_GM(return new ImageFromYUV(ImageFromYUV::Source::kTextures);)
362DEF_GM(return new ImageFromYUV(ImageFromYUV::Source::kImages);)
363} // namespace skiagm
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
static void draw_image(SkCanvas *canvas, SkImage *img)
static const uint32_t rgba[kNumPixels]
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkUNREACHABLE
Definition: SkAssert.h:135
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define sk_float_round2int(x)
@ kJPEG_Full_SkYUVColorSpace
describes full range
Definition: SkImageInfo.h:69
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static void copy(void *dst, const uint8_t *src, int width, int bpp, int deltaSrc, int offset, const SkPMColor ctable[])
Definition: SkSwizzler.cpp:31
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
constexpr uint8_t SkToU8(S x)
Definition: SkTo.h:22
void SkColorMatrix_RGB2YUV(SkYUVColorSpace cs, float m[20])
Definition: SkYUVMath.cpp:389
GLenum type
SkISize dimensions() const
Definition: SkBitmap.h:388
SkColorType colorType() const
Definition: SkBitmap.h:160
const SkImageInfo & info() const
Definition: SkBitmap.h:139
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Definition: SkBitmap.cpp:488
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
virtual skgpu::graphite::Recorder * recorder() const
Definition: SkCanvas.cpp:1641
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition: SkCanvas.h:1542
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
int save()
Definition: SkCanvas.cpp:447
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
SkISize dimensions() const
Definition: SkImage.h:297
int width() const
Definition: SkImage.h:285
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
int height() const
Definition: SkImage.h:291
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
void setStroke(bool)
Definition: SkPaint.cpp:115
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
@ kY_U_V_A
Plane 0: Y, Plane 1: U, Plane 2: V, Plane 3: A.
@ k420
1 set of UV values for each 2x2 block of Y values.
@ kUnorm8
8 bit unsigned normalized
static SkYUVAPixmaps Allocate(const SkYUVAPixmapInfo &yuvaPixmapInfo)
static std::unique_ptr< LazyYUVImage > Make(sk_sp< SkData > data, skgpu::Mipmapped=skgpu::Mipmapped::kNo, sk_sp< SkColorSpace >=nullptr)
Definition: YUVUtils.cpp:200
T * get() const
Definition: SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
SkScalar width()
Definition: gm.h:159
SkScalar height()
Definition: gm.h:162
void setBGColor(SkColor)
Definition: gm.cpp:159
DrawResult draw(SkCanvas *canvas)
Definition: gm.h:140
static std::unique_ptr< sk_gpu_test::LazyYUVImage > CreatePlanes(const char *name)
DrawResult onGpuSetup(SkCanvas *canvas, SkString *errorMsg, GraphiteTestContext *) override
SkString getName() const override
SkISize getISize() override
sk_sp< SkImage > makeYUVAImage(GrDirectContext *context, skgpu::graphite::Recorder *recorder)
sk_sp< SkImage > createReferenceImage(GrDirectContext *dContext, skgpu::graphite::Recorder *recorder)
void onDraw(SkCanvas *canvas) override
SkImage * getYUVAImage(int index)
const Paint & paint
Definition: color_source.cc:38
SkBitmap source
Definition: examples.cpp:28
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct a[10]
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
double y
double x
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SkSamplingOptions sampling
Definition: SkRecords.h:337
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition: DecodeUtils.h:21
Definition: copy.py:1
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
dst
Definition: cp.py:12
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
static SkPath scale(const SkPath &path, SkScalar scale)
Definition: patheffects.cpp:68
DrawResult
Definition: gm.h:104
SIN Vec< N, float > ceil(const Vec< N, float > &x)
Definition: SkVx.h:702
static constexpr SkCubicResampler CatmullRom()
Definition: SkSize.h:16
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkImageInfo makeColorType(SkColorType newColorType) const
Definition: SkImageInfo.h:475
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
Definition: SkSize.h:52