dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | static 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 | |
| 54 | namespace skiagm { |
| 55 | /** |
| 56 | * This GM draws a grid of patches, it only uses colors so it could be considered a mesh gradient. |
| 57 | */ |
| 58 | class SkPatchGridGM : public GM { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 59 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 60 | public: |
| 61 | SkPatchGridGM() { |
| 62 | this->setBGColor(0xFFFFFFFF); |
| 63 | } |
| 64 | |
| 65 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 66 | SkString onShortName() override { |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 67 | return SkString("patch_grid"); |
| 68 | } |
| 69 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 70 | SkISize onISize() override { |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 71 | return SkISize::Make(800, 800); |
| 72 | } |
| 73 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | void onDraw(SkCanvas* canvas) override { |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 75 | |
| 76 | SkPaint paint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 77 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 78 | 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 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 84 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 85 | 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 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 91 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 92 | 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 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 99 | 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 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 107 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 108 | static const int kRows = 3; |
| 109 | static const int kCols = 4; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 110 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 111 | canvas->scale(3, 3); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | SkPatchGrid grid(kRows, kCols, SkPatchGrid::kColors_VertexType, nullptr); |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 113 | for (int i = 0; i < kRows; i++) { |
| 114 | for (int j = 0; j < kCols; j++) { |
| 115 | SkPoint points[12]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 116 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 117 | //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]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 122 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 123 | 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]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 127 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 128 | 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]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 132 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 133 | 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]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 138 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | grid.setPatch(j, i, points, colors, nullptr); |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 142 | |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 143 | 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]; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 148 | grid.getPatch(x, y, cubics, nullptr, nullptr); |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 149 | draw_control_points(canvas, cubics); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | typedef GM INHERITED; |
| 156 | }; |
| 157 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 158 | DEF_GM(return new SkPatchGridGM;) |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 159 | } |