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

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

Issue 405163003: SkPatch abstraction (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Adjusted names and comments for more readability 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 SkFwDCubicEvaluator::SkFwDCubicEvaluator(SkPoint a, SkPoint b, SkPoint c, SkPoin t d){
17 fPoints[0] = a;
18 fPoints[1] = b;
19 fPoints[2] = c;
20 fPoints[3] = d;
21
22 SkScalar cx[4], cy[4];
23 SkGetCubicCoeff(fPoints, cx, cy);
24 fCoefs[0].set(cx[0], cy[0]);
25 fCoefs[1].set(cx[1], cy[1]);
26 fCoefs[2].set(cx[2], cy[2]);
27 fCoefs[3].set(cx[3], cy[3]);
28
29 this->reset(1);
30 }
31
32 void SkFwDCubicEvaluator::reset(int divisions){
33 fDivisions = divisions;
34 SkScalar h = 1.f/fDivisions;
35 fCurrent = 0;
36 fMax = fDivisions + 1;
37 fFwDiff[0] = fCoefs[3];
38 SkScalar h2 = h*h;
39 SkScalar h3 = h2*h;
40
41 fFwDiff[3].set(6.f*fCoefs[0].x()*h3, 6.f*fCoefs[0].y()*h3); //6ah^3
42 fFwDiff[2].set(fFwDiff[3].x() + 2.f*fCoefs[1].x()*h2, //6ah^3 + 2bh^2
43 fFwDiff[3].y() + 2.f*fCoefs[1].y()*h2);
44 fFwDiff[1].set(fCoefs[0].x()*h3 + fCoefs[1].x()*h2 + fCoefs[2].x()*h, //ah^3 + bh^2 + ch
45 fCoefs[0].y()*h3 + fCoefs[1].y()*h2 + fCoefs[2].y()*h);
46 }
47
48 const SkPoint* SkFwDCubicEvaluator::getPoints() {
49 return fPoints;
50 }
51
52 ////////////////////////////////////////////////////////////////////////////////
53
54 SkPatch::SkPatch(SkPoint points[12], SkColor colors[4], int divisions)
55 : fCtrlPoints(points)
56 , fBottom(points[0], points[1], points[2], points[3])
57 , fTop(points[4], points[5], points[6], points[7])
58 , fLeft(points[0], points[8], points[9], points[4])
59 , fRight(points[3], points[10], points[11], points[7])
60 , fDivX(divisions)
61 , fDivY(divisions)
62 , fVertCount(0)
63 , fIndexCount(0)
64 , fPoints(NULL)
65 , fTexCoords(NULL)
66 , fColors(NULL)
67 , fIndices(NULL)
68 , fIsSet(false) {
69 fCornerColors[0] = SkPreMultiplyColor(colors[0]);
dandov 2014/07/22 20:23:28 will fix this indent in the next set of changes
70 fCornerColors[1] = SkPreMultiplyColor(colors[1]);
71 fCornerColors[2] = SkPreMultiplyColor(colors[2]);
72 fCornerColors[3] = SkPreMultiplyColor(colors[3]);
73 }
74
75 void SkPatch::clean() {
76 SkDELETE_ARRAY (fPoints);
bsalomon 2014/07/24 18:10:23 remove space between ARRAY and (
77 SkDELETE_ARRAY (fTexCoords);
78 SkDELETE_ARRAY (fColors);
79 SkDELETE_ARRAY (fIndices);
80 fVertCount = 0;
81 fIndexCount = 0;
82 fPoints = NULL;
83 fTexCoords = NULL;
84 fColors = NULL;
85 fIndices = NULL;
86 fIsSet = false;
87 }
88
89 SkPatch::~SkPatch() {
90 this->clean();
91 }
92
93 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, SkScalar c11) {
94 SkScalar a = c00 * (1.f - tx) + c10 * tx;
95 SkScalar b = c01 * (1.f - tx) + c11 * tx;
96 return uint8_t(a * (1.f - ty) + b * ty);
97 }
98
99 void SkPatch::setData() {
100
101 if (fIsSet) {
102 return;
103 }
104
105 fIsSet = true;
106 fVertCount = (fDivX + 1) * (fDivY + 1);
107 fIndexCount = fDivX * fDivY * 6;
108
109 fPoints = SkNEW_ARRAY(SkPoint, fVertCount);
110 fColors = SkNEW_ARRAY(uint32_t, fVertCount);
111 fTexCoords = SkNEW_ARRAY(SkPoint, fVertCount);
112 fIndices = SkNEW_ARRAY(uint16_t, fIndexCount);
113
114 fBottom.reset(fDivX);
115 fTop.reset(fDivX);
116
117 SkScalar u = 0.0f;
118 int stride = fDivY+1;
119 for (int x = 0; x <= fDivX; x++) {
120 SkPoint bottom = fBottom++, top = fTop++;
121 fLeft.reset(fDivY);
122 fRight.reset(fDivY);
123 SkScalar v = 0.f;
124 for (int y = 0; y <= fDivY; y++) {
125 int dataIndex = x*(fDivX + 1) + y;
126
127 SkPoint left = fLeft++, right = fRight++;
128
129 SkPoint s0 = SkPoint::Make((1.0f - v)*bottom.x() + v*top.x(),
130 (1.0f - v)*bottom.y() + v*top.y());
131 SkPoint s1 = SkPoint::Make((1.0f - u)*left.x() + u*right.x(),
132 (1.0f - u)*left.y() + u*right.y());
133 SkPoint s2 = SkPoint::Make(
134 (1.0f - v)*((1.0f - u)*fBottom.getPoints()[0].x()
135 + u*fBottom.getPoints()[3].x())
136 + v*((1.0f - u)*fTop.getPoints()[0].x() + u*fTop.get Points()[3].x()),
137 (1.0f - v)*((1.0f - u)*fBottom.getPoints()[0].y()
138 + u*fBottom.getPoints()[3].y())
139 + v*((1.0f - u)*fTop.getPoints()[0].y() + u*fTop.get Points()[3].y()));
140 fPoints[dataIndex] = s0 + s1 - s2;
141
142 uint8_t a = bilinear(u, v,
143 SkScalar(SkColorGetA(fCornerColors[0])),
144 SkScalar(SkColorGetA(fCornerColors[1])),
145 SkScalar(SkColorGetA(fCornerColors[2])),
146 SkScalar(SkColorGetA(fCornerColors[3])));
147 uint8_t r = bilinear(u, v,
148 SkScalar(SkColorGetR(fCornerColors[0])),
149 SkScalar(SkColorGetR(fCornerColors[1])),
150 SkScalar(SkColorGetR(fCornerColors[2])),
151 SkScalar(SkColorGetR(fCornerColors[3])));
152 uint8_t g = bilinear(u, v,
153 SkScalar(SkColorGetG(fCornerColors[0])),
154 SkScalar(SkColorGetG(fCornerColors[1])),
155 SkScalar(SkColorGetG(fCornerColors[2])),
156 SkScalar(SkColorGetG(fCornerColors[3])));
157 uint8_t b = bilinear(u, v,
158 SkScalar(SkColorGetB(fCornerColors[0])),
159 SkScalar(SkColorGetB(fCornerColors[1])),
160 SkScalar(SkColorGetB(fCornerColors[2])),
161 SkScalar(SkColorGetB(fCornerColors[3])));
162 fColors[dataIndex] = SkPackARGB32(a,r,g,b);
163
164
165 fTexCoords[dataIndex] = SkPoint::Make(u, v);
166
167 if(x < fDivX && y < fDivY) {
168 int i = 6*(x*fDivY + y);
169 fIndices[i] = x*stride+y;
170 fIndices[i+1] = x*stride+1+y;
171 fIndices[i+2] = (x+1)*stride+1+y;
172 fIndices[i+3] = fIndices[i];
173 fIndices[i+4] = fIndices[i+2];
174 fIndices[i+5] = (x+1)*stride+y;
175 }
176 v+=1.f/fDivY;
177 }
178 u+=1.f/fDivX;
179 }
180 }
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