blob: 6f77d48a884f859f10c02583e4608d1de44445f9 [file] [log] [blame]
dandovcc03adb2014-08-12 17:14:57 -07001
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "gm.h"
10#include "SkPatchGrid.h"
11
12static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
13 //draw control points
14 SkPaint paint;
15 SkPoint bottom[4];
16 SkPatchUtils::getBottomCubic(cubics, bottom);
17 SkPoint top[4];
18 SkPatchUtils::getTopCubic(cubics, top);
19 SkPoint left[4];
20 SkPatchUtils::getLeftCubic(cubics, left);
21 SkPoint right[4];
22 SkPatchUtils::getRightCubic(cubics, right);
23
24 paint.setColor(SK_ColorBLACK);
25 paint.setStrokeWidth(0.5);
26 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
27 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
28 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom+1, paint);
29 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
30 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
31 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
32
33 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top+1, paint);
34 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left+1, paint);
35 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right+1, paint);
36
37 paint.setStrokeWidth(2);
38
39 paint.setColor(SK_ColorRED);
40 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
41
42 paint.setColor(SK_ColorBLUE);
43 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom+1, paint);
44
45 paint.setColor(SK_ColorCYAN);
46 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top+1, paint);
47
48 paint.setColor(SK_ColorYELLOW);
49 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left+1, paint);
50
51 paint.setColor(SK_ColorGREEN);
52 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right+1, paint);
53}
54
55namespace skiagm {
56/**
57 * This GM draws a grid of patches, it only uses colors so it could be considered a mesh gradient.
58 */
59class SkPatchGridGM : public GM {
60
61public:
62 SkPatchGridGM() {
63 this->setBGColor(0xFFFFFFFF);
64 }
65
66protected:
mtklein36352bf2015-03-25 18:17:31 -070067 SkString onShortName() override {
dandovcc03adb2014-08-12 17:14:57 -070068 return SkString("patch_grid");
69 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 SkISize onISize() override {
dandovcc03adb2014-08-12 17:14:57 -070072 return SkISize::Make(800, 800);
73 }
74
mtklein36352bf2015-03-25 18:17:31 -070075 void onDraw(SkCanvas* canvas) override {
dandovcc03adb2014-08-12 17:14:57 -070076
77 SkPaint paint;
78
79 SkPoint vertices[4][5] = {
80 {{50,50}, {150,50}, {250,50},{350,50},{450,50}},
81 {{50,150}, {120,120}, {250,150},{350,150},{450,150}},
82 {{50,250}, {150,250}, {250,250},{350,250},{450,250}},
83 {{100,300}, {150,350}, {250,350},{350,350},{450,350}}
84 };
85
86 SkColor cornerColors[4][5] = {
87 {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE},
88 {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED},
89 {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE},
90 {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED},
91 };
92
93 SkPoint hrzCtrl[4][8] = {
94 {{75,30},{125,45},{175,70},{225,20},{275,50},{325,50},{375,5},{425,90}},
95 {{75,150},{125,150},{175,150},{225,150},{275,150},{325,150},{375,150},{425,150}},
96 {{75,250},{125,250},{175,250},{225,250},{275,200},{325,150},{375,250},{425,250}},
97 {{75,350},{125,350},{175,350},{225,350},{275,350},{325,350},{375,350},{425,350}}
98 };
99
100 SkPoint vrtCtrl[6][5] = {
101 {{50,75},{150,75},{250,75},{350,75},{450,75}},
102 {{50,125},{150,125},{250,125},{350,125},{450,125}},
103 {{50,175},{150,175},{220,225},{350,175},{470,225}},
104 {{50,225},{150,225},{220,175},{350,225},{470,155}},
105 {{50,275},{150,275},{250,275},{350,275},{400,305}},
106 {{50,325},{150,325},{250,325},{350,325},{450,325}}
107 };
108
109 static const int kRows = 3;
110 static const int kCols = 4;
111
112 canvas->scale(3, 3);
halcanary96fcdcc2015-08-27 07:41:13 -0700113 SkPatchGrid grid(kRows, kCols, SkPatchGrid::kColors_VertexType, nullptr);
dandovcc03adb2014-08-12 17:14:57 -0700114 for (int i = 0; i < kRows; i++) {
115 for (int j = 0; j < kCols; j++) {
116 SkPoint points[12];
117
118 //set corners
119 points[SkPatchUtils::kTopP0_CubicCtrlPts] = vertices[i][j];
120 points[SkPatchUtils::kTopP3_CubicCtrlPts] = vertices[i][j + 1];
121 points[SkPatchUtils::kBottomP0_CubicCtrlPts] = vertices[i + 1][j];
122 points[SkPatchUtils::kBottomP3_CubicCtrlPts] = vertices[i + 1][j + 1];
123
124 points[SkPatchUtils::kTopP1_CubicCtrlPts] = hrzCtrl[i][j * 2];
125 points[SkPatchUtils::kTopP2_CubicCtrlPts] = hrzCtrl[i][j * 2 + 1];
126 points[SkPatchUtils::kBottomP1_CubicCtrlPts] = hrzCtrl[i + 1][j * 2];
127 points[SkPatchUtils::kBottomP2_CubicCtrlPts] = hrzCtrl[i + 1][j * 2 + 1];
128
129 points[SkPatchUtils::kLeftP1_CubicCtrlPts] = vrtCtrl[i * 2][j];
130 points[SkPatchUtils::kLeftP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j];
131 points[SkPatchUtils::kRightP1_CubicCtrlPts] = vrtCtrl[i * 2][j + 1];
132 points[SkPatchUtils::kRightP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j + 1];
133
134 SkColor colors[4];
135 colors[0] = cornerColors[i][j];
136 colors[1] = cornerColors[i][j + 1];
137 colors[3] = cornerColors[i + 1][j];
138 colors[2] = cornerColors[i + 1][j + 1];
139
halcanary96fcdcc2015-08-27 07:41:13 -0700140 grid.setPatch(j, i, points, colors, nullptr);
dandovcc03adb2014-08-12 17:14:57 -0700141 }
142 }
143
144 grid.draw(canvas, paint);
145 SkISize dims = grid.getDimensions();
146 for (int y = 0; y < dims.height(); y++) {
147 for (int x = 0; x < dims.width(); x++) {
148 SkPoint cubics[12];
halcanary96fcdcc2015-08-27 07:41:13 -0700149 grid.getPatch(x, y, cubics, nullptr, nullptr);
dandovcc03adb2014-08-12 17:14:57 -0700150 draw_control_points(canvas, cubics);
151 }
152 }
153 }
154
155private:
156 typedef GM INHERITED;
157};
158
halcanary385fe4d2015-08-26 13:07:48 -0700159DEF_GM(return new SkPatchGridGM;)
dandovcc03adb2014-08-12 17:14:57 -0700160}