blob: e4cc4c1f84665feb5eb28527856ce80830effe2b [file] [log] [blame]
Brian Salomon201700f2019-05-17 12:05:44 -04001/*
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 Salomon451b01f2019-05-17 12:39:17 -040022static sk_sp<SkImage> do_read_and_scale(SkSurface* surface, const SkIRect& srcRect,
23 const SkImageInfo& ii, SkSurface::RescaleGamma rescaleGamma,
24 SkFilterQuality quality) {
Brian Salomon201700f2019-05-17 12:05:44 -040025 SkBitmap bmp;
26 bmp.allocPixels(ii);
Brian Salomon201700f2019-05-17 12:05:44 -040027 SkPaint paint;
28 paint.setBlendMode(SkBlendMode::kSrc);
Brian Salomon201700f2019-05-17 12:05:44 -040029 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 Salomon451b01f2019-05-17 12:39:17 -040044 surface->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, callback, &context);
Brian Salomon201700f2019-05-17 12:05:44 -040045 while (!context.fCalled) {
46 // Only GPU should actually be asynchronous.
Brian Salomon451b01f2019-05-17 12:39:17 -040047 SkASSERT(surface->getCanvas()->getGrContext());
48 surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
Brian Salomon201700f2019-05-17 12:05:44 -040049 }
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 Salomon451b01f2019-05-17 12:39:17 -040055static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, SkSurface* surface,
Brian Salomon201700f2019-05-17 12:05:44 -040056 const SkIRect& srcRect, int newW, int newH,
Brian Salomon451b01f2019-05-17 12:39:17 -040057 SkString* errorMsg, int pad = 0) {
Brian Salomon201700f2019-05-17 12:05:44 -040058 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
59 *errorMsg = "Not supported on recording/vector backends.";
60 return skiagm::DrawResult::kSkip;
61 }
Brian Salomon201700f2019-05-17 12:05:44 -040062 const auto ii = canvas->imageInfo().makeWH(newW, newH);
Brian Salomon201700f2019-05-17 12:05:44 -040063
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 Salomon451b01f2019-05-17 12:39:17 -040068 auto rescaled = do_read_and_scale(surface, srcRect, ii, linear, quality);
Brian Salomon201700f2019-05-17 12:05:44 -040069 canvas->drawImage(rescaled, 0, 0);
Brian Salomon451b01f2019-05-17 12:39:17 -040070 canvas->translate(newW + pad, 0);
Brian Salomon201700f2019-05-17 12:05:44 -040071 }
72 canvas->restore();
Brian Salomon451b01f2019-05-17 12:39:17 -040073 canvas->translate(0, newH + pad);
Brian Salomon201700f2019-05-17 12:05:44 -040074 }
75 canvas->restore();
76 return skiagm::DrawResult::kOk;
77}
78
Brian Salomon451b01f2019-05-17 12:39:17 -040079static 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 surface = canvas->makeSurface(image->imageInfo().makeWH(image->width(), image->height()));
93 if (!surface) {
94 *errorMsg = "Could not create surface for image.";
95 return skiagm::DrawResult::kFail;
96 }
97 return do_rescale_grid(canvas, surface.get(), srcRect, newW, newH, errorMsg);
98}
99
Brian Salomon201700f2019-05-17 12:05:44 -0400100#define DEF_RESCALE_AND_READ_GM(IMAGE_FILE, TAG, SRC_RECT, W, H) \
101 DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
102 ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); \
Brian Salomon451b01f2019-05-17 12:39:17 -0400103 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, W, H, errorMsg); \
Brian Salomon201700f2019-05-17 12:05:44 -0400104 }
105
106DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100),
107 410, 410)
108
109DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
110DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 680, 400)
111
112DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637),
113 (int)(0.7 * 105))
114DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637),
115 (int)(1.2 * 105))
116DEF_RESCALE_AND_READ_GM(images/text.png, text_up_large, SkIRect::MakeXYWH(300, 0, 300, 105),
117 (int)(2.4 * 300), (int)(2.4 * 105))
Brian Salomon451b01f2019-05-17 12:39:17 -0400118
119DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_no_bleed, canvas, errorMsg, 60, 60) {
120 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
121 *errorMsg = "Not supported on recording/vector backends.";
122 return skiagm::DrawResult::kSkip;
123 }
124
125 static constexpr int kBorder = 5;
126 static constexpr int kInner = 5;
127 const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner);
128 auto surfaceII =
129 SkImageInfo::Make(kInner + 2 * kBorder, kInner + 2 * kBorder, kRGBA_8888_SkColorType,
130 kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
131 auto surface = canvas->makeSurface(surfaceII);
132 surface->getCanvas()->clear(SK_ColorRED);
133 surface->getCanvas()->save();
134 surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false);
135 surface->getCanvas()->clear(SK_ColorBLUE);
136 surface->getCanvas()->restore();
137 static constexpr int kPad = 2;
138 canvas->translate(kPad, kPad);
139 skiagm::DrawResult result;
140 auto downW = static_cast<int>(kInner / 2);
141 auto downH = static_cast<int>(kInner / 2);
142 result = do_rescale_grid(canvas, surface.get(), srcRect, downW, downH, errorMsg, kPad);
143 if (result != skiagm::DrawResult::kOk) {
144 return result;
145 }
146 canvas->translate(0, 2 * downH);
147 auto upW = static_cast<int>(kInner * 3.5);
148 auto upH = static_cast<int>(kInner * 4.6);
149 result = do_rescale_grid(canvas, surface.get(), srcRect, upW, upH, errorMsg, kPad);
150 if (result != skiagm::DrawResult::kOk) {
151 return result;
152 }
153 return skiagm::DrawResult::kOk;
154}