blob: 5f26508cea5042871e63d7364b24fd639ce0e4a0 [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);
102 gr->deleteBackendTexture(backendTextures[0]);
103 gr->deleteBackendTexture(backendTextures[1]);
104 gr->deleteBackendTexture(backendTextures[2]);
105 }};
106
Brian Salomon9241a6d2019-10-03 13:26:54 -0400107 return SkImage::MakeFromYUVATextures(gr, yuvCS, backendTextures, indices, size,
Brian Salomon024bd002019-06-11 11:38:16 -0400108 kTopLeft_GrSurfaceOrigin, SkColorSpace::MakeSRGB());
109}
110
Brian Salomon201700f2019-05-17 12:05:44 -0400111// 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.
Brian Salomon286b5572019-05-20 10:25:50 -0400113static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, SkSurface* surface,
Brian Salomon9241a6d2019-10-03 13:26:54 -0400114 const SkIRect& srcRect, SkISize newSize, bool doYUV420,
Brian Salomon286b5572019-05-20 10:25:50 -0400115 SkString* errorMsg, int pad = 0) {
Brian Salomon024bd002019-06-11 11:38:16 -0400116 if (doYUV420) {
Brian Salomoncd5caa32019-06-11 17:02:19 -0400117 if (!canvas->getGrContext() || !canvas->getGrContext()->priv().asDirectContext()) {
118 errorMsg->printf("YUV420 only supported on direct GPU for now.");
Brian Salomon024bd002019-06-11 11:38:16 -0400119 return skiagm::DrawResult::kSkip;
120 }
121 }
Brian Salomon201700f2019-05-17 12:05:44 -0400122 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
123 *errorMsg = "Not supported on recording/vector backends.";
124 return skiagm::DrawResult::kSkip;
125 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400126 const auto ii = canvas->imageInfo().makeDimensions(newSize);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000127
Brian Salomon024bd002019-06-11 11:38:16 -0400128 SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
Brian Salomona34e0fc2019-05-17 19:56:59 +0000129 canvas->save();
Brian Salomon024bd002019-06-11 11:38:16 -0400130 for (auto gamma : {SkSurface::RescaleGamma::kSrc, SkSurface::RescaleGamma::kLinear}) {
Brian Salomona34e0fc2019-05-17 19:56:59 +0000131 canvas->save();
132 for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) {
Brian Salomon024bd002019-06-11 11:38:16 -0400133 SkScopeExit cleanup;
134 sk_sp<SkImage> result;
135 if (doYUV420) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400136 result = do_read_and_scale_yuv(surface, yuvColorSpace, srcRect, newSize, gamma,
Brian Salomon024bd002019-06-11 11:38:16 -0400137 quality, &cleanup);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400138 if (!result) {
139 errorMsg->printf("YUV420 async call failed. Allowed for now.");
140 return skiagm::DrawResult::kSkip;
141 }
Brian Salomon024bd002019-06-11 11:38:16 -0400142 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);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400146 if (!result) {
147 errorMsg->printf("async read call failed.");
148 return skiagm::DrawResult::kFail;
149 }
Brian Salomon024bd002019-06-11 11:38:16 -0400150 }
151 canvas->drawImage(result, 0, 0);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400152 canvas->translate(newSize.width() + pad, 0);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000153 }
154 canvas->restore();
Brian Salomon9241a6d2019-10-03 13:26:54 -0400155 canvas->translate(0, newSize.height() + pad);
Brian Salomon451b01f2019-05-17 12:39:17 -0400156 }
Brian Salomona34e0fc2019-05-17 19:56:59 +0000157 canvas->restore();
158 return skiagm::DrawResult::kOk;
Brian Salomon451b01f2019-05-17 12:39:17 -0400159}
160
Brian Salomon286b5572019-05-20 10:25:50 -0400161static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, const char* imageFile,
Brian Salomon9241a6d2019-10-03 13:26:54 -0400162 const SkIRect& srcRect, SkISize newSize,
Brian Salomon024bd002019-06-11 11:38:16 -0400163 bool doYUV420, SkString* errorMsg) {
Brian Salomon286b5572019-05-20 10:25:50 -0400164 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
Brian Salomon9241a6d2019-10-03 13:26:54 -0400174 auto surfInfo = image->imageInfo().makeDimensions(image->dimensions());
Brian Salomon286b5572019-05-20 10:25:50 -0400175 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.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400182 // When testing abandoned GrContext we expect surface creation to fail.
183 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
184 return skiagm::DrawResult::kSkip;
185 }
Brian Salomon286b5572019-05-20 10:25:50 -0400186 return skiagm::DrawResult::kFail;
187 }
188 SkPaint paint;
189 paint.setBlendMode(SkBlendMode::kSrc);
190 surface->getCanvas()->drawImage(image, 0, 0, &paint);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400191 return do_rescale_grid(canvas, surface.get(), srcRect, newSize, doYUV420, errorMsg);
Brian Salomon286b5572019-05-20 10:25:50 -0400192}
193
Brian Salomon9241a6d2019-10-03 13:26:54 -0400194#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); \
Brian Salomon201700f2019-05-17 12:05:44 -0400198 }
199
Brian Salomon9241a6d2019-10-03 13:26:54 -0400200#define DEF_RESCALE_AND_READ_YUV_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
Brian Salomon024bd002019-06-11 11:38:16 -0400201 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
Brian Salomon9241a6d2019-10-03 13:26:54 -0400202 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
203 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, errorMsg); \
Brian Salomon024bd002019-06-11 11:38:16 -0400204 }
205
206DEF_RESCALE_AND_READ_YUV_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150),
207 410, 376)
208
Brian Salomon201700f2019-05-17 12:05:44 -0400209DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100),
210 410, 410)
211
212DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
Brian Salomonc7e9f782019-05-28 20:39:53 -0400213DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
Brian Salomon201700f2019-05-17 12:05:44 -0400214
215DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637),
216 (int)(0.7 * 105))
217DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637),
218 (int)(1.2 * 105))
219DEF_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))
Brian Salomon286b5572019-05-20 10:25:50 -0400221
222DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) {
223 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
224 *errorMsg = "Not supported on recording/vector backends.";
225 return skiagm::DrawResult::kSkip;
226 }
227
228 static constexpr int kBorder = 5;
229 static constexpr int kInner = 5;
230 const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner);
231 auto surfaceII =
232 SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType,
233 kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
234 auto surface = canvas->makeSurface(surfaceII);
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400235 if (!surface) {
236 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400237 // When testing abandoned GrContext we expect surface creation to fail.
238 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
239 return skiagm::DrawResult::kSkip;
240 }
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400241 return skiagm::DrawResult::kFail;
242 }
Brian Salomon286b5572019-05-20 10:25:50 -0400243 surface->getCanvas()->clear(SK_ColorRED);
244 surface->getCanvas()->save();
245 surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false);
246 surface->getCanvas()->clear(SK_ColorBLUE);
247 surface->getCanvas()->restore();
248 static constexpr int kPad = 2;
249 canvas->translate(kPad, kPad);
250 skiagm::DrawResult result;
Brian Salomon9241a6d2019-10-03 13:26:54 -0400251 SkISize downSize = {static_cast<int>(kInner/2), static_cast<int>(kInner / 2)};
252 result = do_rescale_grid(canvas, surface.get(), srcRect, downSize, false, errorMsg, kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400253 if (result != skiagm::DrawResult::kOk) {
254 return result;
255 }
Mike Kleinf68213f2020-01-27 12:16:22 -0600256 canvas->translate(0, 4 * downSize.height());
Brian Salomon9241a6d2019-10-03 13:26:54 -0400257 SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)};
258 result = do_rescale_grid(canvas, surface.get(), srcRect, upSize, false, errorMsg, kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400259 if (result != skiagm::DrawResult::kOk) {
260 return result;
261 }
262 return skiagm::DrawResult::kOk;
263}