3 * Copyright 2014 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkPatchGrid.h"
12 static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
16 SkPatchUtils::getBottomCubic(cubics, bottom);
18 SkPatchUtils::getTopCubic(cubics, top);
20 SkPatchUtils::getLeftCubic(cubics, left);
22 SkPatchUtils::getRightCubic(cubics, right);
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);
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);
37 paint.setStrokeWidth(2);
39 paint.setColor(SK_ColorRED);
40 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
42 paint.setColor(SK_ColorBLUE);
43 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom+1, paint);
45 paint.setColor(SK_ColorCYAN);
46 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top+1, paint);
48 paint.setColor(SK_ColorYELLOW);
49 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left+1, paint);
51 paint.setColor(SK_ColorGREEN);
52 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right+1, paint);
57 * This GM draws a grid of patches, it only uses colors so it could be considered a mesh gradient.
59 class SkPatchGridGM : public GM {
63 this->setBGColor(0xFFFFFFFF);
67 SkString onShortName() SK_OVERRIDE {
68 return SkString("patch_grid");
71 SkISize onISize() SK_OVERRIDE {
72 return SkISize::Make(800, 800);
75 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
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}}
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},
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}}
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}}
109 static const int kRows = 3;
110 static const int kCols = 4;
113 SkPatchGrid grid(kRows, kCols, SkPatchGrid::kColors_VertexType, NULL);
114 for (int i = 0; i < kRows; i++) {
115 for (int j = 0; j < kCols; j++) {
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];
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];
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];
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];
140 grid.setPatch(j, i, points, colors, NULL);
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++) {
149 grid.getPatch(x, y, cubics, NULL, NULL);
150 draw_control_points(canvas, cubics);
156 typedef GM INHERITED;
159 DEF_GM(return SkNEW(SkPatchGridGM); )