Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | List of all members
skiagm::ImageFromYUV Class Reference
Inheritance diagram for skiagm::ImageFromYUV:
skiagm::GM

Public Types

enum class  Source { kTextures , kImages }
 
- Public Types inherited from skiagm::GM
enum  Mode { kGM_Mode , kSample_Mode , kBench_Mode }
 
using DrawResult = skiagm::DrawResult
 
using GraphiteTestContext = skiatest::graphite::GraphiteTestContext
 

Public Member Functions

 ImageFromYUV (Source source)
 
- Public Member Functions inherited from skiagm::GM
 GM (SkColor backgroundColor=SK_ColorWHITE)
 
virtual ~GM ()
 
void setMode (Mode mode)
 
Mode getMode () const
 
DrawResult gpuSetup (SkCanvas *, SkString *errorMsg, GraphiteTestContext *=nullptr)
 
void gpuTeardown ()
 
void onceBeforeDraw ()
 
DrawResult draw (SkCanvas *canvas)
 
DrawResult draw (SkCanvas *, SkString *errorMsg)
 
void drawBackground (SkCanvas *)
 
DrawResult drawContent (SkCanvas *canvas)
 
DrawResult drawContent (SkCanvas *, SkString *errorMsg)
 
virtual bool runAsBench () const
 
SkScalar width ()
 
SkScalar height ()
 
SkColor getBGColor () const
 
void setBGColor (SkColor)
 
void drawSizeBounds (SkCanvas *, SkColor)
 
bool animate (double)
 
virtual bool onChar (SkUnichar)
 
bool getControls (SkMetaData *controls)
 
void setControls (const SkMetaData &controls)
 
virtual void modifyGrContextOptions (GrContextOptions *)
 
virtual void modifyGraphiteContextOptions (skgpu::graphite::ContextOptions *) const
 
virtual bool isBazelOnly () const
 
virtual std::map< std::string, std::string > getGoldKeys () const
 

Protected Member Functions

SkString getName () const override
 
SkISize getISize () override
 
sk_sp< SkImagemakeYUVAImage (GrDirectContext *context, skgpu::graphite::Recorder *recorder)
 
sk_sp< SkImagecreateReferenceImage (GrDirectContext *dContext, skgpu::graphite::Recorder *recorder)
 
DrawResult onGpuSetup (SkCanvas *canvas, SkString *errorMsg, GraphiteTestContext *) override
 
void onGpuTeardown () override
 
SkImagegetYUVAImage (int index)
 
void onDraw (SkCanvas *canvas) override
 
- Protected Member Functions inherited from skiagm::GM
virtual void onOnceBeforeDraw ()
 
virtual DrawResult onDraw (SkCanvas *, SkString *errorMsg)
 
virtual bool onAnimate (double)
 
virtual bool onGetControls (SkMetaData *)
 
virtual void onSetControls (const SkMetaData &)
 
GraphiteTestContextgraphiteTestContext () const
 

Static Protected Member Functions

static std::unique_ptr< sk_gpu_test::LazyYUVImageCreatePlanes (const char *name)
 

Additional Inherited Members

- Static Public Attributes inherited from skiagm::GM
static constexpr char kErrorMsg_DrawSkippedGpuOnly []
 

Detailed Description

Definition at line 44 of file imagefromyuvtextures.cpp.

Member Enumeration Documentation

◆ Source

enum class skiagm::ImageFromYUV::Source
strong
Enumerator
kTextures 
kImages 

Definition at line 46 of file imagefromyuvtextures.cpp.

Constructor & Destructor Documentation

◆ ImageFromYUV()

skiagm::ImageFromYUV::ImageFromYUV ( Source  source)
inline

Definition at line 51 of file imagefromyuvtextures.cpp.

51 : fSource(source) {
52 this->setBGColor(0xFFFFFFFF);
53 }
void setBGColor(SkColor)
Definition gm.cpp:159
SkBitmap source
Definition examples.cpp:28

Member Function Documentation

◆ CreatePlanes()

static std::unique_ptr< sk_gpu_test::LazyYUVImage > skiagm::ImageFromYUV::CreatePlanes ( const char *  name)
inlinestaticprotected

Definition at line 66 of file imagefromyuvtextures.cpp.

