| 1 |
/* |
| 2 |
* Copyright 2018 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/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkPathBuilder.h" |
| 15 |
#include "include/core/SkPoint.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkScalar.h" |
| 18 |
#include "include/core/SkSize.h" |
| 19 |
#include "include/core/SkString.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "src/base/SkRandom.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
|
| 24 |
#include <array> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
namespace skiagm { |
| 28 |
|
| 29 |
static constexpr int kPadSize = 20; |
| 30 |
static constexpr int kBoxSize = 100; |
| 31 |
static constexpr SkPoint kJitters[] = {{0, 0}, {.5f, .5f}, {2/3.f, 1/3.f}}; |
| 32 |
|
| 33 |
// Tests various corners of different angles falling on the same pixel, particularly to ensure |
| 34 |
// analytic AA is working properly. |
| 35 |
class SharedCornersGM : public GM { |
| 36 |
public: |
| 37 |
SharedCornersGM() { this->setBGColor(ToolUtils::color_to_565(0xFF1A65D7)); } |
| 38 |
|
| 39 |
protected: |
| 40 |
SkString getName() const override { return SkString("sharedcorners"); } |
| 41 |
|
| 42 |
SkISize getISize() override { |
| 43 |
constexpr int numRows = 3 * 2; |
| 44 |
constexpr int numCols = (1 + std::size(kJitters)) * 2; |
| 45 |
return SkISize::Make(numCols * (kBoxSize + kPadSize) + kPadSize, |
| 46 |
numRows * (kBoxSize + kPadSize) + kPadSize); |
| 47 |
} |
| 48 |
|
| 49 |
void onOnceBeforeDraw() override { |
| 50 |
fFillPaint.setColor(SK_ColorWHITE); |
| 51 |
fFillPaint.setAntiAlias(true); |
| 52 |
|
| 53 |
fWireFramePaint = fFillPaint; |
| 54 |
fWireFramePaint.setStyle(SkPaint::kStroke_Style); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
void onDraw(SkCanvas* canvas) override { |
| 59 |
canvas->translate(kPadSize, kPadSize); |
| 60 |
canvas->save(); |
| 61 |
|
| 62 |
// Adjacent rects. |
| 63 |
this->drawTriangleBoxes(canvas, |
| 64 |
{{0, 0}, {40, 0}, {80, 0}, {120, 0}, |
| 65 |
{0, 20}, {40, 20}, {80, 20}, {120, 20}, |
| 66 |
{40, 40}, {80, 40}, |
| 67 |
{40, 60}, {80, 60}}, |
| 68 |
{{{0, 1, 4}}, {{1, 5, 4}}, |
| 69 |
{{5, 1, 6}}, {{1, 2, 6}}, |
| 70 |
{{2, 3, 6}}, {{3, 7, 6}}, |
| 71 |
{{8, 5, 9}}, {{5, 6, 9}}, |
| 72 |
{{10, 8, 11}}, {{8, 9, 11}}}); |
| 73 |
|
| 74 |
// Obtuse angles. |
| 75 |
this->drawTriangleBoxes(canvas, |
| 76 |
{{ 0, 0}, {10, 0}, {20, 0}, |
| 77 |
{ 0, 2}, {20, 2}, |
| 78 |
{10, 4}, |
| 79 |
{ 0, 6}, {20, 6}, |
| 80 |
{ 0, 8}, {10, 8}, {20, 8}}, |
| 81 |
{{{3, 1, 4}}, {{4, 5, 3}}, {{6, 5, 7}}, {{7, 9, 6}}, |
| 82 |
{{0, 1, 3}}, {{1, 2, 4}}, |
| 83 |
{{3, 5, 6}}, {{5, 4, 7}}, |
| 84 |
{{6, 9, 8}}, {{9, 7, 10}}}); |
| 85 |
|
| 86 |
canvas->restore(); |
| 87 |
canvas->translate((kBoxSize + kPadSize) * 4, 0); |
| 88 |
|
| 89 |
// Right angles. |
| 90 |
this->drawTriangleBoxes(canvas, |
| 91 |
{{0, 0}, {-1, 0}, {0, -1}, {1, 0}, {0, 1}}, |
| 92 |
{{{0, 1, 2}}, {{0, 2, 3}}, {{0, 3, 4}}, {{0, 4, 1}}}); |
| 93 |
|
| 94 |
// Acute angles. |
| 95 |
SkRandom rand; |
| 96 |
std::vector<SkPoint> pts; |
| 97 |
std::vector<std::array<int, 3>> indices; |
| 98 |
SkScalar theta = 0; |
| 99 |
pts.push_back({0, 0}); |
| 100 |
while (theta < 2*SK_ScalarPI) { |
| 101 |
pts.push_back({SkScalarCos(theta), SkScalarSin(theta)}); |
| 102 |
if (pts.size() > 2) { |
| 103 |
indices.push_back({{0, (int)pts.size() - 2, (int)pts.size() - 1}}); |
| 104 |
} |
| 105 |
theta += rand.nextRangeF(0, SK_ScalarPI/3); |
| 106 |
} |
| 107 |
indices.push_back({{0, (int)pts.size() - 1, 1}}); |
| 108 |
this->drawTriangleBoxes(canvas, pts, indices); |
| 109 |
} |
| 110 |
|
| 111 |
void drawTriangleBoxes(SkCanvas* canvas, const std::vector<SkPoint>& points, |
| 112 |
const std::vector<std::array<int, 3>>& triangles) { |
| 113 |
SkPathBuilder builder(SkPathFillType::kEvenOdd); |
| 114 |
builder.setIsVolatile(true); |
| 115 |
for (const std::array<int, 3>& triangle : triangles) { |
| 116 |
builder.moveTo(points[triangle[0]]); |
| 117 |
builder.lineTo(points[triangle[1]]); |
| 118 |
builder.lineTo(points[triangle[2]]); |
| 119 |
builder.close(); |
| 120 |
} |
| 121 |
const SkRect bounds = builder.computeBounds(); |
| 122 |
SkScalar scale = kBoxSize / std::max(bounds.height(), bounds.width()); |
| 123 |
builder.transform(SkMatrix::Scale(scale, scale)); |
| 124 |
SkPath path = builder.detach(); |
| 125 |
|
| 126 |
this->drawRow(canvas, path); |
| 127 |
canvas->translate(0, kBoxSize + kPadSize); |
| 128 |
|
| 129 |
SkMatrix rot; |
| 130 |
rot.setRotate(45, path.getBounds().centerX(), path.getBounds().centerY()); |
| 131 |
path = path.makeTransform(rot); |
| 132 |
this->drawRow(canvas, path); |
| 133 |
canvas->translate(0, kBoxSize + kPadSize); |
| 134 |
|
| 135 |
rot.setRotate(-45 - 69.38111f, path.getBounds().centerX(), path.getBounds().centerY()); |
| 136 |
path = path.makeTransform(rot); |
| 137 |
this->drawRow(canvas, path); |
| 138 |
canvas->translate(0, kBoxSize + kPadSize); |
| 139 |
} |
| 140 |
|
| 141 |
void drawRow(SkCanvas* canvas, const SkPath& path) { |
| 142 |
SkAutoCanvasRestore acr(canvas, true); |
| 143 |
const SkRect& bounds = path.getBounds(); |
| 144 |
canvas->translate((kBoxSize - bounds.width()) / 2 - bounds.left(), |
| 145 |
(kBoxSize - bounds.height()) / 2 - bounds.top()); |
| 146 |
|
| 147 |
canvas->drawPath(path, fWireFramePaint); |
| 148 |
canvas->translate(kBoxSize + kPadSize, 0); |
| 149 |
|
| 150 |
for (SkPoint jitter : kJitters) { |
| 151 |
{ |
| 152 |
SkAutoCanvasRestore acr2(canvas, true); |
| 153 |
canvas->translate(jitter.x(), jitter.y()); |
| 154 |
canvas->drawPath(path, fFillPaint); |
| 155 |
} |
| 156 |
canvas->translate(kBoxSize + kPadSize, 0); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
SkPaint fWireFramePaint; |
| 161 |
SkPaint fFillPaint; |
| 162 |
}; |
| 163 |
|
| 164 |
DEF_GM(return new SharedCornersGM;) |
| 165 |
|
| 166 |
} // namespace skiagm |