| 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/gm.h" |
| 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/private/base/SkTArray.h" |
| 27 |
#include "src/image/SkImage_Base.h" |
| 28 |
#include "tools/ToolUtils.h" |
| 29 |
#include "tools/fonts/FontToolUtils.h" |
| 30 |
|
| 31 |
#if defined(SK_GANESH) |
| 32 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 33 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 34 |
#include "include/gpu/ganesh/GrTypes.h" |
| 35 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 36 |
#include "src/gpu/ganesh/GrPixmap.h" |
| 37 |
#include "src/gpu/ganesh/image/SkImage_Ganesh.h" |
| 38 |
#include "tools/ganesh/ProxyUtils.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <string.h> |
| 42 |
#include <utility> |
| 43 |
|
| 44 |
using namespace skia_private; |
| 45 |
|
| 46 |
static const int kNumMatrices = 6; |
| 47 |
static const int kImageSize = 128; |
| 48 |
static const int kLabelSize = 32; |
| 49 |
static const int kNumLabels = 4; |
| 50 |
static const int kInset = 16; |
| 51 |
|
| 52 |
static const int kCellSize = kImageSize+2*kLabelSize; |
| 53 |
static const int kGMWidth = kNumMatrices*kCellSize; |
| 54 |
static const int kGMHeight = 4*kCellSize; |
| 55 |
|
| 56 |
static const SkPoint kPoints[kNumLabels] = { |
| 57 |
{ 0, kImageSize }, // LL |
| 58 |
{ kImageSize, kImageSize }, // LR |
| 59 |
{ 0, 0 }, // UL |
| 60 |
{ kImageSize, 0 }, // UR |
| 61 |
}; |
| 62 |
|
| 63 |
static const SkMatrix kUVMatrices[kNumMatrices] = { |
| 64 |
SkMatrix::MakeAll( 0, -1, 1, |
| 65 |
-1, 0, 1, |
| 66 |
0, 0, 1), |
| 67 |
SkMatrix::MakeAll( 1, 0, 0, |
| 68 |
0, -1, 1, |
| 69 |
0, 0, 1), |
| 70 |
// flip x |
| 71 |
SkMatrix::MakeAll(-1, 0, 1, |
| 72 |
0, 1, 0, |
| 73 |
0, 0, 1), |
| 74 |
SkMatrix::MakeAll( 0, 1, 0, |
| 75 |
-1, 0, 1, |
| 76 |
0, 0, 1), |
| 77 |
// flip both x & y == rotate 180 |
| 78 |
SkMatrix::MakeAll(-1, 0, 1, |
| 79 |
0, -1, 1, |
| 80 |
0, 0, 1), |
| 81 |
// identity |
| 82 |
SkMatrix::MakeAll(1, 0, 0, |
| 83 |
0, 1, 0, |
| 84 |
0, 0, 1) |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
// Create a fixed size text label like "LL" or "LR". |
| 89 |
static sk_sp<SkImage> make_text_image(const char* text, SkColor color) { |
| 90 |
SkPaint paint; |
| 91 |
paint.setAntiAlias(true); |
| 92 |
paint.setColor(color); |
| 93 |
|
| 94 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 95 |
font.setEdging(SkFont::Edging::kAntiAlias); |
| 96 |
font.setSize(32); |
| 97 |
|
| 98 |
SkRect bounds; |
| 99 |
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
| 100 |
const SkMatrix mat = SkMatrix::RectToRectOrIdentity(bounds, |
| 101 |
SkRect::MakeWH(kLabelSize, kLabelSize)); |
| 102 |
|
| 103 |
const SkImageInfo ii = SkImageInfo::MakeN32Premul(kLabelSize, kLabelSize); |
| 104 |
sk_sp<SkSurface> surf = SkSurfaces::Raster(ii); |
| 105 |
|
| 106 |
SkCanvas* canvas = surf->getCanvas(); |
| 107 |
|
| 108 |
canvas->clear(SK_ColorWHITE); |
| 109 |
canvas->concat(mat); |
| 110 |
canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0, 0, font, paint); |
| 111 |
|
| 112 |
return surf->makeImageSnapshot(); |
| 113 |
} |
| 114 |
|
| 115 |
// Create an image with each corner marked w/ "LL", "LR", etc., with the origin either bottom-left |
| 116 |
// or top-left. |
| 117 |
static sk_sp<SkImage> make_reference_image(SkCanvas* mainCanvas, |
| 118 |
const TArray<sk_sp<SkImage>>& labels, |
| 119 |
bool bottomLeftOrigin) { |
| 120 |
SkASSERT(kNumLabels == labels.size()); |
| 121 |
|
| 122 |
SkImageInfo ii = SkImageInfo::Make(kImageSize, kImageSize, |
| 123 |
kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 124 |
SkBitmap bm; |
| 125 |
bm.allocPixels(ii); |
| 126 |
|
| 127 |
{ |
| 128 |
SkCanvas canvas(bm); |
| 129 |
|
| 130 |
canvas.clear(SK_ColorWHITE); |
| 131 |
for (int i = 0; i < kNumLabels; ++i) { |
| 132 |
canvas.drawImage(labels[i], |
| 133 |
0.0 != kPoints[i].fX ? kPoints[i].fX-kLabelSize-kInset : kInset, |
| 134 |
0.0 != kPoints[i].fY ? kPoints[i].fY-kLabelSize-kInset : kInset); |
| 135 |
} |
| 136 |
|
| 137 |
bm.setImmutable(); |
| 138 |
} |
| 139 |
|
| 140 |
#if defined(SK_GANESH) |
| 141 |
if (auto dContext = GrAsDirectContext(mainCanvas->recordingContext()); |
| 142 |
dContext && !dContext->abandoned()) { |
| 143 |
auto origin = bottomLeftOrigin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 144 |
|
| 145 |
auto view = sk_gpu_test::MakeTextureProxyViewFromData(dContext, GrRenderable::kNo, origin, |
| 146 |
bm.pixmap()); |
| 147 |
if (!view) { |
| 148 |
return nullptr; |
| 149 |
} |
| 150 |
|
| 151 |
return sk_make_sp<SkImage_Ganesh>( |
| 152 |
sk_ref_sp(dContext), kNeedNewImageUniqueID, std::move(view), ii.colorInfo()); |
| 153 |
} |
| 154 |
#endif |
| 155 |
|
| 156 |
return SkImages::RasterFromBitmap(bm); |
| 157 |
} |
| 158 |
|
| 159 |
// Here we're converting from a matrix that is intended for UVs to a matrix that is intended |
| 160 |
// for rect geometry used for a drawImage call. They are, in some sense, inverses of each |
| 161 |
// other but we also need a scale to map from the [0..1] uv range to the actual size of |
| 162 |
// image. |
| 163 |
static bool UVMatToGeomMatForImage(SkMatrix* geomMat, const SkMatrix& uvMat) { |
| 164 |
|
| 165 |
const SkMatrix yFlip = SkMatrix::MakeAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
| 166 |
|
| 167 |
SkMatrix tmp = uvMat; |
| 168 |
tmp.preConcat(yFlip); |
| 169 |
tmp.preScale(1.0f/kImageSize, 1.0f/kImageSize); |
| 170 |
|
| 171 |
tmp.postConcat(yFlip); |
| 172 |
tmp.postScale(kImageSize, kImageSize); |
| 173 |
|
| 174 |
return tmp.invert(geomMat); |
| 175 |
} |
| 176 |
|
| 177 |
// This GM exercises drawImage with a set of matrices that use an unusual amount of flips and |
| 178 |
// rotates. |
| 179 |
class FlippityGM : public skiagm::GM { |
| 180 |
public: |
| 181 |
FlippityGM() { |
| 182 |
this->setBGColor(0xFFCCCCCC); |
| 183 |
} |
| 184 |
|
| 185 |
private: |
| 186 |
SkString getName() const override { return SkString("flippity"); } |
| 187 |
|
| 188 |
SkISize getISize() override { return SkISize::Make(kGMWidth, kGMHeight); } |
| 189 |
|
| 190 |
// Draw the reference image and the four corner labels in the matrix's coordinate space |
| 191 |
void drawImageWithMatrixAndLabels(SkCanvas* canvas, SkImage* image, int matIndex, |
| 192 |
bool drawSubset, bool drawScaled) { |
| 193 |
static const SkRect kSubsets[kNumMatrices] = { |
| 194 |
SkRect::MakeXYWH(kInset, 0, kImageSize-kInset, kImageSize), |
| 195 |
SkRect::MakeXYWH(0, kInset, kImageSize, kImageSize-kInset), |
| 196 |
SkRect::MakeXYWH(0, 0, kImageSize-kInset, kImageSize), |
| 197 |
SkRect::MakeXYWH(0, 0, kImageSize, kImageSize-kInset), |
| 198 |
SkRect::MakeXYWH(kInset/2, kInset/2, kImageSize-kInset, kImageSize-kInset), |
| 199 |
SkRect::MakeXYWH(kInset, kInset, kImageSize-2*kInset, kImageSize-2*kInset), |
| 200 |
}; |
| 201 |
|
| 202 |
SkMatrix imageGeomMat; |
| 203 |
SkAssertResult(UVMatToGeomMatForImage(&imageGeomMat, kUVMatrices[matIndex])); |
| 204 |
|
| 205 |
canvas->save(); |
| 206 |
|
| 207 |
// draw the reference image |
| 208 |
canvas->concat(imageGeomMat); |
| 209 |
if (drawSubset) { |
| 210 |
canvas->drawImageRect(image, kSubsets[matIndex], |
| 211 |
drawScaled ? SkRect::MakeWH(kImageSize, kImageSize) |
| 212 |
: kSubsets[matIndex], |
| 213 |
SkSamplingOptions(), nullptr, |
| 214 |
SkCanvas::kFast_SrcRectConstraint); |
| 215 |
} else { |
| 216 |
canvas->drawImage(image, 0, 0); |
| 217 |
} |
| 218 |
|
| 219 |
// draw the labels |
| 220 |
for (int i = 0; i < kNumLabels; ++i) { |
| 221 |
canvas->drawImage(fLabels[i], |
| 222 |
0.0f == kPoints[i].fX ? -kLabelSize : kPoints[i].fX, |
| 223 |
0.0f == kPoints[i].fY ? -kLabelSize : kPoints[i].fY); |
| 224 |
} |
| 225 |
canvas->restore(); |
| 226 |
} |
| 227 |
|
| 228 |
void drawRow(SkCanvas* canvas, bool bottomLeftImage, bool drawSubset, bool drawScaled) { |
| 229 |
|
| 230 |
canvas->save(); |
| 231 |
canvas->translate(kLabelSize, kLabelSize); |
| 232 |
|
| 233 |
for (int i = 0; i < kNumMatrices; ++i) { |
| 234 |
this->drawImageWithMatrixAndLabels(canvas, fReferenceImages[bottomLeftImage].get(), |
| 235 |
i, drawSubset, drawScaled); |
| 236 |
canvas->translate(kCellSize, 0); |
| 237 |
} |
| 238 |
canvas->restore(); |
| 239 |
} |
| 240 |
|
| 241 |
void makeLabels() { |
| 242 |
if (fLabels.size()) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
static const char* kLabelText[kNumLabels] = { "LL", "LR", "UL", "UR" }; |
| 247 |
|
| 248 |
static const SkColor kLabelColors[kNumLabels] = { |
| 249 |
SK_ColorRED, |
| 250 |
SK_ColorGREEN, |
| 251 |
SK_ColorBLUE, |
| 252 |
SK_ColorCYAN |
| 253 |
}; |
| 254 |
|
| 255 |
for (int i = 0; i < kNumLabels; ++i) { |
| 256 |
fLabels.push_back(make_text_image(kLabelText[i], kLabelColors[i])); |
| 257 |
} |
| 258 |
SkASSERT(kNumLabels == fLabels.size()); |
| 259 |
} |
| 260 |
|
| 261 |
DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override { |
| 262 |
this->makeLabels(); |
| 263 |
fReferenceImages[0] = make_reference_image(canvas, fLabels, false); |
| 264 |
fReferenceImages[1] = make_reference_image(canvas, fLabels, true); |
| 265 |
if (!fReferenceImages[0] || !fReferenceImages[1]) { |
| 266 |
*errorMsg = "Failed to create reference images."; |
| 267 |
return DrawResult::kFail; |
| 268 |
} |
| 269 |
|
| 270 |
return DrawResult::kOk; |
| 271 |
} |
| 272 |
|
| 273 |
void onGpuTeardown() override { |
| 274 |
fLabels.clear(); |
| 275 |
fReferenceImages[0] = fReferenceImages[1] = nullptr; |
| 276 |
} |
| 277 |
|
| 278 |
void onDraw(SkCanvas* canvas) override { |
| 279 |
SkASSERT(fReferenceImages[0] && fReferenceImages[1]); |
| 280 |
|
| 281 |
canvas->save(); |
| 282 |
|
| 283 |
// Top row gets TL image |
| 284 |
this->drawRow(canvas, false, false, false); |
| 285 |
|
| 286 |
canvas->translate(0, kCellSize); |
| 287 |
|
| 288 |
// Bottom row gets BL image |
| 289 |
this->drawRow(canvas, true, false, false); |
| 290 |
|
| 291 |
canvas->translate(0, kCellSize); |
| 292 |
|
| 293 |
// Third row gets subsets of BL images |
| 294 |
this->drawRow(canvas, true, true, false); |
| 295 |
|
| 296 |
canvas->translate(0, kCellSize); |
| 297 |
|
| 298 |
// Fourth row gets scaled subsets of BL images |
| 299 |
this->drawRow(canvas, true, true, true); |
| 300 |
|
| 301 |
canvas->restore(); |
| 302 |
|
| 303 |
// separator grid |
| 304 |
for (int i = 0; i < 4; ++i) { |
| 305 |
canvas->drawLine(0, i * kCellSize, kGMWidth, i * kCellSize, SkPaint()); |
| 306 |
} |
| 307 |
for (int i = 0; i < kNumMatrices; ++i) { |
| 308 |
canvas->drawLine(i * kCellSize, 0, i * kCellSize, kGMHeight, SkPaint()); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
private: |
| 313 |
TArray<sk_sp<SkImage>> fLabels; |
| 314 |
sk_sp<SkImage> fReferenceImages[2]; |
| 315 |
|
| 316 |
using INHERITED = GM; |
| 317 |
}; |
| 318 |
|
| 319 |
DEF_GM(return new FlippityGM;) |