blob: 962c1a9dffed7f43dc2eed9961dfb89277683960 [file] [log] [blame]
dandovcc03adb2014-08-12 17:14:57 -07001/*
2 * Copyright 2014 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.h"
9#include "SkPatchGrid.h"
10
11static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
12 //draw control points
13 SkPaint paint;
14 SkPoint bottom[4];
15 SkPatchUtils::getBottomCubic(cubics, bottom);
16 SkPoint top[4];
17 SkPatchUtils::getTopCubic(cubics, top);
18 SkPoint left[4];
19 SkPatchUtils::getLeftCubic(cubics, left);
20 SkPoint right[4];
21 SkPatchUtils::getRightCubic(cubics, right);
22
23 paint.setColor(SK_ColorBLACK);
24 paint.setStrokeWidth(0.5);
25 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
26 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
27 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom+1, paint);
28 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
29 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
30 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
31
32 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top+1, paint);
33 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left+1, paint);
34 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right+1, paint);
35
36 paint.setStrokeWidth(2);
37
38 paint.setColor(SK_ColorRED);
39 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
40
41 paint.setColor(SK_ColorBLUE);
42 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom+1, paint);
43
44 paint.setColor(SK_ColorCYAN);
45 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top+1, paint);
46
47 paint.setColor(SK_ColorYELLOW);
48 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left+1, paint);
49
50 paint.setColor(SK_ColorGREEN);
51 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right+1, paint);
52}
53
54namespace skiagm {
55/**
56 * This GM draws a grid of patches, it only uses colors so it could be considered a mesh gradient.
57 */
58class SkPatchGridGM : public GM {
halcanary9d524f22016-03-29 09:03:52 -070059
dandovcc03adb2014-08-12 17:14:57 -070060public:
61 SkPatchGridGM() {
62 this->setBGColor(0xFFFFFFFF);
63 }
64
65protected:
mtklein36352bf2015-03-25 18:17:31 -070066 SkString onShortName() override {
dandovcc03adb2014-08-12 17:14:57 -070067 return SkString("patch_grid");
68 }
69
mtklein36352bf2015-03-25 18:17:31 -070070 SkISize onISize() override {
dandovcc03adb2014-08-12 17:14:57 -070071 return SkISize::Make(800, 800);
72 }
73
mtklein36352bf2015-03-25 18:17:31 -070074 void onDraw(SkCanvas* canvas) override {
dandovcc03adb2014-08-12 17:14:57 -070075
76 SkPaint paint;
halcanary9d524f22016-03-29 09:03:52 -070077
dandovcc03adb2014-08-12 17:14:57 -070078 SkPoint vertices[4][5] = {
79 {{50,50}, {150,50}, {250,50},{350,50},{450,50}},
80 {{50,150}, {120,120}, {250,150},{350,150},{450,150}},
81 {{50,250}, {150,250}, {250,250},{350,250},{450,250}},
82 {{100,300}, {150,350}, {250,350},{350,350},{450,350}}
83 };
halcanary9d524f22016-03-29 09:03:52 -070084
dandovcc03adb2014-08-12 17:14:57 -070085 SkColor cornerColors[4][5] = {
86 {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE},
87 {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED},
88 {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE},
89 {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED},
90 };
halcanary9d524f22016-03-29 09:03:52 -070091
dandovcc03adb2014-08-12 17:14:57 -070092 SkPoint hrzCtrl[4][8] = {
93 {{75,30},{125,45},{175,70},{225,20},{275,50},{325,50},{375,5},{425,90}},
94 {{75,150},{125,150},{175,150},{225,150},{275,150},{325,150},{375,150},{425,150}},
95 {{75,250},{125,250},{175,250},{225,250},{275,200},{325,150},{375,250},{425,250}},
96 {{75,350},{125,350},{175,350},{225,350},{275,350},{325,350},{375,350},{425,350}}
97 };
halcanary9d524f22016-03-29 09:03:52 -070098
dandovcc03adb2014-08-12 17:14:57 -070099 SkPoint vrtCtrl[6][5] = {
100 {{50,75},{150,75},{250,75},{350,75},{450,75}},
101 {{50,125},{150,125},{250,125},{350,125},{450,125}},
102 {{50,175},{150,175},{220,225},{350,175},{470,225}},
103 {{50,225},{150,225},{220,175},{350,225},{470,155}},
104 {{50,275},{150,275},{250,275},{350,275},{400,305}},
105 {{50,325},{150,325},{250,325},{350,325},{450,325}}
106 };
halcanary9d524f22016-03-29 09:03:52 -0700107
dandovcc03adb2014-08-12 17:14:57 -0700108 static const int kRows = 3;
109 static const int kCols = 4;
halcanary9d524f22016-03-29 09:03:52 -0700110
dandovcc03adb2014-08-12 17:14:57 -0700111 canvas->scale(3, 3);
halcanary96fcdcc2015-08-27 07:41:13 -0700112 SkPatchGrid grid(kRows, kCols, SkPatchGrid::kColors_VertexType, nullptr);
dandovcc03adb2014-08-12 17:14:57 -0700113 for (int i = 0; i < kRows; i++) {
114 for (int j = 0; j < kCols; j++) {
115 SkPoint points[12];
halcanary9d524f22016-03-29 09:03:52 -0700116
dandovcc03adb2014-08-12 17:14:57 -0700117 //set corners
118 points[SkPatchUtils::kTopP0_CubicCtrlPts] = vertices[i][j];
119 points[SkPatchUtils::kTopP3_CubicCtrlPts] = vertices[i][j + 1];
120 points[SkPatchUtils::kBottomP0_CubicCtrlPts] = vertices[i + 1][j];
121 points[SkPatchUtils::kBottomP3_CubicCtrlPts] = vertices[i + 1][j + 1];
halcanary9d524f22016-03-29 09:03:52 -0700122
dandovcc03adb2014-08-12 17:14:57 -0700123 points[SkPatchUtils::kTopP1_CubicCtrlPts] = hrzCtrl[i][j * 2];
124 points[SkPatchUtils::kTopP2_CubicCtrlPts] = hrzCtrl[i][j * 2 + 1];
125 points[SkPatchUtils::kBottomP1_CubicCtrlPts] = hrzCtrl[i + 1][j * 2];
126 points[SkPatchUtils::kBottomP2_CubicCtrlPts] = hrzCtrl[i + 1][j * 2 + 1];
halcanary9d524f22016-03-29 09:03:52 -0700127
dandovcc03adb2014-08-12 17:14:57 -0700128 points[SkPatchUtils::kLeftP1_CubicCtrlPts] = vrtCtrl[i * 2][j];
129 points[SkPatchUtils::kLeftP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j];
130 points[SkPatchUtils::kRightP1_CubicCtrlPts] = vrtCtrl[i * 2][j + 1];
131 points[SkPatchUtils::kRightP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j + 1];
halcanary9d524f22016-03-29 09:03:52 -0700132
dandovcc03adb2014-08-12 17:14:57 -0700133 SkColor colors[4];
134 colors[0] = cornerColors[i][j];
135 colors[1] = cornerColors[i][j + 1];
136 colors[3] = cornerColors[i + 1][j];
137 colors[2] = cornerColors[i + 1][j + 1];
halcanary9d524f22016-03-29 09:03:52 -0700138
halcanary96fcdcc2015-08-27 07:41:13 -0700139 grid.setPatch(j, i, points, colors, nullptr);
dandovcc03adb2014-08-12 17:14:57 -0700140 }
141 }
halcanary9d524f22016-03-29 09:03:52 -0700142
dandovcc03adb2014-08-12 17:14:57 -0700143 grid.draw(canvas, paint);
144 SkISize dims = grid.getDimensions();
145 for (int y = 0; y < dims.height(); y++) {
146 for (int x = 0; x < dims.width(); x++) {
147 SkPoint cubics[12];
halcanary96fcdcc2015-08-27 07:41:13 -0700148 grid.getPatch(x, y, cubics, nullptr, nullptr);
dandovcc03adb2014-08-12 17:14:57 -0700149 draw_control_points(canvas, cubics);
150 }
151 }
152 }
153
154private:
155 typedef GM INHERITED;
156};
157
halcanary385fe4d2015-08-26 13:07:48 -0700158DEF_GM(return new SkPatchGridGM;)
dandovcc03adb2014-08-12 17:14:57 -0700159}