blob: 81bc013f4298fcb77ad94fe1516504ff6270831a [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 Salomon286b5572019-05-20 10:25:50 -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 Salomon286b5572019-05-20 10:25:50 -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 Salomon286b5572019-05-20 10:25:50 -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 Salomon286b5572019-05-20 10:25:50 -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 Salomon286b5572019-05-20 10:25:50 -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 Salomona34e0fc2019-05-17 19:56:59 +000062 const auto ii = canvas->imageInfo().makeWH(newW, newH);
Brian Salomona34e0fc2019-05-17 19:56:59 +000063
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 Salomon286b5572019-05-20 10:25:50 -040068 auto rescaled = do_read_and_scale(surface, srcRect, ii, linear, quality);
Brian Salomona34e0fc2019-05-17 19:56:59 +000069 canvas->drawImage(rescaled, 0, 0);
Brian Salomon286b5572019-05-20 10:25:50 -040070 canvas->translate(newW + pad, 0);
Brian Salomona34e0fc2019-05-17 19:56:59 +000071 }
72 canvas->restore();
Brian Salomon286b5572019-05-20 10:25:50 -040073 canvas->translate(0, newH + pad);
Brian Salomon451b01f2019-05-17 12:39:17 -040074 }
Brian Salomona34e0fc2019-05-17 19:56:59 +000075 canvas->restore();
76 return skiagm::DrawResult::kOk;
Brian Salomon451b01f2019-05-17 12:39:17 -040077}
78
Brian Salomon286b5572019-05-20 10:25:50 -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 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 Salomon9be911c2019-05-20 14:01:21 -0400100 // When testing abandoned GrContext we expect surface creation to fail.
101 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
102 return skiagm::DrawResult::kSkip;
103 }
Brian Salomon286b5572019-05-20 10:25:50 -0400104 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 Salomon201700f2019-05-17 12:05:44 -0400112#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 Salomon286b5572019-05-20 10:25:50 -0400115 return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, W, H, errorMsg); \
Brian Salomon201700f2019-05-17 12:05:44 -0400116 }
117
118DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100),
119 410, 410)
120
121DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
Brian Salomonc7e9f782019-05-28 20:39:53 -0400122DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
Brian Salomon201700f2019-05-17 12:05:44 -0400123
124DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637),
125 (int)(0.7 * 105))
126DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637),
127 (int)(1.2 * 105))
128DEF_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 Salomon286b5572019-05-20 10:25:50 -0400130
131DEF_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 Salomonaf9b7b92019-05-20 12:19:37 -0400144 if (!surface) {
145 *errorMsg = "Could not create surface for image.";
Brian Salomon9be911c2019-05-20 14:01:21 -0400146 // When testing abandoned GrContext we expect surface creation to fail.
147 if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
148 return skiagm::DrawResult::kSkip;
149 }
Brian Salomonaf9b7b92019-05-20 12:19:37 -0400150 return skiagm::DrawResult::kFail;
151 }
Brian Salomon286b5572019-05-20 10:25:50 -0400152 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}