blob: 89bfb575b6afa33ff334c6ac277821127acef409 [file] [log] [blame]
Brian Salomon201700f2019-05-17 12:05:44 -04001/*
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"
Brian Salomon7db71392020-10-16 10:05:21 -040014#include "include/core/SkYUVAInfo.h"
15#include "include/core/SkYUVAPixmaps.h"
Brian Salomonbacbb922021-01-21 19:48:00 -050016#include "include/effects/SkGradientShader.h"
Robert Phillips2a4acf32020-07-06 15:50:15 -040017#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040018#include "include/gpu/GrRecordingContext.h"
Brian Salomon201700f2019-05-17 12:05:44 -040019#include "src/core/SkAutoPixmapStorage.h"
Brian Salomon024bd002019-06-11 11:38:16 -040020#include "src/core/SkScopeExit.h"
Brian Salomon201700f2019-05-17 12:05:44 -040021#include "tools/Resources.h"
22#include "tools/ToolUtils.h"
Brian Salomon7db71392020-10-16 10:05:21 -040023#include "tools/gpu/YUVUtils.h"
Brian Salomon201700f2019-05-17 12:05:44 -040024
Brian Salomon9241a6d2019-10-03 13:26:54 -040025namespace {
26struct AsyncContext {
27 bool fCalled = false;
Brian Salomon63a0a752020-06-26 13:32:09 -040028 std::unique_ptr<const SkImage::AsyncReadResult> fResult;
Brian Salomon9241a6d2019-10-03 13:26:54 -040029};
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'.
Brian Salomon63a0a752020-06-26 13:32:09 -040035static void async_callback(void* c, std::unique_ptr<const SkImage::AsyncReadResult> result) {
Brian Salomon9241a6d2019-10-03 13:26:54 -040036 auto context = static_cast<AsyncContext*>(c);
37 context->fResult = std::move(result);
38 context->fCalled = true;
39};
40
Brian Salomon201700f2019-05-17 12:05:44 -040041// Draws the image to a surface, does a asyncRescaleAndReadPixels of the image, and then sticks
42// the result in a raster image.
Brian Salomon63a0a752020-06-26 13:32:09 -040043template <typename Src>
44static sk_sp<SkImage> do_read_and_scale(Src* src,
Robert Phillipsd436b782020-07-10 11:49:06 -040045 GrDirectContext* direct,
Brian Salomon63a0a752020-06-26 13:32:09 -040046 const SkIRect& srcRect,
47 const SkImageInfo& ii,
48 SkImage::RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -050049 SkImage::RescaleMode rescaleMode) {
Brian Salomon63a0a752020-06-26 13:32:09 -040050 auto* asyncContext = new AsyncContext();
Mike Reed1efa14d2021-01-02 21:44:59 -050051 src->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, rescaleMode, async_callback,
Brian Salomon63a0a752020-06-26 13:32:09 -040052 asyncContext);
Robert Phillipsd436b782020-07-10 11:49:06 -040053 if (direct) {
54 direct->submit();
Brian Salomon39278022020-06-02 11:47:26 -040055 }
Brian Salomon63a0a752020-06-26 13:32:09 -040056 while (!asyncContext->fCalled) {
Brian Salomon9c219782019-10-02 22:51:08 +000057 // Only GPU should actually be asynchronous.
Robert Phillipsd436b782020-07-10 11:49:06 -040058 SkASSERT(direct);
59 direct->checkAsyncWorkCompletion();
Brian Salomon9c219782019-10-02 22:51:08 +000060 }
Brian Salomon63a0a752020-06-26 13:32:09 -040061 if (!asyncContext->fResult) {
Brian Salomon9241a6d2019-10-03 13:26:54 -040062 return nullptr;
63 }
Brian Salomon63a0a752020-06-26 13:32:09 -040064 SkPixmap pixmap(ii, asyncContext->fResult->data(0), asyncContext->fResult->rowBytes(0));
Brian Salomon9241a6d2019-10-03 13:26:54 -040065 auto releasePixels = [](const void*, void* c) { delete static_cast<AsyncContext*>(c); };
Brian Salomon63a0a752020-06-26 13:32:09 -040066 return SkImage::MakeFromRaster(pixmap, releasePixels, asyncContext);
Ravi Mistrycb550102019-10-03 09:06:25 +000067}
68
Brian Salomon63a0a752020-06-26 13:32:09 -040069template <typename Src>
70static sk_sp<SkImage> do_read_and_scale_yuv(Src* src,
Robert Phillipsd436b782020-07-10 11:49:06 -040071 GrDirectContext* direct,
Brian Salomon63a0a752020-06-26 13:32:09 -040072 SkYUVColorSpace yuvCS,
73 const SkIRect& srcRect,
74 SkISize size,
75 SkImage::RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -050076 SkImage::RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -040077 SkScopeExit* cleanup) {
Brian Salomon9241a6d2019-10-03 13:26:54 -040078 SkASSERT(!(size.width() & 0b1) && !(size.height() & 0b1));
Ravi Mistrycb550102019-10-03 09:06:25 +000079
Brian Salomon9241a6d2019-10-03 13:26:54 -040080 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);
Ravi Mistrycb550102019-10-03 09:06:25 +000083
Brian Salomon63a0a752020-06-26 13:32:09 -040084 AsyncContext asyncContext;
85 src->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, size,
Mike Reed1efa14d2021-01-02 21:44:59 -050086 rescaleGamma, rescaleMode, async_callback, &asyncContext);
Robert Phillipsd436b782020-07-10 11:49:06 -040087 if (direct) {
88 direct->submit();
Brian Salomon39278022020-06-02 11:47:26 -040089 }
Brian Salomon63a0a752020-06-26 13:32:09 -040090 while (!asyncContext.fCalled) {
Ravi Mistrycb550102019-10-03 09:06:25 +000091 // Only GPU should actually be asynchronous.
Robert Phillipsd436b782020-07-10 11:49:06 -040092 SkASSERT(direct);
93 direct->checkAsyncWorkCompletion();
Ravi Mistrycb550102019-10-03 09:06:25 +000094 }
Brian Salomon63a0a752020-06-26 13:32:09 -040095 if (!asyncContext.fResult) {
Brian Salomoncd5caa32019-06-11 17:02:19 -040096 return nullptr;
97 }
Brian Salomone4387382020-11-11 16:34:19 -050098 SkYUVAInfo yuvaInfo(size,
99 SkYUVAInfo::PlaneConfig::kY_U_V,
100 SkYUVAInfo::Subsampling::k420,
101 yuvCS);
Brian Salomon7db71392020-10-16 10:05:21 -0400102 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)}
Robert Phillips2e1142c2019-08-14 12:33:29 -0400106 };
Brian Salomon7db71392020-10-16 10:05:21 -0400107 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);
Brian Salomon024bd002019-06-11 11:38:16 -0400112}
113
Brian Salomon201700f2019-05-17 12:05:44 -0400114// 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.
Brian Salomon63a0a752020-06-26 13:32:09 -0400116template <typename Src>
117static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas,
118 Src* src,
Robert Phillipsd436b782020-07-10 11:49:06 -0400119 GrDirectContext* direct,
Brian Salomon63a0a752020-06-26 13:32:09 -0400120 const SkIRect& srcRect,
121 SkISize newSize,
122 bool doYUV420,
123 SkString* errorMsg,
124 int pad = 0) {
Robert Phillipsd436b782020-07-10 11:49:06 -0400125 if (doYUV420 && !direct) {
Adlai Hollere3ad5272020-07-07 10:27:55 -0400126 errorMsg->printf("YUV420 only supported on direct GPU for now.");
127 return skiagm::DrawResult::kSkip;
Brian Salomon024bd002019-06-11 11:38:16 -0400128 }
Brian Salomon201700f2019-05-17 12:05:44 -0400129 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
130 *errorMsg = "Not supported on recording/vector backends.";
131 return skiagm::DrawResult::kSkip;
132 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400133 const auto ii = canvas->imageInfo().makeDimensions(newSize);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000134
Brian Salomon024bd002019-06-11 11:38:16 -0400135 SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
Brian Salomona34e0fc2019-05-17 19:56:59 +0000136 canvas->save();
Brian Salomon63a0a752020-06-26 13:32:09 -0400137 for (auto gamma : {SkImage::RescaleGamma::kSrc, SkImage::RescaleGamma::kLinear}) {
Brian Salomona34e0fc2019-05-17 19:56:59 +0000138 canvas->save();
Mike Reed1efa14d2021-01-02 21:44:59 -0500139 for (auto mode : {
140 SkImage::RescaleMode::kNearest,
141 SkImage::RescaleMode::kRepeatedLinear,
142 SkImage::RescaleMode::kRepeatedCubic}) {
Brian Salomon024bd002019-06-11 11:38:16 -0400143 SkScopeExit cleanup;
144 sk_sp<SkImage> result;
145 if (doYUV420) {
Robert Phillipsd436b782020-07-10 11:49:06 -0400146 result = do_read_and_scale_yuv(src, direct, yuvColorSpace, srcRect, newSize, gamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500147 mode, &cleanup);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400148 if (!result) {
149 errorMsg->printf("YUV420 async call failed. Allowed for now.");
150 return skiagm::DrawResult::kSkip;
151 }
Brian Salomon024bd002019-06-11 11:38:16 -0400152 int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1);
153 yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS);
154 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -0500155 result = do_read_and_scale(src, direct, srcRect, ii, gamma, mode);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400156 if (!result) {
157 errorMsg->printf("async read call failed.");
158 return skiagm::DrawResult::kFail;
159 }
Brian Salomon024bd002019-06-11 11:38:16 -0400160 }
161 canvas->drawImage(result, 0, 0);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400162 canvas->translate(newSize.width() + pad, 0);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000163 }
164 canvas->restore();
Brian Salomon9241a6d2019-10-03 13:26:54 -0400165 canvas->translate(0, newSize.height() + pad);
Brian Salomon451b01f2019-05-17 12:39:17 -0400166 }
Brian Salomona34e0fc2019-05-17 19:56:59 +0000167 canvas->restore();
168 return skiagm::DrawResult::kOk;
Brian Salomon451b01f2019-05-17 12:39:17 -0400169}
170
Brian Salomon63a0a752020-06-26 13:32:09 -0400171static 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) {
Brian Salomon286b5572019-05-20 10:25:50 -0400178 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 }
Robert Phillips41fc1742020-10-15 10:04:03 -0400187
Robert Phillips27f283f2020-10-14 11:46:51 -0400188 auto dContext = GrAsDirectContext(canvas->recordingContext());
Robert Phillips41fc1742020-10-15 10:04:03 -0400189 if (!dContext && canvas->recordingContext()) {
Robert Phillips27f283f2020-10-14 11:46:51 -0400190 *errorMsg = "Not supported in DDL mode";
191 return skiagm::DrawResult::kSkip;
192 }
Robert Phillipsd436b782020-07-10 11:49:06 -0400193
Brian Salomon63a0a752020-06-26 13:32:09 -0400194 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);
Brian Salomon1caf3782020-06-26 17:06:31 +0000201 }
Brian Salomon63a0a752020-06-26 13:32:09 -0400202 if (!surface) {
203 *errorMsg = "Could not create surface for image.";
204 // When testing abandoned GrContext we expect surface creation to fail.
Robert Phillips9eb00022020-06-30 15:30:12 -0400205 if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400206 return skiagm::DrawResult::kSkip;
207 }
208 return skiagm::DrawResult::kFail;
209 }
210 SkPaint paint;
211 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reed8d29ab62021-01-23 18:10:39 -0500212 surface->getCanvas()->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
Robert Phillips27f283f2020-10-14 11:46:51 -0400213 return do_rescale_grid(canvas, surface.get(), dContext, srcRect, newSize,
Brian Salomon63a0a752020-06-26 13:32:09 -0400214 doYUV420, errorMsg);
Robert Phillips27f283f2020-10-14 11:46:51 -0400215 } else if (dContext) {
216 image = image->makeTextureImage(dContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400217 if (!image) {
218 *errorMsg = "Could not create image.";
219 // When testing abandoned GrContext we expect surface creation to fail.
Robert Phillips9eb00022020-06-30 15:30:12 -0400220 if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400221 return skiagm::DrawResult::kSkip;
222 }
223 return skiagm::DrawResult::kFail;
224 }
Brian Salomon1caf3782020-06-26 17:06:31 +0000225 }
Robert Phillips27f283f2020-10-14 11:46:51 -0400226 return do_rescale_grid(canvas, image.get(), dContext, srcRect, newSize, doYUV420,
Brian Salomon63a0a752020-06-26 13:32:09 -0400227 errorMsg);
Brian Salomon286b5572019-05-20 10:25:50 -0400228}
229
Brian Salomon63a0a752020-06-26 13:32:09 -0400230#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); \
Brian Salomon201700f2019-05-17 12:05:44 -0400235 }
236
Brian Salomon63a0a752020-06-26 13:32:09 -0400237#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); \
Brian Salomon024bd002019-06-11 11:38:16 -0400241 }
242
Brian Salomon63a0a752020-06-26 13:32:09 -0400243#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 }
Brian Salomon024bd002019-06-11 11:38:16 -0400249
Brian Salomon63a0a752020-06-26 13:32:09 -0400250#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 }
Brian Salomon201700f2019-05-17 12:05:44 -0400255
Brian Salomon63a0a752020-06-26 13:32:09 -0400256DEF_RESCALE_AND_READ_YUV_SURF_GM(
257 images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150), 410, 376)
Brian Salomon201700f2019-05-17 12:05:44 -0400258
Brian Salomon63a0a752020-06-26 13:32:09 -0400259DEF_RESCALE_AND_READ_YUV_IMG_GM(
260 images/yellow_rose.webp, rose_down, SkIRect::MakeXYWH(50, 5, 200, 150), 106, 60)
261
262DEF_RESCALE_AND_READ_SURF_GM(
263 images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100), 410, 410)
264
265DEF_RESCALE_AND_READ_SURF_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
266DEF_RESCALE_AND_READ_IMG_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
267
268DEF_RESCALE_AND_READ_IMG_GM(
269 images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637), (int)(0.7 * 105))
270DEF_RESCALE_AND_READ_SURF_GM(
271 images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637), (int)(1.2 * 105))
272DEF_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))
Brian Salomon286b5572019-05-20 10:25:50 -0400277
Brian Salomona7e5c7c2020-01-27 15:41:40 -0500278// 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).
280DEF_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
Robert Phillips27f283f2020-10-14 11:46:51 -0400287 auto dContext = GrAsDirectContext(surface->recordingContext());
Robert Phillips41fc1742020-10-15 10:04:03 -0400288 if (!dContext && surface->recordingContext()) {
Robert Phillips27f283f2020-10-14 11:46:51 -0400289 *errorMsg = "Not supported in DDL mode";
290 return skiagm::DrawResult::kSkip;
291 }
292
Brian Salomona7e5c7c2020-01-27 15:41:40 -0500293 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(
Robert Phillips27f283f2020-10-14 11:46:51 -0400302 surface, dContext, kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300),
Mike Reed1efa14d2021-01-02 21:44:59 -0500303 {400, 300}, SkImage::RescaleGamma::kSrc, SkImage::RescaleMode::kNearest, &scopeExit);
Brian Salomona7e5c7c2020-01-27 15:41:40 -0500304
305 canvas->clear(SK_ColorWHITE);
306 canvas->drawImage(yuvImage.get(), 0, 0);
307
308 return skiagm::DrawResult::kOk;
309}
310
Brian Salomon286b5572019-05-20 10:25:50 -0400311DEF_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
Robert Phillips27f283f2020-10-14 11:46:51 -0400317 auto dContext = GrAsDirectContext(canvas->recordingContext());
Robert Phillips41fc1742020-10-15 10:04:03 -0400318 if (!dContext && canvas->recordingContext()) {
Robert Phillips27f283f2020-10-14 11:46:51 -0400319 *errorMsg = "Not supported in DDL mode";
320 return skiagm::DrawResult::kSkip;
321 }
322
Brian Salomon286b5572019-05-20 10:25:50 -0400323 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);
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400330 if (!surface) {
331 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400332 // When testing abandoned GrContext we expect surface creation to fail.
Robert Phillipsd436b782020-07-10 11:49:06 -0400333 if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
Brian Salomon9be911c2019-05-20 14:01:21 -0400334 return skiagm::DrawResult::kSkip;
335 }
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400336 return skiagm::DrawResult::kFail;
337 }
Brian Salomon286b5572019-05-20 10:25:50 -0400338 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;
Brian Salomon9241a6d2019-10-03 13:26:54 -0400346 SkISize downSize = {static_cast<int>(kInner/2), static_cast<int>(kInner / 2)};
Robert Phillips27f283f2020-10-14 11:46:51 -0400347 result = do_rescale_grid(canvas, surface.get(), dContext, srcRect, downSize, false, errorMsg,
Brian Salomon63a0a752020-06-26 13:32:09 -0400348 kPad);
349
Brian Salomon286b5572019-05-20 10:25:50 -0400350 if (result != skiagm::DrawResult::kOk) {
351 return result;
352 }
Mike Kleinf68213f2020-01-27 12:16:22 -0600353 canvas->translate(0, 4 * downSize.height());
Brian Salomon9241a6d2019-10-03 13:26:54 -0400354 SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)};
Robert Phillips27f283f2020-10-14 11:46:51 -0400355 result = do_rescale_grid(canvas, surface.get(), dContext, srcRect, upSize, false, errorMsg,
356 kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400357 if (result != skiagm::DrawResult::kOk) {
358 return result;
359 }
360 return skiagm::DrawResult::kOk;
361}
Brian Salomonbacbb922021-01-21 19:48:00 -0500362
363DEF_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));
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}