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