blob: b277cd37d50bc32e449c66f3d827ceaa73bc769b [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"
20#include "src/gpu/GrGpu.h"
Brian Salomon201700f2019-05-17 12:05:44 -040021#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.
Brian Salomon286b5572019-05-20 10:25:50 -040026static sk_sp<SkImage> do_read_and_scale(SkSurface* surface, const SkIRect& srcRect,
27 const SkImageInfo& ii, SkSurface::RescaleGamma rescaleGamma,
28 SkFilterQuality quality) {
Brian Salomon201700f2019-05-17 12:05:44 -040029 SkBitmap bmp;
30 bmp.allocPixels(ii);
Brian Salomon201700f2019-05-17 12:05:44 -040031 SkPaint paint;
32 paint.setBlendMode(SkBlendMode::kSrc);
Brian Salomon201700f2019-05-17 12:05:44 -040033 struct Context {
34 SkPixmap fPixmap;
35 bool fCalled = false;
Brian Salomoncd5caa32019-06-11 17:02:19 -040036 bool fSucceeded = false;
Brian Salomon201700f2019-05-17 12:05:44 -040037 } 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 }
Brian Salomoncd5caa32019-06-11 17:02:19 -040046 context->fSucceeded = true;
Brian Salomon201700f2019-05-17 12:05:44 -040047 SkRectMemcpy(context->fPixmap.writable_addr(), context->fPixmap.rowBytes(), data, rowBytes,
48 context->fPixmap.info().minRowBytes(), context->fPixmap.height());
49 };
Brian Salomon286b5572019-05-20 10:25:50 -040050 surface->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, callback, &context);
Brian Salomon201700f2019-05-17 12:05:44 -040051 while (!context.fCalled) {
52 // Only GPU should actually be asynchronous.
Brian Salomon286b5572019-05-20 10:25:50 -040053 SkASSERT(surface->getCanvas()->getGrContext());
54 surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
Brian Salomon201700f2019-05-17 12:05:44 -040055 }
Brian Salomoncd5caa32019-06-11 17:02:19 -040056 return context.fSucceeded ? SkImage::MakeFromBitmap(bmp) : nullptr;
Brian Salomon201700f2019-05-17 12:05:44 -040057}
58
Brian Salomon024bd002019-06-11 11:38:16 -040059static 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 std::unique_ptr<uint8_t[]> yData(new uint8_t[dstW * dstH]);
65 std::unique_ptr<uint8_t[]> uData(new uint8_t[dstW / 2 * dstH / 2]);
66 std::unique_ptr<uint8_t[]> vData(new uint8_t[dstW / 2 * dstH / 2]);
67 struct Context {
68 int fW;
69 int fH;
70 uint8_t* fYData;
71 uint8_t* fUData;
72 uint8_t* fVData;
73 bool fCalled = false;
Brian Salomoncd5caa32019-06-11 17:02:19 -040074 bool fSucceeded = false;
Brian Salomon024bd002019-06-11 11:38:16 -040075 } context{dstW, dstH, yData.get(), uData.get(), vData.get()};
76 auto callback = [](void* c, const void* data[2], size_t rowBytes[2]) {
77 auto context = reinterpret_cast<Context*>(c);
78 context->fCalled = true;
79 if (!data) {
80 return;
81 }
Brian Salomoncd5caa32019-06-11 17:02:19 -040082 context->fSucceeded = true;
Brian Salomon024bd002019-06-11 11:38:16 -040083 int w = context->fW;
84 int h = context->fH;
85 SkRectMemcpy(context->fYData, w, data[0], rowBytes[0], w, h);
86 SkRectMemcpy(context->fUData, w / 2, data[1], rowBytes[1], w / 2, h / 2);
87 SkRectMemcpy(context->fVData, w / 2, data[2], rowBytes[2], w / 2, h / 2);
88 };
89 surface->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, dstW, dstH,
90 rescaleGamma, quality, callback, &context);
91 while (!context.fCalled) {
92 // Only GPU should actually be asynchronous.
93 SkASSERT(surface->getCanvas()->getGrContext());
94 surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
95 }
Brian Salomoncd5caa32019-06-11 17:02:19 -040096 if (!context.fSucceeded) {
97 return nullptr;
98 }
Brian Salomon024bd002019-06-11 11:38:16 -040099 auto* gr = surface->getCanvas()->getGrContext();
100 GrBackendTexture backendTextures[3];
101 GrBackendFormat format = gr->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
102 backendTextures[0] = gr->priv().getGpu()->createBackendTexture(
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400103 dstW, dstH, format, GrMipMapped::kNo, GrRenderable::kNo, yData.get(), 0, nullptr,
104 GrProtected::kNo);
Brian Salomon024bd002019-06-11 11:38:16 -0400105 backendTextures[1] = gr->priv().getGpu()->createBackendTexture(
106 dstW / 2, dstH / 2, format, GrMipMapped::kNo, GrRenderable::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400107 uData.get(), 0, nullptr, GrProtected::kNo);
Brian Salomon024bd002019-06-11 11:38:16 -0400108 backendTextures[2] = gr->priv().getGpu()->createBackendTexture(
109 dstW / 2, dstH / 2, format, GrMipMapped::kNo, GrRenderable::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400110 vData.get(), 0, nullptr, GrProtected::kNo);
Brian Salomon024bd002019-06-11 11:38:16 -0400111 auto config = gr->priv().caps()->getConfigFromBackendFormat(format, kAlpha_8_SkColorType);
112 SkColorChannel channel;
113 if (config == kAlpha_8_as_Red_GrPixelConfig) {
114 channel = SkColorChannel::kR;
115 } else {
116 SkASSERT(config == kAlpha_8_as_Alpha_GrPixelConfig);
117 channel = SkColorChannel::kA;
118 }
119 SkYUVAIndex indices[4]{{0, channel}, {1, channel}, {2, channel}, {-1, SkColorChannel::kR}};
120 *cleanup = {[gr, backendTextures] {
121 GrFlushInfo flushInfo;
122 flushInfo.fFlags = kSyncCpu_GrFlushFlag;
123 gr->flush(flushInfo);
124 gr->deleteBackendTexture(backendTextures[0]);
125 gr->deleteBackendTexture(backendTextures[1]);
126 gr->deleteBackendTexture(backendTextures[2]);
127 }};
128
129 return SkImage::MakeFromYUVATextures(gr, yuvCS, backendTextures, indices, {dstW, dstH},
130 kTopLeft_GrSurfaceOrigin, SkColorSpace::MakeSRGB());
131}
132
Brian Salomon201700f2019-05-17 12:05:44 -0400133// Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are
134// rescale in src gamma and rescale in linear gamma.
Brian Salomon286b5572019-05-20 10:25:50 -0400135static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, SkSurface* surface,
Brian Salomon024bd002019-06-11 11:38:16 -0400136 const SkIRect& srcRect, int newW, int newH, bool doYUV420,
Brian Salomon286b5572019-05-20 10:25:50 -0400137 SkString* errorMsg, int pad = 0) {
Brian Salomon024bd002019-06-11 11:38:16 -0400138 if (doYUV420) {
Brian Salomoncd5caa32019-06-11 17:02:19 -0400139 if (!canvas->getGrContext() || !canvas->getGrContext()->priv().asDirectContext()) {
140 errorMsg->printf("YUV420 only supported on direct GPU for now.");
Brian Salomon024bd002019-06-11 11:38:16 -0400141 return skiagm::DrawResult::kSkip;
142 }
143 }
Brian Salomon201700f2019-05-17 12:05:44 -0400144 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
145 *errorMsg = "Not supported on recording/vector backends.";
146 return skiagm::DrawResult::kSkip;
147 }
Brian Salomona34e0fc2019-05-17 19:56:59 +0000148 const auto ii = canvas->imageInfo().makeWH(newW, newH);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000149
Brian Salomon024bd002019-06-11 11:38:16 -0400150 SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
Brian Salomona34e0fc2019-05-17 19:56:59 +0000151 canvas->save();
Brian Salomon024bd002019-06-11 11:38:16 -0400152 for (auto gamma : {SkSurface::RescaleGamma::kSrc, SkSurface::RescaleGamma::kLinear}) {
Brian Salomona34e0fc2019-05-17 19:56:59 +0000153 canvas->save();
154 for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) {
Brian Salomon024bd002019-06-11 11:38:16 -0400155 SkScopeExit cleanup;
156 sk_sp<SkImage> result;
157 if (doYUV420) {
158 result = do_read_and_scale_yuv(surface, yuvColorSpace, srcRect, newW, newH, gamma,
159 quality, &cleanup);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400160 if (!result) {
161 errorMsg->printf("YUV420 async call failed. Allowed for now.");
162 return skiagm::DrawResult::kSkip;
163 }
Brian Salomon024bd002019-06-11 11:38:16 -0400164 int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1);
165 yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS);
166 } else {
167 result = do_read_and_scale(surface, srcRect, ii, gamma, quality);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400168 if (!result) {
169 errorMsg->printf("async read call failed.");
170 return skiagm::DrawResult::kFail;
171 }
Brian Salomon024bd002019-06-11 11:38:16 -0400172 }
173 canvas->drawImage(result, 0, 0);
Brian Salomon286b5572019-05-20 10:25:50 -0400174 canvas->translate(newW + pad, 0);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000175 }
176 canvas->restore();
Brian Salomon286b5572019-05-20 10:25:50 -0400177 canvas->translate(0, newH + pad);
Brian Salomon451b01f2019-05-17 12:39:17 -0400178 }
Brian Salomona34e0fc2019-05-17 19:56:59 +0000179 canvas->restore();
180 return skiagm::DrawResult::kOk;
Brian Salomon451b01f2019-05-17 12:39:17 -0400181}
182
Brian Salomon286b5572019-05-20 10:25:50 -0400183static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, const char* imageFile,
184 const SkIRect& srcRect, int newW, int newH,
Brian Salomon024bd002019-06-11 11:38:16 -0400185 bool doYUV420, SkString* errorMsg) {
Brian Salomon286b5572019-05-20 10:25:50 -0400186 auto image = GetResourceAsImage(imageFile);
187 if (!image) {
188 errorMsg->printf("Could not load image file %s.", imageFile);
189 return skiagm::DrawResult::kFail;
190 }
191 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
192 *errorMsg = "Not supported on recording/vector backends.";
193 return skiagm::DrawResult::kSkip;
194 }
195 // Turn the image into a surface in order to call the read and rescale API
196 auto surfInfo = image->imageInfo().makeWH(image->width(), image->height());
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);
201 }
202 if (!surface) {
203 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400204 // When testing abandoned GrContext we expect surface creation to fail.
205 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
206 return skiagm::DrawResult::kSkip;
207 }
Brian Salomon286b5572019-05-20 10:25:50 -0400208 return skiagm::DrawResult::kFail;
209 }
210 SkPaint paint;
211 paint.setBlendMode(SkBlendMode::kSrc);
212 surface->getCanvas()->drawImage(image, 0, 0, &paint);
Brian Salomon024bd002019-06-11 11:38:16 -0400213 return do_rescale_grid(canvas, surface.get(), srcRect, newW, newH, doYUV420, errorMsg);
Brian Salomon286b5572019-05-20 10:25:50 -0400214}
215
Brian Salomon024bd002019-06-11 11:38:16 -0400216#define DEF_RESCALE_AND_READ_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
217 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
218 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
219 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, W, H, false, errorMsg); \
Brian Salomon201700f2019-05-17 12:05:44 -0400220 }
221
Brian Salomon024bd002019-06-11 11:38:16 -0400222#define DEF_RESCALE_AND_READ_YUV_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
223 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
224 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
225 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, W, H, true, errorMsg); \
226 }
227
228DEF_RESCALE_AND_READ_YUV_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150),
229 410, 376)
230
Brian Salomon201700f2019-05-17 12:05:44 -0400231DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100),
232 410, 410)
233
234DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
Brian Salomonc7e9f782019-05-28 20:39:53 -0400235DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
Brian Salomon201700f2019-05-17 12:05:44 -0400236
237DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637),
238 (int)(0.7 * 105))
239DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637),
240 (int)(1.2 * 105))
241DEF_RESCALE_AND_READ_GM(images/text.png, text_up_large, SkIRect::MakeXYWH(300, 0, 300, 105),
242 (int)(2.4 * 300), (int)(2.4 * 105))
Brian Salomon286b5572019-05-20 10:25:50 -0400243
244DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) {
245 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
246 *errorMsg = "Not supported on recording/vector backends.";
247 return skiagm::DrawResult::kSkip;
248 }
249
250 static constexpr int kBorder = 5;
251 static constexpr int kInner = 5;
252 const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner);
253 auto surfaceII =
254 SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType,
255 kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
256 auto surface = canvas->makeSurface(surfaceII);
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400257 if (!surface) {
258 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400259 // When testing abandoned GrContext we expect surface creation to fail.
260 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
261 return skiagm::DrawResult::kSkip;
262 }
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400263 return skiagm::DrawResult::kFail;
264 }
Brian Salomon286b5572019-05-20 10:25:50 -0400265 surface->getCanvas()->clear(SK_ColorRED);
266 surface->getCanvas()->save();
267 surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false);
268 surface->getCanvas()->clear(SK_ColorBLUE);
269 surface->getCanvas()->restore();
270 static constexpr int kPad = 2;
271 canvas->translate(kPad, kPad);
272 skiagm::DrawResult result;
273 auto downW = static_cast<int>(kInner / 2);
274 auto downH = static_cast<int>(kInner / 2);
Brian Salomon024bd002019-06-11 11:38:16 -0400275 result = do_rescale_grid(canvas, surface.get(), srcRect, downW, downH, false, errorMsg, kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400276 if (result != skiagm::DrawResult::kOk) {
277 return result;
278 }
279 canvas->translate(0, 2 * downH);
280 auto upW = static_cast<int>(kInner * 3.5);
281 auto upH = static_cast<int>(kInner * 4.6);
Brian Salomon024bd002019-06-11 11:38:16 -0400282 result = do_rescale_grid(canvas, surface.get(), srcRect, upW, upH, false, errorMsg, kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400283 if (result != skiagm::DrawResult::kOk) {
284 return result;
285 }
286 return skiagm::DrawResult::kOk;
287}