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. |
| 22 | static sk_sp<SkImage> do_read_and_scale( |
| 23 | SkImage* image, const SkIRect& srcRect, const SkImageInfo& ii, |
| 24 | SkSurface::RescaleGamma rescaleGamma, SkFilterQuality quality, |
| 25 | std::function<sk_sp<SkSurface>(const SkImageInfo&)> makeSurface) { |
| 26 | SkBitmap bmp; |
| 27 | bmp.allocPixels(ii); |
| 28 | // Turn the image into a surface in order to call the read and rescale API |
| 29 | auto surf = makeSurface(image->imageInfo().makeWH(image->width(), image->height())); |
| 30 | if (!surf) { |
| 31 | return nullptr; |
| 32 | } |
| 33 | SkPaint paint; |
| 34 | paint.setBlendMode(SkBlendMode::kSrc); |
| 35 | surf->getCanvas()->drawImage(image, 0, 0, &paint); |
| 36 | struct Context { |
| 37 | SkPixmap fPixmap; |
| 38 | bool fCalled = false; |
| 39 | } context; |
| 40 | SkAssertResult(bmp.peekPixels(&context.fPixmap)); |
| 41 | auto callback = [](void* c, const void* data, size_t rowBytes) { |
| 42 | auto context = reinterpret_cast<Context*>(c); |
| 43 | context->fCalled = true; |
| 44 | if (!data) { |
| 45 | context->fPixmap.reset(); |
| 46 | return; |
| 47 | } |
| 48 | SkRectMemcpy(context->fPixmap.writable_addr(), context->fPixmap.rowBytes(), data, rowBytes, |
| 49 | context->fPixmap.info().minRowBytes(), context->fPixmap.height()); |
| 50 | }; |
| 51 | surf->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, callback, &context); |
| 52 | while (!context.fCalled) { |
| 53 | // Only GPU should actually be asynchronous. |
| 54 | SkASSERT(surf->getCanvas()->getGrContext()); |
| 55 | surf->getCanvas()->getGrContext()->checkAsyncWorkCompletion(); |
| 56 | } |
| 57 | return SkImage::MakeFromBitmap(bmp); |
| 58 | } |
| 59 | |
| 60 | // Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are |
| 61 | // rescale in src gamma and rescale in linear gamma. |
| 62 | static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, const char* imageFile, |
| 63 | const SkIRect& srcRect, int newW, int newH, |
| 64 | SkString* errorMsg) { |
| 65 | if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 66 | *errorMsg = "Not supported on recording/vector backends."; |
| 67 | return skiagm::DrawResult::kSkip; |
| 68 | } |
| 69 | auto image = GetResourceAsImage(imageFile); |
| 70 | if (!image) { |
| 71 | errorMsg->printf("Could not load image file %s.", imageFile); |
| 72 | return skiagm::DrawResult::kFail; |
| 73 | } |
| 74 | const auto ii = canvas->imageInfo().makeWH(newW, newH); |
| 75 | auto makeSurface = [canvas](const SkImageInfo& info) { return canvas->makeSurface(info); }; |
| 76 | |
| 77 | canvas->save(); |
| 78 | for (auto linear : {SkSurface::RescaleGamma::kSrc, SkSurface::RescaleGamma::kLinear}) { |
| 79 | canvas->save(); |
| 80 | for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) { |
| 81 | auto rescaled = |
| 82 | do_read_and_scale(image.get(), srcRect, ii, linear, quality, makeSurface); |
| 83 | canvas->drawImage(rescaled, 0, 0); |
| 84 | canvas->translate(newW, 0); |
| 85 | } |
| 86 | canvas->restore(); |
| 87 | canvas->translate(0, newH); |
| 88 | } |
| 89 | canvas->restore(); |
| 90 | return skiagm::DrawResult::kOk; |
| 91 | } |
| 92 | |
| 93 | #define DEF_RESCALE_AND_READ_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \ |
| 94 | DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \ |
| 95 | ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \ |
| 96 | return do_rescale_grid(canvas, #IMAGE_FILE, SRC_RECT, W, H, errorMsg); \ |
| 97 | } |
| 98 | |
| 99 | DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100), |
| 100 | 410, 410) |
| 101 | |
| 102 | DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45) |
| 103 | DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 680, 400) |
| 104 | |
| 105 | DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637), |
| 106 | (int)(0.7 * 105)) |
| 107 | DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637), |
| 108 | (int)(1.2 * 105)) |
| 109 | DEF_RESCALE_AND_READ_GM(images/text.png, text_up_large, SkIRect::MakeXYWH(300, 0, 300, 105), |
| 110 | (int)(2.4 * 300), (int)(2.4 * 105)) |