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