blob: c9e4f1997430321310e44e312cdffa21bccbe346 [file] [log] [blame]
Robert Phillipsa0971732017-10-31 12:26:35 -04001/*
2 * Copyright 2017 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkFontTypes.h"
14#include "include/core/SkImage.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkMatrix.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkPoint.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkTypeface.h"
25#include "include/core/SkTypes.h"
26#include "include/gpu/GrContext.h"
27#include "include/gpu/GrTypes.h"
28#include "include/private/SkTArray.h"
29#include "src/image/SkImage_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/image/SkImage_Gpu.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include "tools/ToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "tools/gpu/ProxyUtils.h"
Robert Phillipsa0971732017-10-31 12:26:35 -040033
Ben Wagner7fde8e12019-05-01 17:28:53 -040034#include <string.h>
35#include <utility>
36
37class GrRenderTargetContext;
38
Robert Phillipsa0971732017-10-31 12:26:35 -040039static const int kNumMatrices = 6;
40static const int kImageSize = 128;
41static const int kLabelSize = 32;
42static const int kNumLabels = 4;
Robert Phillips206bda52017-11-01 09:26:06 -040043static const int kInset = 16;
Robert Phillipsa0971732017-10-31 12:26:35 -040044
45static const int kCellSize = kImageSize+2*kLabelSize;
46static const int kGMWidth = kNumMatrices*kCellSize;
Robert Phillips206bda52017-11-01 09:26:06 -040047static const int kGMHeight = 4*kCellSize;
Robert Phillipsa0971732017-10-31 12:26:35 -040048
49static const SkPoint kPoints[kNumLabels] = {
Robert Phillips206bda52017-11-01 09:26:06 -040050 { 0, kImageSize }, // LL
51 { kImageSize, kImageSize }, // LR
52 { 0, 0 }, // UL
53 { kImageSize, 0 }, // UR
Robert Phillipsa0971732017-10-31 12:26:35 -040054};
55
56static const SkMatrix kUVMatrices[kNumMatrices] = {
57 SkMatrix::MakeAll( 0, -1, 1,
58 -1, 0, 1,
59 0, 0, 1),
60 SkMatrix::MakeAll( 1, 0, 0,
61 0, -1, 1,
62 0, 0, 1),
63 // flip x
64 SkMatrix::MakeAll(-1, 0, 1,
65 0, 1, 0,
66 0, 0, 1),
67 SkMatrix::MakeAll( 0, 1, 0,
68 -1, 0, 1,
69 0, 0, 1),
70 // flip both x & y == rotate 180
71 SkMatrix::MakeAll(-1, 0, 1,
72 0, -1, 1,
73 0, 0, 1),
74 // identity
75 SkMatrix::MakeAll(1, 0, 0,
76 0, 1, 0,
77 0, 0, 1)
78};
79
Robert Phillips206bda52017-11-01 09:26:06 -040080
Robert Phillipsa0971732017-10-31 12:26:35 -040081// Create a fixed size text label like "LL" or "LR".
82static sk_sp<SkImage> make_text_image(GrContext* context, const char* text, SkColor color) {
83 SkPaint paint;
84 paint.setAntiAlias(true);
Robert Phillipsa0971732017-10-31 12:26:35 -040085 paint.setColor(color);
86
Mike Reed94cca602018-12-02 16:04:27 -050087 SkFont font;
88 font.setEdging(SkFont::Edging::kAntiAlias);
Mike Kleinea3f0142019-03-20 11:12:10 -050089 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reed94cca602018-12-02 16:04:27 -050090 font.setSize(32);
91
Robert Phillipsa0971732017-10-31 12:26:35 -040092 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040093 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
Robert Phillipsa0971732017-10-31 12:26:35 -040094 const SkMatrix mat = SkMatrix::MakeRectToRect(bounds, SkRect::MakeWH(kLabelSize, kLabelSize),
95 SkMatrix::kFill_ScaleToFit);
96
97 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kLabelSize, kLabelSize);
98 sk_sp<SkSurface> surf = SkSurface::MakeRaster(ii);
99
100 SkCanvas* canvas = surf->getCanvas();
101
102 canvas->clear(SK_ColorWHITE);
103 canvas->concat(mat);
Ben Wagner51e15a62019-05-07 15:38:46 -0400104 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0, 0, font, paint);
Robert Phillipsa0971732017-10-31 12:26:35 -0400105
106 sk_sp<SkImage> image = surf->makeImageSnapshot();
107
Brian Osmand566e2e2019-08-14 13:19:04 -0400108 return image->makeTextureImage(context);
Robert Phillipsa0971732017-10-31 12:26:35 -0400109}
110
Robert Phillipsa0971732017-10-31 12:26:35 -0400111// Create an image with each corner marked w/ "LL", "LR", etc., with the origin either bottom-left
112// or top-left.
Robert Phillips206bda52017-11-01 09:26:06 -0400113static sk_sp<SkImage> make_reference_image(GrContext* context,
114 const SkTArray<sk_sp<SkImage>>& labels,
115 bool bottomLeftOrigin) {
Robert Phillipsa0971732017-10-31 12:26:35 -0400116 SkASSERT(kNumLabels == labels.count());
117
Brian Salomon58389b92018-03-07 13:01:25 -0500118 SkImageInfo ii =
119 SkImageInfo::Make(kImageSize, kImageSize, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
Robert Phillipsa0971732017-10-31 12:26:35 -0400120 SkBitmap bm;
121 bm.allocPixels(ii);
122 SkCanvas canvas(bm);
123
124 canvas.clear(SK_ColorWHITE);
125 for (int i = 0; i < kNumLabels; ++i) {
126 canvas.drawImage(labels[i],
Robert Phillips206bda52017-11-01 09:26:06 -0400127 0.0 != kPoints[i].fX ? kPoints[i].fX-kLabelSize-kInset : kInset,
128 0.0 != kPoints[i].fY ? kPoints[i].fY-kLabelSize-kInset : kInset);
Robert Phillipsa0971732017-10-31 12:26:35 -0400129 }
130
Brian Salomon2a4f9832018-03-03 22:43:43 -0500131 auto origin = bottomLeftOrigin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
Robert Phillipsa0971732017-10-31 12:26:35 -0400132
Brian Salomone7499c72019-06-24 12:12:36 -0400133 auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, GrRenderable::kNo, kImageSize,
134 kImageSize, bm.colorType(), bm.alphaType(),
135 origin, bm.getPixels(), bm.rowBytes());
Robert Phillipsa0971732017-10-31 12:26:35 -0400136 if (!proxy) {
137 return nullptr;
138 }
139
Brian Salomon8a8dd332018-05-24 14:08:31 -0400140 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, kOpaque_SkAlphaType,
Brian Salomonf05e6d32018-12-20 08:41:41 -0500141 std::move(proxy), nullptr);
Robert Phillipsa0971732017-10-31 12:26:35 -0400142}
143
144// Here we're converting from a matrix that is intended for UVs to a matrix that is intended
145// for rect geometry used for a drawImage call. They are, in some sense, inverses of each
146// other but we also need a scale to map from the [0..1] uv range to the actual size of
147// image.
148static bool UVMatToGeomMatForImage(SkMatrix* geomMat, const SkMatrix& uvMat) {
Robert Phillips206bda52017-11-01 09:26:06 -0400149
150 const SkMatrix yFlip = SkMatrix::MakeAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
151
Robert Phillipsa0971732017-10-31 12:26:35 -0400152 SkMatrix tmp = uvMat;
Robert Phillips206bda52017-11-01 09:26:06 -0400153 tmp.preConcat(yFlip);
Robert Phillipsa0971732017-10-31 12:26:35 -0400154 tmp.preScale(1.0f/kImageSize, 1.0f/kImageSize);
Robert Phillips206bda52017-11-01 09:26:06 -0400155
156 tmp.postConcat(yFlip);
Robert Phillipsa0971732017-10-31 12:26:35 -0400157 tmp.postScale(kImageSize, kImageSize);
158
159 return tmp.invert(geomMat);
160}
161
162// This GM exercises drawImage with a set of matrices that use an unusual amount of flips and
163// rotates.
Chris Dalton3a778372019-02-07 15:23:36 -0700164class FlippityGM : public skiagm::GpuGM {
Robert Phillipsa0971732017-10-31 12:26:35 -0400165public:
166 FlippityGM() {
Mike Kleind46dce32018-08-16 10:17:03 -0400167 this->setBGColor(0xFFCCCCCC);
Robert Phillipsa0971732017-10-31 12:26:35 -0400168 }
169
Ben Wagnerc061d312019-08-14 15:31:52 -0400170private:
Robert Phillipsa0971732017-10-31 12:26:35 -0400171 SkString onShortName() override {
172 return SkString("flippity");
173 }
174
175 SkISize onISize() override {
176 return SkISize::Make(kGMWidth, kGMHeight);
177 }
178
179 // Draw the reference image and the four corner labels in the matrix's coordinate space
Robert Phillips206bda52017-11-01 09:26:06 -0400180 void drawImageWithMatrixAndLabels(SkCanvas* canvas, SkImage* image, int matIndex,
181 bool drawSubset, bool drawScaled) {
182 static const SkRect kSubsets[kNumMatrices] = {
183 SkRect::MakeXYWH(kInset, 0, kImageSize-kInset, kImageSize),
184 SkRect::MakeXYWH(0, kInset, kImageSize, kImageSize-kInset),
185 SkRect::MakeXYWH(0, 0, kImageSize-kInset, kImageSize),
186 SkRect::MakeXYWH(0, 0, kImageSize, kImageSize-kInset),
187 SkRect::MakeXYWH(kInset/2, kInset/2, kImageSize-kInset, kImageSize-kInset),
188 SkRect::MakeXYWH(kInset, kInset, kImageSize-2*kInset, kImageSize-2*kInset),
189 };
190
Robert Phillipsa0971732017-10-31 12:26:35 -0400191 SkMatrix imageGeomMat;
192 SkAssertResult(UVMatToGeomMatForImage(&imageGeomMat, kUVMatrices[matIndex]));
193
194 canvas->save();
Robert Phillipsa0971732017-10-31 12:26:35 -0400195
196 // draw the reference image
Robert Phillips206bda52017-11-01 09:26:06 -0400197 canvas->concat(imageGeomMat);
198 if (drawSubset) {
199 canvas->drawImageRect(image, kSubsets[matIndex],
200 drawScaled ? SkRect::MakeWH(kImageSize, kImageSize)
201 : kSubsets[matIndex],
202 nullptr, SkCanvas::kFast_SrcRectConstraint);
203 } else {
204 canvas->drawImage(image, 0, 0);
205 }
Robert Phillipsa0971732017-10-31 12:26:35 -0400206
207 // draw the labels
208 for (int i = 0; i < kNumLabels; ++i) {
209 canvas->drawImage(fLabels[i],
210 0.0f == kPoints[i].fX ? -kLabelSize : kPoints[i].fX,
211 0.0f == kPoints[i].fY ? -kLabelSize : kPoints[i].fY);
212 }
213 canvas->restore();
214 }
215
Robert Phillips206bda52017-11-01 09:26:06 -0400216 void drawRow(GrContext* context, SkCanvas* canvas,
217 bool bottomLeftImage, bool drawSubset, bool drawScaled) {
218
219 sk_sp<SkImage> referenceImage = make_reference_image(context, fLabels, bottomLeftImage);
220
221 canvas->save();
222 canvas->translate(kLabelSize, kLabelSize);
223
224 for (int i = 0; i < kNumMatrices; ++i) {
225 this->drawImageWithMatrixAndLabels(canvas, referenceImage.get(), i,
226 drawSubset, drawScaled);
227 canvas->translate(kCellSize, 0);
228 }
229 canvas->restore();
230 }
231
Robert Phillipsa0971732017-10-31 12:26:35 -0400232 void makeLabels(GrContext* context) {
Ben Wagnerc061d312019-08-14 15:31:52 -0400233 if (fLabels.count()) {
234 return;
235 }
236
Robert Phillipsa0971732017-10-31 12:26:35 -0400237 static const char* kLabelText[kNumLabels] = { "LL", "LR", "UL", "UR" };
238
239 static const SkColor kLabelColors[kNumLabels] = {
240 SK_ColorRED,
241 SK_ColorGREEN,
242 SK_ColorBLUE,
243 SK_ColorCYAN
244 };
245
Robert Phillipsa0971732017-10-31 12:26:35 -0400246 for (int i = 0; i < kNumLabels; ++i) {
247 fLabels.push_back(make_text_image(context, kLabelText[i], kLabelColors[i]));
248 }
249 SkASSERT(kNumLabels == fLabels.count());
250 }
251
Chris Dalton3a778372019-02-07 15:23:36 -0700252 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Robert Phillipsa0971732017-10-31 12:26:35 -0400253 this->makeLabels(context);
254
Robert Phillipsf0a30d72017-11-01 14:05:12 -0400255 canvas->save();
256
Robert Phillipsa0971732017-10-31 12:26:35 -0400257 // Top row gets TL image
Robert Phillips206bda52017-11-01 09:26:06 -0400258 this->drawRow(context, canvas, false, false, false);
Robert Phillipsa0971732017-10-31 12:26:35 -0400259
Robert Phillips206bda52017-11-01 09:26:06 -0400260 canvas->translate(0, kCellSize);
Robert Phillipsa0971732017-10-31 12:26:35 -0400261
262 // Bottom row gets BL image
Robert Phillips206bda52017-11-01 09:26:06 -0400263 this->drawRow(context, canvas, true, false, false);
Robert Phillipsa0971732017-10-31 12:26:35 -0400264
Robert Phillips206bda52017-11-01 09:26:06 -0400265 canvas->translate(0, kCellSize);
Robert Phillipsa0971732017-10-31 12:26:35 -0400266
Robert Phillips206bda52017-11-01 09:26:06 -0400267 // Third row gets subsets of BL images
268 this->drawRow(context, canvas, true, true, false);
269
270 canvas->translate(0, kCellSize);
271
272 // Fourth row gets scaled subsets of BL images
273 this->drawRow(context, canvas, true, true, true);
Robert Phillipsa0971732017-10-31 12:26:35 -0400274
Robert Phillipsf0a30d72017-11-01 14:05:12 -0400275 canvas->restore();
276
Robert Phillipsa0971732017-10-31 12:26:35 -0400277 // separator grid
Robert Phillipsf0a30d72017-11-01 14:05:12 -0400278 for (int i = 0; i < 4; ++i) {
279 canvas->drawLine(0, i * kCellSize, kGMWidth, i * kCellSize, SkPaint());
280 }
Robert Phillipsa0971732017-10-31 12:26:35 -0400281 for (int i = 0; i < kNumMatrices; ++i) {
282 canvas->drawLine(i * kCellSize, 0, i * kCellSize, kGMHeight, SkPaint());
283 }
284 }
285
286private:
287 SkTArray<sk_sp<SkImage>> fLabels;
288
289 typedef GM INHERITED;
290};
291
292DEF_GM(return new FlippityGM;)