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" |
| 14 | #include "include/gpu/GrContext.h" |
| 15 | #include "src/core/SkAutoPixmapStorage.h" |
| 16 | #include "src/core/SkConvertPixels.h" |
| 17 | #include "tools/Resources.h" |
| 18 | #include "tools/ToolUtils.h" |
| 19 | |
| 20 | // Draws the image to a surface, does a asyncRescaleAndReadPixels of the image, and then sticks |
| 21 | // the result in a raster image. |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 22 | static sk_sp<SkImage> do_read_and_scale(SkSurface* surface, const SkIRect& srcRect, |
| 23 | const SkImageInfo& ii, SkSurface::RescaleGamma rescaleGamma, |
| 24 | SkFilterQuality quality) { |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 25 | SkBitmap bmp; |
| 26 | bmp.allocPixels(ii); |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 27 | SkPaint paint; |
| 28 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 29 | struct Context { |
| 30 | SkPixmap fPixmap; |
| 31 | bool fCalled = false; |
| 32 | } context; |
| 33 | SkAssertResult(bmp.peekPixels(&context.fPixmap)); |
| 34 | auto callback = [](void* c, const void* data, size_t rowBytes) { |
| 35 | auto context = reinterpret_cast<Context*>(c); |
| 36 | context->fCalled = true; |
| 37 | if (!data) { |
| 38 | context->fPixmap.reset(); |
| 39 | return; |
| 40 | } |
| 41 | SkRectMemcpy(context->fPixmap.writable_addr(), context->fPixmap.rowBytes(), data, rowBytes, |
| 42 | context->fPixmap.info().minRowBytes(), context->fPixmap.height()); |
| 43 | }; |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 44 | surface->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, callback, &context); |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 45 | while (!context.fCalled) { |
| 46 | // Only GPU should actually be asynchronous. |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 47 | SkASSERT(surface->getCanvas()->getGrContext()); |
| 48 | surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion(); |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 49 | } |
| 50 | return SkImage::MakeFromBitmap(bmp); |
| 51 | } |
| 52 | |
| 53 | // Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are |
| 54 | // rescale in src gamma and rescale in linear gamma. |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 55 | static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, SkSurface* surface, |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 56 | const SkIRect& srcRect, int newW, int newH, |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 57 | SkString* errorMsg, int pad = 0) { |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 58 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 59 | *errorMsg = "Not supported on recording/vector backends."; |
| 60 | return skiagm::DrawResult::kSkip; |
| 61 | } |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 62 | const auto ii = canvas->imageInfo().makeWH(newW, newH); |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 63 | |
| 64 | canvas->save(); |
| 65 | for (auto linear : {SkSurface::RescaleGamma::kSrc, SkSurface::RescaleGamma::kLinear}) { |
| 66 | canvas->save(); |
| 67 | for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) { |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 68 | auto rescaled = do_read_and_scale(surface, srcRect, ii, linear, quality); |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 69 | canvas->drawImage(rescaled, 0, 0); |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 70 | canvas->translate(newW + pad, 0); |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 71 | } |
| 72 | canvas->restore(); |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 73 | canvas->translate(0, newH + pad); |
Brian Salomon | 451b01f | 2019-05-17 12:39:17 -0400 | [diff] [blame] | 74 | } |
Brian Salomon | a34e0fc | 2019-05-17 19:56:59 +0000 | [diff] [blame] | 75 | canvas->restore(); |
| 76 | return skiagm::DrawResult::kOk; |
Brian Salomon | 451b01f | 2019-05-17 12:39:17 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 79 | static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, const char* imageFile, |
| 80 | const SkIRect& srcRect, int newW, int newH, |
| 81 | SkString* errorMsg) { |
| 82 | auto image = GetResourceAsImage(imageFile); |
| 83 | if (!image) { |
| 84 | errorMsg->printf("Could not load image file %s.", imageFile); |
| 85 | return skiagm::DrawResult::kFail; |
| 86 | } |
| 87 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 88 | *errorMsg = "Not supported on recording/vector backends."; |
| 89 | return skiagm::DrawResult::kSkip; |
| 90 | } |
| 91 | // Turn the image into a surface in order to call the read and rescale API |
| 92 | auto surfInfo = image->imageInfo().makeWH(image->width(), image->height()); |
| 93 | auto surface = canvas->makeSurface(surfInfo); |
| 94 | if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) { |
| 95 | surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType); |
| 96 | surface = canvas->makeSurface(surfInfo); |
| 97 | } |
| 98 | if (!surface) { |
| 99 | *errorMsg = "Could not create surface for image."; |
Brian Salomon | 9be911c | 2019-05-20 14:01:21 -0400 | [diff] [blame^] | 100 | // When testing abandoned GrContext we expect surface creation to fail. |
| 101 | if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) { |
| 102 | return skiagm::DrawResult::kSkip; |
| 103 | } |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 104 | return skiagm::DrawResult::kFail; |
| 105 | } |
| 106 | SkPaint paint; |
| 107 | paint.setBlendMode(SkBlendMode::kSrc); |
| 108 | surface->getCanvas()->drawImage(image, 0, 0, &paint); |
| 109 | return do_rescale_grid(canvas, surface.get(), srcRect, newW, newH, errorMsg); |
| 110 | } |
| 111 | |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 112 | #define DEF_RESCALE_AND_READ_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 113 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 114 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 115 | return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, W, H, errorMsg); \ |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100), |
| 119 | 410, 410) |
| 120 | |
| 121 | DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45) |
| 122 | DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 680, 400) |
| 123 | |
| 124 | DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637), |
| 125 | (int)(0.7 * 105)) |
| 126 | DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637), |
| 127 | (int)(1.2 * 105)) |
| 128 | DEF_RESCALE_AND_READ_GM(images/text.png, text_up_large, SkIRect::MakeXYWH(300, 0, 300, 105), |
| 129 | (int)(2.4 * 300), (int)(2.4 * 105)) |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 130 | |
| 131 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) { |
| 132 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 133 | *errorMsg = "Not supported on recording/vector backends."; |
| 134 | return skiagm::DrawResult::kSkip; |
| 135 | } |
| 136 | |
| 137 | static constexpr int kBorder = 5; |
| 138 | static constexpr int kInner = 5; |
| 139 | const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner); |
| 140 | auto surfaceII = |
| 141 | SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType, |
| 142 | kPremul_SkAlphaType, SkColorSpace::MakeSRGB()); |
| 143 | auto surface = canvas->makeSurface(surfaceII); |
Brian Salomon | af9b7b9 | 2019-05-20 12:19:37 -0400 | [diff] [blame] | 144 | if (!surface) { |
| 145 | *errorMsg = "Could not create surface for image."; |
Brian Salomon | 9be911c | 2019-05-20 14:01:21 -0400 | [diff] [blame^] | 146 | // When testing abandoned GrContext we expect surface creation to fail. |
| 147 | if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) { |
| 148 | return skiagm::DrawResult::kSkip; |
| 149 | } |
Brian Salomon | af9b7b9 | 2019-05-20 12:19:37 -0400 | [diff] [blame] | 150 | return skiagm::DrawResult::kFail; |
| 151 | } |
Brian Salomon | 286b557 | 2019-05-20 10:25:50 -0400 | [diff] [blame] | 152 | surface->getCanvas()->clear(SK_ColorRED); |
| 153 | surface->getCanvas()->save(); |
| 154 | surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false); |
| 155 | surface->getCanvas()->clear(SK_ColorBLUE); |
| 156 | surface->getCanvas()->restore(); |
| 157 | static constexpr int kPad = 2; |
| 158 | canvas->translate(kPad, kPad); |
| 159 | skiagm::DrawResult result; |
| 160 | auto downW = static_cast<int>(kInner / 2); |
| 161 | auto downH = static_cast<int>(kInner / 2); |
| 162 | result = do_rescale_grid(canvas, surface.get(), srcRect, downW, downH, errorMsg, kPad); |
| 163 | if (result != skiagm::DrawResult::kOk) { |
| 164 | return result; |
| 165 | } |
| 166 | canvas->translate(0, 2 * downH); |
| 167 | auto upW = static_cast<int>(kInner * 3.5); |
| 168 | auto upH = static_cast<int>(kInner * 4.6); |
| 169 | result = do_rescale_grid(canvas, surface.get(), srcRect, upW, upH, errorMsg, kPad); |
| 170 | if (result != skiagm::DrawResult::kOk) { |
| 171 | return result; |
| 172 | } |
| 173 | return skiagm::DrawResult::kOk; |
| 174 | } |