2017-10-31 16:26:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-01 18:45:24 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-07-01 16:55:01 +00:00
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/gpu/GrTypes.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/image/SkImage_Base.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/image/SkImage_Gpu.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/ProxyUtils.h"
|
2017-10-31 16:26:35 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
class GrRenderTargetContext;
|
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
static const int kNumMatrices = 6;
|
|
|
|
static const int kImageSize = 128;
|
|
|
|
static const int kLabelSize = 32;
|
|
|
|
static const int kNumLabels = 4;
|
2017-11-01 13:26:06 +00:00
|
|
|
static const int kInset = 16;
|
2017-10-31 16:26:35 +00:00
|
|
|
|
|
|
|
static const int kCellSize = kImageSize+2*kLabelSize;
|
|
|
|
static const int kGMWidth = kNumMatrices*kCellSize;
|
2017-11-01 13:26:06 +00:00
|
|
|
static const int kGMHeight = 4*kCellSize;
|
2017-10-31 16:26:35 +00:00
|
|
|
|
|
|
|
static const SkPoint kPoints[kNumLabels] = {
|
2017-11-01 13:26:06 +00:00
|
|
|
{ 0, kImageSize }, // LL
|
|
|
|
{ kImageSize, kImageSize }, // LR
|
|
|
|
{ 0, 0 }, // UL
|
|
|
|
{ kImageSize, 0 }, // UR
|
2017-10-31 16:26:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const SkMatrix kUVMatrices[kNumMatrices] = {
|
|
|
|
SkMatrix::MakeAll( 0, -1, 1,
|
|
|
|
-1, 0, 1,
|
|
|
|
0, 0, 1),
|
|
|
|
SkMatrix::MakeAll( 1, 0, 0,
|
|
|
|
0, -1, 1,
|
|
|
|
0, 0, 1),
|
|
|
|
// flip x
|
|
|
|
SkMatrix::MakeAll(-1, 0, 1,
|
|
|
|
0, 1, 0,
|
|
|
|
0, 0, 1),
|
|
|
|
SkMatrix::MakeAll( 0, 1, 0,
|
|
|
|
-1, 0, 1,
|
|
|
|
0, 0, 1),
|
|
|
|
// flip both x & y == rotate 180
|
|
|
|
SkMatrix::MakeAll(-1, 0, 1,
|
|
|
|
0, -1, 1,
|
|
|
|
0, 0, 1),
|
|
|
|
// identity
|
|
|
|
SkMatrix::MakeAll(1, 0, 0,
|
|
|
|
0, 1, 0,
|
|
|
|
0, 0, 1)
|
|
|
|
};
|
|
|
|
|
2017-11-01 13:26:06 +00:00
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
// Create a fixed size text label like "LL" or "LR".
|
2020-07-16 14:58:58 +00:00
|
|
|
static sk_sp<SkImage> make_text_image(GrDirectContext* direct, const char* text, SkColor color) {
|
2017-10-31 16:26:35 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(color);
|
|
|
|
|
2018-12-02 21:04:27 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::create_portable_typeface());
|
2018-12-02 21:04:27 +00:00
|
|
|
font.setSize(32);
|
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
SkRect bounds;
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2017-10-31 16:26:35 +00:00
|
|
|
const SkMatrix mat = SkMatrix::MakeRectToRect(bounds, SkRect::MakeWH(kLabelSize, kLabelSize),
|
|
|
|
SkMatrix::kFill_ScaleToFit);
|
|
|
|
|
|
|
|
const SkImageInfo ii = SkImageInfo::MakeN32Premul(kLabelSize, kLabelSize);
|
|
|
|
sk_sp<SkSurface> surf = SkSurface::MakeRaster(ii);
|
|
|
|
|
|
|
|
SkCanvas* canvas = surf->getCanvas();
|
|
|
|
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
canvas->concat(mat);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0, 0, font, paint);
|
2017-10-31 16:26:35 +00:00
|
|
|
|
|
|
|
sk_sp<SkImage> image = surf->makeImageSnapshot();
|
|
|
|
|
2020-07-16 14:58:58 +00:00
|
|
|
return image->makeTextureImage(direct);
|
2017-10-31 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an image with each corner marked w/ "LL", "LR", etc., with the origin either bottom-left
|
|
|
|
// or top-left.
|
2020-07-01 18:45:24 +00:00
|
|
|
static sk_sp<SkImage> make_reference_image(GrDirectContext* context,
|
2017-11-01 13:26:06 +00:00
|
|
|
const SkTArray<sk_sp<SkImage>>& labels,
|
|
|
|
bool bottomLeftOrigin) {
|
2017-10-31 16:26:35 +00:00
|
|
|
SkASSERT(kNumLabels == labels.count());
|
|
|
|
|
2018-03-07 18:01:25 +00:00
|
|
|
SkImageInfo ii =
|
|
|
|
SkImageInfo::Make(kImageSize, kImageSize, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
|
2017-10-31 16:26:35 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(ii);
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
|
|
|
|
canvas.clear(SK_ColorWHITE);
|
|
|
|
for (int i = 0; i < kNumLabels; ++i) {
|
|
|
|
canvas.drawImage(labels[i],
|
2017-11-01 13:26:06 +00:00
|
|
|
0.0 != kPoints[i].fX ? kPoints[i].fX-kLabelSize-kInset : kInset,
|
|
|
|
0.0 != kPoints[i].fY ? kPoints[i].fY-kLabelSize-kInset : kInset);
|
2017-10-31 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:43:43 +00:00
|
|
|
auto origin = bottomLeftOrigin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
|
2017-10-31 16:26:35 +00:00
|
|
|
|
2020-08-12 18:06:50 +00:00
|
|
|
auto view = sk_gpu_test::MakeTextureProxyViewFromData(context, GrRenderable::kNo, origin,
|
|
|
|
bm.info(), bm.getPixels(), bm.rowBytes());
|
|
|
|
if (!view) {
|
2017-10-31 16:26:35 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-22 17:22:36 +00:00
|
|
|
return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, std::move(view),
|
|
|
|
ii.colorType(), kOpaque_SkAlphaType, nullptr);
|
2017-10-31 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Here we're converting from a matrix that is intended for UVs to a matrix that is intended
|
|
|
|
// for rect geometry used for a drawImage call. They are, in some sense, inverses of each
|
|
|
|
// other but we also need a scale to map from the [0..1] uv range to the actual size of
|
|
|
|
// image.
|
|
|
|
static bool UVMatToGeomMatForImage(SkMatrix* geomMat, const SkMatrix& uvMat) {
|
2017-11-01 13:26:06 +00:00
|
|
|
|
|
|
|
const SkMatrix yFlip = SkMatrix::MakeAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
|
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
SkMatrix tmp = uvMat;
|
2017-11-01 13:26:06 +00:00
|
|
|
tmp.preConcat(yFlip);
|
2017-10-31 16:26:35 +00:00
|
|
|
tmp.preScale(1.0f/kImageSize, 1.0f/kImageSize);
|
2017-11-01 13:26:06 +00:00
|
|
|
|
|
|
|
tmp.postConcat(yFlip);
|
2017-10-31 16:26:35 +00:00
|
|
|
tmp.postScale(kImageSize, kImageSize);
|
|
|
|
|
|
|
|
return tmp.invert(geomMat);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This GM exercises drawImage with a set of matrices that use an unusual amount of flips and
|
|
|
|
// rotates.
|
2019-02-07 22:23:36 +00:00
|
|
|
class FlippityGM : public skiagm::GpuGM {
|
2017-10-31 16:26:35 +00:00
|
|
|
public:
|
|
|
|
FlippityGM() {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFCCCCCC);
|
2017-10-31 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 19:31:52 +00:00
|
|
|
private:
|
2017-10-31 16:26:35 +00:00
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("flippity");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kGMWidth, kGMHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the reference image and the four corner labels in the matrix's coordinate space
|
2017-11-01 13:26:06 +00:00
|
|
|
void drawImageWithMatrixAndLabels(SkCanvas* canvas, SkImage* image, int matIndex,
|
|
|
|
bool drawSubset, bool drawScaled) {
|
|
|
|
static const SkRect kSubsets[kNumMatrices] = {
|
|
|
|
SkRect::MakeXYWH(kInset, 0, kImageSize-kInset, kImageSize),
|
|
|
|
SkRect::MakeXYWH(0, kInset, kImageSize, kImageSize-kInset),
|
|
|
|
SkRect::MakeXYWH(0, 0, kImageSize-kInset, kImageSize),
|
|
|
|
SkRect::MakeXYWH(0, 0, kImageSize, kImageSize-kInset),
|
|
|
|
SkRect::MakeXYWH(kInset/2, kInset/2, kImageSize-kInset, kImageSize-kInset),
|
|
|
|
SkRect::MakeXYWH(kInset, kInset, kImageSize-2*kInset, kImageSize-2*kInset),
|
|
|
|
};
|
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
SkMatrix imageGeomMat;
|
|
|
|
SkAssertResult(UVMatToGeomMatForImage(&imageGeomMat, kUVMatrices[matIndex]));
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
// draw the reference image
|
2017-11-01 13:26:06 +00:00
|
|
|
canvas->concat(imageGeomMat);
|
|
|
|
if (drawSubset) {
|
|
|
|
canvas->drawImageRect(image, kSubsets[matIndex],
|
|
|
|
drawScaled ? SkRect::MakeWH(kImageSize, kImageSize)
|
|
|
|
: kSubsets[matIndex],
|
|
|
|
nullptr, SkCanvas::kFast_SrcRectConstraint);
|
|
|
|
} else {
|
|
|
|
canvas->drawImage(image, 0, 0);
|
|
|
|
}
|
2017-10-31 16:26:35 +00:00
|
|
|
|
|
|
|
// draw the labels
|
|
|
|
for (int i = 0; i < kNumLabels; ++i) {
|
|
|
|
canvas->drawImage(fLabels[i],
|
|
|
|
0.0f == kPoints[i].fX ? -kLabelSize : kPoints[i].fX,
|
|
|
|
0.0f == kPoints[i].fY ? -kLabelSize : kPoints[i].fY);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2020-06-19 13:50:33 +00:00
|
|
|
void drawRow(SkCanvas* canvas, bool bottomLeftImage, bool drawSubset, bool drawScaled) {
|
2017-11-01 13:26:06 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(kLabelSize, kLabelSize);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumMatrices; ++i) {
|
2020-06-19 13:50:33 +00:00
|
|
|
this->drawImageWithMatrixAndLabels(canvas, fReferenceImages[bottomLeftImage].get(),
|
|
|
|
i, drawSubset, drawScaled);
|
2017-11-01 13:26:06 +00:00
|
|
|
canvas->translate(kCellSize, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2020-07-16 14:58:58 +00:00
|
|
|
void makeLabels(GrDirectContext* direct) {
|
2019-08-14 19:31:52 +00:00
|
|
|
if (fLabels.count()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
static const char* kLabelText[kNumLabels] = { "LL", "LR", "UL", "UR" };
|
|
|
|
|
|
|
|
static const SkColor kLabelColors[kNumLabels] = {
|
|
|
|
SK_ColorRED,
|
|
|
|
SK_ColorGREEN,
|
|
|
|
SK_ColorBLUE,
|
|
|
|
SK_ColorCYAN
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumLabels; ++i) {
|
2020-07-16 14:58:58 +00:00
|
|
|
fLabels.push_back(make_text_image(direct, kLabelText[i], kLabelColors[i]));
|
2017-10-31 16:26:35 +00:00
|
|
|
}
|
|
|
|
SkASSERT(kNumLabels == fLabels.count());
|
|
|
|
}
|
|
|
|
|
2020-07-01 18:45:24 +00:00
|
|
|
DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
|
2020-06-19 13:50:33 +00:00
|
|
|
if (!context || context->abandoned()) {
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
this->makeLabels(context);
|
2020-06-19 13:50:33 +00:00
|
|
|
fReferenceImages[0] = make_reference_image(context, fLabels, false);
|
|
|
|
fReferenceImages[1] = make_reference_image(context, fLabels, true);
|
|
|
|
if (!fReferenceImages[0] || !fReferenceImages[1]) {
|
|
|
|
*errorMsg = "Failed to create reference images.";
|
|
|
|
return DrawResult::kFail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DrawResult::kOk;
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:38:53 +00:00
|
|
|
void onGpuTeardown() override {
|
|
|
|
fLabels.reset();
|
|
|
|
fReferenceImages[0] = fReferenceImages[1] = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:36:12 +00:00
|
|
|
void onDraw(GrRecordingContext*, GrRenderTargetContext*, SkCanvas* canvas) override {
|
2020-06-19 13:50:33 +00:00
|
|
|
SkASSERT(fReferenceImages[0] && fReferenceImages[1]);
|
2017-10-31 16:26:35 +00:00
|
|
|
|
2017-11-01 18:05:12 +00:00
|
|
|
canvas->save();
|
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
// Top row gets TL image
|
2020-06-19 13:50:33 +00:00
|
|
|
this->drawRow(canvas, false, false, false);
|
2017-10-31 16:26:35 +00:00
|
|
|
|
2017-11-01 13:26:06 +00:00
|
|
|
canvas->translate(0, kCellSize);
|
2017-10-31 16:26:35 +00:00
|
|
|
|
|
|
|
// Bottom row gets BL image
|
2020-06-19 13:50:33 +00:00
|
|
|
this->drawRow(canvas, true, false, false);
|
2017-10-31 16:26:35 +00:00
|
|
|
|
2017-11-01 13:26:06 +00:00
|
|
|
canvas->translate(0, kCellSize);
|
2017-10-31 16:26:35 +00:00
|
|
|
|
2017-11-01 13:26:06 +00:00
|
|
|
// Third row gets subsets of BL images
|
2020-06-19 13:50:33 +00:00
|
|
|
this->drawRow(canvas, true, true, false);
|
2017-11-01 13:26:06 +00:00
|
|
|
|
|
|
|
canvas->translate(0, kCellSize);
|
|
|
|
|
|
|
|
// Fourth row gets scaled subsets of BL images
|
2020-06-19 13:50:33 +00:00
|
|
|
this->drawRow(canvas, true, true, true);
|
2017-10-31 16:26:35 +00:00
|
|
|
|
2017-11-01 18:05:12 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
2017-10-31 16:26:35 +00:00
|
|
|
// separator grid
|
2017-11-01 18:05:12 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
canvas->drawLine(0, i * kCellSize, kGMWidth, i * kCellSize, SkPaint());
|
|
|
|
}
|
2017-10-31 16:26:35 +00:00
|
|
|
for (int i = 0; i < kNumMatrices; ++i) {
|
|
|
|
canvas->drawLine(i * kCellSize, 0, i * kCellSize, kGMHeight, SkPaint());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkTArray<sk_sp<SkImage>> fLabels;
|
2020-06-19 13:50:33 +00:00
|
|
|
sk_sp<SkImage> fReferenceImages[2];
|
2017-10-31 16:26:35 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2017-10-31 16:26:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new FlippityGM;)
|