2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #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"
23 #include "include/core/SkSurface.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/core/SkTypes.h"
26 #include "include/gpu/GrDirectContext.h"
27 #include "include/gpu/GrRecordingContext.h"
28 #include "include/gpu/GrTypes.h"
29 #include "include/private/SkTArray.h"
30 #include "src/gpu/ganesh/GrDirectContextPriv.h"
31 #include "src/gpu/ganesh/GrPixmap.h"
32 #include "src/image/SkImage_Base.h"
33 #include "src/image/SkImage_Gpu.h"
34 #include "tools/ToolUtils.h"
35 #include "tools/gpu/ProxyUtils.h"
40 static const int kNumMatrices = 6;
41 static const int kImageSize = 128;
42 static const int kLabelSize = 32;
43 static const int kNumLabels = 4;
44 static const int kInset = 16;
46 static const int kCellSize = kImageSize+2*kLabelSize;
47 static const int kGMWidth = kNumMatrices*kCellSize;
48 static const int kGMHeight = 4*kCellSize;
50 static const SkPoint kPoints[kNumLabels] = {
51 { 0, kImageSize }, // LL
52 { kImageSize, kImageSize }, // LR
54 { kImageSize, 0 }, // UR
57 static const SkMatrix kUVMatrices[kNumMatrices] = {
58 SkMatrix::MakeAll( 0, -1, 1,
61 SkMatrix::MakeAll( 1, 0, 0,
65 SkMatrix::MakeAll(-1, 0, 1,
68 SkMatrix::MakeAll( 0, 1, 0,
71 // flip both x & y == rotate 180
72 SkMatrix::MakeAll(-1, 0, 1,
76 SkMatrix::MakeAll(1, 0, 0,
82 // Create a fixed size text label like "LL" or "LR".
83 static sk_sp<SkImage> make_text_image(GrDirectContext* dContext, const char* text, SkColor color) {
85 paint.setAntiAlias(true);
86 paint.setColor(color);
89 font.setEdging(SkFont::Edging::kAntiAlias);
90 font.setTypeface(ToolUtils::create_portable_typeface());
94 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
95 const SkMatrix mat = SkMatrix::RectToRect(bounds, SkRect::MakeWH(kLabelSize, kLabelSize));
97 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kLabelSize, kLabelSize);
98 sk_sp<SkSurface> surf = SkSurface::MakeRaster(ii);
100 SkCanvas* canvas = surf->getCanvas();
102 canvas->clear(SK_ColorWHITE);
104 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0, 0, font, paint);
106 sk_sp<SkImage> image = surf->makeImageSnapshot();
108 return image->makeTextureImage(dContext);
111 // Create an image with each corner marked w/ "LL", "LR", etc., with the origin either bottom-left
113 static sk_sp<SkImage> make_reference_image(GrDirectContext* dContext,
114 const SkTArray<sk_sp<SkImage>>& labels,
115 bool bottomLeftOrigin) {
116 SkASSERT(kNumLabels == labels.count());
119 SkImageInfo::Make(kImageSize, kImageSize, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
124 canvas.clear(SK_ColorWHITE);
125 for (int i = 0; i < kNumLabels; ++i) {
126 canvas.drawImage(labels[i],
127 0.0 != kPoints[i].fX ? kPoints[i].fX-kLabelSize-kInset : kInset,
128 0.0 != kPoints[i].fY ? kPoints[i].fY-kLabelSize-kInset : kInset);
131 auto origin = bottomLeftOrigin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
133 auto view = sk_gpu_test::MakeTextureProxyViewFromData(dContext, GrRenderable::kNo, origin,
139 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(dContext),
140 kNeedNewImageUniqueID,
145 // Here we're converting from a matrix that is intended for UVs to a matrix that is intended
146 // for rect geometry used for a drawImage call. They are, in some sense, inverses of each
147 // other but we also need a scale to map from the [0..1] uv range to the actual size of
149 static bool UVMatToGeomMatForImage(SkMatrix* geomMat, const SkMatrix& uvMat) {
151 const SkMatrix yFlip = SkMatrix::MakeAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
153 SkMatrix tmp = uvMat;
154 tmp.preConcat(yFlip);
155 tmp.preScale(1.0f/kImageSize, 1.0f/kImageSize);
157 tmp.postConcat(yFlip);
158 tmp.postScale(kImageSize, kImageSize);
160 return tmp.invert(geomMat);
163 // This GM exercises drawImage with a set of matrices that use an unusual amount of flips and
165 class FlippityGM : public skiagm::GM {
168 this->setBGColor(0xFFCCCCCC);
172 SkString onShortName() override {
173 return SkString("flippity");
176 SkISize onISize() override {
177 return SkISize::Make(kGMWidth, kGMHeight);
180 // Draw the reference image and the four corner labels in the matrix's coordinate space
181 void drawImageWithMatrixAndLabels(SkCanvas* canvas, SkImage* image, int matIndex,
182 bool drawSubset, bool drawScaled) {
183 static const SkRect kSubsets[kNumMatrices] = {
184 SkRect::MakeXYWH(kInset, 0, kImageSize-kInset, kImageSize),
185 SkRect::MakeXYWH(0, kInset, kImageSize, kImageSize-kInset),
186 SkRect::MakeXYWH(0, 0, kImageSize-kInset, kImageSize),
187 SkRect::MakeXYWH(0, 0, kImageSize, kImageSize-kInset),
188 SkRect::MakeXYWH(kInset/2, kInset/2, kImageSize-kInset, kImageSize-kInset),
189 SkRect::MakeXYWH(kInset, kInset, kImageSize-2*kInset, kImageSize-2*kInset),
192 SkMatrix imageGeomMat;
193 SkAssertResult(UVMatToGeomMatForImage(&imageGeomMat, kUVMatrices[matIndex]));
197 // draw the reference image
198 canvas->concat(imageGeomMat);
200 canvas->drawImageRect(image, kSubsets[matIndex],
201 drawScaled ? SkRect::MakeWH(kImageSize, kImageSize)
202 : kSubsets[matIndex],
203 SkSamplingOptions(), nullptr,
204 SkCanvas::kFast_SrcRectConstraint);
206 canvas->drawImage(image, 0, 0);
210 for (int i = 0; i < kNumLabels; ++i) {
211 canvas->drawImage(fLabels[i],
212 0.0f == kPoints[i].fX ? -kLabelSize : kPoints[i].fX,
213 0.0f == kPoints[i].fY ? -kLabelSize : kPoints[i].fY);
218 void drawRow(SkCanvas* canvas, bool bottomLeftImage, bool drawSubset, bool drawScaled) {
221 canvas->translate(kLabelSize, kLabelSize);
223 for (int i = 0; i < kNumMatrices; ++i) {
224 this->drawImageWithMatrixAndLabels(canvas, fReferenceImages[bottomLeftImage].get(),
225 i, drawSubset, drawScaled);
226 canvas->translate(kCellSize, 0);
231 void makeLabels(GrDirectContext* dContext) {
232 if (fLabels.count()) {
236 static const char* kLabelText[kNumLabels] = { "LL", "LR", "UL", "UR" };
238 static const SkColor kLabelColors[kNumLabels] = {
245 for (int i = 0; i < kNumLabels; ++i) {
246 fLabels.push_back(make_text_image(dContext, kLabelText[i], kLabelColors[i]));
248 SkASSERT(kNumLabels == fLabels.count());
251 DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
252 if (!dContext || dContext->abandoned()) {
253 *errorMsg = "DirectContext required to create reference images";
254 return DrawResult::kSkip;
257 this->makeLabels(dContext);
258 fReferenceImages[0] = make_reference_image(dContext, fLabels, false);
259 fReferenceImages[1] = make_reference_image(dContext, fLabels, true);
260 if (!fReferenceImages[0] || !fReferenceImages[1]) {
261 *errorMsg = "Failed to create reference images.";
262 return DrawResult::kFail;
265 return DrawResult::kOk;
268 void onGpuTeardown() override {
270 fReferenceImages[0] = fReferenceImages[1] = nullptr;
273 void onDraw(SkCanvas* canvas) override {
274 SkASSERT(fReferenceImages[0] && fReferenceImages[1]);
278 // Top row gets TL image
279 this->drawRow(canvas, false, false, false);
281 canvas->translate(0, kCellSize);
283 // Bottom row gets BL image
284 this->drawRow(canvas, true, false, false);
286 canvas->translate(0, kCellSize);
288 // Third row gets subsets of BL images
289 this->drawRow(canvas, true, true, false);
291 canvas->translate(0, kCellSize);
293 // Fourth row gets scaled subsets of BL images
294 this->drawRow(canvas, true, true, true);
299 for (int i = 0; i < 4; ++i) {
300 canvas->drawLine(0, i * kCellSize, kGMWidth, i * kCellSize, SkPaint());
302 for (int i = 0; i < kNumMatrices; ++i) {
303 canvas->drawLine(i * kCellSize, 0, i * kCellSize, kGMHeight, SkPaint());
308 SkTArray<sk_sp<SkImage>> fLabels;
309 sk_sp<SkImage> fReferenceImages[2];
311 using INHERITED = GM;
314 DEF_GM(return new FlippityGM;)