OLD | NEW |
(Empty) | |
| 1 |
| 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 |
| 12 static 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 |
| 55 namespace skiagm { |
| 56 /** |
| 57 * This GM draws a grid of patches, it only uses colors so it could be considere
d a mesh gradient. |
| 58 */ |
| 59 class SkPatchGridGM : public GM { |
| 60 |
| 61 public: |
| 62 SkPatchGridGM() { |
| 63 this->setBGColor(0xFFFFFFFF); |
| 64 } |
| 65 |
| 66 protected: |
| 67 virtual SkString onShortName() SK_OVERRIDE { |
| 68 return SkString("patch_grid"); |
| 69 } |
| 70 |
| 71 virtual SkISize onISize() SK_OVERRIDE { |
| 72 return SkISize::Make(800, 800); |
| 73 } |
| 74 |
| 75 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 76 return kSkipTiled_Flag; |
| 77 } |
| 78 |
| 79 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 80 |
| 81 SkPaint paint; |
| 82 |
| 83 SkPoint vertices[4][5] = { |
| 84 {{50,50}, {150,50}, {250,50},{350,50},{450,50}}, |
| 85 {{50,150}, {120,120}, {250,150},{350,150},{450,150}}, |
| 86 {{50,250}, {150,250}, {250,250},{350,250},{450,250}}, |
| 87 {{100,300}, {150,350}, {250,350},{350,350},{450,350}} |
| 88 }; |
| 89 |
| 90 SkColor cornerColors[4][5] = { |
| 91 {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE}
, |
| 92 {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED}, |
| 93 {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE}
, |
| 94 {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED}, |
| 95 }; |
| 96 |
| 97 SkPoint hrzCtrl[4][8] = { |
| 98 {{75,30},{125,45},{175,70},{225,20},{275,50},{325,50},{375,5},{425,9
0}}, |
| 99 {{75,150},{125,150},{175,150},{225,150},{275,150},{325,150},{375,150
},{425,150}}, |
| 100 {{75,250},{125,250},{175,250},{225,250},{275,200},{325,150},{375,250
},{425,250}}, |
| 101 {{75,350},{125,350},{175,350},{225,350},{275,350},{325,350},{375,350
},{425,350}} |
| 102 }; |
| 103 |
| 104 SkPoint vrtCtrl[6][5] = { |
| 105 {{50,75},{150,75},{250,75},{350,75},{450,75}}, |
| 106 {{50,125},{150,125},{250,125},{350,125},{450,125}}, |
| 107 {{50,175},{150,175},{220,225},{350,175},{470,225}}, |
| 108 {{50,225},{150,225},{220,175},{350,225},{470,155}}, |
| 109 {{50,275},{150,275},{250,275},{350,275},{400,305}}, |
| 110 {{50,325},{150,325},{250,325},{350,325},{450,325}} |
| 111 }; |
| 112 |
| 113 static const int kRows = 3; |
| 114 static const int kCols = 4; |
| 115 |
| 116 canvas->scale(3, 3); |
| 117 SkPatchGrid grid(kRows, kCols, SkPatchGrid::kColors_VertexType, NULL); |
| 118 for (int i = 0; i < kRows; i++) { |
| 119 for (int j = 0; j < kCols; j++) { |
| 120 SkPoint points[12]; |
| 121 |
| 122 //set corners |
| 123 points[SkPatchUtils::kTopP0_CubicCtrlPts] = vertices[i][j]; |
| 124 points[SkPatchUtils::kTopP3_CubicCtrlPts] = vertices[i][j + 1]; |
| 125 points[SkPatchUtils::kBottomP0_CubicCtrlPts] = vertices[i + 1][j
]; |
| 126 points[SkPatchUtils::kBottomP3_CubicCtrlPts] = vertices[i + 1][j
+ 1]; |
| 127 |
| 128 points[SkPatchUtils::kTopP1_CubicCtrlPts] = hrzCtrl[i][j * 2]; |
| 129 points[SkPatchUtils::kTopP2_CubicCtrlPts] = hrzCtrl[i][j * 2 + 1
]; |
| 130 points[SkPatchUtils::kBottomP1_CubicCtrlPts] = hrzCtrl[i + 1][j
* 2]; |
| 131 points[SkPatchUtils::kBottomP2_CubicCtrlPts] = hrzCtrl[i + 1][j
* 2 + 1]; |
| 132 |
| 133 points[SkPatchUtils::kLeftP1_CubicCtrlPts] = vrtCtrl[i * 2][j]; |
| 134 points[SkPatchUtils::kLeftP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][
j]; |
| 135 points[SkPatchUtils::kRightP1_CubicCtrlPts] = vrtCtrl[i * 2][j +
1]; |
| 136 points[SkPatchUtils::kRightP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1]
[j + 1]; |
| 137 |
| 138 SkColor colors[4]; |
| 139 colors[0] = cornerColors[i][j]; |
| 140 colors[1] = cornerColors[i][j + 1]; |
| 141 colors[3] = cornerColors[i + 1][j]; |
| 142 colors[2] = cornerColors[i + 1][j + 1]; |
| 143 |
| 144 grid.setPatch(j, i, points, colors, NULL); |
| 145 } |
| 146 } |
| 147 |
| 148 grid.draw(canvas, paint); |
| 149 SkISize dims = grid.getDimensions(); |
| 150 for (int y = 0; y < dims.height(); y++) { |
| 151 for (int x = 0; x < dims.width(); x++) { |
| 152 SkPoint cubics[12]; |
| 153 grid.getPatch(x, y, cubics, NULL, NULL); |
| 154 draw_control_points(canvas, cubics); |
| 155 } |
| 156 } |
| 157 } |
| 158 |
| 159 private: |
| 160 typedef GM INHERITED; |
| 161 }; |
| 162 |
| 163 DEF_GM(return SkNEW(SkPatchGridGM); ) |
| 164 |
| 165 } |
OLD | NEW |