Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: src/core/SkPatch.cpp

Issue 405163003: SkPatch abstraction (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Skip tiled flag Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/core/SkPatch.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "SkPatch.h"
9
10 #include "SkGeometry.h"
11 #include "SkColorPriv.h"
12
13 ////////////////////////////////////////////////////////////////////////////////
14
15 /**
16 * Evaluator to sample the values of a cubic bezier using forward differences.
17 * Forward differences is a method for evaluating a nth degree polynomial at a u niform step by only
18 * adding precalculated values.
19 * For a linear example we have the function f(t) = m*t+b, then the value of tha t function at t+h
20 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first
21 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After
22 * obtaining this value (mh) we could just add this constant step to our first s ampled point
23 * to compute the next one.
24 *
25 * For the cubic case the first difference gives as a result a quadratic polynom ial to which we can
26 * apply again forward differences and get linear function to which we can apply again forward
27 * differences to get a constant difference. This is why we keep an array of siz e 4, the 0th
28 * position keeps the sampled value while the next ones keep the quadratic, line ar and constant
29 * difference values.
30 */
31
32 class FwDCubicEvaluator {
33
34 public:
35 FwDCubicEvaluator() { }
36
37 /**
38 * Receives the 4 control points of the cubic bezier.
39 */
40 FwDCubicEvaluator(SkPoint a, SkPoint b, SkPoint c, SkPoint d) {
41 fPoints[0] = a;
42 fPoints[1] = b;
43 fPoints[2] = c;
44 fPoints[3] = d;
45
46 SkScalar cx[4], cy[4];
47 SkGetCubicCoeff(fPoints, cx, cy);
48 fCoefs[0].set(cx[0], cy[0]);
49 fCoefs[1].set(cx[1], cy[1]);
50 fCoefs[2].set(cx[2], cy[2]);
51 fCoefs[3].set(cx[3], cy[3]);
52
53 this->restart(1);
54 }
55
56 /**
57 * Restarts the forward differences evaluator to the first value of t = 0.
58 */
59 void restart(int divisions) {
60 fDivisions = divisions;
61 SkScalar h = 1.f / fDivisions;
62 fCurrent = 0;
63 fMax = fDivisions + 1;
64 fFwDiff[0] = fCoefs[3];
65 SkScalar h2 = h * h;
66 SkScalar h3 = h2 * h;
67
68 fFwDiff[3].set(6.f * fCoefs[0].x() * h3, 6.f * fCoefs[0].y() * h3); //6a h^3
69 fFwDiff[2].set(fFwDiff[3].x() + 2.f * fCoefs[1].x() * h2, //6ah^3 + 2bh^ 2
70 fFwDiff[3].y() + 2.f * fCoefs[1].y() * h2);
71 fFwDiff[1].set(fCoefs[0].x() * h3 + fCoefs[1].x() * h2 + fCoefs[2].x() * h,//ah^3 + bh^2 +ch
72 fCoefs[0].y() * h3 + fCoefs[1].y() * h2 + fCoefs[2].y() * h);
73 }
74
75 /**
76 * Check if the evaluator is still within the range of 0<=t<=1
77 */
78 bool done() const {
79 return fCurrent > fMax;
80 }
81
82 /**
83 * Call next to obtain the SkPoint sampled and move to the next one.
84 */
85 SkPoint next() {
86 SkPoint point = fFwDiff[0];
87 fFwDiff[0] += fFwDiff[1];
88 fFwDiff[1] += fFwDiff[2];
89 fFwDiff[2] += fFwDiff[3];
90 fCurrent++;
91 return point;
92 }
93
94 const SkPoint* getCtrlPoints() const {
95 return fPoints;
96 }
97
98 private:
99 int fMax, fCurrent, fDivisions;
100 SkPoint fFwDiff[4], fCoefs[4], fPoints[4];
101 };
102
103 ////////////////////////////////////////////////////////////////////////////////
104
105 SkPatch::SkPatch(SkPoint points[12], SkColor colors[4]) {
106
107 for (int i = 0; i<12; i++) {
108 fCtrlPoints[i] = points[i];
109 }
110
111 fCornerColors[0] = SkPreMultiplyColor(colors[0]);
112 fCornerColors[1] = SkPreMultiplyColor(colors[1]);
113 fCornerColors[2] = SkPreMultiplyColor(colors[2]);
114 fCornerColors[3] = SkPreMultiplyColor(colors[3]);
115 }
116
117 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, SkScalar c11) {
118 SkScalar a = c00 * (1.f - tx) + c10 * tx;
119 SkScalar b = c01 * (1.f - tx) + c11 * tx;
120 return uint8_t(a * (1.f - ty) + b * ty);
121 }
122
123 bool SkPatch::getVertexData(SkPatch::VertexData* data, int divisions) {
124
125 if (divisions < 1) {
126 return false;
127 }
128
129 int divX = divisions, divY = divisions;
130
131 data->fVertexCount = (divX + 1) * (divY + 1);
132 data->fIndexCount = divX * divY * 6;
133
134 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount);
135 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount);
136 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount);
137 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount);
138
139 FwDCubicEvaluator fBottom(fCtrlPoints[kBottomP0_CubicCtrlPts],
140 fCtrlPoints[kBottomP1_CubicCtrlPts],
141 fCtrlPoints[kBottomP2_CubicCtrlPts],
142 fCtrlPoints[kBottomP3_CubicCtrlPts]),
143 fTop(fCtrlPoints[kTopP0_CubicCtrlPts],
144 fCtrlPoints[kTopP1_CubicCtrlPts],
145 fCtrlPoints[kTopP2_CubicCtrlPts],
146 fCtrlPoints[kTopP2_CubicCtrlPts]),
147 fLeft(fCtrlPoints[kLeftP0_CubicCtrlPts],
148 fCtrlPoints[kLeftP1_CubicCtrlPts],
149 fCtrlPoints[kLeftP2_CubicCtrlPts],
150 fCtrlPoints[kLeftP3_CubicCtrlPts]),
151 fRight(fCtrlPoints[kRightP0_CubicCtrlPts],
152 fCtrlPoints[kRightP1_CubicCtrlPts],
153 fCtrlPoints[kRightP2_CubicCtrlPts],
154 fCtrlPoints[kRightP3_CubicCtrlPts]);
155
156 fBottom.restart(divX);
157 fTop.restart(divX);
158
159 SkScalar u = 0.0f;
160 int stride = divY + 1;
161 for (int x = 0; x <= divX; x++) {
162 SkPoint bottom = fBottom.next(), top = fTop.next();
163 fLeft.restart(divY);
164 fRight.restart(divY);
165 SkScalar v = 0.f;
166 for (int y = 0; y <= divY; y++) {
167 int dataIndex = x * (divX + 1) + y;
168
169 SkPoint left = fLeft.next(), right = fRight.next();
170
171 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
172 (1.0f - v) * top.y() + v * bottom.y());
173 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
174 (1.0f - u) * left.y() + u * right.y());
175 SkPoint s2 = SkPoint::Make(
176 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo ints()[0].x()
177 + u * fTop.getCtrlPoints()[3].x())
178 + v * ((1.0f - u) * fBottom.getCtrlPoint s()[0].x()
179 + u * fBottom.getCtrlPoints()[3].x()),
180 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo ints()[0].y()
181 + u * fTop.getCtrlPoints()[3].y())
182 + v * ((1.0f - u) * fBottom.getCtrlPoint s()[0].y()
183 + u * fBottom.getCtrlPoints()[3].y()));
184 data->fPoints[dataIndex] = s0 + s1 - s2;
185
186 uint8_t a = bilinear(u, v,
187 SkScalar(SkColorGetA(fCornerColors[0])),
188 SkScalar(SkColorGetA(fCornerColors[1])),
189 SkScalar(SkColorGetA(fCornerColors[2])),
190 SkScalar(SkColorGetA(fCornerColors[3])));
191 uint8_t r = bilinear(u, v,
192 SkScalar(SkColorGetR(fCornerColors[0])),
193 SkScalar(SkColorGetR(fCornerColors[1])),
194 SkScalar(SkColorGetR(fCornerColors[2])),
195 SkScalar(SkColorGetR(fCornerColors[3])));
196 uint8_t g = bilinear(u, v,
197 SkScalar(SkColorGetG(fCornerColors[0])),
198 SkScalar(SkColorGetG(fCornerColors[1])),
199 SkScalar(SkColorGetG(fCornerColors[2])),
200 SkScalar(SkColorGetG(fCornerColors[3])));
201 uint8_t b = bilinear(u, v,
202 SkScalar(SkColorGetB(fCornerColors[0])),
203 SkScalar(SkColorGetB(fCornerColors[1])),
204 SkScalar(SkColorGetB(fCornerColors[2])),
205 SkScalar(SkColorGetB(fCornerColors[3])));
206 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b);
207
208 data->fTexCoords[dataIndex] = SkPoint::Make(u, v);
209
210 if(x < divX && y < divY) {
211 int i = 6 * (x * divY + y);
212 data->fIndices[i] = x * stride + y;
213 data->fIndices[i + 1] = x * stride + 1 + y;
214 data->fIndices[i + 2] = (x + 1) * stride + 1 + y;
215 data->fIndices[i + 3] = data->fIndices[i];
216 data->fIndices[i + 4] = data->fIndices[i + 2];
217 data->fIndices[i + 5] = (x + 1) * stride + y;
218 }
219 v += 1.f / divY;
220 }
221 u += 1.f / divX;
222 }
223 return true;
224 }
OLDNEW
« no previous file with comments | « include/core/SkPatch.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698