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

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

Issue 405163003: SkPatch abstraction (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Moved ownership of vertex data out of SkPatch 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
« include/core/SkPatch.h ('K') | « 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 "SkGr.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){
bsalomon 2014/07/25 13:14:35 tiny nit here and a few other places, space betwee
dandov 2014/07/25 14:08:14 Done.
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); //6ah^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 inline bool done() {
bsalomon 2014/07/25 13:14:35 const?
dandov 2014/07/25 14:08:14 Done. Also set to const the return value of the ne
79 return fCurrent <= fMax;
80 }
81
82 /**
83 * Call next to obtain the SkPoint sampled and move to the next one.
84 */
85 inline 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() {
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], int divisions)
106 : fDivX(divisions)
bsalomon 2014/07/25 13:14:34 Maybe SkPatch shouldn't store the divisions but sh
dandov 2014/07/25 14:08:14 Yeah I think it makes more sense that way.
107 , fDivY(divisions) {
108
109 for (int i = 0; i<12; i++) {
110 fCtrlPoints[i] = points[i];
111 }
112
113 fCornerColors[0] = SkPreMultiplyColor(colors[0]);
114 fCornerColors[1] = SkPreMultiplyColor(colors[1]);
115 fCornerColors[2] = SkPreMultiplyColor(colors[2]);
116 fCornerColors[3] = SkPreMultiplyColor(colors[3]);
117 }
118
119 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, SkScalar c11) {
120 SkScalar a = c00 * (1.f - tx) + c10 * tx;
121 SkScalar b = c01 * (1.f - tx) + c11 * tx;
122 return uint8_t(a * (1.f - ty) + b * ty);
123 }
124
125 void SkPatch::getVertexData(SkPatch::PatchData* data) {
126
127 data->fVertCount = (fDivX + 1) * (fDivY + 1);
128 data->fIndexCount = fDivX * fDivY * 6;
129
130 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertCount);
131 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertCount);
132 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertCount);
133 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount);
134
135 FwDCubicEvaluator fBottom(fCtrlPoints[0], fCtrlPoints[1], fCtrlPoints[2], fC trlPoints[3]),
136 fTop(fCtrlPoints[9], fCtrlPoints[8], fCtrlPoints[7], fCtrlPoints[6]),
137 fLeft(fCtrlPoints[0], fCtrlPoints[11], fCtrlPoints[10], fCtrlPoints[9]),
138 fRight(fCtrlPoints[3], fCtrlPoints[4], fCtrlPoints[5], fCtrlPoints[6]);
139
140 fBottom.restart(fDivX);
141 fTop.restart(fDivX);
142
143 SkScalar u = 0.0f;
144 int stride = fDivY+1;
145 for (int x = 0; x <= fDivX; x++) {
146 SkPoint bottom = fBottom.next(), top = fTop.next();
147 fLeft.restart(fDivY);
148 fRight.restart(fDivY);
149 SkScalar v = 0.f;
150 for (int y = 0; y <= fDivY; y++) {
151 int dataIndex = x*(fDivX + 1) + y;
152
153 SkPoint left = fLeft.next(), right = fRight.next();
154
155 SkPoint s0 = SkPoint::Make((1.0f - v)*bottom.x() + v*top.x(),
156 (1.0f - v)*bottom.y() + v*top.y());
157 SkPoint s1 = SkPoint::Make((1.0f - u)*left.x() + u*right.x(),
158 (1.0f - u)*left.y() + u*right.y());
159 SkPoint s2 = SkPoint::Make(
160 (1.0f - v)*((1.0f - u)*fBottom.getCtrlPoints()[0].x()
161 + u*fBottom.getCtrlPoints()[3].x())
162 + v*((1.0f - u)*fTop.getCtrlPoints()[0].x() + u*fTop .getCtrlPoints()[3].x()),
163 (1.0f - v)*((1.0f - u)*fBottom.getCtrlPoints()[0].y()
164 + u*fBottom.getCtrlPoints()[3].y())
165 + v*((1.0f - u)*fTop.getCtrlPoints()[0].y() + u*fTop .getCtrlPoints()[3].y()));
166 data->fPoints[dataIndex] = s0 + s1 - s2;
167
168 uint8_t a = bilinear(u, v,
169 SkScalar(SkColorGetA(fCornerColors[0])),
170 SkScalar(SkColorGetA(fCornerColors[1])),
171 SkScalar(SkColorGetA(fCornerColors[2])),
172 SkScalar(SkColorGetA(fCornerColors[3])));
173 uint8_t r = bilinear(u, v,
174 SkScalar(SkColorGetR(fCornerColors[0])),
175 SkScalar(SkColorGetR(fCornerColors[1])),
176 SkScalar(SkColorGetR(fCornerColors[2])),
177 SkScalar(SkColorGetR(fCornerColors[3])));
178 uint8_t g = bilinear(u, v,
179 SkScalar(SkColorGetG(fCornerColors[0])),
180 SkScalar(SkColorGetG(fCornerColors[1])),
181 SkScalar(SkColorGetG(fCornerColors[2])),
182 SkScalar(SkColorGetG(fCornerColors[3])));
183 uint8_t b = bilinear(u, v,
184 SkScalar(SkColorGetB(fCornerColors[0])),
185 SkScalar(SkColorGetB(fCornerColors[1])),
186 SkScalar(SkColorGetB(fCornerColors[2])),
187 SkScalar(SkColorGetB(fCornerColors[3])));
188 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b);
189
190
191 data->fTexCoords[dataIndex] = SkPoint::Make(u, v);
192
193 if(x < fDivX && y < fDivY) {
194 int i = 6*(x*fDivY + y);
195 data->fIndices[i] = x*stride+y;
196 data->fIndices[i+1] = x*stride+1+y;
197 data->fIndices[i+2] = (x+1)*stride+1+y;
198 data->fIndices[i+3] = data->fIndices[i];
199 data->fIndices[i+4] = data->fIndices[i+2];
200 data->fIndices[i+5] = (x+1)*stride+y;
201 }
202 v+=1.f/fDivY;
203 }
204 u+=1.f/fDivX;
205 }
206 }
OLDNEW
« include/core/SkPatch.h ('K') | « include/core/SkPatch.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698