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 | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 14 | #include "include/core/SkYUVAInfo.h" |
| 15 | #include "include/core/SkYUVAPixmaps.h" |
Robert Phillips | 2a4acf3 | 2020-07-06 15:50:15 -0400 | [diff] [blame] | 16 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 17 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 18 | #include "src/core/SkAutoPixmapStorage.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" |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 22 | #include "tools/gpu/YUVUtils.h" |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 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, |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 44 | GrDirectContext* direct, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 45 | const SkIRect& srcRect, |
| 46 | const SkImageInfo& ii, |
| 47 | SkImage::RescaleGamma rescaleGamma, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame^] | 48 | SkImage::RescaleMode rescaleMode) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 49 | auto* asyncContext = new AsyncContext(); |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame^] | 50 | src->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, rescaleMode, async_callback, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 51 | asyncContext); |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 52 | if (direct) { |
| 53 | direct->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. |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 57 | SkASSERT(direct); |
| 58 | direct->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, |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 70 | GrDirectContext* direct, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 71 | SkYUVColorSpace yuvCS, |
| 72 | const SkIRect& srcRect, |
| 73 | SkISize size, |
| 74 | SkImage::RescaleGamma rescaleGamma, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame^] | 75 | SkImage::RescaleMode rescaleMode, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 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, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame^] | 85 | rescaleGamma, rescaleMode, async_callback, &asyncContext); |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 86 | if (direct) { |
| 87 | direct->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. |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 91 | SkASSERT(direct); |
| 92 | direct->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 | e438738 | 2020-11-11 16:34:19 -0500 | [diff] [blame] | 97 | SkYUVAInfo yuvaInfo(size, |
| 98 | SkYUVAInfo::PlaneConfig::kY_U_V, |
| 99 | SkYUVAInfo::Subsampling::k420, |
| 100 | yuvCS); |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 101 | SkPixmap yuvPMs[] = { |
| 102 | {yII, asyncContext.fResult->data(0), asyncContext.fResult->rowBytes(0)}, |
| 103 | {uvII, asyncContext.fResult->data(1), asyncContext.fResult->rowBytes(1)}, |
| 104 | {uvII, asyncContext.fResult->data(2), asyncContext.fResult->rowBytes(2)} |
Robert Phillips | 2e1142c | 2019-08-14 12:33:29 -0400 | [diff] [blame] | 105 | }; |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 106 | auto pixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, yuvPMs); |
| 107 | SkASSERT(pixmaps.isValid()); |
| 108 | auto lazyYUVImage = sk_gpu_test::LazyYUVImage::Make(pixmaps); |
| 109 | SkASSERT(lazyYUVImage); |
| 110 | return lazyYUVImage->refImage(direct, sk_gpu_test::LazyYUVImage::Type::kFromTextures); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 113 | // Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are |
| 114 | // rescale in src gamma and rescale in linear gamma. |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 115 | template <typename Src> |
| 116 | static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, |
| 117 | Src* src, |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 118 | GrDirectContext* direct, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 119 | const SkIRect& srcRect, |
| 120 | SkISize newSize, |
| 121 | bool doYUV420, |
| 122 | SkString* errorMsg, |
| 123 | int pad = 0) { |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 124 | if (doYUV420 && !direct) { |
Adlai Holler | e3ad527 | 2020-07-07 10:27:55 -0400 | [diff] [blame] | 125 | errorMsg->printf("YUV420 only supported on direct GPU for now."); |
| 126 | return skiagm::DrawResult::kSkip; |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 127 | } |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 128 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 129 | *errorMsg = "Not supported on recording/vector backends."; |
| 130 | return skiagm::DrawResult::kSkip; |
| 131 | } |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 132 | const auto ii = canvas->imageInfo().makeDimensions(newSize); |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 133 | |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 134 | SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace; |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 135 | canvas->save(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 136 | for (auto gamma : {SkImage::RescaleGamma::kSrc, SkImage::RescaleGamma::kLinear}) { |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 137 | canvas->save(); |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame^] | 138 | for (auto mode : { |
| 139 | SkImage::RescaleMode::kNearest, |
| 140 | SkImage::RescaleMode::kRepeatedLinear, |
| 141 | SkImage::RescaleMode::kRepeatedCubic}) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 142 | SkScopeExit cleanup; |
| 143 | sk_sp<SkImage> result; |
| 144 | if (doYUV420) { |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 145 | result = do_read_and_scale_yuv(src, direct, yuvColorSpace, srcRect, newSize, gamma, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame^] | 146 | mode, &cleanup); |
Brian Salomon | cd5caa3 | 2019-06-11 17:02:19 -0400 | [diff] [blame] | 147 | if (!result) { |
| 148 | errorMsg->printf("YUV420 async call failed. Allowed for now."); |
| 149 | return skiagm::DrawResult::kSkip; |
| 150 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 151 | int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1); |
| 152 | yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS); |
| 153 | } else { |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame^] | 154 | result = do_read_and_scale(src, direct, srcRect, ii, gamma, mode); |
Brian Salomon | cd5caa3 | 2019-06-11 17:02:19 -0400 | [diff] [blame] | 155 | if (!result) { |
| 156 | errorMsg->printf("async read call failed."); |
| 157 | return skiagm::DrawResult::kFail; |
| 158 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 159 | } |
| 160 | canvas->drawImage(result, 0, 0); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 161 | canvas->translate(newSize.width() + pad, 0); |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 162 | } |
| 163 | canvas->restore(); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 164 | canvas->translate(0, newSize.height() + pad); |
Brian Salomon | 451b01f | 2019-05-17 12:39:17 -0400 | [diff] [blame] | 165 | } |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 166 | canvas->restore(); |
| 167 | return skiagm::DrawResult::kOk; |
Brian Salomon | 451b01f | 2019-05-17 12:39:17 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 170 | static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, |
| 171 | const char* imageFile, |
| 172 | const SkIRect& srcRect, |
| 173 | SkISize newSize, |
| 174 | bool doSurface, |
| 175 | bool doYUV420, |
| 176 | SkString* errorMsg) { |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 177 | auto image = GetResourceAsImage(imageFile); |
| 178 | if (!image) { |
| 179 | errorMsg->printf("Could not load image file %s.", imageFile); |
| 180 | return skiagm::DrawResult::kFail; |
| 181 | } |
| 182 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 183 | *errorMsg = "Not supported on recording/vector backends."; |
| 184 | return skiagm::DrawResult::kSkip; |
| 185 | } |
Robert Phillips | 41fc174 | 2020-10-15 10:04:03 -0400 | [diff] [blame] | 186 | |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 187 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
Robert Phillips | 41fc174 | 2020-10-15 10:04:03 -0400 | [diff] [blame] | 188 | if (!dContext && canvas->recordingContext()) { |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 189 | *errorMsg = "Not supported in DDL mode"; |
| 190 | return skiagm::DrawResult::kSkip; |
| 191 | } |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 192 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 193 | if (doSurface) { |
| 194 | // Turn the image into a surface in order to call the read and rescale API |
| 195 | auto surfInfo = image->imageInfo().makeDimensions(image->dimensions()); |
| 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); |
Brian Salomon | 1caf378 | 2020-06-26 17:06:31 +0000 | [diff] [blame] | 200 | } |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 201 | if (!surface) { |
| 202 | *errorMsg = "Could not create surface for image."; |
| 203 | // When testing abandoned GrContext we expect surface creation to fail. |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 204 | if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 205 | return skiagm::DrawResult::kSkip; |
| 206 | } |
| 207 | return skiagm::DrawResult::kFail; |
| 208 | } |
| 209 | SkPaint paint; |
| 210 | paint.setBlendMode(SkBlendMode::kSrc); |
| 211 | surface->getCanvas()->drawImage(image, 0, 0, &paint); |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 212 | return do_rescale_grid(canvas, surface.get(), dContext, srcRect, newSize, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 213 | doYUV420, errorMsg); |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 214 | } else if (dContext) { |
| 215 | image = image->makeTextureImage(dContext); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 216 | if (!image) { |
| 217 | *errorMsg = "Could not create image."; |
| 218 | // When testing abandoned GrContext we expect surface creation to fail. |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 219 | if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 220 | return skiagm::DrawResult::kSkip; |
| 221 | } |
| 222 | return skiagm::DrawResult::kFail; |
| 223 | } |
Brian Salomon | 1caf378 | 2020-06-26 17:06:31 +0000 | [diff] [blame] | 224 | } |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 225 | return do_rescale_grid(canvas, image.get(), dContext, srcRect, newSize, doYUV420, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 226 | errorMsg); |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 229 | #define DEF_RESCALE_AND_READ_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 230 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 231 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 232 | return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, false, \ |
| 233 | errorMsg); \ |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 236 | #define DEF_RESCALE_AND_READ_YUV_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 237 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 238 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 239 | 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] | 240 | } |
| 241 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 242 | #define DEF_RESCALE_AND_READ_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 243 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 244 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 245 | return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, false, false, \ |
| 246 | errorMsg); \ |
| 247 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 248 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 249 | #define DEF_RESCALE_AND_READ_YUV_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 250 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 251 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 252 | return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \ |
| 253 | } |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 254 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 255 | DEF_RESCALE_AND_READ_YUV_SURF_GM( |
| 256 | 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] | 257 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 258 | DEF_RESCALE_AND_READ_YUV_IMG_GM( |
| 259 | images/yellow_rose.webp, rose_down, SkIRect::MakeXYWH(50, 5, 200, 150), 106, 60) |
| 260 | |
| 261 | DEF_RESCALE_AND_READ_SURF_GM( |
| 262 | images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100), 410, 410) |
| 263 | |
| 264 | DEF_RESCALE_AND_READ_SURF_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45) |
| 265 | DEF_RESCALE_AND_READ_IMG_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400) |
| 266 | |
| 267 | DEF_RESCALE_AND_READ_IMG_GM( |
| 268 | images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637), (int)(0.7 * 105)) |
| 269 | DEF_RESCALE_AND_READ_SURF_GM( |
| 270 | images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637), (int)(1.2 * 105)) |
| 271 | DEF_RESCALE_AND_READ_IMG_GM(images/text.png, |
| 272 | text_up_large, |
| 273 | SkIRect::MakeXYWH(300, 0, 300, 105), |
| 274 | (int)(2.4 * 300), |
| 275 | (int)(2.4 * 105)) |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 276 | |
Brian Salomon | a7e5c7c | 2020-01-27 15:41:40 -0500 | [diff] [blame] | 277 | // Exercises non-scaling YUV420. Reads from the original canvas's surface in order to |
| 278 | // exercise case where source surface is not a texture (in glbert config). |
| 279 | DEF_SIMPLE_GM_CAN_FAIL(async_yuv_no_scale, canvas, errorMsg, 400, 300) { |
| 280 | auto surface = canvas->getSurface(); |
| 281 | if (!surface) { |
| 282 | *errorMsg = "Not supported on recording/vector backends."; |
| 283 | return skiagm::DrawResult::kSkip; |
| 284 | } |
| 285 | |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 286 | auto dContext = GrAsDirectContext(surface->recordingContext()); |
Robert Phillips | 41fc174 | 2020-10-15 10:04:03 -0400 | [diff] [blame] | 287 | if (!dContext && surface->recordingContext()) { |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 288 | *errorMsg = "Not supported in DDL mode"; |
| 289 | return skiagm::DrawResult::kSkip; |
| 290 | } |
| 291 | |
Brian Salomon | a7e5c7c | 2020-01-27 15:41:40 -0500 | [diff] [blame] | 292 | auto image = GetResourceAsImage("images/yellow_rose.webp"); |
| 293 | if (!image) { |
| 294 | return skiagm::DrawResult::kFail; |
| 295 | } |
| 296 | SkPaint paint; |
| 297 | canvas->drawImage(image.get(), 0, 0); |
| 298 | |
| 299 | SkScopeExit scopeExit; |
| 300 | auto yuvImage = do_read_and_scale_yuv( |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 301 | surface, dContext, kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300), |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame^] | 302 | {400, 300}, SkImage::RescaleGamma::kSrc, SkImage::RescaleMode::kNearest, &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 | |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 316 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
Robert Phillips | 41fc174 | 2020-10-15 10:04:03 -0400 | [diff] [blame] | 317 | if (!dContext && canvas->recordingContext()) { |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 318 | *errorMsg = "Not supported in DDL mode"; |
| 319 | return skiagm::DrawResult::kSkip; |
| 320 | } |
| 321 | |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 322 | static constexpr int kBorder = 5; |
| 323 | static constexpr int kInner = 5; |
| 324 | const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner); |
| 325 | auto surfaceII = |
| 326 | SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType, |
| 327 | kPremul_SkAlphaType, SkColorSpace::MakeSRGB()); |
| 328 | auto surface = canvas->makeSurface(surfaceII); |
Brian Salomon | af9b7b9 | 2019-05-20 12:19:37 -0400 | [diff] [blame] | 329 | if (!surface) { |
| 330 | *errorMsg = "Could not create surface for image."; |
Brian Salomon | 9be911c | 2019-05-20 14:01:21 -0400 | [diff] [blame] | 331 | // When testing abandoned GrContext we expect surface creation to fail. |
Robert Phillips | d436b78 | 2020-07-10 11:49:06 -0400 | [diff] [blame] | 332 | if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) { |
Brian Salomon | 9be911c | 2019-05-20 14:01:21 -0400 | [diff] [blame] | 333 | return skiagm::DrawResult::kSkip; |
| 334 | } |
Brian Salomon | af9b7b9 | 2019-05-20 12:19:37 -0400 | [diff] [blame] | 335 | return skiagm::DrawResult::kFail; |
| 336 | } |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 337 | surface->getCanvas()->clear(SK_ColorRED); |
| 338 | surface->getCanvas()->save(); |
| 339 | surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false); |
| 340 | surface->getCanvas()->clear(SK_ColorBLUE); |
| 341 | surface->getCanvas()->restore(); |
| 342 | static constexpr int kPad = 2; |
| 343 | canvas->translate(kPad, kPad); |
| 344 | skiagm::DrawResult result; |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 345 | SkISize downSize = {static_cast<int>(kInner/2), static_cast<int>(kInner / 2)}; |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 346 | result = do_rescale_grid(canvas, surface.get(), dContext, srcRect, downSize, false, errorMsg, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 347 | kPad); |
| 348 | |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 349 | if (result != skiagm::DrawResult::kOk) { |
| 350 | return result; |
| 351 | } |
Mike Klein | f68213f | 2020-01-27 12:16:22 -0600 | [diff] [blame] | 352 | canvas->translate(0, 4 * downSize.height()); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 353 | SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)}; |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 354 | result = do_rescale_grid(canvas, surface.get(), dContext, srcRect, upSize, false, errorMsg, |
| 355 | kPad); |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 356 | if (result != skiagm::DrawResult::kOk) { |
| 357 | return result; |
| 358 | } |
| 359 | return skiagm::DrawResult::kOk; |
| 360 | } |