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