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

Side by Side Diff: src/utils/SkShadowPaintFilterCanvas.cpp

Issue 2294323003: made point light shadows (Closed)
Patch Set: Created 4 years, 3 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
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 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 #include "SkPathEffect.h" 8 #include "SkPathEffect.h"
9 #include "SkShadowPaintFilterCanvas.h" 9 #include "SkShadowPaintFilterCanvas.h"
10 10
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 *paint->writable() = newPaint; 45 *paint->writable() = newPaint;
46 } 46 }
47 47
48 return true; 48 return true;
49 } 49 }
50 50
51 SkISize SkShadowPaintFilterCanvas::ComputeDepthMapSize(const SkLights::Light& li ght, int maxDepth, 51 SkISize SkShadowPaintFilterCanvas::ComputeDepthMapSize(const SkLights::Light& li ght, int maxDepth,
52 int width, int height) { 52 int width, int height) {
53 if (light.type() != SkLights::Light::kDirectional_LightType) { 53 if (light.type() != SkLights::Light::kDirectional_LightType) {
54 return SkISize::Make(width *2 , height * 2); 54 return SkISize::Make(width * 2, height * 2);
55 } 55 }
56 56
57 int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + width, 57 int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + width,
58 width * 2); 58 width * 2);
59 int dMapHeight = SkMin32(maxDepth * fabs(light.dir().fY) + height, 59 int dMapHeight = SkMin32(maxDepth * fabs(light.dir().fY) + height,
60 height * 2); 60 height * 2);
61 return SkISize::Make(dMapWidth, dMapHeight); 61 return SkISize::Make(dMapWidth, dMapHeight);
62 } 62 }
63 63
64 void SkShadowPaintFilterCanvas::setShadowParams(const SkShadowParams &params) { 64 void SkShadowPaintFilterCanvas::setShadowParams(const SkShadowParams &params) {
65 fShadowParams = params; 65 fShadowParams = params;
66 } 66 }
67 67
68 void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const Sk Matrix *matrix, 68 void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const Sk Matrix *matrix,
69 const SkPaint *paint) { 69 const SkPaint *paint) {
70 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); 70 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint);
71 if (this->onFilter(&filteredPaint, kPicture_Type)) { 71 if (this->onFilter(&filteredPaint, kPicture_Type)) {
72 SkCanvas::onDrawPicture(picture, matrix, filteredPaint); 72 SkCanvas::onDrawPicture(picture, matrix, filteredPaint);
73 } 73 }
74 } 74 }
75 75
robertphillips 2016/08/31 22:18:19 const SkRect&
vjiaoblack 2016/09/01 19:22:47 Done.
76 void SkShadowPaintFilterCanvas::updateMatrix() { 76 void SkShadowPaintFilterCanvas::updateMatrix(const SkRect boundRect) {
77 this->save(); 77 this->save();
78 78
79 // It is up to the user to set the 0th light in fLights to 79 // It is up to the user to set the 0th light in fLights to
80 // the light the want to render the depth map with. 80 // the light the want to render the depth map with.
81 if (this->fLights->light(0).type() == SkLights::Light::kDirectional_LightTyp e) { 81 if (this->fLights->light(0).type() == SkLights::Light::kDirectional_LightTyp e) {
82 const SkVector3& lightDir = this->fLights->light(0).dir(); 82 const SkVector3& lightDir = this->fLights->light(0).dir();
83 SkScalar x = lightDir.fX * this->getZ(); 83 SkScalar x = lightDir.fX * this->getZ();
84 SkScalar y = lightDir.fY * this->getZ(); 84 SkScalar y = lightDir.fY * this->getZ();
85 85
86 this->translate(x, y); 86 this->translate(x, y);
87 } else if (this->fLights->light(0).type() == SkLights::Light::kPoint_LightTy pe) {
robertphillips 2016/08/31 22:18:19 How does all this interact with the CTM and saveLa
vjiaoblack 2016/09/01 19:22:47 I'll test it.
88 const SkPoint3& lightPos = this->fLights->light(0).pos();
89
90 SkScalar scale = (lightPos.fZ) / (lightPos.fZ - this->getZ());
91
92 this->translate(-boundRect.centerX() * scale, -boundRect.centerY() * sca le);
93 this->scale(scale, scale);
94 this->translate(boundRect.centerX() / scale, boundRect.centerY() / scale );
95
robertphillips 2016/08/31 22:18:19 Why 400?
vjiaoblack 2016/09/01 19:22:48 because it's the current height of the diffuse map
96 // pass in 400 as height
97 this->translate((boundRect.centerX() - lightPos.fX) *
98 (this->getZ() / (lightPos.fZ - this->getZ())) / scale,
99 - ((400 - boundRect.centerY()) - lightPos.fY) *
100 (this->getZ() / (lightPos.fZ - this->getZ())) / scale);
87 } 101 }
88 } 102 }
89 103
90 void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) { 104 void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) {
91 this->updateMatrix(); 105 SkISize size = this->getBaseLayerSize();
106 this->updateMatrix(SkRect::MakeIWH(size.fWidth, size.fHeight));
92 this->INHERITED::onDrawPaint(paint); 107 this->INHERITED::onDrawPaint(paint);
93 this->restore(); 108 this->restore();
94 } 109 }
95 110
96 void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], 111 void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
97 const SkPaint &paint) { 112 const SkPaint &paint) {
98 this->updateMatrix(); 113 SkISize size = this->getBaseLayerSize();
114 this->updateMatrix(SkRect::MakeIWH(size.fWidth, size.fHeight));
99 this->INHERITED::onDrawPoints(mode, count, pts, paint); 115 this->INHERITED::onDrawPoints(mode, count, pts, paint);
100 this->restore(); 116 this->restore();
101 } 117 }
102 118
103 void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &pa int) { 119 void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &pa int) {
104 this->updateMatrix(); 120 this->updateMatrix(rect);
105 this->INHERITED::onDrawRect(rect, paint); 121 this->INHERITED::onDrawRect(rect, paint);
106 this->restore(); 122 this->restore();
107 } 123 }
108 124
109 void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) { 125 void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) {
110 this->updateMatrix(); 126 this->updateMatrix(rrect.getBounds());
111 this->INHERITED::onDrawRRect(rrect, paint); 127 this->INHERITED::onDrawRRect(rrect, paint);
112 this->restore(); 128 this->restore();
113 } 129 }
114 130
115 void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner, 131 void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner,
116 const SkPaint &paint) { 132 const SkPaint &paint) {
117 this->updateMatrix(); 133 this->updateMatrix(outer.getBounds());
118 this->INHERITED::onDrawDRRect(outer, inner, paint); 134 this->INHERITED::onDrawDRRect(outer, inner, paint);
119 this->restore(); 135 this->restore();
120 } 136 }
121 137
122 void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &pa int) { 138 void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &pa int) {
123 this->updateMatrix(); 139 this->updateMatrix(rect);
124 this->INHERITED::onDrawOval(rect, paint); 140 this->INHERITED::onDrawOval(rect, paint);
125 this->restore(); 141 this->restore();
126 } 142 }
127 143
128 void SkShadowPaintFilterCanvas::onDrawArc(const SkRect &rect, SkScalar startAngl e, 144 void SkShadowPaintFilterCanvas::onDrawArc(const SkRect &rect, SkScalar startAngl e,
129 SkScalar sweepAngle, bool useCenter, 145 SkScalar sweepAngle, bool useCenter,
130 const SkPaint &paint) { 146 const SkPaint &paint) {
131 this->updateMatrix(); 147 this->updateMatrix(rect);
132 this->INHERITED::onDrawArc(rect, startAngle, sweepAngle, useCenter, paint); 148 this->INHERITED::onDrawArc(rect, startAngle, sweepAngle, useCenter, paint);
133 this->restore(); 149 this->restore();
134 } 150 }
135 151
136 void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &pa int) { 152 void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &pa int) {
137 this->updateMatrix(); 153 this->updateMatrix(path.getBounds());
138 this->INHERITED::onDrawPath(path, paint); 154 this->INHERITED::onDrawPath(path, paint);
139 this->restore(); 155 this->restore();
140 } 156 }
141 157
142 void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top, 158 void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top,
143 const SkPaint *paint) { 159 const SkPaint *paint) {
144 this->updateMatrix(); 160 this->updateMatrix(SkRect::MakeXYWH(left, top, bm.width(), bm.height()));
145 this->INHERITED::onDrawBitmap(bm, left, top, paint); 161 this->INHERITED::onDrawBitmap(bm, left, top, paint);
146 this->restore(); 162 this->restore();
147 } 163 }
148 164
149 void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRec t *src, 165 void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRec t *src,
150 const SkRect &dst, const SkPain t *paint, 166 const SkRect &dst, const SkPain t *paint,
151 SrcRectConstraint constraint) { 167 SrcRectConstraint constraint) {
152 this->updateMatrix(); 168 this->updateMatrix(dst);
153 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); 169 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint);
154 this->restore(); 170 this->restore();
155 } 171 }
156 172
157 void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRe ct &center, 173 void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRe ct &center,
158 const SkRect &dst, const SkPain t *paint) { 174 const SkRect &dst, const SkPain t *paint) {
159 this->updateMatrix(); 175 this->updateMatrix(dst);
160 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); 176 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint);
161 this->restore(); 177 this->restore();
162 } 178 }
163 179
164 void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left, 180 void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left,
165 SkScalar top, const SkPaint *paint) { 181 SkScalar top, const SkPaint *paint) {
166 this->updateMatrix(); 182 this->updateMatrix(SkRect::MakeXYWH(left, top, image->width(), image->height ()));
167 this->INHERITED::onDrawImage(image, left, top, paint); 183 this->INHERITED::onDrawImage(image, left, top, paint);
168 this->restore(); 184 this->restore();
169 } 185 }
170 186
171 void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRe ct *src, 187 void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRe ct *src,
172 const SkRect &dst, const SkPaint *paint, 188 const SkRect &dst, const SkPaint *paint,
173 SrcRectConstraint constraint) { 189 SrcRectConstraint constraint) {
174 this->updateMatrix(); 190 this->updateMatrix(dst);
175 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); 191 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint);
176 this->restore(); 192 this->restore();
177 } 193 }
178 194
179 void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIR ect &center, 195 void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIR ect &center,
180 const SkRect &dst, const SkPaint *paint) { 196 const SkRect &dst, const SkPaint *paint) {
181 this->updateMatrix(); 197 this->updateMatrix(dst);
182 this->INHERITED::onDrawImageNine(image, center, dst, paint); 198 this->INHERITED::onDrawImageNine(image, center, dst, paint);
183 this->restore(); 199 this->restore();
184 } 200 }
185 201
186 202
187 void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount , 203 void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount ,
188 const SkPoint vertices[], const S kPoint texs[], 204 const SkPoint vertices[], const S kPoint texs[],
189 const SkColor colors[], SkXfermod e *xmode, 205 const SkColor colors[], SkXfermod e *xmode,
190 const uint16_t indices[], int ind exCount, 206 const uint16_t indices[], int ind exCount,
191 const SkPaint &paint) { 207 const SkPaint &paint) {
192 this->updateMatrix(); 208 SkISize size = this->getBaseLayerSize();
209 this->updateMatrix(SkRect::MakeIWH(size.fWidth, size.fHeight));
193 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, 210 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors,
194 xmode, indices, indexCount, paint); 211 xmode, indices, indexCount, paint);
195 this->restore(); 212 this->restore();
196 } 213 }
197 214
198 void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColo r colors[], 215 void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColo r colors[],
199 const SkPoint texCoords[], SkXfermod e *xmode, 216 const SkPoint texCoords[], SkXfermod e *xmode,
200 const SkPaint &paint) { 217 const SkPaint &paint) {
201 this->updateMatrix(); 218 SkISize size = this->getBaseLayerSize();
219 this->updateMatrix(SkRect::MakeIWH(size.fWidth, size.fHeight));
202 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); 220 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint);
203 this->restore(); 221 this->restore();
204 } 222 }
205 223
206 void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x, 224 void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x,
207 SkScalar y, const SkPaint &paint) { 225 SkScalar y, const SkPaint &paint) {
208 this->updateMatrix(); 226 this->updateMatrix(paint.getFontBounds().makeOffset(x, y));
209 this->INHERITED::onDrawText(text, byteLength, x, y, paint); 227 this->INHERITED::onDrawText(text, byteLength, x, y, paint);
210 this->restore(); 228 this->restore();
211 } 229 }
212 230
213 void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLengt h, 231 void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLengt h,
214 const SkPoint pos[], const SkPaint &paint) { 232 const SkPoint pos[], const SkPaint &paint) {
215 this->updateMatrix(); 233 SkISize size = this->getBaseLayerSize();
234 this->updateMatrix(SkRect::MakeIWH(size.fWidth, size.fHeight));
216 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); 235 this->INHERITED::onDrawPosText(text, byteLength, pos, paint);
217 this->restore(); 236 this->restore();
218 } 237 }
219 238
220 void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLeng th, 239 void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLeng th,
221 const SkScalar xpos[], 240 const SkScalar xpos[],
222 SkScalar constY, const SkPaint &p aint) { 241 SkScalar constY, const SkPaint &p aint) {
223 this->updateMatrix(); 242 SkISize size = this->getBaseLayerSize();
243 this->updateMatrix(SkRect::MakeIWH(size.fWidth, size.fHeight));
224 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); 244 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint);
225 this->restore(); 245 this->restore();
226 } 246 }
227 247
228 void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLe ngth, 248 void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLe ngth,
229 const SkPath &path, const SkMat rix *matrix, 249 const SkPath &path, const SkMat rix *matrix,
230 const SkPaint &paint) { 250 const SkPaint &paint) {
231 this->updateMatrix(); 251 this->updateMatrix(path.getBounds());
232 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint); 252 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint);
233 this->restore(); 253 this->restore();
234 } 254 }
235 255
236 void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteL ength, 256 void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteL ength,
237 const SkRSXform xform[], const SkRect *cull, 257 const SkRSXform xform[], const SkRect *cull,
238 const SkPaint &paint) { 258 const SkPaint &paint) {
robertphillips 2016/08/31 22:18:19 Isn't the cull parameter optional?
vjiaoblack 2016/09/01 19:22:47 Done.
239 this->updateMatrix(); 259 this->updateMatrix(*cull);
240 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint); 260 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint);
241 this->restore(); 261 this->restore();
242 } 262 }
243 263
244 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, 264 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
245 const SkPaint &paint) { 265 const SkPaint &paint) {
246 this->updateMatrix(); 266 SkISize size = this->getBaseLayerSize();
267 this->updateMatrix(SkRect::MakeIWH(size.fWidth, size.fHeight));
247 this->INHERITED::onDrawTextBlob(blob, x, y, paint); 268 this->INHERITED::onDrawTextBlob(blob, x, y, paint);
248 this->restore(); 269 this->restore();
249 } 270 }
250 271
251 #endif 272 #endif
OLDNEW
« src/utils/SkShadowPaintFilterCanvas.h ('K') | « src/utils/SkShadowPaintFilterCanvas.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698