Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #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" |
Greg Daniel | 81b9897 | 2019-12-13 11:09:43 -0500 | [diff] [blame] | 29 | #include "src/gpu/GrContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "src/image/SkImage_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "src/image/SkImage_Gpu.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 32 | #include "tools/ToolUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 33 | #include "tools/gpu/ProxyUtils.h" |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 34 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <utility> |
| 37 | |
| 38 | class GrRenderTargetContext; |
| 39 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 40 | static const int kNumMatrices = 6; |
| 41 | static const int kImageSize = 128; |
| 42 | static const int kLabelSize = 32; |
| 43 | static const int kNumLabels = 4; |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 44 | static const int kInset = 16; |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 45 | |
| 46 | static const int kCellSize = kImageSize+2*kLabelSize; |
| 47 | static const int kGMWidth = kNumMatrices*kCellSize; |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 48 | static const int kGMHeight = 4*kCellSize; |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 49 | |
| 50 | static const SkPoint kPoints[kNumLabels] = { |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 51 | { 0, kImageSize }, // LL |
| 52 | { kImageSize, kImageSize }, // LR |
| 53 | { 0, 0 }, // UL |
| 54 | { kImageSize, 0 }, // UR |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static const SkMatrix kUVMatrices[kNumMatrices] = { |
| 58 | SkMatrix::MakeAll( 0, -1, 1, |
| 59 | -1, 0, 1, |
| 60 | 0, 0, 1), |
| 61 | SkMatrix::MakeAll( 1, 0, 0, |
| 62 | 0, -1, 1, |
| 63 | 0, 0, 1), |
| 64 | // flip x |
| 65 | SkMatrix::MakeAll(-1, 0, 1, |
| 66 | 0, 1, 0, |
| 67 | 0, 0, 1), |
| 68 | SkMatrix::MakeAll( 0, 1, 0, |
| 69 | -1, 0, 1, |
| 70 | 0, 0, 1), |
| 71 | // flip both x & y == rotate 180 |
| 72 | SkMatrix::MakeAll(-1, 0, 1, |
| 73 | 0, -1, 1, |
| 74 | 0, 0, 1), |
| 75 | // identity |
| 76 | SkMatrix::MakeAll(1, 0, 0, |
| 77 | 0, 1, 0, |
| 78 | 0, 0, 1) |
| 79 | }; |
| 80 | |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 81 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 82 | // Create a fixed size text label like "LL" or "LR". |
| 83 | static sk_sp<SkImage> make_text_image(GrContext* context, const char* text, SkColor color) { |
| 84 | SkPaint paint; |
| 85 | paint.setAntiAlias(true); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 86 | paint.setColor(color); |
| 87 | |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 88 | SkFont font; |
| 89 | font.setEdging(SkFont::Edging::kAntiAlias); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 90 | font.setTypeface(ToolUtils::create_portable_typeface()); |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 91 | font.setSize(32); |
| 92 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 93 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 94 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 95 | const SkMatrix mat = SkMatrix::MakeRectToRect(bounds, SkRect::MakeWH(kLabelSize, kLabelSize), |
| 96 | SkMatrix::kFill_ScaleToFit); |
| 97 | |
| 98 | const SkImageInfo ii = SkImageInfo::MakeN32Premul(kLabelSize, kLabelSize); |
| 99 | sk_sp<SkSurface> surf = SkSurface::MakeRaster(ii); |
| 100 | |
| 101 | SkCanvas* canvas = surf->getCanvas(); |
| 102 | |
| 103 | canvas->clear(SK_ColorWHITE); |
| 104 | canvas->concat(mat); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 105 | canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0, 0, font, paint); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 106 | |
| 107 | sk_sp<SkImage> image = surf->makeImageSnapshot(); |
| 108 | |
Brian Osman | d566e2e | 2019-08-14 13:19:04 -0400 | [diff] [blame] | 109 | return image->makeTextureImage(context); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 112 | // Create an image with each corner marked w/ "LL", "LR", etc., with the origin either bottom-left |
| 113 | // or top-left. |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 114 | static sk_sp<SkImage> make_reference_image(GrContext* context, |
| 115 | const SkTArray<sk_sp<SkImage>>& labels, |
| 116 | bool bottomLeftOrigin) { |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 117 | SkASSERT(kNumLabels == labels.count()); |
| 118 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 119 | SkImageInfo ii = |
| 120 | SkImageInfo::Make(kImageSize, kImageSize, kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 121 | SkBitmap bm; |
| 122 | bm.allocPixels(ii); |
| 123 | SkCanvas canvas(bm); |
| 124 | |
| 125 | canvas.clear(SK_ColorWHITE); |
| 126 | for (int i = 0; i < kNumLabels; ++i) { |
| 127 | canvas.drawImage(labels[i], |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 128 | 0.0 != kPoints[i].fX ? kPoints[i].fX-kLabelSize-kInset : kInset, |
| 129 | 0.0 != kPoints[i].fY ? kPoints[i].fY-kLabelSize-kInset : kInset); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 132 | auto origin = bottomLeftOrigin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 133 | |
Greg Daniel | 81b9897 | 2019-12-13 11:09:43 -0500 | [diff] [blame] | 134 | // TODO: make MakeTextureProxyFromData return a GrSurfaceProxyView |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 135 | auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, GrRenderable::kNo, origin, |
| 136 | bm.info(), bm.getPixels(), bm.rowBytes()); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 137 | if (!proxy) { |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
Greg Daniel | 14b5721 | 2019-12-17 16:18:06 -0500 | [diff] [blame] | 141 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), |
Mike Klein | 42032cb | 2020-01-14 22:57:02 +0000 | [diff] [blame^] | 142 | GrColorType::kRGBA_8888); |
Greg Daniel | 81b9897 | 2019-12-13 11:09:43 -0500 | [diff] [blame] | 143 | GrSurfaceProxyView view(std::move(proxy), origin, swizzle); |
Mike Klein | 42032cb | 2020-01-14 22:57:02 +0000 | [diff] [blame^] | 144 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, kOpaque_SkAlphaType, |
| 145 | std::move(view), nullptr); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Here we're converting from a matrix that is intended for UVs to a matrix that is intended |
| 149 | // for rect geometry used for a drawImage call. They are, in some sense, inverses of each |
| 150 | // other but we also need a scale to map from the [0..1] uv range to the actual size of |
| 151 | // image. |
| 152 | static bool UVMatToGeomMatForImage(SkMatrix* geomMat, const SkMatrix& uvMat) { |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 153 | |
| 154 | const SkMatrix yFlip = SkMatrix::MakeAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
| 155 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 156 | SkMatrix tmp = uvMat; |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 157 | tmp.preConcat(yFlip); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 158 | tmp.preScale(1.0f/kImageSize, 1.0f/kImageSize); |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 159 | |
| 160 | tmp.postConcat(yFlip); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 161 | tmp.postScale(kImageSize, kImageSize); |
| 162 | |
| 163 | return tmp.invert(geomMat); |
| 164 | } |
| 165 | |
| 166 | // This GM exercises drawImage with a set of matrices that use an unusual amount of flips and |
| 167 | // rotates. |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 168 | class FlippityGM : public skiagm::GpuGM { |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 169 | public: |
| 170 | FlippityGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 171 | this->setBGColor(0xFFCCCCCC); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Ben Wagner | c061d31 | 2019-08-14 15:31:52 -0400 | [diff] [blame] | 174 | private: |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 175 | SkString onShortName() override { |
| 176 | return SkString("flippity"); |
| 177 | } |
| 178 | |
| 179 | SkISize onISize() override { |
| 180 | return SkISize::Make(kGMWidth, kGMHeight); |
| 181 | } |
| 182 | |
| 183 | // Draw the reference image and the four corner labels in the matrix's coordinate space |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 184 | void drawImageWithMatrixAndLabels(SkCanvas* canvas, SkImage* image, int matIndex, |
| 185 | bool drawSubset, bool drawScaled) { |
| 186 | static const SkRect kSubsets[kNumMatrices] = { |
| 187 | SkRect::MakeXYWH(kInset, 0, kImageSize-kInset, kImageSize), |
| 188 | SkRect::MakeXYWH(0, kInset, kImageSize, kImageSize-kInset), |
| 189 | SkRect::MakeXYWH(0, 0, kImageSize-kInset, kImageSize), |
| 190 | SkRect::MakeXYWH(0, 0, kImageSize, kImageSize-kInset), |
| 191 | SkRect::MakeXYWH(kInset/2, kInset/2, kImageSize-kInset, kImageSize-kInset), |
| 192 | SkRect::MakeXYWH(kInset, kInset, kImageSize-2*kInset, kImageSize-2*kInset), |
| 193 | }; |
| 194 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 195 | SkMatrix imageGeomMat; |
| 196 | SkAssertResult(UVMatToGeomMatForImage(&imageGeomMat, kUVMatrices[matIndex])); |
| 197 | |
| 198 | canvas->save(); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 199 | |
| 200 | // draw the reference image |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 201 | canvas->concat(imageGeomMat); |
| 202 | if (drawSubset) { |
| 203 | canvas->drawImageRect(image, kSubsets[matIndex], |
| 204 | drawScaled ? SkRect::MakeWH(kImageSize, kImageSize) |
| 205 | : kSubsets[matIndex], |
| 206 | nullptr, SkCanvas::kFast_SrcRectConstraint); |
| 207 | } else { |
| 208 | canvas->drawImage(image, 0, 0); |
| 209 | } |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 210 | |
| 211 | // draw the labels |
| 212 | for (int i = 0; i < kNumLabels; ++i) { |
| 213 | canvas->drawImage(fLabels[i], |
| 214 | 0.0f == kPoints[i].fX ? -kLabelSize : kPoints[i].fX, |
| 215 | 0.0f == kPoints[i].fY ? -kLabelSize : kPoints[i].fY); |
| 216 | } |
| 217 | canvas->restore(); |
| 218 | } |
| 219 | |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 220 | void drawRow(GrContext* context, SkCanvas* canvas, |
| 221 | bool bottomLeftImage, bool drawSubset, bool drawScaled) { |
| 222 | |
| 223 | sk_sp<SkImage> referenceImage = make_reference_image(context, fLabels, bottomLeftImage); |
| 224 | |
| 225 | canvas->save(); |
| 226 | canvas->translate(kLabelSize, kLabelSize); |
| 227 | |
| 228 | for (int i = 0; i < kNumMatrices; ++i) { |
| 229 | this->drawImageWithMatrixAndLabels(canvas, referenceImage.get(), i, |
| 230 | drawSubset, drawScaled); |
| 231 | canvas->translate(kCellSize, 0); |
| 232 | } |
| 233 | canvas->restore(); |
| 234 | } |
| 235 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 236 | void makeLabels(GrContext* context) { |
Ben Wagner | c061d31 | 2019-08-14 15:31:52 -0400 | [diff] [blame] | 237 | if (fLabels.count()) { |
| 238 | return; |
| 239 | } |
| 240 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 241 | static const char* kLabelText[kNumLabels] = { "LL", "LR", "UL", "UR" }; |
| 242 | |
| 243 | static const SkColor kLabelColors[kNumLabels] = { |
| 244 | SK_ColorRED, |
| 245 | SK_ColorGREEN, |
| 246 | SK_ColorBLUE, |
| 247 | SK_ColorCYAN |
| 248 | }; |
| 249 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 250 | for (int i = 0; i < kNumLabels; ++i) { |
| 251 | fLabels.push_back(make_text_image(context, kLabelText[i], kLabelColors[i])); |
| 252 | } |
| 253 | SkASSERT(kNumLabels == fLabels.count()); |
| 254 | } |
| 255 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 256 | void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 257 | this->makeLabels(context); |
| 258 | |
Robert Phillips | f0a30d7 | 2017-11-01 14:05:12 -0400 | [diff] [blame] | 259 | canvas->save(); |
| 260 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 261 | // Top row gets TL image |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 262 | this->drawRow(context, canvas, false, false, false); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 263 | |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 264 | canvas->translate(0, kCellSize); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 265 | |
| 266 | // Bottom row gets BL image |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 267 | this->drawRow(context, canvas, true, false, false); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 268 | |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 269 | canvas->translate(0, kCellSize); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 270 | |
Robert Phillips | 206bda5 | 2017-11-01 09:26:06 -0400 | [diff] [blame] | 271 | // Third row gets subsets of BL images |
| 272 | this->drawRow(context, canvas, true, true, false); |
| 273 | |
| 274 | canvas->translate(0, kCellSize); |
| 275 | |
| 276 | // Fourth row gets scaled subsets of BL images |
| 277 | this->drawRow(context, canvas, true, true, true); |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 278 | |
Robert Phillips | f0a30d7 | 2017-11-01 14:05:12 -0400 | [diff] [blame] | 279 | canvas->restore(); |
| 280 | |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 281 | // separator grid |
Robert Phillips | f0a30d7 | 2017-11-01 14:05:12 -0400 | [diff] [blame] | 282 | for (int i = 0; i < 4; ++i) { |
| 283 | canvas->drawLine(0, i * kCellSize, kGMWidth, i * kCellSize, SkPaint()); |
| 284 | } |
Robert Phillips | a097173 | 2017-10-31 12:26:35 -0400 | [diff] [blame] | 285 | for (int i = 0; i < kNumMatrices; ++i) { |
| 286 | canvas->drawLine(i * kCellSize, 0, i * kCellSize, kGMHeight, SkPaint()); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private: |
| 291 | SkTArray<sk_sp<SkImage>> fLabels; |
| 292 | |
| 293 | typedef GM INHERITED; |
| 294 | }; |
| 295 | |
| 296 | DEF_GM(return new FlippityGM;) |