OLD | NEW |
---|---|
(Empty) | |
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 "SkPatchGrid.h" | |
9 #include "SkPatchUtils.h" | |
10 | |
11 SkPatchGrid::SkPatchGrid(int rows, int cols) | |
12 : fRows(rows) | |
13 , fCols(cols) | |
14 , fCornerPts(NULL) | |
15 , fCornerColors(NULL) | |
16 , fHrzCtrlPts(NULL) | |
17 , fVrtCtrlPts(NULL) { | |
18 this->reset(fRows, fCols); | |
19 } | |
20 | |
21 SkPatchGrid::~SkPatchGrid() { | |
22 SkDELETE_ARRAY(fCornerPts); | |
23 SkDELETE_ARRAY(fCornerColors); | |
24 SkDELETE_ARRAY(fHrzCtrlPts); | |
25 SkDELETE_ARRAY(fVrtCtrlPts); | |
26 } | |
27 | |
28 bool SkPatchGrid::setPatch(int x, int y, const SkPoint ctrlPts[12], const SkColo r colors[4]) { | |
29 // Check for the passed paramaters to be within the range of the grid dimens ions. | |
30 if (x < 0 || y < 0 || x > fCols - 1 || y > fRows - 1) { | |
31 return false; | |
32 } | |
33 | |
34 //setup corners and colors | |
egdaniel
2014/08/08 18:45:41
space after //
dandov
2014/08/08 19:00:55
Done.
| |
35 int cornerPos = y * (fCols + 1) + x; | |
36 fCornerPts[cornerPos] = ctrlPts[SkPatch::kTopP0_CubicCtrlPts]; | |
37 fCornerPts[cornerPos + 1] = ctrlPts[SkPatch::kTopP3_CubicCtrlPts]; | |
38 fCornerPts[cornerPos + (fCols + 1)] = ctrlPts[SkPatch::kBottomP0_CubicCtrlPt s]; | |
39 fCornerPts[cornerPos + (fCols + 1) + 1] = ctrlPts[SkPatch::kBottomP3_CubicCt rlPts]; | |
40 | |
41 fCornerColors[cornerPos] = colors[0]; | |
42 fCornerColors[cornerPos + 1] = colors[1]; | |
43 fCornerColors[cornerPos + (fCols + 1)] = colors[3]; | |
44 fCornerColors[cornerPos + (fCols + 1) + 1] = colors[2]; | |
45 | |
46 // set horizontal control points | |
47 int hrzPos = y * (fCols * 2) + (x * 2); | |
48 fHrzCtrlPts[hrzPos] = ctrlPts[SkPatch::kTopP1_CubicCtrlPts]; | |
49 fHrzCtrlPts[hrzPos + 1] = ctrlPts[SkPatch::kTopP2_CubicCtrlPts]; | |
50 fHrzCtrlPts[hrzPos + (fCols * 2)] = ctrlPts[SkPatch::kBottomP1_CubicCtrlPts] ; | |
51 fHrzCtrlPts[hrzPos + (fCols * 2) + 1] = ctrlPts[SkPatch::kBottomP2_CubicCtrl Pts]; | |
52 | |
53 int vrtPos = (y*2) * (fCols + 1) + x; | |
54 fVrtCtrlPts[vrtPos] = ctrlPts[SkPatch::kLeftP1_CubicCtrlPts]; | |
55 fVrtCtrlPts[vrtPos + 1] = ctrlPts[SkPatch::kRightP1_CubicCtrlPts]; | |
56 fVrtCtrlPts[vrtPos + (fCols + 1)] = ctrlPts[SkPatch::kLeftP2_CubicCtrlPts]; | |
57 fVrtCtrlPts[vrtPos + (fCols + 1) + 1] = ctrlPts[SkPatch::kRightP2_CubicCtrlP ts]; | |
58 return true; | |
59 } | |
60 | |
61 bool SkPatchGrid::setPatch(int x, int y, const SkPatch& patch) { | |
62 return this->setPatch(x, y, patch.getControlPoints(), patch.getColors()); | |
63 } | |
64 | |
65 bool SkPatchGrid::getPatch(int x, int y, SkPatch* patch) const { | |
66 | |
67 if (x < 0 || y < 0 || x > fCols - 1 || y > fRows - 1 || NULL == patch) { | |
68 return false; | |
69 } | |
70 //set the patch by building the array of points and colors with the correspo nding values. | |
egdaniel
2014/08/08 18:45:41
space after //
dandov
2014/08/08 19:00:55
Done.
| |
71 | |
72 SkPoint ctrlPts[12]; | |
73 SkColor colors[4]; | |
74 | |
75 int cornerPos = y * (fCols + 1) + x; | |
76 ctrlPts[SkPatch::kTopP0_CubicCtrlPts] = fCornerPts[cornerPos]; | |
77 ctrlPts[SkPatch::kTopP3_CubicCtrlPts] = fCornerPts[cornerPos + 1]; | |
78 ctrlPts[SkPatch::kBottomP0_CubicCtrlPts] = fCornerPts[cornerPos + (fCols + 1 )]; | |
79 ctrlPts[SkPatch::kBottomP3_CubicCtrlPts] = fCornerPts[cornerPos + (fCols + 1 ) + 1]; | |
80 | |
81 colors[0] = fCornerColors[cornerPos]; | |
82 colors[1] = fCornerColors[cornerPos + 1]; | |
83 colors[3] = fCornerColors[cornerPos + (fCols + 1)]; | |
84 colors[2] = fCornerColors[cornerPos + (fCols + 1) + 1]; | |
85 | |
86 int hrzPos = y * (fCols * 2) + (x * 2); | |
87 ctrlPts[SkPatch::kTopP1_CubicCtrlPts] = fHrzCtrlPts[hrzPos]; | |
88 ctrlPts[SkPatch::kTopP2_CubicCtrlPts] = fHrzCtrlPts[hrzPos + 1]; | |
89 ctrlPts[SkPatch::kBottomP1_CubicCtrlPts] = fHrzCtrlPts[hrzPos + (fCols * 2)] ; | |
90 ctrlPts[SkPatch::kBottomP2_CubicCtrlPts] = fHrzCtrlPts[hrzPos + (fCols * 2) + 1]; | |
91 | |
92 int vrtPos = (y*2) * (fCols + 1) + x; | |
93 ctrlPts[SkPatch::kLeftP1_CubicCtrlPts] = fVrtCtrlPts[vrtPos]; | |
94 ctrlPts[SkPatch::kRightP1_CubicCtrlPts] = fVrtCtrlPts[vrtPos + 1]; | |
95 ctrlPts[SkPatch::kLeftP2_CubicCtrlPts] = fVrtCtrlPts[vrtPos + (fCols + 1)]; | |
96 ctrlPts[SkPatch::kRightP2_CubicCtrlPts] = fVrtCtrlPts[vrtPos + (fCols + 1) + 1]; | |
97 | |
98 patch->reset(ctrlPts, colors); | |
99 | |
100 return true; | |
101 } | |
102 | |
103 void SkPatchGrid::reset(int rows, int cols) { | |
104 SkDELETE_ARRAY(fCornerPts); | |
105 SkDELETE_ARRAY(fCornerColors); | |
106 SkDELETE_ARRAY(fHrzCtrlPts); | |
107 SkDELETE_ARRAY(fVrtCtrlPts); | |
108 | |
109 fCols = cols; | |
110 fRows = rows; | |
111 | |
112 fCornerPts = SkNEW_ARRAY(SkPoint, (fRows + 1) * (fCols + 1)); | |
113 fCornerColors = SkNEW_ARRAY(SkColor, (fRows + 1) * (fCols + 1)); | |
114 fHrzCtrlPts = SkNEW_ARRAY(SkPoint, (fRows + 1) * fCols * 2); | |
115 fVrtCtrlPts = SkNEW_ARRAY(SkPoint, fRows * 2 * (fCols + 1)); | |
116 | |
117 memset(fCornerPts, 0, (fRows + 1) * (fCols + 1) * sizeof(SkPoint)); | |
118 memset(fCornerColors, 0, (fRows + 1) * (fCols + 1) * sizeof(SkColor)); | |
119 memset(fHrzCtrlPts, 0, (fRows + 1) * fCols * 2 * sizeof(SkPoint)); | |
120 memset(fVrtCtrlPts, 0, fRows * 2 * (fCols + 1) * sizeof(SkPoint)); | |
121 } | |
122 | |
123 void SkPatchGrid::draw(SkCanvas* canvas, SkPaint& paint) { | |
124 int* maxCols = SkNEW_ARRAY(int, fCols); | |
125 int* maxRows = SkNEW_ARRAY(int, fRows); | |
126 memset(maxCols, 0, fCols * sizeof(int)); | |
127 memset(maxRows, 0, fRows * sizeof(int)); | |
128 | |
129 // Get the maximum level of detail per axis for each row and column | |
130 for (int y = 0; y < fRows; y++) { | |
131 for (int x = 0; x < fCols; x++) { | |
132 SkPatch patch; | |
133 this->getPatch(x, y, &patch); | |
134 SkMatrix matrix = canvas->getTotalMatrix(); | |
135 SkISize lod = SkPatchUtils::GetLevelOfDetail(patch, &matrix); | |
136 maxCols[x] = SkMax32(maxCols[x], lod.width()); | |
137 maxRows[y] = SkMax32(maxRows[y], lod.height()); | |
138 } | |
139 } | |
140 // Draw the patches by generating their geometry with the maximum level of d etail per axis. | |
141 for (int x = 0; x < fCols; x++) { | |
142 for (int y = 0; y < fRows; y++) { | |
143 SkPatch patch; | |
144 this->getPatch(x, y, &patch); | |
145 SkPatch::VertexData data; | |
146 patch.getVertexData(&data, maxCols[x], maxRows[y]); | |
147 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCo unt, | |
148 data.fPoints, data.fTexCoords, data.fColors, NU LL, | |
149 data.fIndices, data.fIndexCount, paint); | |
150 } | |
151 } | |
152 SkDELETE_ARRAY(maxCols); | |
153 SkDELETE_ARRAY(maxRows); | |
154 } | |
OLD | NEW |