blob: d92d02edcb42ebb3ba292a44be19a9a43ddc6fee [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"
Robert Phillips2a4acf32020-07-06 15:50:15 -040016#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040017#include "include/gpu/GrRecordingContext.h"
Brian Salomon201700f2019-05-17 12:05:44 -040018#include "src/core/SkAutoPixmapStorage.h"
Brian Salomon024bd002019-06-11 11:38:16 -040019#include "src/core/SkScopeExit.h"
Brian Salomon201700f2019-05-17 12:05:44 -040020#include "tools/Resources.h"
21#include "tools/ToolUtils.h"
Brian Salomon7db71392020-10-16 10:05:21 -040022#include "tools/gpu/YUVUtils.h"
Brian Salomon201700f2019-05-17 12:05:44 -040023
Brian Salomon9241a6d2019-10-03 13:26:54 -040024namespace {
25struct AsyncContext {
26 bool fCalled = false;
Brian Salomon63a0a752020-06-26 13:32:09 -040027 std::unique_ptr<const SkImage::AsyncReadResult> fResult;
Brian Salomon9241a6d2019-10-03 13:26:54 -040028};
29} // anonymous namespace
30
31// Making this a lambda in the test functions caused:
32// "error: cannot compile this forwarded non-trivially copyable parameter yet"
33// on x86/Win/Clang bot, referring to 'result'.
Brian Salomon63a0a752020-06-26 13:32:09 -040034static void async_callback(void* c, std::unique_ptr<const SkImage::AsyncReadResult> result) {
Brian Salomon9241a6d2019-10-03 13:26:54 -040035 auto context = static_cast<AsyncContext*>(c);
36 context->fResult = std::move(result);
37 context->fCalled = true;
38};
39
Brian Salomon201700f2019-05-17 12:05:44 -040040// Draws the image to a surface, does a asyncRescaleAndReadPixels of the image, and then sticks
41// the result in a raster image.
Brian Salomon63a0a752020-06-26 13:32:09 -040042template <typename Src>
43static sk_sp<SkImage> do_read_and_scale(Src* src,
Robert Phillipsd436b782020-07-10 11:49:06 -040044 GrDirectContext* direct,
Brian Salomon63a0a752020-06-26 13:32:09 -040045 const SkIRect& srcRect,
46 const SkImageInfo& ii,
47 SkImage::RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -050048 SkImage::RescaleMode rescaleMode) {
Brian Salomon63a0a752020-06-26 13:32:09 -040049 auto* asyncContext = new AsyncContext();
Mike Reed1efa14d2021-01-02 21:44:59 -050050 src->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, rescaleMode, async_callback,
Brian Salomon63a0a752020-06-26 13:32:09 -040051 asyncContext);
Robert Phillipsd436b782020-07-10 11:49:06 -040052 if (direct) {
53 direct->submit();
Brian Salomon39278022020-06-02 11:47:26 -040054 }
Brian Salomon63a0a752020-06-26 13:32:09 -040055 while (!asyncContext->fCalled) {
Brian Salomon9c219782019-10-02 22:51:08 +000056 // Only GPU should actually be asynchronous.
Robert Phillipsd436b782020-07-10 11:49:06 -040057 SkASSERT(direct);
58 direct->checkAsyncWorkCompletion();
Brian Salomon9c219782019-10-02 22:51:08 +000059 }
Brian Salomon63a0a752020-06-26 13:32:09 -040060 if (!asyncContext->fResult) {
Brian Salomon9241a6d2019-10-03 13:26:54 -040061 return nullptr;
62 }
Brian Salomon63a0a752020-06-26 13:32:09 -040063 SkPixmap pixmap(ii, asyncContext->fResult->data(0), asyncContext->fResult->rowBytes(0));
Brian Salomon9241a6d2019-10-03 13:26:54 -040064 auto releasePixels = [](const void*, void* c) { delete static_cast<AsyncContext*>(c); };
Brian Salomon63a0a752020-06-26 13:32:09 -040065 return SkImage::MakeFromRaster(pixmap, releasePixels, asyncContext);
Ravi Mistrycb550102019-10-03 09:06:25 +000066}
67
Brian Salomon63a0a752020-06-26 13:32:09 -040068template <typename Src>
69static sk_sp<SkImage> do_read_and_scale_yuv(Src* src,
Robert Phillipsd436b782020-07-10 11:49:06 -040070 GrDirectContext* direct,
Brian Salomon63a0a752020-06-26 13:32:09 -040071 SkYUVColorSpace yuvCS,
72 const SkIRect& srcRect,
73 SkISize size,
74 SkImage::RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -050075 SkImage::RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -040076 SkScopeExit* cleanup) {
Brian Salomon9241a6d2019-10-03 13:26:54 -040077 SkASSERT(!(size.width() & 0b1) && !(size.height() & 0b1));
Ravi Mistrycb550102019-10-03 09:06:25 +000078
Brian Salomon9241a6d2019-10-03 13:26:54 -040079 SkISize uvSize = {size.width()/2, size.height()/2};
80 SkImageInfo yII = SkImageInfo::Make(size, kGray_8_SkColorType, kPremul_SkAlphaType);
81 SkImageInfo uvII = SkImageInfo::Make(uvSize, kGray_8_SkColorType, kPremul_SkAlphaType);
Ravi Mistrycb550102019-10-03 09:06:25 +000082
Brian Salomon63a0a752020-06-26 13:32:09 -040083 AsyncContext asyncContext;
84 src->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, size,
Mike Reed1efa14d2021-01-02 21:44:59 -050085 rescaleGamma, rescaleMode, async_callback, &asyncContext);
Robert Phillipsd436b782020-07-10 11:49:06 -040086 if (direct) {
87 direct->submit();
Brian Salomon39278022020-06-02 11:47:26 -040088 }
Brian Salomon63a0a752020-06-26 13:32:09 -040089 while (!asyncContext.fCalled) {
Ravi Mistrycb550102019-10-03 09:06:25 +000090 // Only GPU should actually be asynchronous.
Robert Phillipsd436b782020-07-10 11:49:06 -040091 SkASSERT(direct);
92 direct->checkAsyncWorkCompletion();
Ravi Mistrycb550102019-10-03 09:06:25 +000093 }
Brian Salomon63a0a752020-06-26 13:32:09 -040094 if (!asyncContext.fResult) {
Brian Salomoncd5caa32019-06-11 17:02:19 -040095 return nullptr;
96 }
Brian Salomone4387382020-11-11 16:34:19 -050097 SkYUVAInfo yuvaInfo(size,
98 SkYUVAInfo::PlaneConfig::kY_U_V,
99 SkYUVAInfo::Subsampling::k420,
100 yuvCS);
Brian Salomon7db71392020-10-16 10:05:21 -0400101 SkPixmap yuvPMs[] = {
102 {yII, asyncContext.fResult->data(0), asyncContext.fResult->rowBytes(0)},
103 {uvII, asyncContext.fResult->data(1), asyncContext.fResult->rowBytes(1)},
104 {uvII, asyncContext.fResult->data(2), asyncContext.fResult->rowBytes(2)}
Robert Phillips2e1142c2019-08-14 12:33:29 -0400105 };
Brian Salomon7db71392020-10-16 10:05:21 -0400106 auto pixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, yuvPMs);
107 SkASSERT(pixmaps.isValid());
108 auto lazyYUVImage = sk_gpu_test::LazyYUVImage::Make(pixmaps);
109 SkASSERT(lazyYUVImage);
110 return lazyYUVImage->refImage(direct, sk_gpu_test::LazyYUVImage::Type::kFromTextures);
Brian Salomon024bd002019-06-11 11:38:16 -0400111}
112
Brian Salomon201700f2019-05-17 12:05:44 -0400113// Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are
114// rescale in src gamma and rescale in linear gamma.
Brian Salomon63a0a752020-06-26 13:32:09 -0400115template <typename Src>
116static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas,
117 Src* src,
Robert Phillipsd436b782020-07-10 11:49:06 -0400118 GrDirectContext* direct,
Brian Salomon63a0a752020-06-26 13:32:09 -0400119 const SkIRect& srcRect,
120 SkISize newSize,
121 bool doYUV420,
122 SkString* errorMsg,
123 int pad = 0) {
Robert Phillipsd436b782020-07-10 11:49:06 -0400124 if (doYUV420 && !direct) {
Adlai Hollere3ad5272020-07-07 10:27:55 -0400125 errorMsg->printf("YUV420 only supported on direct GPU for now.");
126 return skiagm::DrawResult::kSkip;
Brian Salomon024bd002019-06-11 11:38:16 -0400127 }
Brian Salomon201700f2019-05-17 12:05:44 -0400128 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
129 *errorMsg = "Not supported on recording/vector backends.";
130 return skiagm::DrawResult::kSkip;
131 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400132 const auto ii = canvas->imageInfo().makeDimensions(newSize);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000133
Brian Salomon024bd002019-06-11 11:38:16 -0400134 SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
Brian Salomona34e0fc2019-05-17 19:56:59 +0000135 canvas->save();
Brian Salomon63a0a752020-06-26 13:32:09 -0400136 for (auto gamma : {SkImage::RescaleGamma::kSrc, SkImage::RescaleGamma::kLinear}) {
Brian Salomona34e0fc2019-05-17 19:56:59 +0000137 canvas->save();
Mike Reed1efa14d2021-01-02 21:44:59 -0500138 for (auto mode : {
139 SkImage::RescaleMode::kNearest,
140 SkImage::RescaleMode::kRepeatedLinear,
141 SkImage::RescaleMode::kRepeatedCubic}) {
Brian Salomon024bd002019-06-11 11:38:16 -0400142 SkScopeExit cleanup;
143 sk_sp<SkImage> result;
144 if (doYUV420) {
Robert Phillipsd436b782020-07-10 11:49:06 -0400145 result = do_read_and_scale_yuv(src, direct, yuvColorSpace, srcRect, newSize, gamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500146 mode, &cleanup);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400147 if (!result) {
148 errorMsg->printf("YUV420 async call failed. Allowed for now.");
149 return skiagm::DrawResult::kSkip;
150 }
Brian Salomon024bd002019-06-11 11:38:16 -0400151 int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1);
152 yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS);
153 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -0500154 result = do_read_and_scale(src, direct, srcRect, ii, gamma, mode);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400155 if (!result) {
156 errorMsg->printf("async read call failed.");
157 return skiagm::DrawResult::kFail;
158 }
Brian Salomon024bd002019-06-11 11:38:16 -0400159 }
160 canvas->drawImage(result, 0, 0);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400161 canvas->translate(newSize.width() + pad, 0);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000162 }
163 canvas->restore();
Brian Salomon9241a6d2019-10-03 13:26:54 -0400164 canvas->translate(0, newSize.height() + pad);
Brian Salomon451b01f2019-05-17 12:39:17 -0400165 }
Brian Salomona34e0fc2019-05-17 19:56:59 +0000166 canvas->restore();
167 return skiagm::DrawResult::kOk;
Brian Salomon451b01f2019-05-17 12:39:17 -0400168}
169
Brian Salomon63a0a752020-06-26 13:32:09 -0400170static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas,
171 const char* imageFile,
172 const SkIRect& srcRect,
173 SkISize newSize,
174 bool doSurface,
175 bool doYUV420,
176 SkString* errorMsg) {
Brian Salomon286b5572019-05-20 10:25:50 -0400177 auto image = GetResourceAsImage(imageFile);
178 if (!image) {
179 errorMsg->printf("Could not load image file %s.", imageFile);
180 return skiagm::DrawResult::kFail;
181 }
182 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
183 *errorMsg = "Not supported on recording/vector backends.";
184 return skiagm::DrawResult::kSkip;
185 }
Robert Phillips41fc1742020-10-15 10:04:03 -0400186
Robert Phillips27f283f2020-10-14 11:46:51 -0400187 auto dContext = GrAsDirectContext(canvas->recordingContext());
Robert Phillips41fc1742020-10-15 10:04:03 -0400188 if (!dContext && canvas->recordingContext()) {
Robert Phillips27f283f2020-10-14 11:46:51 -0400189 *errorMsg = "Not supported in DDL mode";
190 return skiagm::DrawResult::kSkip;
191 }
Robert Phillipsd436b782020-07-10 11:49:06 -0400192
Brian Salomon63a0a752020-06-26 13:32:09 -0400193 if (doSurface) {
194 // Turn the image into a surface in order to call the read and rescale API
195 auto surfInfo = image->imageInfo().makeDimensions(image->dimensions());
196 auto surface = canvas->makeSurface(surfInfo);
197 if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) {
198 surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType);
199 surface = canvas->makeSurface(surfInfo);
Brian Salomon1caf3782020-06-26 17:06:31 +0000200 }
Brian Salomon63a0a752020-06-26 13:32:09 -0400201 if (!surface) {
202 *errorMsg = "Could not create surface for image.";
203 // When testing abandoned GrContext we expect surface creation to fail.
Robert Phillips9eb00022020-06-30 15:30:12 -0400204 if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400205 return skiagm::DrawResult::kSkip;
206 }
207 return skiagm::DrawResult::kFail;
208 }
209 SkPaint paint;
210 paint.setBlendMode(SkBlendMode::kSrc);
211 surface->getCanvas()->drawImage(image, 0, 0, &paint);
Robert Phillips27f283f2020-10-14 11:46:51 -0400212 return do_rescale_grid(canvas, surface.get(), dContext, srcRect, newSize,
Brian Salomon63a0a752020-06-26 13:32:09 -0400213 doYUV420, errorMsg);
Robert Phillips27f283f2020-10-14 11:46:51 -0400214 } else if (dContext) {
215 image = image->makeTextureImage(dContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400216 if (!image) {
217 *errorMsg = "Could not create image.";
218 // When testing abandoned GrContext we expect surface creation to fail.
Robert Phillips9eb00022020-06-30 15:30:12 -0400219 if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400220 return skiagm::DrawResult::kSkip;
221 }
222 return skiagm::DrawResult::kFail;
223 }
Brian Salomon1caf3782020-06-26 17:06:31 +0000224 }
Robert Phillips27f283f2020-10-14 11:46:51 -0400225 return do_rescale_grid(canvas, image.get(), dContext, srcRect, newSize, doYUV420,
Brian Salomon63a0a752020-06-26 13:32:09 -0400226 errorMsg);
Brian Salomon286b5572019-05-20 10:25:50 -0400227}
228
Brian Salomon63a0a752020-06-26 13:32:09 -0400229#define DEF_RESCALE_AND_READ_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
230 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
231 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
232 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, false, \
233 errorMsg); \
Brian Salomon201700f2019-05-17 12:05:44 -0400234 }
235
Brian Salomon63a0a752020-06-26 13:32:09 -0400236#define DEF_RESCALE_AND_READ_YUV_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
237 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
238 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
239 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \
Brian Salomon024bd002019-06-11 11:38:16 -0400240 }
241
Brian Salomon63a0a752020-06-26 13:32:09 -0400242#define DEF_RESCALE_AND_READ_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
243 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
244 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
245 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, false, false, \
246 errorMsg); \
247 }
Brian Salomon024bd002019-06-11 11:38:16 -0400248
Brian Salomon63a0a752020-06-26 13:32:09 -0400249#define DEF_RESCALE_AND_READ_YUV_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
250 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
251 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
252 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \
253 }
Brian Salomon201700f2019-05-17 12:05:44 -0400254
Brian Salomon63a0a752020-06-26 13:32:09 -0400255DEF_RESCALE_AND_READ_YUV_SURF_GM(
256 images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150), 410, 376)
Brian Salomon201700f2019-05-17 12:05:44 -0400257
Brian Salomon63a0a752020-06-26 13:32:09 -0400258DEF_RESCALE_AND_READ_YUV_IMG_GM(
259 images/yellow_rose.webp, rose_down, SkIRect::MakeXYWH(50, 5, 200, 150), 106, 60)
260
261DEF_RESCALE_AND_READ_SURF_GM(
262 images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100), 410, 410)
263
264DEF_RESCALE_AND_READ_SURF_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
265DEF_RESCALE_AND_READ_IMG_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
266
267DEF_RESCALE_AND_READ_IMG_GM(
268 images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637), (int)(0.7 * 105))
269DEF_RESCALE_AND_READ_SURF_GM(
270 images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637), (int)(1.2 * 105))
271DEF_RESCALE_AND_READ_IMG_GM(images/text.png,
272 text_up_large,
273 SkIRect::MakeXYWH(300, 0, 300, 105),
274 (int)(2.4 * 300),
275 (int)(2.4 * 105))
Brian Salomon286b5572019-05-20 10:25:50 -0400276
Brian Salomona7e5c7c2020-01-27 15:41:40 -0500277// Exercises non-scaling YUV420. Reads from the original canvas's surface in order to
278// exercise case where source surface is not a texture (in glbert config).
279DEF_SIMPLE_GM_CAN_FAIL(async_yuv_no_scale, canvas, errorMsg, 400, 300) {
280 auto surface = canvas->getSurface();
281 if (!surface) {
282 *errorMsg = "Not supported on recording/vector backends.";
283 return skiagm::DrawResult::kSkip;
284 }
285
Robert Phillips27f283f2020-10-14 11:46:51 -0400286 auto dContext = GrAsDirectContext(surface->recordingContext());
Robert Phillips41fc1742020-10-15 10:04:03 -0400287 if (!dContext && surface->recordingContext()) {
Robert Phillips27f283f2020-10-14 11:46:51 -0400288 *errorMsg = "Not supported in DDL mode";
289 return skiagm::DrawResult::kSkip;
290 }
291
Brian Salomona7e5c7c2020-01-27 15:41:40 -0500292 auto image = GetResourceAsImage("images/yellow_rose.webp");
293 if (!image) {
294 return skiagm::DrawResult::kFail;
295 }
296 SkPaint paint;
297 canvas->drawImage(image.get(), 0, 0);
298
299 SkScopeExit scopeExit;
300 auto yuvImage = do_read_and_scale_yuv(
Robert Phillips27f283f2020-10-14 11:46:51 -0400301 surface, dContext, kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300),
Mike Reed1efa14d2021-01-02 21:44:59 -0500302 {400, 300}, SkImage::RescaleGamma::kSrc, SkImage::RescaleMode::kNearest, &scopeExit);
Brian Salomona7e5c7c2020-01-27 15:41:40 -0500303
304 canvas->clear(SK_ColorWHITE);
305 canvas->drawImage(yuvImage.get(), 0, 0);
306
307 return skiagm::DrawResult::kOk;
308}
309
Brian Salomon286b5572019-05-20 10:25:50 -0400310DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) {
311 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
312 *errorMsg = "Not supported on recording/vector backends.";
313 return skiagm::DrawResult::kSkip;
314 }
315
Robert Phillips27f283f2020-10-14 11:46:51 -0400316 auto dContext = GrAsDirectContext(canvas->recordingContext());
Robert Phillips41fc1742020-10-15 10:04:03 -0400317 if (!dContext && canvas->recordingContext()) {
Robert Phillips27f283f2020-10-14 11:46:51 -0400318 *errorMsg = "Not supported in DDL mode";
319 return skiagm::DrawResult::kSkip;
320 }
321
Brian Salomon286b5572019-05-20 10:25:50 -0400322 static constexpr int kBorder = 5;
323 static constexpr int kInner = 5;
324 const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner);
325 auto surfaceII =
326 SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType,
327 kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
328 auto surface = canvas->makeSurface(surfaceII);
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400329 if (!surface) {
330 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400331 // When testing abandoned GrContext we expect surface creation to fail.
Robert Phillipsd436b782020-07-10 11:49:06 -0400332 if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) {
Brian Salomon9be911c2019-05-20 14:01:21 -0400333 return skiagm::DrawResult::kSkip;
334 }
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400335 return skiagm::DrawResult::kFail;
336 }
Brian Salomon286b5572019-05-20 10:25:50 -0400337 surface->getCanvas()->clear(SK_ColorRED);
338 surface->getCanvas()->save();
339 surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false);
340 surface->getCanvas()->clear(SK_ColorBLUE);
341 surface->getCanvas()->restore();
342 static constexpr int kPad = 2;
343 canvas->translate(kPad, kPad);
344 skiagm::DrawResult result;
Brian Salomon9241a6d2019-10-03 13:26:54 -0400345 SkISize downSize = {static_cast<int>(kInner/2), static_cast<int>(kInner / 2)};
Robert Phillips27f283f2020-10-14 11:46:51 -0400346 result = do_rescale_grid(canvas, surface.get(), dContext, srcRect, downSize, false, errorMsg,
Brian Salomon63a0a752020-06-26 13:32:09 -0400347 kPad);
348
Brian Salomon286b5572019-05-20 10:25:50 -0400349 if (result != skiagm::DrawResult::kOk) {
350 return result;
351 }
Mike Kleinf68213f2020-01-27 12:16:22 -0600352 canvas->translate(0, 4 * downSize.height());
Brian Salomon9241a6d2019-10-03 13:26:54 -0400353 SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)};
Robert Phillips27f283f2020-10-14 11:46:51 -0400354 result = do_rescale_grid(canvas, surface.get(), dContext, srcRect, upSize, false, errorMsg,
355 kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400356 if (result != skiagm::DrawResult::kOk) {
357 return result;
358 }
359 return skiagm::DrawResult::kOk;
360}