blob: 2cd4d8793d8199fd39f1459ba773e85675ca7d85 [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(
103 dstW, dstH, format, GrMipMapped::kNo, GrRenderable::kNo, yData.get(), 0, nullptr);
104 backendTextures[1] = gr->priv().getGpu()->createBackendTexture(
105 dstW / 2, dstH / 2, format, GrMipMapped::kNo, GrRenderable::kNo,
106 uData.get(), 0, nullptr);
107 backendTextures[2] = gr->priv().getGpu()->createBackendTexture(
108 dstW / 2, dstH / 2, format, GrMipMapped::kNo, GrRenderable::kNo,
109 vData.get(), 0, nullptr);
110 auto config = gr->priv().caps()->getConfigFromBackendFormat(format, kAlpha_8_SkColorType);
111 SkColorChannel channel;
112 if (config == kAlpha_8_as_Red_GrPixelConfig) {
113 channel = SkColorChannel::kR;
114 } else {
115 SkASSERT(config == kAlpha_8_as_Alpha_GrPixelConfig);
116 channel = SkColorChannel::kA;
117 }
118 SkYUVAIndex indices[4]{{0, channel}, {1, channel}, {2, channel}, {-1, SkColorChannel::kR}};
119 *cleanup = {[gr, backendTextures] {
120 GrFlushInfo flushInfo;
121 flushInfo.fFlags = kSyncCpu_GrFlushFlag;
122 gr->flush(flushInfo);
123 gr->deleteBackendTexture(backendTextures[0]);
124 gr->deleteBackendTexture(backendTextures[1]);
125 gr->deleteBackendTexture(backendTextures[2]);
126 }};
127
128 return SkImage::MakeFromYUVATextures(gr, yuvCS, backendTextures, indices, {dstW, dstH},
129 kTopLeft_GrSurfaceOrigin, SkColorSpace::MakeSRGB());
130}
131
Brian Salomon201700f2019-05-17 12:05:44 -0400132// Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are
133// rescale in src gamma and rescale in linear gamma.
Brian Salomon286b5572019-05-20 10:25:50 -0400134static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, SkSurface* surface,
Brian Salomon024bd002019-06-11 11:38:16 -0400135 const SkIRect& srcRect, int newW, int newH, bool doYUV420,
Brian Salomon286b5572019-05-20 10:25:50 -0400136 SkString* errorMsg, int pad = 0) {
Brian Salomon024bd002019-06-11 11:38:16 -0400137 if (doYUV420) {
Brian Salomoncd5caa32019-06-11 17:02:19 -0400138 if (!canvas->getGrContext() || !canvas->getGrContext()->priv().asDirectContext()) {
139 errorMsg->printf("YUV420 only supported on direct GPU for now.");
Brian Salomon024bd002019-06-11 11:38:16 -0400140 return skiagm::DrawResult::kSkip;
141 }
142 }
Brian Salomon201700f2019-05-17 12:05:44 -0400143 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
144 *errorMsg = "Not supported on recording/vector backends.";
145 return skiagm::DrawResult::kSkip;
146 }
Brian Salomona34e0fc2019-05-17 19:56:59 +0000147 const auto ii = canvas->imageInfo().makeWH(newW, newH);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000148
Brian Salomon024bd002019-06-11 11:38:16 -0400149 SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
Brian Salomona34e0fc2019-05-17 19:56:59 +0000150 canvas->save();
Brian Salomon024bd002019-06-11 11:38:16 -0400151 for (auto gamma : {SkSurface::RescaleGamma::kSrc, SkSurface::RescaleGamma::kLinear}) {
Brian Salomona34e0fc2019-05-17 19:56:59 +0000152 canvas->save();
153 for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) {
Brian Salomon024bd002019-06-11 11:38:16 -0400154 SkScopeExit cleanup;
155 sk_sp<SkImage> result;
156 if (doYUV420) {
157 result = do_read_and_scale_yuv(surface, yuvColorSpace, srcRect, newW, newH, gamma,
158 quality, &cleanup);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400159 if (!result) {
160 errorMsg->printf("YUV420 async call failed. Allowed for now.");
161 return skiagm::DrawResult::kSkip;
162 }
Brian Salomon024bd002019-06-11 11:38:16 -0400163 int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1);
164 yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS);
165 } else {
166 result = do_read_and_scale(surface, srcRect, ii, gamma, quality);
Brian Salomoncd5caa32019-06-11 17:02:19 -0400167 if (!result) {
168 errorMsg->printf("async read call failed.");
169 return skiagm::DrawResult::kFail;
170 }
Brian Salomon024bd002019-06-11 11:38:16 -0400171 }
172 canvas->drawImage(result, 0, 0);
Brian Salomon286b5572019-05-20 10:25:50 -0400173 canvas->translate(newW + pad, 0);
Brian Salomona34e0fc2019-05-17 19:56:59 +0000174 }
175 canvas->restore();
Brian Salomon286b5572019-05-20 10:25:50 -0400176 canvas->translate(0, newH + pad);
Brian Salomon451b01f2019-05-17 12:39:17 -0400177 }
Brian Salomona34e0fc2019-05-17 19:56:59 +0000178 canvas->restore();
179 return skiagm::DrawResult::kOk;
Brian Salomon451b01f2019-05-17 12:39:17 -0400180}
181
Brian Salomon286b5572019-05-20 10:25:50 -0400182static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, const char* imageFile,
183 const SkIRect& srcRect, int newW, int newH,
Brian Salomon024bd002019-06-11 11:38:16 -0400184 bool doYUV420, SkString* errorMsg) {
Brian Salomon286b5572019-05-20 10:25:50 -0400185 auto image = GetResourceAsImage(imageFile);
186 if (!image) {
187 errorMsg->printf("Could not load image file %s.", imageFile);
188 return skiagm::DrawResult::kFail;
189 }
190 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
191 *errorMsg = "Not supported on recording/vector backends.";
192 return skiagm::DrawResult::kSkip;
193 }
194 // Turn the image into a surface in order to call the read and rescale API
195 auto surfInfo = image->imageInfo().makeWH(image->width(), image->height());
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);
200 }
201 if (!surface) {
202 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400203 // When testing abandoned GrContext we expect surface creation to fail.
204 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
205 return skiagm::DrawResult::kSkip;
206 }
Brian Salomon286b5572019-05-20 10:25:50 -0400207 return skiagm::DrawResult::kFail;
208 }
209 SkPaint paint;
210 paint.setBlendMode(SkBlendMode::kSrc);
211 surface->getCanvas()->drawImage(image, 0, 0, &paint);
Brian Salomon024bd002019-06-11 11:38:16 -0400212 return do_rescale_grid(canvas, surface.get(), srcRect, newW, newH, doYUV420, errorMsg);
Brian Salomon286b5572019-05-20 10:25:50 -0400213}
214
Brian Salomon024bd002019-06-11 11:38:16 -0400215#define DEF_RESCALE_AND_READ_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
216 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
217 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
218 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, W, H, false, errorMsg); \
Brian Salomon201700f2019-05-17 12:05:44 -0400219 }
220
Brian Salomon024bd002019-06-11 11:38:16 -0400221#define DEF_RESCALE_AND_READ_YUV_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
222 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
223 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
224 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, W, H, true, errorMsg); \
225 }
226
227DEF_RESCALE_AND_READ_YUV_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150),
228 410, 376)
229
Brian Salomon201700f2019-05-17 12:05:44 -0400230DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100),
231 410, 410)
232
233DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
Brian Salomonc7e9f782019-05-28 20:39:53 -0400234DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
Brian Salomon201700f2019-05-17 12:05:44 -0400235
236DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637),
237 (int)(0.7 * 105))
238DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637),
239 (int)(1.2 * 105))
240DEF_RESCALE_AND_READ_GM(images/text.png, text_up_large, SkIRect::MakeXYWH(300, 0, 300, 105),
241 (int)(2.4 * 300), (int)(2.4 * 105))
Brian Salomon286b5572019-05-20 10:25:50 -0400242
243DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) {
244 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
245 *errorMsg = "Not supported on recording/vector backends.";
246 return skiagm::DrawResult::kSkip;
247 }
248
249 static constexpr int kBorder = 5;
250 static constexpr int kInner = 5;
251 const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner);
252 auto surfaceII =
253 SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType,
254 kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
255 auto surface = canvas->makeSurface(surfaceII);
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400256 if (!surface) {
257 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400258 // When testing abandoned GrContext we expect surface creation to fail.
259 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
260 return skiagm::DrawResult::kSkip;
261 }
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400262 return skiagm::DrawResult::kFail;
263 }
Brian Salomon286b5572019-05-20 10:25:50 -0400264 surface->getCanvas()->clear(SK_ColorRED);
265 surface->getCanvas()->save();
266 surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false);
267 surface->getCanvas()->clear(SK_ColorBLUE);
268 surface->getCanvas()->restore();
269 static constexpr int kPad = 2;
270 canvas->translate(kPad, kPad);
271 skiagm::DrawResult result;
272 auto downW = static_cast<int>(kInner / 2);
273 auto downH = static_cast<int>(kInner / 2);
Brian Salomon024bd002019-06-11 11:38:16 -0400274 result = do_rescale_grid(canvas, surface.get(), srcRect, downW, downH, false, errorMsg, kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400275 if (result != skiagm::DrawResult::kOk) {
276 return result;
277 }
278 canvas->translate(0, 2 * downH);
279 auto upW = static_cast<int>(kInner * 3.5);
280 auto upH = static_cast<int>(kInner * 4.6);
Brian Salomon024bd002019-06-11 11:38:16 -0400281 result = do_rescale_grid(canvas, surface.get(), srcRect, upW, upH, false, errorMsg, kPad);
Brian Salomon286b5572019-05-20 10:25:50 -0400282 if (result != skiagm::DrawResult::kOk) {
283 return result;
284 }
285 return skiagm::DrawResult::kOk;
286}