OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkPatch_DEFINED | 8 #ifndef SkPatch_DEFINED |
9 #define SkPatch_DEFINED | 9 #define SkPatch_DEFINED |
10 | 10 |
11 #include "SkColor.h" | 11 #include "SkColor.h" |
12 #include "SkPreConfig.h" | 12 #include "SkPreConfig.h" |
13 #include "SkPoint.h" | 13 #include "SkPoint.h" |
| 14 #include "SkRect.h" |
14 | 15 |
15 /** | 16 /** |
16 * Class that represents a coons patch. | 17 * Class that represents a coons patch. |
17 */ | 18 */ |
18 class SK_API SkPatch { | 19 class SK_API SkPatch { |
19 | 20 |
20 public: | 21 public: |
21 /** | 22 /** |
22 * Structure that holds the vertex data related to the tessellation of a SkP
atch. It is passed | 23 * Structure that holds the vertex data related to the tessellation of a SkP
atch. It is passed |
23 * as a parameter to the function getVertexData which sets the points, color
s and texture | 24 * as a parameter to the function getVertexData which sets the points, color
s and texture |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 kBottomP1_CubicCtrlPts = 8, | 63 kBottomP1_CubicCtrlPts = 8, |
63 kBottomP2_CubicCtrlPts = 7, | 64 kBottomP2_CubicCtrlPts = 7, |
64 kBottomP3_CubicCtrlPts = 6, | 65 kBottomP3_CubicCtrlPts = 6, |
65 | 66 |
66 kLeftP0_CubicCtrlPts = 0, | 67 kLeftP0_CubicCtrlPts = 0, |
67 kLeftP1_CubicCtrlPts = 11, | 68 kLeftP1_CubicCtrlPts = 11, |
68 kLeftP2_CubicCtrlPts = 10, | 69 kLeftP2_CubicCtrlPts = 10, |
69 kLeftP3_CubicCtrlPts = 9, | 70 kLeftP3_CubicCtrlPts = 9, |
70 }; | 71 }; |
71 | 72 |
72 // Enum for corner colors also clockwise. | 73 // Enum for corner also clockwise. |
73 enum CornerColors { | 74 enum Corner { |
74 kTopLeft_CornerColors = 0, | 75 kTopLeft_Corner = 0, |
75 kTopRight_CornerColors, | 76 kTopRight_Corner, |
76 kBottomRight_CornerColors, | 77 kBottomRight_Corner, |
77 kBottomLeft_CornerColors | 78 kBottomLeft_Corner |
78 }; | 79 }; |
79 | 80 |
80 enum { | 81 enum { |
81 kNumCtrlPts = 12, | 82 kNumCtrlPts = 12, |
82 kNumColors = 4, | 83 kNumCorners = 4, |
83 kNumPtsCubic = 4 | 84 kNumPtsCubic = 4 |
84 }; | 85 }; |
85 | 86 |
86 /** | 87 /** |
87 * Points are in the following order: | 88 * Points are in the following order: |
88 * (top curve) | 89 * (top curve) |
89 * 0 1 2 3 | 90 * 0 1 2 3 |
90 * (left curve) 11 4 (right curve) | 91 * (left curve) 11 4 (right curve) |
91 * 10 5 | 92 * 10 5 |
92 * 9 8 7 6 | 93 * 9 8 7 6 |
93 * (bottom curve) | 94 * (bottom curve) |
94 */ | 95 */ |
95 SkPatch() { } | 96 SkPatch() { } |
96 SkPatch(const SkPoint points[12], const SkColor colors[4]); | 97 SkPatch(const SkPoint points[12]); |
97 | 98 |
98 /** | 99 /** |
99 * Function that evaluates the coons patch interpolation. | 100 * Function that evaluates the coons patch interpolation. |
100 * data refers to the pointer of the PatchData struct in which the tessellat
ion data is set. | 101 * data refers to the pointer of the PatchData struct in which the tessellat
ion data is set. |
101 * lod refers the level of detail for each axis. | 102 * lod refers the level of detail for each axis. |
102 */ | 103 */ |
103 bool getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const; | 104 bool getVertexData(SkPatch::VertexData* data, const SkColor colors[4], |
| 105 const SkPoint texCoords[4], int lodX, int lodY) const; |
104 | 106 |
105 void getTopPoints(SkPoint points[4]) const { | 107 void getTopPoints(SkPoint points[4]) const { |
| 108 if (NULL == points) { |
| 109 return; |
| 110 } |
106 points[0] = fCtrlPoints[kTopP0_CubicCtrlPts]; | 111 points[0] = fCtrlPoints[kTopP0_CubicCtrlPts]; |
107 points[1] = fCtrlPoints[kTopP1_CubicCtrlPts]; | 112 points[1] = fCtrlPoints[kTopP1_CubicCtrlPts]; |
108 points[2] = fCtrlPoints[kTopP2_CubicCtrlPts]; | 113 points[2] = fCtrlPoints[kTopP2_CubicCtrlPts]; |
109 points[3] = fCtrlPoints[kTopP3_CubicCtrlPts]; | 114 points[3] = fCtrlPoints[kTopP3_CubicCtrlPts]; |
110 } | 115 } |
111 | 116 |
112 void getBottomPoints(SkPoint points[4]) const { | 117 void getBottomPoints(SkPoint points[4]) const { |
| 118 if (NULL == points) { |
| 119 return; |
| 120 } |
113 points[0] = fCtrlPoints[kBottomP0_CubicCtrlPts]; | 121 points[0] = fCtrlPoints[kBottomP0_CubicCtrlPts]; |
114 points[1] = fCtrlPoints[kBottomP1_CubicCtrlPts]; | 122 points[1] = fCtrlPoints[kBottomP1_CubicCtrlPts]; |
115 points[2] = fCtrlPoints[kBottomP2_CubicCtrlPts]; | 123 points[2] = fCtrlPoints[kBottomP2_CubicCtrlPts]; |
116 points[3] = fCtrlPoints[kBottomP3_CubicCtrlPts]; | 124 points[3] = fCtrlPoints[kBottomP3_CubicCtrlPts]; |
117 } | 125 } |
118 | 126 |
119 void getLeftPoints(SkPoint points[4]) const { | 127 void getLeftPoints(SkPoint points[4]) const { |
| 128 if (NULL == points) { |
| 129 return; |
| 130 } |
120 points[0] = fCtrlPoints[kLeftP0_CubicCtrlPts]; | 131 points[0] = fCtrlPoints[kLeftP0_CubicCtrlPts]; |
121 points[1] = fCtrlPoints[kLeftP1_CubicCtrlPts]; | 132 points[1] = fCtrlPoints[kLeftP1_CubicCtrlPts]; |
122 points[2] = fCtrlPoints[kLeftP2_CubicCtrlPts]; | 133 points[2] = fCtrlPoints[kLeftP2_CubicCtrlPts]; |
123 points[3] = fCtrlPoints[kLeftP3_CubicCtrlPts]; | 134 points[3] = fCtrlPoints[kLeftP3_CubicCtrlPts]; |
124 } | 135 } |
125 | 136 |
126 void getRightPoints(SkPoint points[4]) const { | 137 void getRightPoints(SkPoint points[4]) const { |
| 138 if (NULL == points) { |
| 139 return; |
| 140 } |
127 points[0] = fCtrlPoints[kRightP0_CubicCtrlPts]; | 141 points[0] = fCtrlPoints[kRightP0_CubicCtrlPts]; |
128 points[1] = fCtrlPoints[kRightP1_CubicCtrlPts]; | 142 points[1] = fCtrlPoints[kRightP1_CubicCtrlPts]; |
129 points[2] = fCtrlPoints[kRightP2_CubicCtrlPts]; | 143 points[2] = fCtrlPoints[kRightP2_CubicCtrlPts]; |
130 points[3] = fCtrlPoints[kRightP3_CubicCtrlPts]; | 144 points[3] = fCtrlPoints[kRightP3_CubicCtrlPts]; |
131 } | 145 } |
132 | 146 |
133 void getCornerPoints(SkPoint points[4]) const { | 147 void getCornerPoints(SkPoint points[4]) const { |
| 148 if (NULL == points) { |
| 149 return; |
| 150 } |
134 points[0] = fCtrlPoints[kTopP0_CubicCtrlPts]; | 151 points[0] = fCtrlPoints[kTopP0_CubicCtrlPts]; |
135 points[1] = fCtrlPoints[kTopP3_CubicCtrlPts]; | 152 points[1] = fCtrlPoints[kTopP3_CubicCtrlPts]; |
136 points[2] = fCtrlPoints[kBottomP3_CubicCtrlPts]; | 153 points[2] = fCtrlPoints[kBottomP3_CubicCtrlPts]; |
137 points[3] = fCtrlPoints[kBottomP0_CubicCtrlPts]; | 154 points[3] = fCtrlPoints[kBottomP0_CubicCtrlPts]; |
138 } | 155 } |
139 | 156 |
| 157 const SkRect getBounds() const; |
| 158 |
140 const SkPoint* getControlPoints() const { | 159 const SkPoint* getControlPoints() const { |
141 return fCtrlPoints; | 160 return fCtrlPoints; |
142 } | 161 } |
143 | 162 |
144 const SkColor* getColors() const { | 163 void reset(const SkPoint points[12]) { |
145 return fCornerColors; | |
146 } | |
147 | |
148 void setPoints(const SkPoint points[12]) { | |
149 memcpy(fCtrlPoints, points, kNumCtrlPts * sizeof(SkPoint)); | 164 memcpy(fCtrlPoints, points, kNumCtrlPts * sizeof(SkPoint)); |
150 } | 165 } |
151 | |
152 void setColors(const SkColor colors[4]) { | |
153 memcpy(fCornerColors, colors, kNumColors * sizeof(SkColor)); | |
154 } | |
155 | |
156 void reset(const SkPoint points[12], const SkColor colors[4]) { | |
157 this->setPoints(points); | |
158 this->setColors(colors); | |
159 } | |
160 | 166 |
161 /** | 167 /** |
162 * Write the patch to the buffer, and return the number of bytes written. | 168 * Write the patch to the buffer, and return the number of bytes written. |
163 * If buffer is NULL, it still returns the number of bytes. | 169 * If buffer is NULL, it still returns the number of bytes. |
164 */ | 170 */ |
165 size_t writeToMemory(void* buffer) const; | 171 size_t writeToMemory(void* buffer) const; |
166 | 172 |
167 /** | 173 /** |
168 * Initializes the patch from the buffer | 174 * Initializes the patch from the buffer |
169 * | 175 * |
170 * buffer Memory to read from | 176 * buffer Memory to read from |
171 * length Amount of memory available in the buffer | 177 * length Amount of memory available in the buffer |
172 * returns the number of bytes read (must be a multiple of 4) or | 178 * returns the number of bytes read (must be a multiple of 4) or |
173 * 0 if there was not enough memory available | 179 * 0 if there was not enough memory available |
174 */ | 180 */ |
175 size_t readFromMemory(const void* buffer, size_t length); | 181 size_t readFromMemory(const void* buffer, size_t length); |
176 | 182 |
177 private: | 183 private: |
178 SkPoint fCtrlPoints[kNumCtrlPts]; | 184 SkPoint fCtrlPoints[kNumCtrlPts]; |
179 SkColor fCornerColors[kNumColors]; | 185 }; |
| 186 |
| 187 /** |
| 188 * Evaluator to sample the values of a cubic bezier using forward differences. |
| 189 * Forward differences is a method for evaluating a nth degree polynomial at a u
niform step by only |
| 190 * adding precalculated values. |
| 191 * For a linear example we have the function f(t) = m*t+b, then the value of tha
t function at t+h |
| 192 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must
add to the first |
| 193 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t
+ b = mh. After |
| 194 * obtaining this value (mh) we could just add this constant step to our first s
ampled point |
| 195 * to compute the next one. |
| 196 * |
| 197 * For the cubic case the first difference gives as a result a quadratic polynom
ial to which we can |
| 198 * apply again forward differences and get linear function to which we can apply
again forward |
| 199 * differences to get a constant difference. This is why we keep an array of siz
e 4, the 0th |
| 200 * position keeps the sampled value while the next ones keep the quadratic, line
ar and constant |
| 201 * difference values. |
| 202 */ |
| 203 |
| 204 class FwDCubicEvaluator { |
| 205 |
| 206 public: |
| 207 FwDCubicEvaluator(); |
| 208 |
| 209 /** |
| 210 * Receives the 4 control points of the cubic bezier. |
| 211 */ |
| 212 FwDCubicEvaluator(SkPoint a, SkPoint b, SkPoint c, SkPoint d); |
| 213 |
| 214 explicit FwDCubicEvaluator(const SkPoint points[4]); |
| 215 |
| 216 /** |
| 217 * Restarts the forward differences evaluator to the first value of t = 0. |
| 218 */ |
| 219 void restart(int divisions); |
| 220 |
| 221 /** |
| 222 * Check if the evaluator is still within the range of 0<=t<=1 |
| 223 */ |
| 224 bool done() const { |
| 225 return fCurrent > fMax; |
| 226 } |
| 227 |
| 228 /** |
| 229 * Call next to obtain the SkPoint sampled and move to the next one. |
| 230 */ |
| 231 SkPoint next() { |
| 232 SkPoint point = fFwDiff[0]; |
| 233 fFwDiff[0] += fFwDiff[1]; |
| 234 fFwDiff[1] += fFwDiff[2]; |
| 235 fFwDiff[2] += fFwDiff[3]; |
| 236 fCurrent++; |
| 237 return point; |
| 238 } |
| 239 |
| 240 const SkPoint* getCtrlPoints() const { |
| 241 return fPoints; |
| 242 } |
| 243 |
| 244 private: |
| 245 int fMax, fCurrent, fDivisions; |
| 246 SkPoint fFwDiff[4], fCoefs[4], fPoints[4]; |
180 }; | 247 }; |
181 | 248 |
182 #endif | 249 #endif |
OLD | NEW |