Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 1 | /* |
| 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 Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 14 | #include "include/core/SkYUVAIndex.h" |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrContext.h" |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 16 | #include "include/private/GrRecordingContext.h" |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 17 | #include "src/core/SkAutoPixmapStorage.h" |
| 18 | #include "src/core/SkConvertPixels.h" |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 19 | #include "src/core/SkScopeExit.h" |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 21 | #include "tools/Resources.h" |
| 22 | #include "tools/ToolUtils.h" |
| 23 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 24 | namespace { |
| 25 | struct AsyncContext { |
| 26 | bool fCalled = false; |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 27 | std::unique_ptr<const SkImage::AsyncReadResult> fResult; |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 28 | }; |
| 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 Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 34 | static void async_callback(void* c, std::unique_ptr<const SkImage::AsyncReadResult> result) { |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 35 | auto context = static_cast<AsyncContext*>(c); |
| 36 | context->fResult = std::move(result); |
| 37 | context->fCalled = true; |
| 38 | }; |
| 39 | |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 40 | // Draws the image to a surface, does a asyncRescaleAndReadPixels of the image, and then sticks |
| 41 | // the result in a raster image. |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 42 | template <typename Src> |
| 43 | static sk_sp<SkImage> do_read_and_scale(Src* src, |
| 44 | GrContext* context, |
| 45 | const SkIRect& srcRect, |
| 46 | const SkImageInfo& ii, |
| 47 | SkImage::RescaleGamma rescaleGamma, |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 48 | SkFilterQuality quality) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 49 | auto* asyncContext = new AsyncContext(); |
| 50 | src->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, async_callback, |
| 51 | asyncContext); |
| 52 | if (context) { |
| 53 | context->submit(); |
Brian Salomon | 3927802 | 2020-06-02 11:47:26 -0400 | [diff] [blame] | 54 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 55 | while (!asyncContext->fCalled) { |
Brian Salomon | 9c21978 | 2019-10-02 22:51:08 +0000 | [diff] [blame] | 56 | // Only GPU should actually be asynchronous. |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 57 | SkASSERT(context); |
| 58 | context->checkAsyncWorkCompletion(); |
Brian Salomon | 9c21978 | 2019-10-02 22:51:08 +0000 | [diff] [blame] | 59 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 60 | if (!asyncContext->fResult) { |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 61 | return nullptr; |
| 62 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 63 | SkPixmap pixmap(ii, asyncContext->fResult->data(0), asyncContext->fResult->rowBytes(0)); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 64 | auto releasePixels = [](const void*, void* c) { delete static_cast<AsyncContext*>(c); }; |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 65 | return SkImage::MakeFromRaster(pixmap, releasePixels, asyncContext); |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 68 | template <typename Src> |
| 69 | static sk_sp<SkImage> do_read_and_scale_yuv(Src* src, |
| 70 | GrContext* context, |
| 71 | SkYUVColorSpace yuvCS, |
| 72 | const SkIRect& srcRect, |
| 73 | SkISize size, |
| 74 | SkImage::RescaleGamma rescaleGamma, |
| 75 | SkFilterQuality quality, |
| 76 | SkScopeExit* cleanup) { |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 77 | SkASSERT(!(size.width() & 0b1) && !(size.height() & 0b1)); |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 78 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 79 | 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 Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 82 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 83 | AsyncContext asyncContext; |
| 84 | src->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, size, |
| 85 | rescaleGamma, quality, async_callback, &asyncContext); |
| 86 | if (context) { |
| 87 | context->submit(); |
Brian Salomon | 3927802 | 2020-06-02 11:47:26 -0400 | [diff] [blame] | 88 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 89 | while (!asyncContext.fCalled) { |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 90 | // Only GPU should actually be asynchronous. |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 91 | SkASSERT(context); |
| 92 | context->checkAsyncWorkCompletion(); |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 93 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 94 | if (!asyncContext.fResult) { |
Brian Salomon | cd5caa3 | 2019-06-11 17:02:19 -0400 | [diff] [blame] | 95 | return nullptr; |
| 96 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 97 | GrBackendTexture backendTextures[3]; |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 98 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 99 | SkPixmap yPM(yII, asyncContext.fResult->data(0), asyncContext.fResult->rowBytes(0)); |
| 100 | SkPixmap uPM(uvII, asyncContext.fResult->data(1), asyncContext.fResult->rowBytes(1)); |
| 101 | SkPixmap vPM(uvII, asyncContext.fResult->data(2), asyncContext.fResult->rowBytes(2)); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 102 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 103 | backendTextures[0] = context->createBackendTexture(yPM, GrRenderable::kNo, GrProtected::kNo); |
| 104 | backendTextures[1] = context->createBackendTexture(uPM, GrRenderable::kNo, GrProtected::kNo); |
| 105 | backendTextures[2] = context->createBackendTexture(vPM, GrRenderable::kNo, GrProtected::kNo); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 106 | |
Robert Phillips | 2e1142c | 2019-08-14 12:33:29 -0400 | [diff] [blame] | 107 | SkYUVAIndex indices[4] = { |
| 108 | { 0, SkColorChannel::kR}, |
| 109 | { 1, SkColorChannel::kR}, |
| 110 | { 2, SkColorChannel::kR}, |
| 111 | {-1, SkColorChannel::kR} |
| 112 | }; |
| 113 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 114 | *cleanup = {[context, backendTextures] { |
Greg Daniel | ce9f016 | 2020-06-30 13:42:46 -0400 | [diff] [blame] | 115 | context->flush(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 116 | context->submit(true); |
| 117 | context->deleteBackendTexture(backendTextures[0]); |
| 118 | context->deleteBackendTexture(backendTextures[1]); |
| 119 | context->deleteBackendTexture(backendTextures[2]); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 120 | }}; |
| 121 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 122 | return SkImage::MakeFromYUVATextures(context, yuvCS, backendTextures, indices, size, |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 123 | kTopLeft_GrSurfaceOrigin, SkColorSpace::MakeSRGB()); |
| 124 | } |
| 125 | |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 126 | // Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are |
| 127 | // rescale in src gamma and rescale in linear gamma. |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 128 | template <typename Src> |
| 129 | static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, |
| 130 | Src* src, |
| 131 | GrContext* context, |
| 132 | const SkIRect& srcRect, |
| 133 | SkISize newSize, |
| 134 | bool doYUV420, |
| 135 | SkString* errorMsg, |
| 136 | int pad = 0) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 137 | if (doYUV420) { |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 138 | if (!canvas->recordingContext() || !canvas->recordingContext()->priv().asDirectContext()) { |
Brian Salomon | cd5caa3 | 2019-06-11 17:02:19 -0400 | [diff] [blame] | 139 | errorMsg->printf("YUV420 only supported on direct GPU for now."); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 140 | return skiagm::DrawResult::kSkip; |
| 141 | } |
| 142 | } |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 143 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 144 | *errorMsg = "Not supported on recording/vector backends."; |
| 145 | return skiagm::DrawResult::kSkip; |
| 146 | } |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 147 | const auto ii = canvas->imageInfo().makeDimensions(newSize); |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 148 | |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 149 | SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace; |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 150 | canvas->save(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 151 | for (auto gamma : {SkImage::RescaleGamma::kSrc, SkImage::RescaleGamma::kLinear}) { |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 152 | canvas->save(); |
| 153 | for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 154 | SkScopeExit cleanup; |
| 155 | sk_sp<SkImage> result; |
| 156 | if (doYUV420) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 157 | result = do_read_and_scale_yuv(src, context, yuvColorSpace, srcRect, newSize, gamma, |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 158 | quality, &cleanup); |
Brian Salomon | cd5caa3 | 2019-06-11 17:02:19 -0400 | [diff] [blame] | 159 | if (!result) { |
| 160 | errorMsg->printf("YUV420 async call failed. Allowed for now."); |
| 161 | return skiagm::DrawResult::kSkip; |
| 162 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 163 | int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1); |
| 164 | yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS); |
| 165 | } else { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 166 | result = do_read_and_scale(src, context, srcRect, ii, gamma, quality); |
Brian Salomon | cd5caa3 | 2019-06-11 17:02:19 -0400 | [diff] [blame] | 167 | if (!result) { |
| 168 | errorMsg->printf("async read call failed."); |
| 169 | return skiagm::DrawResult::kFail; |
| 170 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 171 | } |
| 172 | canvas->drawImage(result, 0, 0); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 173 | canvas->translate(newSize.width() + pad, 0); |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 174 | } |
| 175 | canvas->restore(); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 176 | canvas->translate(0, newSize.height() + pad); |
Brian Salomon | 451b01f | 2019-05-17 12:39:17 -0400 | [diff] [blame] | 177 | } |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 178 | canvas->restore(); |
| 179 | return skiagm::DrawResult::kOk; |
Brian Salomon | 451b01f | 2019-05-17 12:39:17 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 182 | static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, |
| 183 | const char* imageFile, |
| 184 | const SkIRect& srcRect, |
| 185 | SkISize newSize, |
| 186 | bool doSurface, |
| 187 | bool doYUV420, |
| 188 | SkString* errorMsg) { |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 189 | auto image = GetResourceAsImage(imageFile); |
| 190 | if (!image) { |
| 191 | errorMsg->printf("Could not load image file %s.", imageFile); |
| 192 | return skiagm::DrawResult::kFail; |
| 193 | } |
| 194 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 195 | *errorMsg = "Not supported on recording/vector backends."; |
| 196 | return skiagm::DrawResult::kSkip; |
| 197 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 198 | if (doSurface) { |
| 199 | // Turn the image into a surface in order to call the read and rescale API |
| 200 | auto surfInfo = image->imageInfo().makeDimensions(image->dimensions()); |
| 201 | auto surface = canvas->makeSurface(surfInfo); |
| 202 | if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) { |
| 203 | surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType); |
| 204 | surface = canvas->makeSurface(surfInfo); |
Brian Salomon | 1caf378 | 2020-06-26 17:06:31 +0000 | [diff] [blame] | 205 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 206 | if (!surface) { |
| 207 | *errorMsg = "Could not create surface for image."; |
| 208 | // When testing abandoned GrContext we expect surface creation to fail. |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame^] | 209 | if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 210 | return skiagm::DrawResult::kSkip; |
| 211 | } |
| 212 | return skiagm::DrawResult::kFail; |
| 213 | } |
| 214 | SkPaint paint; |
| 215 | paint.setBlendMode(SkBlendMode::kSrc); |
| 216 | surface->getCanvas()->drawImage(image, 0, 0, &paint); |
| 217 | return do_rescale_grid(canvas, surface.get(), canvas->getGrContext(), srcRect, newSize, |
| 218 | doYUV420, errorMsg); |
| 219 | } else if (auto ctx = canvas->getGrContext()) { |
| 220 | image = image->makeTextureImage(ctx); |
| 221 | if (!image) { |
| 222 | *errorMsg = "Could not create image."; |
| 223 | // When testing abandoned GrContext we expect surface creation to fail. |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame^] | 224 | if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 225 | return skiagm::DrawResult::kSkip; |
| 226 | } |
| 227 | return skiagm::DrawResult::kFail; |
| 228 | } |
Brian Salomon | 1caf378 | 2020-06-26 17:06:31 +0000 | [diff] [blame] | 229 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 230 | return do_rescale_grid(canvas, image.get(), canvas->getGrContext(), srcRect, newSize, doYUV420, |
| 231 | errorMsg); |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 234 | #define DEF_RESCALE_AND_READ_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 235 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 236 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 237 | return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, false, \ |
| 238 | errorMsg); \ |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 241 | #define DEF_RESCALE_AND_READ_YUV_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 242 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 243 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 244 | return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \ |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 247 | #define DEF_RESCALE_AND_READ_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 248 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 249 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 250 | return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, false, false, \ |
| 251 | errorMsg); \ |
| 252 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 253 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 254 | #define DEF_RESCALE_AND_READ_YUV_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 255 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 256 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 257 | return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \ |
| 258 | } |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 259 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 260 | DEF_RESCALE_AND_READ_YUV_SURF_GM( |
| 261 | images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150), 410, 376) |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 262 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 263 | DEF_RESCALE_AND_READ_YUV_IMG_GM( |
| 264 | images/yellow_rose.webp, rose_down, SkIRect::MakeXYWH(50, 5, 200, 150), 106, 60) |
| 265 | |
| 266 | DEF_RESCALE_AND_READ_SURF_GM( |
| 267 | images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100), 410, 410) |
| 268 | |
| 269 | DEF_RESCALE_AND_READ_SURF_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45) |
| 270 | DEF_RESCALE_AND_READ_IMG_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400) |
| 271 | |
| 272 | DEF_RESCALE_AND_READ_IMG_GM( |
| 273 | images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637), (int)(0.7 * 105)) |
| 274 | DEF_RESCALE_AND_READ_SURF_GM( |
| 275 | images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637), (int)(1.2 * 105)) |
| 276 | DEF_RESCALE_AND_READ_IMG_GM(images/text.png, |
| 277 | text_up_large, |
| 278 | SkIRect::MakeXYWH(300, 0, 300, 105), |
| 279 | (int)(2.4 * 300), |
| 280 | (int)(2.4 * 105)) |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 281 | |
Brian Salomon | a7e5c7c | 2020-01-27 15:41:40 -0500 | [diff] [blame] | 282 | // Exercises non-scaling YUV420. Reads from the original canvas's surface in order to |
| 283 | // exercise case where source surface is not a texture (in glbert config). |
| 284 | DEF_SIMPLE_GM_CAN_FAIL(async_yuv_no_scale, canvas, errorMsg, 400, 300) { |
| 285 | auto surface = canvas->getSurface(); |
| 286 | if (!surface) { |
| 287 | *errorMsg = "Not supported on recording/vector backends."; |
| 288 | return skiagm::DrawResult::kSkip; |
| 289 | } |
| 290 | |
| 291 | auto image = GetResourceAsImage("images/yellow_rose.webp"); |
| 292 | if (!image) { |
| 293 | return skiagm::DrawResult::kFail; |
| 294 | } |
| 295 | SkPaint paint; |
| 296 | canvas->drawImage(image.get(), 0, 0); |
| 297 | |
| 298 | SkScopeExit scopeExit; |
| 299 | auto yuvImage = do_read_and_scale_yuv( |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 300 | surface, surface->getContext(), kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300), |
| 301 | {400, 300}, SkImage::RescaleGamma::kSrc, kNone_SkFilterQuality, &scopeExit); |
Brian Salomon | a7e5c7c | 2020-01-27 15:41:40 -0500 | [diff] [blame] | 302 | |
| 303 | canvas->clear(SK_ColorWHITE); |
| 304 | canvas->drawImage(yuvImage.get(), 0, 0); |
| 305 | |
| 306 | return skiagm::DrawResult::kOk; |
| 307 | } |
| 308 | |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 309 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) { |
| 310 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 311 | *errorMsg = "Not supported on recording/vector backends."; |
| 312 | return skiagm::DrawResult::kSkip; |
| 313 | } |
| 314 | |
| 315 | static constexpr int kBorder = 5; |
| 316 | static constexpr int kInner = 5; |
| 317 | const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner); |
| 318 | auto surfaceII = |
| 319 | SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType, |
| 320 | kPremul_SkAlphaType, SkColorSpace::MakeSRGB()); |
| 321 | auto surface = canvas->makeSurface(surfaceII); |
Brian Salomon | af9b7b9 | 2019-05-20 12:19:37 -0400 | [diff] [blame] | 322 | if (!surface) { |
| 323 | *errorMsg = "Could not create surface for image."; |
Brian Salomon | 9be911c | 2019-05-20 14:01:21 -0400 | [diff] [blame] | 324 | // When testing abandoned GrContext we expect surface creation to fail. |
| 325 | if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) { |
| 326 | return skiagm::DrawResult::kSkip; |
| 327 | } |
Brian Salomon | af9b7b9 | 2019-05-20 12:19:37 -0400 | [diff] [blame] | 328 | return skiagm::DrawResult::kFail; |
| 329 | } |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 330 | surface->getCanvas()->clear(SK_ColorRED); |
| 331 | surface->getCanvas()->save(); |
| 332 | surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false); |
| 333 | surface->getCanvas()->clear(SK_ColorBLUE); |
| 334 | surface->getCanvas()->restore(); |
| 335 | static constexpr int kPad = 2; |
| 336 | canvas->translate(kPad, kPad); |
| 337 | skiagm::DrawResult result; |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 338 | SkISize downSize = {static_cast<int>(kInner/2), static_cast<int>(kInner / 2)}; |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 339 | GrContext* context = canvas->getGrContext(); |
| 340 | result = do_rescale_grid(canvas, surface.get(), context, srcRect, downSize, false, errorMsg, |
| 341 | kPad); |
| 342 | |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 343 | if (result != skiagm::DrawResult::kOk) { |
| 344 | return result; |
| 345 | } |
Mike Klein | f68213f | 2020-01-27 12:16:22 -0600 | [diff] [blame] | 346 | canvas->translate(0, 4 * downSize.height()); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 347 | SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)}; |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 348 | result = |
| 349 | do_rescale_grid(canvas, surface.get(), context, srcRect, upSize, false, errorMsg, kPad); |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 350 | if (result != skiagm::DrawResult::kOk) { |
| 351 | return result; |
| 352 | } |
| 353 | return skiagm::DrawResult::kOk; |
| 354 | } |