2018-11-28 20:34:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 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/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2020-06-29 19:36:12 +00:00
|
|
|
#include "include/private/GrRecordingContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/private/GrTypesPriv.h"
|
2020-04-15 20:43:00 +00:00
|
|
|
#include "src/core/SkMatrixProvider.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrRenderTargetContext.h"
|
|
|
|
#include "src/gpu/SkGr.h"
|
2020-05-05 13:18:50 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2018-11-28 20:34:34 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2018-11-28 20:34:34 +00:00
|
|
|
static constexpr SkScalar kTileWidth = 40;
|
|
|
|
static constexpr SkScalar kTileHeight = 30;
|
|
|
|
|
|
|
|
static constexpr int kRowCount = 4;
|
|
|
|
static constexpr int kColCount = 3;
|
|
|
|
|
|
|
|
static void draw_text(SkCanvas* canvas, const char* text) {
|
2020-05-05 13:18:50 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 12);
|
|
|
|
canvas->drawString(text, 0, 0, font, SkPaint());
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
static void draw_gradient_tiles(SkCanvas* canvas, bool alignGradients) {
|
|
|
|
// Always draw the same gradient
|
|
|
|
static constexpr SkPoint pts[] = { {0.f, 0.f}, {0.25f * kTileWidth, 0.25f * kTileHeight} };
|
|
|
|
static constexpr SkColor colors[] = { SK_ColorBLUE, SK_ColorWHITE };
|
2018-11-28 20:34:34 +00:00
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
|
2018-11-28 20:34:34 +00:00
|
|
|
|
2020-06-29 19:36:12 +00:00
|
|
|
auto context = canvas->recordingContext();
|
2019-02-15 20:19:15 +00:00
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
auto gradient = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror);
|
2019-01-23 16:14:29 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(gradient);
|
2018-11-28 20:34:34 +00:00
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
for (int i = 0; i < kRowCount; ++i) {
|
|
|
|
for (int j = 0; j < kColCount; ++j) {
|
|
|
|
SkRect tile = SkRect::MakeWH(kTileWidth, kTileHeight);
|
|
|
|
if (alignGradients) {
|
|
|
|
tile.offset(j * kTileWidth, i * kTileHeight);
|
2018-11-28 20:34:34 +00:00
|
|
|
} else {
|
2019-01-23 16:14:29 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(j * kTileWidth, i * kTileHeight);
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
unsigned aa = SkCanvas::kNone_QuadAAFlags;
|
2018-11-28 20:34:34 +00:00
|
|
|
if (i == 0) {
|
2019-01-23 16:14:29 +00:00
|
|
|
aa |= SkCanvas::kTop_QuadAAFlag;
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
if (i == kRowCount - 1) {
|
2019-01-23 16:14:29 +00:00
|
|
|
aa |= SkCanvas::kBottom_QuadAAFlag;
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
if (j == 0) {
|
2019-01-23 16:14:29 +00:00
|
|
|
aa |= SkCanvas::kLeft_QuadAAFlag;
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
if (j == kColCount - 1) {
|
2019-01-23 16:14:29 +00:00
|
|
|
aa |= SkCanvas::kRight_QuadAAFlag;
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
if (rtc) {
|
|
|
|
// Use non-public API to leverage general GrPaint capabilities
|
|
|
|
SkMatrix view = canvas->getTotalMatrix();
|
2020-04-15 20:43:00 +00:00
|
|
|
SkSimpleMatrixProvider matrixProvider(view);
|
2019-01-23 16:14:29 +00:00
|
|
|
GrPaint grPaint;
|
2020-04-15 20:43:00 +00:00
|
|
|
SkPaintToGrPaint(context, rtc->colorInfo(), paint, matrixProvider, &grPaint);
|
2020-05-29 13:54:07 +00:00
|
|
|
rtc->fillRectWithEdgeAA(nullptr, std::move(grPaint), GrAA::kYes,
|
2019-01-23 16:14:29 +00:00
|
|
|
static_cast<GrQuadAAFlags>(aa), view, tile);
|
|
|
|
} else {
|
|
|
|
// Fallback to solid color on raster backend since the public API only has color
|
|
|
|
SkColor color = alignGradients ? SK_ColorBLUE
|
|
|
|
: (i * kColCount + j) % 2 == 0 ? SK_ColorBLUE
|
|
|
|
: SK_ColorWHITE;
|
2019-03-22 20:37:53 +00:00
|
|
|
canvas->experimental_DrawEdgeAAQuad(
|
|
|
|
tile, nullptr, static_cast<SkCanvas::QuadAAFlags>(aa), color,
|
|
|
|
SkBlendMode::kSrcOver);
|
2019-01-23 16:14:29 +00:00
|
|
|
}
|
2018-11-28 20:34:34 +00:00
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
if (!alignGradients) {
|
|
|
|
// Pop off the matrix translation when drawing unaligned
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
static void draw_color_tiles(SkCanvas* canvas, bool multicolor) {
|
2018-11-28 20:34:34 +00:00
|
|
|
for (int i = 0; i < kRowCount; ++i) {
|
|
|
|
for (int j = 0; j < kColCount; ++j) {
|
|
|
|
SkRect tile = SkRect::MakeXYWH(j * kTileWidth, i * kTileHeight, kTileWidth, kTileHeight);
|
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
SkColor4f color;
|
2018-11-28 20:34:34 +00:00
|
|
|
if (multicolor) {
|
2019-01-23 16:14:29 +00:00
|
|
|
color = {(i + 1.f) / kRowCount, (j + 1.f) / kColCount, .4f, 1.f};
|
2018-11-28 20:34:34 +00:00
|
|
|
} else {
|
2019-01-23 16:14:29 +00:00
|
|
|
color = {.2f, .8f, .3f, 1.f};
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:14:29 +00:00
|
|
|
unsigned aa = SkCanvas::kNone_QuadAAFlags;
|
2018-11-28 20:34:34 +00:00
|
|
|
if (i == 0) {
|
2019-01-23 16:14:29 +00:00
|
|
|
aa |= SkCanvas::kTop_QuadAAFlag;
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
if (i == kRowCount - 1) {
|
2019-01-23 16:14:29 +00:00
|
|
|
aa |= SkCanvas::kBottom_QuadAAFlag;
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
if (j == 0) {
|
2019-01-23 16:14:29 +00:00
|
|
|
aa |= SkCanvas::kLeft_QuadAAFlag;
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
if (j == kColCount - 1) {
|
2019-01-23 16:14:29 +00:00
|
|
|
aa |= SkCanvas::kRight_QuadAAFlag;
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
2019-01-23 16:14:29 +00:00
|
|
|
|
2019-03-22 20:37:53 +00:00
|
|
|
canvas->experimental_DrawEdgeAAQuad(
|
|
|
|
tile, nullptr, static_cast<SkCanvas::QuadAAFlags>(aa), color.toSkColor(),
|
2019-01-23 16:14:29 +00:00
|
|
|
SkBlendMode::kSrcOver);
|
2018-11-28 20:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_tile_boundaries(SkCanvas* canvas, const SkMatrix& local) {
|
|
|
|
// Draw grid of red lines at interior tile boundaries.
|
|
|
|
static constexpr SkScalar kLineOutset = 10.f;
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(0.f);
|
|
|
|
for (int x = 1; x < kColCount; ++x) {
|
|
|
|
SkPoint pts[] = {{x * kTileWidth, 0}, {x * kTileWidth, kRowCount * kTileHeight}};
|
|
|
|
local.mapPoints(pts, 2);
|
|
|
|
SkVector v = pts[1] - pts[0];
|
|
|
|
v.setLength(v.length() + kLineOutset);
|
|
|
|
canvas->drawLine(pts[1] - v, pts[0] + v, paint);
|
|
|
|
}
|
|
|
|
for (int y = 1; y < kRowCount; ++y) {
|
|
|
|
SkPoint pts[] = {{0, y * kTileHeight}, {kTileWidth * kColCount, y * kTileHeight}};
|
|
|
|
local.mapPoints(pts, 2);
|
|
|
|
SkVector v = pts[1] - pts[0];
|
|
|
|
v.setLength(v.length() + kLineOutset);
|
|
|
|
canvas->drawLine(pts[1] - v, pts[0] + v, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tile renderers (column variation)
|
2019-01-23 16:14:29 +00:00
|
|
|
typedef void (*TileRenderer)(SkCanvas*);
|
2018-11-28 20:34:34 +00:00
|
|
|
static TileRenderer kTileSets[] = {
|
2019-01-23 16:14:29 +00:00
|
|
|
[](SkCanvas* canvas) { draw_gradient_tiles(canvas, /* aligned */ false); },
|
|
|
|
[](SkCanvas* canvas) { draw_gradient_tiles(canvas, /* aligned */ true); },
|
|
|
|
[](SkCanvas* canvas) { draw_color_tiles(canvas, /* multicolor */ false); },
|
|
|
|
[](SkCanvas* canvas) { draw_color_tiles(canvas, /* multicolor */true); },
|
2018-11-28 20:34:34 +00:00
|
|
|
};
|
|
|
|
static const char* kTileSetNames[] = { "Local", "Aligned", "Green", "Multicolor" };
|
|
|
|
static_assert(SK_ARRAY_COUNT(kTileSets) == SK_ARRAY_COUNT(kTileSetNames), "Count mismatch");
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class DrawQuadSetGM : public GM {
|
|
|
|
private:
|
2019-07-18 16:36:54 +00:00
|
|
|
SkString onShortName() override { return SkString("draw_quad_set"); }
|
2018-11-28 20:34:34 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(800, 800); }
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkMatrix rowMatrices[5];
|
|
|
|
// Identity
|
|
|
|
rowMatrices[0].setIdentity();
|
|
|
|
// Translate/scale
|
|
|
|
rowMatrices[1].setTranslate(5.5f, 20.25f);
|
|
|
|
rowMatrices[1].postScale(.9f, .7f);
|
|
|
|
// Rotation
|
|
|
|
rowMatrices[2].setRotate(20.0f);
|
|
|
|
rowMatrices[2].preTranslate(15.f, -20.f);
|
|
|
|
// Skew
|
|
|
|
rowMatrices[3].setSkew(.5f, .25f);
|
|
|
|
rowMatrices[3].preTranslate(-30.f, 0.f);
|
|
|
|
// Perspective
|
|
|
|
SkPoint src[4];
|
|
|
|
SkRect::MakeWH(kColCount * kTileWidth, kRowCount * kTileHeight).toQuad(src);
|
|
|
|
SkPoint dst[4] = {{0, 0},
|
|
|
|
{kColCount * kTileWidth + 10.f, 15.f},
|
|
|
|
{kColCount * kTileWidth - 28.f, kRowCount * kTileHeight + 40.f},
|
|
|
|
{25.f, kRowCount * kTileHeight - 15.f}};
|
|
|
|
SkAssertResult(rowMatrices[4].setPolyToPoly(src, dst, 4));
|
|
|
|
rowMatrices[4].preTranslate(0.f, +10.f);
|
|
|
|
static const char* matrixNames[] = { "Identity", "T+S", "Rotate", "Skew", "Perspective" };
|
|
|
|
static_assert(SK_ARRAY_COUNT(matrixNames) == SK_ARRAY_COUNT(rowMatrices), "Count mismatch");
|
|
|
|
|
|
|
|
// Print a column header
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(110.f, 20.f);
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(kTileSetNames); ++j) {
|
|
|
|
draw_text(canvas, kTileSetNames[j]);
|
|
|
|
canvas->translate(kColCount * kTileWidth + 30.f, 0.f);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0.f, 40.f);
|
|
|
|
|
|
|
|
// Render all tile variations
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(rowMatrices); ++i) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(10.f, 0.5f * kRowCount * kTileHeight);
|
|
|
|
draw_text(canvas, matrixNames[i]);
|
|
|
|
|
|
|
|
canvas->translate(100.f, -0.5f * kRowCount * kTileHeight);
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(kTileSets); ++j) {
|
|
|
|
canvas->save();
|
|
|
|
draw_tile_boundaries(canvas, rowMatrices[i]);
|
|
|
|
|
|
|
|
canvas->concat(rowMatrices[i]);
|
2019-01-23 16:14:29 +00:00
|
|
|
kTileSets[j](canvas);
|
2018-11-28 20:34:34 +00:00
|
|
|
// Undo the local transformation
|
|
|
|
canvas->restore();
|
|
|
|
// And advance to the next column
|
|
|
|
canvas->translate(kColCount * kTileWidth + 30.f, 0.f);
|
|
|
|
}
|
|
|
|
// Reset back to the left edge
|
|
|
|
canvas->restore();
|
|
|
|
// And advance to the next row
|
|
|
|
canvas->translate(0.f, kRowCount * kTileHeight + 20.f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new DrawQuadSetGM();)
|
|
|
|
|
|
|
|
} // namespace skiagm
|