• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libs/graphics/effects/SkCamera.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #include "SkCamera.h"
19 
SkScalarDotDiv(int count,const SkScalar a[],int step_a,const SkScalar b[],int step_b,SkScalar denom)20 static SkScalar SkScalarDotDiv(int count, const SkScalar a[], int step_a,
21                                const SkScalar b[], int step_b,
22                                SkScalar denom)
23 {
24 #ifdef SK_SCALAR_IS_FLOAT
25     float prod = 0;
26     for (int i = 0; i < count; i++)
27     {
28         prod += a[0] * b[0];
29         a += step_a;
30         b += step_b;
31     }
32     return prod / denom;
33 #else
34     Sk64    prod, tmp;
35 
36     prod.set(0);
37     for (int i = 0; i < count; i++)
38     {
39         tmp.setMul(a[0], b[0]);
40         prod.add(tmp);
41         a += step_a;
42         b += step_b;
43     }
44     prod.div(denom, Sk64::kRound_DivOption);
45     return prod.get32();
46 #endif
47 }
48 
SkScalarDot(int count,const SkScalar a[],int step_a,const SkScalar b[],int step_b)49 static SkScalar SkScalarDot(int count, const SkScalar a[], int step_a,
50                                        const SkScalar b[], int step_b)
51 {
52 #ifdef SK_SCALAR_IS_FLOAT
53     float prod = 0;
54     for (int i = 0; i < count; i++)
55     {
56         prod += a[0] * b[0];
57         a += step_a;
58         b += step_b;
59     }
60     return prod;
61 #else
62     Sk64    prod, tmp;
63 
64     prod.set(0);
65     for (int i = 0; i < count; i++)
66     {
67         tmp.setMul(a[0], b[0]);
68         prod.add(tmp);
69         a += step_a;
70         b += step_b;
71     }
72     return prod.getFixed();
73 #endif
74 }
75 
76 //////////////////////////////////////////////////////////////////////////
77 
normalize(SkUnit3D * unit) const78 SkUnitScalar SkPoint3D::normalize(SkUnit3D* unit) const
79 {
80 #ifdef SK_SCALAR_IS_FLOAT
81     float mag = sk_float_sqrt(fX*fX + fY*fY + fZ*fZ);
82     if (mag)
83     {
84         float scale = 1.0f / mag;
85         unit->fX = fX * scale;
86         unit->fY = fY * scale;
87         unit->fZ = fZ * scale;
88     }
89 #else
90     Sk64    tmp1, tmp2;
91 
92     tmp1.setMul(fX, fX);
93     tmp2.setMul(fY, fY);
94     tmp1.add(tmp2);
95     tmp2.setMul(fZ, fZ);
96     tmp1.add(tmp2);
97 
98     SkFixed mag = tmp1.getSqrt();
99     if (mag)
100     {
101         // what if mag < SK_Fixed1 ??? we will underflow the fixdiv
102         SkFixed scale = SkFixedDiv(SK_Fract1, mag);
103         unit->fX = SkFixedMul(fX, scale);
104         unit->fY = SkFixedMul(fY, scale);
105         unit->fZ = SkFixedMul(fZ, scale);
106     }
107 #endif
108     return mag;
109 }
110 
Dot(const SkUnit3D & a,const SkUnit3D & b)111 SkUnitScalar SkUnit3D::Dot(const SkUnit3D& a, const SkUnit3D& b)
112 {
113     return  SkUnitScalarMul(a.fX, b.fX) +
114             SkUnitScalarMul(a.fY, b.fY) +
115             SkUnitScalarMul(a.fZ, b.fZ);
116 }
117 
Cross(const SkUnit3D & a,const SkUnit3D & b,SkUnit3D * cross)118 void SkUnit3D::Cross(const SkUnit3D& a, const SkUnit3D& b, SkUnit3D* cross)
119 {
120     SkASSERT(cross);
121 
122     // use x,y,z, in case &a == cross or &b == cross
123 
124 
125     SkScalar x = SkUnitScalarMul(a.fY, b.fZ) - SkUnitScalarMul(a.fZ, b.fY);
126     SkScalar y = SkUnitScalarMul(a.fZ, b.fX) - SkUnitScalarMul(a.fX, b.fY);
127     SkScalar z = SkUnitScalarMul(a.fX, b.fY) - SkUnitScalarMul(a.fY, b.fX);
128 
129     cross->set(x, y, z);
130 }
131 
132 ///////////////////////////////////////////////////////////////////////////
133 
SkPatch3D()134 SkPatch3D::SkPatch3D()
135 {
136     this->reset();
137 }
138 
reset()139 void SkPatch3D::reset()
140 {
141     fOrigin.set(0, 0, 0);
142     fU.set(SK_Scalar1, 0, 0);
143     fV.set(0, -SK_Scalar1, 0);
144 }
145 
transform(const SkMatrix3D & m,SkPatch3D * dst) const146 void SkPatch3D::transform(const SkMatrix3D& m, SkPatch3D* dst) const
147 {
148     if (dst == NULL)
149         dst = (SkPatch3D*)this;
150 
151     m.mapVector(fU, &dst->fU);
152     m.mapVector(fV, &dst->fV);
153     m.mapPoint(fOrigin, &dst->fOrigin);
154 }
155 
dotWith(SkScalar dx,SkScalar dy,SkScalar dz) const156 SkScalar SkPatch3D::dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const
157 {
158     SkScalar cx = SkScalarMul(fU.fY, fV.fZ) - SkScalarMul(fU.fZ, fV.fY);
159     SkScalar cy = SkScalarMul(fU.fZ, fV.fX) - SkScalarMul(fU.fX, fV.fY);
160     SkScalar cz = SkScalarMul(fU.fX, fV.fY) - SkScalarMul(fU.fY, fV.fX);
161 
162     return SkScalarMul(cx, dx) + SkScalarMul(cy, dy) + SkScalarMul(cz, dz);
163 }
164 
165 ///////////////////////////////////////////////////////////////////////////
166 
reset()167 void SkMatrix3D::reset()
168 {
169     memset(fMat, 0, sizeof(fMat));
170     fMat[0][0] = fMat[1][1] = fMat[2][2] = SK_Scalar1;
171 }
172 
setTranslate(SkScalar x,SkScalar y,SkScalar z)173 void SkMatrix3D::setTranslate(SkScalar x, SkScalar y, SkScalar z)
174 {
175     memset(fMat, 0, sizeof(fMat));
176     fMat[0][0] = x;
177     fMat[1][1] = y;
178     fMat[2][2] = z;
179 }
180 
setRotateX(SkScalar degX)181 void SkMatrix3D::setRotateX(SkScalar degX)
182 {
183     SkScalar    s, c;
184 
185     s = SkScalarSinCos(SkDegreesToRadians(degX), &c);
186     this->setRow(0, SK_Scalar1, 0, 0);
187     this->setRow(1, 0, c, -s);
188     this->setRow(2, 0, s, c);
189 }
190 
setRotateY(SkScalar degY)191 void SkMatrix3D::setRotateY(SkScalar degY)
192 {
193     SkScalar    s, c;
194 
195     s = SkScalarSinCos(SkDegreesToRadians(degY), &c);
196     this->setRow(0, c, 0, -s);
197     this->setRow(1, 0, SK_Scalar1, 0);
198     this->setRow(2, s, 0, c);
199 }
200 
setRotateZ(SkScalar degZ)201 void SkMatrix3D::setRotateZ(SkScalar degZ)
202 {
203     SkScalar    s, c;
204 
205     s = SkScalarSinCos(SkDegreesToRadians(degZ), &c);
206     this->setRow(0, c, -s, 0);
207     this->setRow(1, s, c, 0);
208     this->setRow(2, 0, 0, SK_Scalar1);
209 }
210 
preTranslate(SkScalar x,SkScalar y,SkScalar z)211 void SkMatrix3D::preTranslate(SkScalar x, SkScalar y, SkScalar z)
212 {
213     SkScalar col[3] = { x, y, z};
214 
215     for (int i = 0; i < 3; i++)
216         fMat[i][3] += SkScalarDot(3, &fMat[i][0], 1, col, 1);
217 }
218 
preRotateX(SkScalar degX)219 void SkMatrix3D::preRotateX(SkScalar degX)
220 {
221     SkMatrix3D m;
222     m.setRotateX(degX);
223     this->setConcat(*this, m);
224 }
225 
preRotateY(SkScalar degY)226 void SkMatrix3D::preRotateY(SkScalar degY)
227 {
228     SkMatrix3D m;
229     m.setRotateY(degY);
230     this->setConcat(*this, m);
231 }
232 
preRotateZ(SkScalar degZ)233 void SkMatrix3D::preRotateZ(SkScalar degZ)
234 {
235     SkMatrix3D m;
236     m.setRotateZ(degZ);
237     this->setConcat(*this, m);
238 }
239 
setConcat(const SkMatrix3D & a,const SkMatrix3D & b)240 void SkMatrix3D::setConcat(const SkMatrix3D& a, const SkMatrix3D& b)
241 {
242     SkMatrix3D  tmp;
243     SkMatrix3D* c = this;
244 
245     if (this == &a || this == &b)
246         c = &tmp;
247 
248     for (int i = 0; i < 3; i++) {
249         for (int j = 0; j < 3; j++)
250             c->fMat[i][j] = SkScalarDot(3, &a.fMat[i][0], 1, &b.fMat[0][j], 4);
251         c->fMat[i][3] = SkScalarDot(3, &a.fMat[i][0], 1, &b.fMat[0][3], 4) + a.fMat[i][3];
252     }
253 
254     if (c == &tmp)
255         *this = tmp;
256 }
257 
mapPoint(const SkPoint3D & src,SkPoint3D * dst) const258 void SkMatrix3D::mapPoint(const SkPoint3D& src, SkPoint3D* dst) const
259 {
260     SkScalar x = SkScalarDot(3, &fMat[0][0], 1, &src.fX, 1) + fMat[0][3];
261     SkScalar y = SkScalarDot(3, &fMat[1][0], 1, &src.fX, 1) + fMat[1][3];
262     SkScalar z = SkScalarDot(3, &fMat[2][0], 1, &src.fX, 1) + fMat[2][3];
263     dst->set(x, y, z);
264 }
265 
mapVector(const SkVector3D & src,SkVector3D * dst) const266 void SkMatrix3D::mapVector(const SkVector3D& src, SkVector3D* dst) const
267 {
268     SkScalar x = SkScalarDot(3, &fMat[0][0], 1, &src.fX, 1);
269     SkScalar y = SkScalarDot(3, &fMat[1][0], 1, &src.fX, 1);
270     SkScalar z = SkScalarDot(3, &fMat[2][0], 1, &src.fX, 1);
271     dst->set(x, y, z);
272 }
273 
274 ///////////////////////////////////////////////////////////////////////////
275 
SkCamera3D()276 SkCamera3D::SkCamera3D()
277 {
278     this->reset();
279 }
280 
reset()281 void SkCamera3D::reset()
282 {
283     fLocation.set(0, 0, -SkIntToScalar(576));   // 8 inches backward
284     fAxis.set(0, 0, SK_Scalar1);                // forward
285     fZenith.set(0, -SK_Scalar1, 0);             // up
286 
287     fObserver.set(0, 0, fLocation.fZ);
288 
289     fNeedToUpdate = true;
290 }
291 
update()292 void SkCamera3D::update()
293 {
294     fNeedToUpdate = true;
295 }
296 
doUpdate() const297 void SkCamera3D::doUpdate() const
298 {
299     SkUnit3D    axis, zenith, cross;
300 
301     fAxis.normalize(&axis);
302 
303     {
304         SkScalar dot = SkUnit3D::Dot(*(const SkUnit3D*)(const void*)&fZenith, axis);
305 
306         zenith.fX = fZenith.fX - SkUnitScalarMul(dot, axis.fX);
307         zenith.fY = fZenith.fY - SkUnitScalarMul(dot, axis.fY);
308         zenith.fZ = fZenith.fZ - SkUnitScalarMul(dot, axis.fZ);
309 
310         (void)((SkPoint3D*)(void*)&zenith)->normalize(&zenith);
311     }
312 
313     SkUnit3D::Cross(axis, zenith, &cross);
314 
315     {
316         SkMatrix* orien = &fOrientation;
317         SkScalar x = fObserver.fX;
318         SkScalar y = fObserver.fY;
319         SkScalar z = fObserver.fZ;
320 
321         orien->set(SkMatrix::kMScaleX, SkUnitScalarMul(x, axis.fX) - SkUnitScalarMul(z, cross.fX));
322         orien->set(SkMatrix::kMSkewX,  SkUnitScalarMul(x, axis.fY) - SkUnitScalarMul(z, cross.fY));
323         orien->set(SkMatrix::kMTransX, SkUnitScalarMul(x, axis.fZ) - SkUnitScalarMul(z, cross.fZ));
324         orien->set(SkMatrix::kMSkewY,  SkUnitScalarMul(y, axis.fX) - SkUnitScalarMul(z, zenith.fX));
325         orien->set(SkMatrix::kMScaleY, SkUnitScalarMul(y, axis.fY) - SkUnitScalarMul(z, zenith.fY));
326         orien->set(SkMatrix::kMTransY, SkUnitScalarMul(y, axis.fZ) - SkUnitScalarMul(z, zenith.fZ));
327         orien->set(SkMatrix::kMPersp0, axis.fX);
328         orien->set(SkMatrix::kMPersp1, axis.fY);
329         orien->set(SkMatrix::kMPersp2, axis.fZ);
330     }
331 }
332 
patchToMatrix(const SkPatch3D & quilt,SkMatrix * matrix) const333 void SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const
334 {
335     if (fNeedToUpdate)
336     {
337         this->doUpdate();
338         fNeedToUpdate = false;
339     }
340 
341     const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation;
342     const SkScalar* patchPtr;
343     SkPoint3D       diff;
344     SkScalar        dot;
345 
346     diff.fX = quilt.fOrigin.fX - fLocation.fX;
347     diff.fY = quilt.fOrigin.fY - fLocation.fY;
348     diff.fZ = quilt.fOrigin.fZ - fLocation.fZ;
349 
350     dot = SkUnit3D::Dot(*(const SkUnit3D*)(const void*)&diff,
351                         *(const SkUnit3D*)(((const SkScalar*)(const void*)&fOrientation) + 6));
352 
353     patchPtr = (const SkScalar*)&quilt;
354     matrix->set(SkMatrix::kMScaleX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
355     matrix->set(SkMatrix::kMSkewY,  SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
356     matrix->set(SkMatrix::kMPersp0, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
357 
358     patchPtr += 3;
359     matrix->set(SkMatrix::kMSkewX,  SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
360     matrix->set(SkMatrix::kMScaleY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
361     matrix->set(SkMatrix::kMPersp1, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
362 
363     patchPtr = (const SkScalar*)(const void*)&diff;
364     matrix->set(SkMatrix::kMTransX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
365     matrix->set(SkMatrix::kMTransY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
366     matrix->set(SkMatrix::kMPersp2, SK_UnitScalar1);
367 }
368 
369 ///////////////////////////////////////////////////////////////////////////////////////////////////
370 
Sk3DView()371 Sk3DView::Sk3DView()
372 {
373     fInitialRec.fMatrix.reset();
374     fRec = &fInitialRec;
375 }
376 
~Sk3DView()377 Sk3DView::~Sk3DView()
378 {
379     Rec* rec = fRec;
380     while (rec != &fInitialRec) {
381         Rec* next = rec->fNext;
382         SkDELETE(rec);
383         rec = next;
384     }
385 }
386 
save()387 void Sk3DView::save()
388 {
389     Rec* rec = SkNEW(Rec);
390     rec->fNext = fRec;
391     rec->fMatrix = fRec->fMatrix;
392     fRec = rec;
393 }
394 
restore()395 void Sk3DView::restore()
396 {
397     SkASSERT(fRec != &fInitialRec);
398     Rec* next = fRec->fNext;
399     SkDELETE(fRec);
400     fRec = next;
401 }
402 
translate(SkScalar x,SkScalar y,SkScalar z)403 void Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z)
404 {
405     fRec->fMatrix.preTranslate(x, y, z);
406 }
407 
rotateX(SkScalar deg)408 void Sk3DView::rotateX(SkScalar deg)
409 {
410     fRec->fMatrix.preRotateX(deg);
411 }
412 
rotateY(SkScalar deg)413 void Sk3DView::rotateY(SkScalar deg)
414 {
415     fRec->fMatrix.preRotateY(deg);
416 }
417 
rotateZ(SkScalar deg)418 void Sk3DView::rotateZ(SkScalar deg)
419 {
420     fRec->fMatrix.preRotateZ(deg);
421 }
422 
dotWithNormal(SkScalar x,SkScalar y,SkScalar z) const423 SkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const
424 {
425     SkPatch3D   patch;
426     patch.transform(fRec->fMatrix);
427     return patch.dotWith(x, y, z);
428 }
429 
getMatrix(SkMatrix * matrix) const430 void Sk3DView::getMatrix(SkMatrix* matrix) const
431 {
432     if (matrix != NULL)
433     {
434         SkPatch3D   patch;
435         patch.transform(fRec->fMatrix);
436         fCamera.patchToMatrix(patch, matrix);
437     }
438 }
439 
440 #include "SkCanvas.h"
441 
applyToCanvas(SkCanvas * canvas) const442 void Sk3DView::applyToCanvas(SkCanvas* canvas) const
443 {
444     SkMatrix    matrix;
445 
446     this->getMatrix(&matrix);
447     canvas->concat(matrix);
448 }
449 
450