blob: 8b56f395067472ccc0002c6c90f4374d524e4ff3 [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 Salomon024bd002019-06-11 11:38:16 -040014#include "include/core/SkYUVAIndex.h"
Brian Salomon201700f2019-05-17 12:05:44 -040015#include "include/gpu/GrContext.h"
16#include "src/core/SkAutoPixmapStorage.h"
17#include "src/core/SkConvertPixels.h"
Brian Salomon024bd002019-06-11 11:38:16 -040018#include "src/core/SkScopeExit.h"
19#include "src/gpu/GrContextPriv.h"
Brian Salomon201700f2019-05-17 12:05:44 -040020#include "tools/Resources.h"
21#include "tools/ToolUtils.h"
22
Brian Salomon9241a6d2019-10-03 13:26:54 -040023namespace {
24struct 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'.
33static 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
Brian Salomon201700f2019-05-17 12:05:44 -040039// Draws the image to a surface, does a asyncRescaleAndReadPixels of the image, and then sticks
40// the result in a raster image.
Brian Salomon286b5572019-05-20 10:25:50 -040041static sk_sp<SkImage> do_read_and_scale(SkSurface* surface, const SkIRect& srcRect,
42 const SkImageInfo& ii, SkSurface::RescaleGamma rescaleGamma,
43 SkFilterQuality quality) {
Brian Salomon9241a6d2019-10-03 13:26:54 -040044 auto* context = new AsyncContext();
45 surface->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, async_callback, context);
46 while (!context->fCalled) {
Brian Salomon9c219782019-10-02 22:51:08 +000047 // Only GPU should actually be asynchronous.
48 SkASSERT(surface->getCanvas()->getGrContext());
49 surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
50 }
Brian Salomon9241a6d2019-10-03 13:26:54 -040051 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);
Ravi Mistrycb550102019-10-03 09:06:25 +000057}
58
59static sk_sp<SkImage> do_read_and_scale_yuv(SkSurface* surface, SkYUVColorSpace yuvCS,
Brian Salomon9241a6d2019-10-03 13:26:54 -040060 const SkIRect& srcRect, SkISize size,
Ravi Mistrycb550102019-10-03 09:06:25 +000061 SkSurface::RescaleGamma rescaleGamma,
62 SkFilterQuality quality, SkScopeExit* cleanup) {
Brian Salomon9241a6d2019-10-03 13:26:54 -040063 SkASSERT(!(size.width() & 0b1) && !(size.height() & 0b1));
Ravi Mistrycb550102019-10-03 09:06:25 +000064
Brian Salomon9241a6d2019-10-03 13:26:54 -040065 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);
Ravi Mistrycb550102019-10-03 09:06:25 +000068
Brian Salomon9241a6d2019-10-03 13:26:54 -040069 AsyncContext context;
70 surface->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, size,
71 rescaleGamma, quality, async_callback, &context);
Ravi Mistrycb550102019-10-03 09:06:25 +000072 while (!context.fCalled) {
73 // Only GPU should actually be asynchronous.
74 SkASSERT(surface->getCanvas()->getGrContext());
75 surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
76 }
Brian Salomon9241a6d2019-10-03 13:26:54 -040077 if (!context.fResult) {
Brian Salomoncd5caa32019-06-11 17:02:19 -040078 return nullptr;
79 }
Brian Salomon024bd002019-06-11 11:38:16 -040080 auto* gr = surface->getCanvas()->getGrContext();
81 GrBackendTexture backendTextures[3];
Robert Phillips0a15cc62019-07-30 12:49:10 -040082
Brian Salomon9241a6d2019-10-03 13:26:54 -040083 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
Robert Phillips2e1142c2019-08-14 12:33:29 -040091 SkYUVAIndex indices[4] = {
92 { 0, SkColorChannel::kR},
93 { 1, SkColorChannel::kR},
94 { 2, SkColorChannel::kR},
95 {-1, SkColorChannel::kR}
96 };
97
Brian Salomon024bd002019-06-11 11:38:16 -040098 *cleanup = {[gr, backendTextures] {
99 GrFlushInfo flushInfo;
100 flushInfo.fFlags = kSyncCpu_GrFlushFlag;
101 gr->flush(flushInfo);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400102 gr->submit(true);
Brian Salomon024bd002019-06-11 11:38:16 -0400103 gr->deleteBackendTexture(backendTextures[0]);
104 gr->deleteBackendTexture(backendTextures[1]);
105 gr->deleteBackendTexture(backendTextures[2]);
106 }};
107
Brian Salomon9241a6d2019-10-03 13:26:54 -0400108 return SkImage::MakeFromYUVATextures(gr, yuvCS, backendTextures, indices, size,
Brian Salomon024bd002019-06-11 11:38:16 -0400109 kTopLeft_GrSurfaceOrigin, SkColorSpace::MakeSRGB());
110}
111
Brian Salomon201700f2019-05-17 12:05:44 -0400112// Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are
113// rescale in src gamma and rescale in linear gamma.
Brian Salomon286b5572019-05-20 10:25:50 -0400114static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, SkSurface* surface,
Brian Salomon9241a6d2019-10-03 13:26:54 -0400115 const SkIRect& srcRect, SkISize newSize, bool doYUV420,
Brian Salomon286b5572019-05-20 10:25:50 -0400116 SkString* errorMsg, int pad = 0) {
Brian Salomon024bd002019-06-11 11:38:16 -0400117 if (doYUV420) {
Brian Salomoncd5caa32019-06-11 17:02:19 -0400118 if (!canvas->getGrContext() || !canvas->getGrContext()->priv().asDirectContext()) {
119 errorMsg->printf("YUV420 only supported on direct GPU for now.");
Brian Salomon024bd002019-06-11 11:38:16 -0400120 return skiagm::DrawResult::kSkip;
121 }
122 }
Brian Salomon201700f2019-05-17 12:05:44 -0400123 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
124 *errorMsg = "Not supported on recording/vector backends.";
125 return skiagm::DrawResult::kSkip;
126 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400127 const auto ii = canvas->imageInfo().makeDimensions(newSize);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000128
Brian Salomon024bd002019-06-11 11:38:16 -0400129 SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
Brian Salomona34e0fc2019-05-17 19:56:59 +0000130 canvas->save();
Brian Salomon024bd002019-06-11 11:38:16 -0400131 for (auto gamma : {SkSurface::RescaleGamma::kSrc, SkSurface::RescaleGamma::kLinear}) {
Brian Salomona34e0fc2019-05-17 19:56:59 +0000132 canvas->save();
133 for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) {
Brian Salomon024bd002019-06-11 11:38:16 -0400134 SkScopeExit cleanup;
135 sk_sp<SkImage> result;
136 if (doYUV420) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400137 result = do_read_and_scale_yuv(surface, yuvColorSpace, srcRect, newSize, gamma,
Brian Salomon024bd002019-06-11 11:38:16 -0400138 quality, &cleanup);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400139 if (!result) {
140 errorMsg->printf("YUV420 async call failed. Allowed for now.");
141 return skiagm::DrawResult::kSkip;
142 }
Brian Salomon024bd002019-06-11 11:38:16 -0400143 int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1);
144 yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS);
145 } else {
146 result = do_read_and_scale(surface, srcRect, ii, gamma, quality);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400147 if (!result) {
148 errorMsg->printf("async read call failed.");
149 return skiagm::DrawResult::kFail;
150 }
Brian Salomon024bd002019-06-11 11:38:16 -0400151 }
152 canvas->drawImage(result, 0, 0);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400153 canvas->translate(newSize.width() + pad, 0);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000154 }
155 canvas->restore();
Brian Salomon9241a6d2019-10-03 13:26:54 -0400156 canvas->translate(0, newSize.height() + pad);
Brian Salomon451b01f2019-05-17 12:39:17 -0400157 }
Brian Salomona34e0fc2019-05-17 19:56:59 +0000158 canvas->restore();
159 return skiagm::DrawResult::kOk;
Brian Salomon451b01f2019-05-17 12:39:17 -0400160}
161
Brian Salomon286b5572019-05-20 10:25:50 -0400162static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, const char* imageFile,
Brian Salomon9241a6d2019-10-03 13:26:54 -0400163 const SkIRect& srcRect, SkISize newSize,
Brian Salomon024bd002019-06-11 11:38:16 -0400164 bool doYUV420, SkString* errorMsg) {
Brian Salomon286b5572019-05-20 10:25:50 -0400165 auto image = GetResourceAsImage(imageFile);
166 if (!image) {
167 errorMsg->printf("Could not load image file %s.", imageFile);
168 return skiagm::DrawResult::kFail;
169 }
170 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
171 *errorMsg = "Not supported on recording/vector backends.";
172 return skiagm::DrawResult::kSkip;
173 }
174 // Turn the image into a surface in order to call the read and rescale API
Brian Salomon9241a6d2019-10-03 13:26:54 -0400175 auto surfInfo = image->imageInfo().makeDimensions(image->dimensions());
Brian Salomon286b5572019-05-20 10:25:50 -0400176 auto surface = canvas->makeSurface(surfInfo);
177 if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) {
178 surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType);
179 surface = canvas->makeSurface(surfInfo);
180 }
181 if (!surface) {
182 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400183 // When testing abandoned GrContext we expect surface creation to fail.
184 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
185 return skiagm::DrawResult::kSkip;
186 }
Brian Salomon286b5572019-05-20 10:25:50 -0400187 return skiagm::DrawResult::kFail;
188 }
189 SkPaint paint;
190 paint.setBlendMode(SkBlendMode::kSrc);
191 surface->getCanvas()->drawImage(image, 0, 0, &paint);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400192 return do_rescale_grid(canvas, surface.get(), srcRect, newSize, doYUV420, errorMsg);
Brian Salomon286b5572019-05-20 10:25:50 -0400193}
194
Brian Salomon9241a6d2019-10-03 13:26:54 -0400195#define DEF_RESCALE_AND_READ_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
196 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
197 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
198 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, false, errorMsg); \
Brian Salomon201700f2019-05-17 12:05:44 -0400199 }
200
Brian Salomon9241a6d2019-10-03 13:26:54 -0400201#define DEF_RESCALE_AND_READ_YUV_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
Brian Salomon024bd002019-06-11 11:38:16 -0400202 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
Brian Salomon9241a6d2019-10-03 13:26:54 -0400203 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
204 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, errorMsg); \
Brian Salomon024bd002019-06-11 11:38:16 -0400205 }
206
207DEF_RESCALE_AND_READ_YUV_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150),
208 410, 376)
209
Brian Salomon201700f2019-05-17 12:05:44 -0400210DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100),
211 410, 410)
212
213DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
Brian Salomonc7e9f782019-05-28 20:39:53 -0400214DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
Brian Salomon201700f2019-05-17 12:05:44 -0400215
216DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637),
217 (int)(0.7 * 105))
218DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637),
219 (int)(1.2 * 105))
220DEF_RESCALE_AND_READ_GM(images/text.png, text_up_large, SkIRect::MakeXYWH(300, 0, 300, 105),
221 (int)(2.4 * 300), (int)(2.4 * 105))
Brian Salomon286b5572019-05-20 10:25:50 -0400222
Brian Salomona7e5c7c2020-01-27 15:41:40 -0500223// Exercises non-scaling YUV420. Reads from the original canvas's surface in order to
224// exercise case where source surface is not a texture (in glbert config).
225DEF_SIMPLE_GM_CAN_FAIL(async_yuv_no_scale, canvas, errorMsg, 400, 300) {
226 auto surface = canvas->getSurface();
227 if (!surface) {
228 *errorMsg = "Not supported on recording/vector backends.";
229 return skiagm::DrawResult::kSkip;
230 }
231
232 auto image = GetResourceAsImage("images/yellow_rose.webp");
233 if (!image) {
234 return skiagm::DrawResult::kFail;
235 }
236 SkPaint paint;
237 canvas->drawImage(image.get(), 0, 0);
238
239 SkScopeExit scopeExit;
240 auto yuvImage = do_read_and_scale_yuv(
241 surface, kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300), {400, 300},
242 SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality, &scopeExit);
243
244 canvas->clear(SK_ColorWHITE);
245 canvas->drawImage(yuvImage.get(), 0, 0);
246
247 return skiagm::DrawResult::kOk;
248}
249
Brian Salomon286b5572019-05-20 10:25:50 -0400250DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) {
251 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
252 *errorMsg = "Not supported on recording/vector backends.";
253 return skiagm::DrawResult::kSkip;
254 }
255
256 static constexpr int kBorder = 5;
257 static constexpr int kInner = 5;
258 const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner);
259 auto surfaceII =
260 SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType,
261 kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
262 auto surface = canvas->makeSurface(surfaceII);
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400263 if (!surface) {
264 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400265 // When testing abandoned GrContext we expect surface creation to fail.
266 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
267 return skiagm::DrawResult::kSkip;
268 }
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400269 return skiagm::DrawResult::kFail;
270 }
Brian Salomon286b5572019-05-20 10:25:50 -0400271 surface->getCanvas()->clear(SK_ColorRED);
272 surface->getCanvas()->save();
273 surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false);
274 surface->getCanvas()->clear(SK_ColorBLUE);
275 surface->getCanvas()->restore();
276 static constexpr int kPad = 2;
277 canvas->translate(kPad, kPad);
278 skiagm::DrawResult result;
Brian Salomon9241a6d2019-10-03 13:26:54 -0400279 SkISize downSize = {static_cast<int>(kInner/2), static_cast<int>(kInner / 2)};
280 result = do_rescale_grid(canvas, surface.get(), srcRect, downSize, false, errorMsg, kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400281 if (result != skiagm::DrawResult::kOk) {
282 return result;
283 }
Mike Kleinf68213f2020-01-27 12:16:22 -0600284 canvas->translate(0, 4 * downSize.height());
Brian Salomon9241a6d2019-10-03 13:26:54 -0400285 SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)};
286 result = do_rescale_grid(canvas, surface.get(), srcRect, upSize, false, errorMsg, kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400287 if (result != skiagm::DrawResult::kOk) {
288 return result;
289 }
290 return skiagm::DrawResult::kOk;
291}