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/SkYUVAIndex.h"
15 #include "include/gpu/GrContext.h"
16 #include "src/core/SkAutoPixmapStorage.h"
17 #include "src/core/SkConvertPixels.h"
18 #include "src/core/SkScopeExit.h"
19 #include "src/gpu/GrContextPriv.h"
20 #include "tools/Resources.h"
21 #include "tools/ToolUtils.h"
22 
23 namespace {
24 struct AsyncContext {
25     bool fCalled = false;
26     std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
27 };
28 }  // anonymous namespace
29 
30 // Making this a lambda in the test functions caused:
31 //   "error: cannot compile this forwarded non-trivially copyable parameter yet"
32 // on x86/Win/Clang bot, referring to 'result'.
async_callback(void * c,std::unique_ptr<const SkSurface::AsyncReadResult> result)33 static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
34     auto context = static_cast<AsyncContext*>(c);
35     context->fResult = std::move(result);
36     context->fCalled = true;
37 };
38 
39 // Draws the image to a surface, does a asyncRescaleAndReadPixels of the image, and then sticks
40 // the result in a raster image.
do_read_and_scale(SkSurface * surface,const SkIRect & srcRect,const SkImageInfo & ii,SkSurface::RescaleGamma rescaleGamma,SkFilterQuality quality)41 static sk_sp<SkImage> do_read_and_scale(SkSurface* surface, const SkIRect& srcRect,
42                                         const SkImageInfo& ii, SkSurface::RescaleGamma rescaleGamma,
43                                         SkFilterQuality quality) {
44     auto* context = new AsyncContext();
45     surface->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, async_callback, context);
46     while (!context->fCalled) {
47         // Only GPU should actually be asynchronous.
48         SkASSERT(surface->getCanvas()->getGrContext());
49         surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
50     }
51     if (!context->fResult) {
52         return nullptr;
53     }
54     SkPixmap pixmap(ii, context->fResult->data(0), context->fResult->rowBytes(0));
55     auto releasePixels = [](const void*, void* c) { delete static_cast<AsyncContext*>(c); };
56     return SkImage::MakeFromRaster(pixmap, releasePixels, context);
57 }
58 
do_read_and_scale_yuv(SkSurface * surface,SkYUVColorSpace yuvCS,const SkIRect & srcRect,SkISize size,SkSurface::RescaleGamma rescaleGamma,SkFilterQuality quality,SkScopeExit * cleanup)59 static sk_sp<SkImage> do_read_and_scale_yuv(SkSurface* surface, SkYUVColorSpace yuvCS,
60                                             const SkIRect& srcRect, SkISize size,
61                                             SkSurface::RescaleGamma rescaleGamma,
62                                             SkFilterQuality quality, SkScopeExit* cleanup) {
63     SkASSERT(!(size.width() & 0b1) && !(size.height() & 0b1));
64 
65     SkISize uvSize = {size.width()/2, size.height()/2};
66     SkImageInfo yII  = SkImageInfo::Make(size,   kGray_8_SkColorType, kPremul_SkAlphaType);
67     SkImageInfo uvII = SkImageInfo::Make(uvSize, kGray_8_SkColorType, kPremul_SkAlphaType);
68 
69     AsyncContext context;
70     surface->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, size,
71                                              rescaleGamma, quality, async_callback, &context);
72     while (!context.fCalled) {
73         // Only GPU should actually be asynchronous.
74         SkASSERT(surface->getCanvas()->getGrContext());
75         surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
76     }
77     if (!context.fResult) {
78         return nullptr;
79     }
80     auto* gr = surface->getCanvas()->getGrContext();
81     GrBackendTexture backendTextures[3];
82 
83     SkPixmap yPM(yII,  context.fResult->data(0), context.fResult->rowBytes(0));
84     SkPixmap uPM(uvII, context.fResult->data(1), context.fResult->rowBytes(1));
85     SkPixmap vPM(uvII, context.fResult->data(2), context.fResult->rowBytes(2));
86 
87     backendTextures[0] = gr->createBackendTexture(yPM, GrRenderable::kNo, GrProtected::kNo);
88     backendTextures[1] = gr->createBackendTexture(uPM, GrRenderable::kNo, GrProtected::kNo);
89     backendTextures[2] = gr->createBackendTexture(vPM, GrRenderable::kNo, GrProtected::kNo);
90 
91     SkYUVAIndex indices[4] = {
92         { 0, SkColorChannel::kR},
93         { 1, SkColorChannel::kR},
94         { 2, SkColorChannel::kR},
95         {-1, SkColorChannel::kR}
96     };
97 
98     *cleanup = {[gr, backendTextures] {
99         GrFlushInfo flushInfo;
100         flushInfo.fFlags = kSyncCpu_GrFlushFlag;
101         gr->flush(flushInfo);
102         gr->deleteBackendTexture(backendTextures[0]);
103         gr->deleteBackendTexture(backendTextures[1]);
104         gr->deleteBackendTexture(backendTextures[2]);
105     }};
106 
107     return SkImage::MakeFromYUVATextures(gr, yuvCS, backendTextures, indices, size,
108                                          kTopLeft_GrSurfaceOrigin, SkColorSpace::MakeSRGB());
109 }
110 
111 // Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are
112 // rescale in src gamma and rescale in linear gamma.
do_rescale_grid(SkCanvas * canvas,SkSurface * surface,const SkIRect & srcRect,SkISize newSize,bool doYUV420,SkString * errorMsg,int pad=0)113 static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, SkSurface* surface,
114                                           const SkIRect& srcRect, SkISize newSize, bool doYUV420,
115                                           SkString* errorMsg, int pad = 0) {
116     if (doYUV420) {
117         if (!canvas->getGrContext() || !canvas->getGrContext()->priv().asDirectContext()) {
118             errorMsg->printf("YUV420 only supported on direct GPU for now.");
119             return skiagm::DrawResult::kSkip;
120         }
121     }
122     if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
123         *errorMsg = "Not supported on recording/vector backends.";
124         return skiagm::DrawResult::kSkip;
125     }
126     const auto ii = canvas->imageInfo().makeDimensions(newSize);
127 
128     SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
129     canvas->save();
130     for (auto gamma : {SkSurface::RescaleGamma::kSrc, SkSurface::RescaleGamma::kLinear}) {
131         canvas->save();
132         for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) {
133             SkScopeExit cleanup;
134             sk_sp<SkImage> result;
135             if (doYUV420) {
136                 result = do_read_and_scale_yuv(surface, yuvColorSpace, srcRect, newSize, gamma,
137                                                quality, &cleanup);
138                 if (!result) {
139                     errorMsg->printf("YUV420 async call failed. Allowed for now.");
140                     return skiagm::DrawResult::kSkip;
141                 }
142                 int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1);
143                 yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS);
144             } else {
145                 result = do_read_and_scale(surface, srcRect, ii, gamma, quality);
146                 if (!result) {
147                     errorMsg->printf("async read call failed.");
148                     return skiagm::DrawResult::kFail;
149                 }
150             }
151             canvas->drawImage(result, 0, 0);
152             canvas->translate(newSize.width() + pad, 0);
153         }
154         canvas->restore();
155         canvas->translate(0, newSize.height() + pad);
156     }
157     canvas->restore();
158     return skiagm::DrawResult::kOk;
159 }
160 
do_rescale_image_grid(SkCanvas * canvas,const char * imageFile,const SkIRect & srcRect,SkISize newSize,bool doYUV420,SkString * errorMsg)161 static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, const char* imageFile,
162                                                 const SkIRect& srcRect, SkISize newSize,
163                                                 bool doYUV420, SkString* errorMsg) {
164     auto image = GetResourceAsImage(imageFile);
165     if (!image) {
166         errorMsg->printf("Could not load image file %s.", imageFile);
167         return skiagm::DrawResult::kFail;
168     }
169     if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
170         *errorMsg = "Not supported on recording/vector backends.";
171         return skiagm::DrawResult::kSkip;
172     }
173     // Turn the image into a surface in order to call the read and rescale API
174     auto surfInfo = image->imageInfo().makeDimensions(image->dimensions());
175     auto surface = canvas->makeSurface(surfInfo);
176     if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) {
177         surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType);
178         surface = canvas->makeSurface(surfInfo);
179     }
180     if (!surface) {
181         *errorMsg = "Could not create surface for image.";
182         // When testing abandoned GrContext we expect surface creation to fail.
183         if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
184             return skiagm::DrawResult::kSkip;
185         }
186         return skiagm::DrawResult::kFail;
187     }
188     SkPaint paint;
189     paint.setBlendMode(SkBlendMode::kSrc);
190     surface->getCanvas()->drawImage(image, 0, 0, &paint);
191     return do_rescale_grid(canvas, surface.get(), srcRect, newSize, doYUV420, errorMsg);
192 }
193 
194 #define DEF_RESCALE_AND_READ_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                              \
195     DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) {    \
196         ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);             \
197         return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, false, errorMsg); \
198     }
199 
200 #define DEF_RESCALE_AND_READ_YUV_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                              \
201     DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
202         ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);                 \
203         return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, errorMsg);      \
204     }
205 
206 DEF_RESCALE_AND_READ_YUV_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150),
207                             410, 376)
208 
209 DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100),
210                         410, 410)
211 
212 DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
213 DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
214 
215 DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637),
216                         (int)(0.7 * 105))
217 DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637),
218                         (int)(1.2 * 105))
219 DEF_RESCALE_AND_READ_GM(images/text.png, text_up_large, SkIRect::MakeXYWH(300, 0, 300, 105),
220                         (int)(2.4 * 300), (int)(2.4 * 105))
221 
222 // Exercises non-scaling YUV420. Reads from the original canvas's surface in order to
223 // exercise case where source surface is not a texture (in glbert config).
224 DEF_SIMPLE_GM_CAN_FAIL(async_yuv_no_scale, canvas, errorMsg, 400, 300) {
225     auto surface = canvas->getSurface();
226     if (!surface) {
227         *errorMsg = "Not supported on recording/vector backends.";
228         return skiagm::DrawResult::kSkip;
229     }
230 
231     auto image = GetResourceAsImage("images/yellow_rose.webp");
232     if (!image) {
233         return skiagm::DrawResult::kFail;
234     }
235     SkPaint paint;
236     canvas->drawImage(image.get(), 0, 0);
237 
238     SkScopeExit scopeExit;
239     auto yuvImage = do_read_and_scale_yuv(
240             surface, kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300), {400, 300},
241             SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality, &scopeExit);
242 
243     canvas->clear(SK_ColorWHITE);
244     canvas->drawImage(yuvImage.get(), 0, 0);
245 
246     return skiagm::DrawResult::kOk;
247 }
248 
249 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) {
250     if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
251         *errorMsg = "Not supported on recording/vector backends.";
252         return skiagm::DrawResult::kSkip;
253     }
254 
255     static constexpr int kBorder = 5;
256     static constexpr int kInner = 5;
257     const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner);
258     auto surfaceII =
259             SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType,
260                               kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
261     auto surface = canvas->makeSurface(surfaceII);
262     if (!surface) {
263         *errorMsg = "Could not create surface for image.";
264         // When testing abandoned GrContext we expect surface creation to fail.
265         if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
266             return skiagm::DrawResult::kSkip;
267         }
268         return skiagm::DrawResult::kFail;
269     }
270     surface->getCanvas()->clear(SK_ColorRED);
271     surface->getCanvas()->save();
272     surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false);
273     surface->getCanvas()->clear(SK_ColorBLUE);
274     surface->getCanvas()->restore();
275     static constexpr int kPad = 2;
276     canvas->translate(kPad, kPad);
277     skiagm::DrawResult result;
278     SkISize downSize = {static_cast<int>(kInner/2),  static_cast<int>(kInner / 2)};
279     result = do_rescale_grid(canvas, surface.get(), srcRect, downSize, false, errorMsg, kPad);
280     if (result != skiagm::DrawResult::kOk) {
281         return result;
282     }
283     canvas->translate(0, 4 * downSize.height());
284     SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)};
285     result = do_rescale_grid(canvas, surface.get(), srcRect, upSize, false, errorMsg, kPad);
286     if (result != skiagm::DrawResult::kOk) {
287         return result;
288     }
289     return skiagm::DrawResult::kOk;
290 }
291