• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google LLC
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/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkYUVAInfo.h"
15 #include "include/core/SkYUVAPixmaps.h"
16 #include "include/effects/SkGradientShader.h"
17 #include "include/gpu/GrDirectContext.h"
18 #include "include/gpu/GrRecordingContext.h"
19 #include "src/core/SkAutoPixmapStorage.h"
20 #include "src/core/SkScopeExit.h"
21 #include "tools/Resources.h"
22 #include "tools/ToolUtils.h"
23 #include "tools/gpu/YUVUtils.h"
24 
25 namespace {
26 struct AsyncContext {
27     bool fCalled = false;
28     std::unique_ptr<const SkImage::AsyncReadResult> fResult;
29 };
30 }  // anonymous namespace
31 
32 // Making this a lambda in the test functions caused:
33 //   "error: cannot compile this forwarded non-trivially copyable parameter yet"
34 // on x86/Win/Clang bot, referring to 'result'.
async_callback(void * c,std::unique_ptr<const SkImage::AsyncReadResult> result)35 static void async_callback(void* c, std::unique_ptr<const SkImage::AsyncReadResult> result) {
36     auto context = static_cast<AsyncContext*>(c);
37     context->fResult = std::move(result);
38     context->fCalled = true;
39 };
40 
41 // Draws the image to a surface, does a asyncRescaleAndReadPixels of the image, and then sticks
42 // the result in a raster image.
43 template <typename Src>
do_read_and_scale(Src * src,GrDirectContext * direct,const SkIRect & srcRect,const SkImageInfo & ii,SkImage::RescaleGamma rescaleGamma,SkImage::RescaleMode rescaleMode)44 static sk_sp<SkImage> do_read_and_scale(Src* src,
45                                         GrDirectContext* direct,
46                                         const SkIRect& srcRect,
47                                         const SkImageInfo& ii,
48                                         SkImage::RescaleGamma rescaleGamma,
49                                         SkImage::RescaleMode rescaleMode) {
50     auto* asyncContext = new AsyncContext();
51     src->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, rescaleMode, async_callback,
52                                    asyncContext);
53     if (direct) {
54         direct->submit();
55     }
56     while (!asyncContext->fCalled) {
57         // Only GPU should actually be asynchronous.
58         SkASSERT(direct);
59         direct->checkAsyncWorkCompletion();
60     }
61     if (!asyncContext->fResult) {
62         return nullptr;
63     }
64     SkPixmap pixmap(ii, asyncContext->fResult->data(0), asyncContext->fResult->rowBytes(0));
65     auto releasePixels = [](const void*, void* c) { delete static_cast<AsyncContext*>(c); };
66     return SkImage::MakeFromRaster(pixmap, releasePixels, asyncContext);
67 }
68 
69 template <typename Src>
do_read_and_scale_yuv(Src * src,GrDirectContext * direct,SkYUVColorSpace yuvCS,const SkIRect & srcRect,SkISize size,SkImage::RescaleGamma rescaleGamma,SkImage::RescaleMode rescaleMode,SkScopeExit * cleanup)70 static sk_sp<SkImage> do_read_and_scale_yuv(Src* src,
71                                             GrDirectContext* direct,
72                                             SkYUVColorSpace yuvCS,
73                                             const SkIRect& srcRect,
74                                             SkISize size,
75                                             SkImage::RescaleGamma rescaleGamma,
76                                             SkImage::RescaleMode rescaleMode,
77                                             SkScopeExit* cleanup) {
78     SkASSERT(!(size.width() & 0b1) && !(size.height() & 0b1));
79 
80     SkISize uvSize = {size.width()/2, size.height()/2};
81     SkImageInfo yII  = SkImageInfo::Make(size,   kGray_8_SkColorType, kPremul_SkAlphaType);
82     SkImageInfo uvII = SkImageInfo::Make(uvSize, kGray_8_SkColorType, kPremul_SkAlphaType);
83 
84     AsyncContext asyncContext;
85     src->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, size,
86                                          rescaleGamma, rescaleMode, async_callback, &asyncContext);
87     if (direct) {
88         direct->submit();
89     }
90     while (!asyncContext.fCalled) {
91         // Only GPU should actually be asynchronous.
92         SkASSERT(direct);
93         direct->checkAsyncWorkCompletion();
94     }
95     if (!asyncContext.fResult) {
96         return nullptr;
97     }
98     SkYUVAInfo yuvaInfo(size,
99                         SkYUVAInfo::PlaneConfig::kY_U_V,
100                         SkYUVAInfo::Subsampling::k420,
101                         yuvCS);
102     SkPixmap yuvPMs[] = {
103             {yII,  asyncContext.fResult->data(0), asyncContext.fResult->rowBytes(0)},
104             {uvII, asyncContext.fResult->data(1), asyncContext.fResult->rowBytes(1)},
105             {uvII, asyncContext.fResult->data(2), asyncContext.fResult->rowBytes(2)}
106     };
107     auto pixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, yuvPMs);
108     SkASSERT(pixmaps.isValid());
109     auto lazyYUVImage = sk_gpu_test::LazyYUVImage::Make(pixmaps);
110     SkASSERT(lazyYUVImage);
111     return lazyYUVImage->refImage(direct, sk_gpu_test::LazyYUVImage::Type::kFromTextures);
112 }
113 
114 // Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are
115 // rescale in src gamma and rescale in linear gamma.
116 template <typename Src>
do_rescale_grid(SkCanvas * canvas,Src * src,GrDirectContext * direct,const SkIRect & srcRect,SkISize newSize,bool doYUV420,SkString * errorMsg,int pad=0)117 static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas,
118                                           Src* src,
119                                           GrDirectContext* direct,
120                                           const SkIRect& srcRect,
121                                           SkISize newSize,
122                                           bool doYUV420,
123                                           SkString* errorMsg,
124                                           int pad = 0) {
125     if (doYUV420 && !direct) {
126         errorMsg->printf("YUV420 only supported on direct GPU for now.");
127         return skiagm::DrawResult::kSkip;
128     }
129     if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
130         *errorMsg = "Not supported on recording/vector backends.";
131         return skiagm::DrawResult::kSkip;
132     }
133     const auto ii = canvas->imageInfo().makeDimensions(newSize);
134 
135     SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
136     canvas->save();
137     for (auto gamma : {SkImage::RescaleGamma::kSrc, SkImage::RescaleGamma::kLinear}) {
138         canvas->save();
139         for (auto mode : {
140                 SkImage::RescaleMode::kNearest,
141                 SkImage::RescaleMode::kRepeatedLinear,
142                 SkImage::RescaleMode::kRepeatedCubic}) {
143             SkScopeExit cleanup;
144             sk_sp<SkImage> result;
145             if (doYUV420) {
146                 result = do_read_and_scale_yuv(src, direct, yuvColorSpace, srcRect, newSize, gamma,
147                                                mode, &cleanup);
148                 if (!result) {
149                     errorMsg->printf("YUV420 async call failed. Allowed for now.");
150                     return skiagm::DrawResult::kSkip;
151                 }
152                 int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1);
153                 yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS);
154             } else {
155                 result = do_read_and_scale(src, direct, srcRect, ii, gamma, mode);
156                 if (!result) {
157                     errorMsg->printf("async read call failed.");
158                     return skiagm::DrawResult::kFail;
159                 }
160             }
161             canvas->drawImage(result, 0, 0);
162             canvas->translate(newSize.width() + pad, 0);
163         }
164         canvas->restore();
165         canvas->translate(0, newSize.height() + pad);
166     }
167     canvas->restore();
168     return skiagm::DrawResult::kOk;
169 }
170 
do_rescale_image_grid(SkCanvas * canvas,const char * imageFile,const SkIRect & srcRect,SkISize newSize,bool doSurface,bool doYUV420,SkString * errorMsg)171 static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas,
172                                                 const char* imageFile,
173                                                 const SkIRect& srcRect,
174                                                 SkISize newSize,
175                                                 bool doSurface,
176                                                 bool doYUV420,
177                                                 SkString* errorMsg) {
178     auto image = GetResourceAsImage(imageFile);
179     if (!image) {
180         errorMsg->printf("Could not load image file %s.", imageFile);
181         return skiagm::DrawResult::kFail;
182     }
183     if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
184         *errorMsg = "Not supported on recording/vector backends.";
185         return skiagm::DrawResult::kSkip;
186     }
187 
188     auto dContext = GrAsDirectContext(canvas->recordingContext());
189     if (!dContext && canvas->recordingContext()) {
190         *errorMsg = "Not supported in DDL mode";
191         return skiagm::DrawResult::kSkip;
192     }
193 
194     if (doSurface) {
195         // Turn the image into a surface in order to call the read and rescale API
196         auto surfInfo = image->imageInfo().makeDimensions(image->dimensions());
197         auto surface = canvas->makeSurface(surfInfo);
198         if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) {
199             surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType);
200             surface = canvas->makeSurface(surfInfo);
201         }
202         if (!surface) {
203             *errorMsg = "Could not create surface for image.";
204             // When testing abandoned GrContext we expect surface creation to fail.
205             if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
206                 return skiagm::DrawResult::kSkip;
207             }
208             return skiagm::DrawResult::kFail;
209         }
210         SkPaint paint;
211         paint.setBlendMode(SkBlendMode::kSrc);
212         surface->getCanvas()->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
213         return do_rescale_grid(canvas, surface.get(), dContext, srcRect, newSize,
214                                doYUV420, errorMsg);
215     } else if (dContext) {
216         image = image->makeTextureImage(dContext);
217         if (!image) {
218             *errorMsg = "Could not create image.";
219             // When testing abandoned GrContext we expect surface creation to fail.
220             if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
221                 return skiagm::DrawResult::kSkip;
222             }
223             return skiagm::DrawResult::kFail;
224         }
225     }
226     return do_rescale_grid(canvas, image.get(), dContext, srcRect, newSize, doYUV420,
227                            errorMsg);
228 }
229 
230 #define DEF_RESCALE_AND_READ_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                      \
231     DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
232         ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);          \
233         return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, false,   \
234                                      errorMsg);                                            \
235     }
236 
237 #define DEF_RESCALE_AND_READ_YUV_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                          \
238     DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) {  \
239         ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);                  \
240         return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \
241     }
242 
243 #define DEF_RESCALE_AND_READ_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                       \
244     DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
245         ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);          \
246         return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, false, false,  \
247                                      errorMsg);                                            \
248     }
249 
250 #define DEF_RESCALE_AND_READ_YUV_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                           \
251     DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) {  \
252         ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);                  \
253         return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \
254     }
255 
256 DEF_RESCALE_AND_READ_YUV_SURF_GM(
257         images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150), 410, 376)
258 
259 DEF_RESCALE_AND_READ_YUV_IMG_GM(
260         images/yellow_rose.webp, rose_down, SkIRect::MakeXYWH(50, 5, 200, 150), 106, 60)
261 
262 DEF_RESCALE_AND_READ_SURF_GM(
263         images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100), 410, 410)
264 
265 DEF_RESCALE_AND_READ_SURF_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
266 DEF_RESCALE_AND_READ_IMG_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
267 
268 DEF_RESCALE_AND_READ_IMG_GM(
269         images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637), (int)(0.7 * 105))
270 DEF_RESCALE_AND_READ_SURF_GM(
271         images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637), (int)(1.2 * 105))
272 DEF_RESCALE_AND_READ_IMG_GM(images/text.png,
273                             text_up_large,
274                             SkIRect::MakeXYWH(300, 0, 300, 105),
275                             (int)(2.4 * 300),
276                             (int)(2.4 * 105))
277 
278 // Exercises non-scaling YUV420. Reads from the original canvas's surface in order to
279 // exercise case where source surface is not a texture (in glbert config).
280 DEF_SIMPLE_GM_CAN_FAIL(async_yuv_no_scale, canvas, errorMsg, 400, 300) {
281     auto surface = canvas->getSurface();
282     if (!surface) {
283         *errorMsg = "Not supported on recording/vector backends.";
284         return skiagm::DrawResult::kSkip;
285     }
286 
287     auto dContext = GrAsDirectContext(surface->recordingContext());
288     if (!dContext && surface->recordingContext()) {
289         *errorMsg = "Not supported in DDL mode";
290         return skiagm::DrawResult::kSkip;
291     }
292 
293     auto image = GetResourceAsImage("images/yellow_rose.webp");
294     if (!image) {
295         return skiagm::DrawResult::kFail;
296     }
297     SkPaint paint;
298     canvas->drawImage(image.get(), 0, 0);
299 
300     SkScopeExit scopeExit;
301     auto yuvImage = do_read_and_scale_yuv(
302             surface, dContext, kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300),
303             {400, 300}, SkImage::RescaleGamma::kSrc, SkImage::RescaleMode::kNearest, &scopeExit);
304 
305     canvas->clear(SK_ColorWHITE);
306     canvas->drawImage(yuvImage.get(), 0, 0);
307 
308     return skiagm::DrawResult::kOk;
309 }
310 
311 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) {
312     if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
313         *errorMsg = "Not supported on recording/vector backends.";
314         return skiagm::DrawResult::kSkip;
315     }
316 
317     auto dContext = GrAsDirectContext(canvas->recordingContext());
318     if (!dContext && canvas->recordingContext()) {
319         *errorMsg = "Not supported in DDL mode";
320         return skiagm::DrawResult::kSkip;
321     }
322 
323     static constexpr int kBorder = 5;
324     static constexpr int kInner = 5;
325     const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner);
326     auto surfaceII =
327             SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType,
328                               kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
329     auto surface = canvas->makeSurface(surfaceII);
330     if (!surface) {
331         *errorMsg = "Could not create surface for image.";
332         // When testing abandoned GrContext we expect surface creation to fail.
333         if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
334             return skiagm::DrawResult::kSkip;
335         }
336         return skiagm::DrawResult::kFail;
337     }
338     surface->getCanvas()->clear(SK_ColorRED);
339     surface->getCanvas()->save();
340     surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false);
341     surface->getCanvas()->clear(SK_ColorBLUE);
342     surface->getCanvas()->restore();
343     static constexpr int kPad = 2;
344     canvas->translate(kPad, kPad);
345     skiagm::DrawResult result;
346     SkISize downSize = {static_cast<int>(kInner/2),  static_cast<int>(kInner / 2)};
347     result = do_rescale_grid(canvas, surface.get(), dContext, srcRect, downSize, false, errorMsg,
348                              kPad);
349 
350     if (result != skiagm::DrawResult::kOk) {
351         return result;
352     }
353     canvas->translate(0, 4 * downSize.height());
354     SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)};
355     result = do_rescale_grid(canvas, surface.get(), dContext, srcRect, upSize, false, errorMsg,
356                              kPad);
357     if (result != skiagm::DrawResult::kOk) {
358         return result;
359     }
360     return skiagm::DrawResult::kOk;
361 }
362 
363 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_alpha_type, canvas, errorMsg, 512, 512) {
364     auto dContext = GrAsDirectContext(canvas->recordingContext());
365     if (!dContext && canvas->recordingContext()) {
366         *errorMsg = "Not supported in DDL mode";
367         return skiagm::DrawResult::kSkip;
368     }
369     if (dContext && dContext->abandoned()) {
370         return skiagm::DrawResult::kSkip;
371     }
372 
373     auto upmII = SkImageInfo::Make(200, 200, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType);
374 
375     auto pmII = upmII.makeAlphaType(kPremul_SkAlphaType);
376 
377     auto upmSurf = SkSurface::MakeRaster(upmII);
378     auto pmSurf  = SkSurface::MakeRaster( pmII);
379 
380     SkColor4f colors[] = {
381             {.3f, .3f, .3f, .3f},
382             {1.f, .2f, .6f, .9f},
383             {0.f, .1f, 1.f, .1f},
384             {.7f, .8f, .2f, .7f},
385     };
386     auto shader = SkGradientShader::MakeRadial({100, 100},
387                                                230,
388                                                colors,
389                                                nullptr,
390                                                nullptr,
391                                                SK_ARRAY_COUNT(colors),
392                                                SkTileMode::kRepeat);
393     SkPaint paint;
394     paint.setShader(std::move(shader));
395 
396     upmSurf->getCanvas()->drawPaint(paint);
397     pmSurf ->getCanvas()->drawPaint(paint);
398 
399     auto pmImg  =  pmSurf->makeImageSnapshot();
400     auto upmImg = upmSurf->makeImageSnapshot();
401 
402     if (dContext) {
403         pmImg  =  pmImg->makeTextureImage(dContext);
404         upmImg = upmImg->makeTextureImage(dContext);
405         if (!pmImg || !upmImg) {
406             *errorMsg = "could not make texture images";
407             return skiagm::DrawResult::kFail;
408         }
409     }
410     int size = 256;
411 
412     ToolUtils::draw_checkerboard(canvas, SK_ColorWHITE, SK_ColorBLACK, 32);
413 
414     for (const auto& img : {pmImg, upmImg}) {
415         canvas->save();
416         for (auto readAT : {kPremul_SkAlphaType, kUnpremul_SkAlphaType}) {
417             auto readInfo = img->imageInfo().makeAlphaType(readAT).makeWH(size, size);
418 
419             auto* asyncContext = new AsyncContext();
420             img->asyncRescaleAndReadPixels(readInfo,
421                                            SkIRect::MakeSize(img->dimensions()),
422                                            SkImage::RescaleGamma::kSrc,
423                                            SkImage::RescaleMode::kRepeatedCubic,
424                                            async_callback,
425                                            asyncContext);
426             if (dContext) {
427                 dContext->submit();
428             }
429             while (!asyncContext->fCalled) {
430                 // Only GPU should actually be asynchronous.
431                 SkASSERT(dContext);
432                 dContext->checkAsyncWorkCompletion();
433             }
434             if (asyncContext->fResult) {
435                 SkPixmap pixmap(readInfo,
436                                 asyncContext->fResult->data(0),
437                                 asyncContext->fResult->rowBytes(0));
__anon15165d9a0302(const void*, void* c) 438                 auto releasePixels = [](const void*, void* c) {
439                     delete static_cast<AsyncContext*>(c);
440                 };
441                 auto result = SkImage::MakeFromRaster(pixmap, releasePixels, asyncContext);
442 
443                 canvas->drawImage(result, 0, 0);
444             } else {
445                 delete asyncContext;
446                 *errorMsg = "async readback failed";
447                 return skiagm::DrawResult::kFail;
448             }
449             canvas->translate(size, 0);
450         }
451         canvas->restore();
452         canvas->translate(0, size);
453     }
454     return skiagm::DrawResult::kOk;
455 }
456