66 {
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 }
137 return sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps), skgpu::Mipmapped::kYes);
138 }
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static const uint32_t rgba[kNumPixels]
#define SkAssertResult(cond)
Definition SkAssert.h:123
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
#define sk_float_round2int(x)
@ kJPEG_Full_SkYUVColorSpace
describes full range
Definition SkImageInfo.h:69
constexpr uint8_t SkToU8(S x)
Definition SkTo.h:22
void SkColorMatrix_RGB2YUV(SkYUVColorSpace cs, float m[20])
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
@ 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
SkScalar width()
Definition gm.h:159
SkScalar height()
Definition gm.h:162
static bool b
struct MyStruct a[10]
const char * name
Definition fuchsia.cc:50
double y
double x
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition DecodeUtils.h:21
Definition copy.py:1
SkImageInfo makeColorType(SkColorType newColorType) const

◆ createReferenceImage()

sk_sp< SkImage > skiagm::ImageFromYUV::createReferenceImage ( GrDirectContext dContext,
skgpu::graphite::Recorder recorder 
)
inlineprotected

Definition at line 156 of file imagefromyuvtextures.cpp.

157 {
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 }
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
sk_sp< SkImage > makeYUVAImage(GrDirectContext *context, skgpu::graphite::Recorder *recorder)
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)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ getISize()

SkISize skiagm::ImageFromYUV::getISize ( )
inlineoverrideprotectedvirtual

Implements skiagm::GM.

Definition at line 64 of file imagefromyuvtextures.cpp.

64{ return {1950, 800}; }

◆ getName()

SkString skiagm::ImageFromYUV::getName ( ) const
inlineoverrideprotectedvirtual

Implements skiagm::GM.

Definition at line 56 of file imagefromyuvtextures.cpp.

56 {
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 }
#define SkUNREACHABLE
Definition SkAssert.h:135

◆ getYUVAImage()

SkImage * skiagm::ImageFromYUV::getYUVAImage ( int  index)
inlineprotected

Definition at line 246 of file imagefromyuvtextures.cpp.

246 {
247 SkASSERT(index >= 0 && index < kNumImages);
248 return fYUVAImages[index].get();
249 }
#define SkASSERT(cond)
Definition SkAssert.h:116
T * get() const
Definition SkRefCnt.h:303

◆ makeYUVAImage()

sk_sp< SkImage > skiagm::ImageFromYUV::makeYUVAImage ( GrDirectContext context,
skgpu::graphite::Recorder recorder 
)
inlineprotected

Definition at line 140 of file imagefromyuvtextures.cpp.

140 {
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 }
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35

◆ onDraw()

void skiagm::ImageFromYUV::onDraw ( SkCanvas canvas)
inlineoverrideprotectedvirtual

Reimplemented from skiagm::GM.

Definition at line 251 of file imagefromyuvtextures.cpp.

251 {
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 }
static void draw_image(SkCanvas *canvas, SkImage *img)
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
#define SkIntToScalar(x)
Definition SkScalar.h:57
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
@ 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)
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
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
DrawResult draw(SkCanvas *canvas)
Definition gm.h:140
SkImage * getYUVAImage(int index)
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350
SkSamplingOptions sampling
Definition SkRecords.h:337
dst
Definition cp.py:12
const Scalar scale
static constexpr SkCubicResampler CatmullRom()
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

◆ onGpuSetup()

DrawResult skiagm::ImageFromYUV::onGpuSetup ( SkCanvas canvas,
SkString errorMsg,
GraphiteTestContext  
)
inlineoverrideprotectedvirtual

Reimplemented from skiagm::GM.

Definition at line 189 of file imagefromyuvtextures.cpp.

189 {
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 }
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
virtual GrRecordingContext * recordingContext() const
virtual skgpu::graphite::Recorder * recorder() const
static std::unique_ptr< sk_gpu_test::LazyYUVImage > CreatePlanes(const char *name)
sk_sp< SkImage > createReferenceImage(GrDirectContext *dContext, skgpu::graphite::Recorder *recorder)

◆ onGpuTeardown()

void skiagm::ImageFromYUV::onGpuTeardown ( )
inlineoverrideprotectedvirtual

Reimplemented from skiagm::GM.

Definition at line 238 of file imagefromyuvtextures.cpp.

238 {
239 fLazyYUVImage.reset();
240 for (sk_sp<SkImage>& image : fYUVAImages) {
241 image.reset();
242 }
243 fReferenceImage.reset();
244 }
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310

The documentation for this class was generated from the following file: