Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 * 4, height * 4); |
| 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 ¶ms) { | 64 void SkShadowPaintFilterCanvas::setShadowParams(const SkShadowParams ¶ms) { |
| 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 |
| 76 void SkShadowPaintFilterCanvas::updateMatrix() { | 76 void SkShadowPaintFilterCanvas::updateMatrix() { |
| 77 this->save(); | |
| 78 | |
| 79 // It is up to the user to set the 0th light in fLights to | 77 // 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. | 78 // the light the want to render the depth map with. |
| 81 if (this->fLights->light(0).type() == SkLights::Light::kDirectional_LightTyp e) { | 79 if (this->fLights->light(0).type() == SkLights::Light::kDirectional_LightTyp e) { |
| 82 const SkVector3& lightDir = this->fLights->light(0).dir(); | 80 const SkVector3& lightDir = this->fLights->light(0).dir(); |
| 83 SkScalar x = lightDir.fX * this->getZ(); | 81 SkScalar x = lightDir.fX * this->getZ(); |
| 84 SkScalar y = lightDir.fY * this->getZ(); | 82 SkScalar y = lightDir.fY * this->getZ(); |
| 85 | 83 |
| 86 this->translate(x, y); | 84 this->translate(x, y); |
| 85 } else if (this->fLights->light(0).type() == SkLights::Light::kPoint_LightTy pe) { | |
| 86 SkISize size = this->getBaseLayerSize(); | |
| 87 | |
| 88 SkPoint3 lightPos = this->fLights->light(0).pos(); | |
|
robertphillips
2016/09/06 17:51:11
This set call seems odd ...
vjiaoblack
2016/09/06 19:30:32
Done.
| |
| 89 lightPos.set(lightPos.fX, lightPos.fY, lightPos.fZ); | |
| 90 | |
| 91 // shadow maps for point lights are 4x the size of the diffuse map | |
|
robertphillips
2016/09/06 17:51:11
How was 4x arrived at?
vjiaoblack
2016/09/06 19:30:32
Done.
| |
| 92 SkScalar diffuseHeight = size.fHeight / 4.0f; | |
| 93 | |
| 94 // move point light with canvas's CTM | |
| 95 SkPoint lightPoint = SkPoint::Make(lightPos.fX, diffuseHeight - lightPos .fY); | |
| 96 SkMatrix mat = this->getTotalMatrix(); | |
| 97 if (mat.invert(&mat)) { | |
| 98 mat.mapPoints(&lightPoint, 1); | |
| 99 } | |
| 100 lightPoint.set(lightPoint.fX, diffuseHeight - lightPoint.fY); | |
| 101 | |
| 102 // center the shadow map | |
| 103 // note: the 3/8 constant is specific to the 4.0 depth map size multipli er | |
| 104 mat = this->getTotalMatrix(); | |
| 105 mat.postTranslate(size.width() * 0.375f, size.height() * 0.375f); | |
| 106 this->setMatrix(mat); | |
| 107 | |
| 108 // project shapes onto canvas as shadows | |
| 109 SkScalar scale = (lightPos.fZ) / (lightPos.fZ - this->getZ()); | |
| 110 this->scale(scale, scale); | |
| 111 | |
| 112 this->translate(-lightPoint.fX * this->getZ() / | |
| 113 ((lightPos.fZ - this->getZ()) * scale), | |
| 114 -(diffuseHeight - lightPoint.fY) * this->getZ() / | |
| 115 ((lightPos.fZ - this->getZ()) * scale)); | |
| 87 } | 116 } |
| 88 } | 117 } |
| 89 | 118 |
| 90 void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) { | 119 void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) { |
| 120 this->save(); | |
| 91 this->updateMatrix(); | 121 this->updateMatrix(); |
| 92 this->INHERITED::onDrawPaint(paint); | 122 this->INHERITED::onDrawPaint(paint); |
| 93 this->restore(); | 123 this->restore(); |
| 94 } | 124 } |
| 95 | 125 |
| 96 void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], | 126 void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 97 const SkPaint &paint) { | 127 const SkPaint &paint) { |
| 128 this->save(); | |
| 98 this->updateMatrix(); | 129 this->updateMatrix(); |
| 99 this->INHERITED::onDrawPoints(mode, count, pts, paint); | 130 this->INHERITED::onDrawPoints(mode, count, pts, paint); |
| 100 this->restore(); | 131 this->restore(); |
| 101 } | 132 } |
| 102 | 133 |
| 103 void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &pa int) { | 134 void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &pa int) { |
| 135 this->save(); | |
| 104 this->updateMatrix(); | 136 this->updateMatrix(); |
| 105 this->INHERITED::onDrawRect(rect, paint); | 137 this->INHERITED::onDrawRect(rect, paint); |
| 106 this->restore(); | 138 this->restore(); |
| 107 } | 139 } |
| 108 | 140 |
| 109 void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) { | 141 void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) { |
| 142 this->save(); | |
| 110 this->updateMatrix(); | 143 this->updateMatrix(); |
| 111 this->INHERITED::onDrawRRect(rrect, paint); | 144 this->INHERITED::onDrawRRect(rrect, paint); |
| 112 this->restore(); | 145 this->restore(); |
| 113 } | 146 } |
| 114 | 147 |
| 115 void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner, | 148 void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner, |
| 116 const SkPaint &paint) { | 149 const SkPaint &paint) { |
| 150 this->save(); | |
| 117 this->updateMatrix(); | 151 this->updateMatrix(); |
| 118 this->INHERITED::onDrawDRRect(outer, inner, paint); | 152 this->INHERITED::onDrawDRRect(outer, inner, paint); |
| 119 this->restore(); | 153 this->restore(); |
| 120 } | 154 } |
| 121 | 155 |
| 122 void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &pa int) { | 156 void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &pa int) { |
| 157 this->save(); | |
| 123 this->updateMatrix(); | 158 this->updateMatrix(); |
| 124 this->INHERITED::onDrawOval(rect, paint); | 159 this->INHERITED::onDrawOval(rect, paint); |
| 125 this->restore(); | 160 this->restore(); |
| 126 } | 161 } |
| 127 | 162 |
| 128 void SkShadowPaintFilterCanvas::onDrawArc(const SkRect &rect, SkScalar startAngl e, | 163 void SkShadowPaintFilterCanvas::onDrawArc(const SkRect &rect, SkScalar startAngl e, |
| 129 SkScalar sweepAngle, bool useCenter, | 164 SkScalar sweepAngle, bool useCenter, |
| 130 const SkPaint &paint) { | 165 const SkPaint &paint) { |
| 166 this->save(); | |
| 131 this->updateMatrix(); | 167 this->updateMatrix(); |
| 132 this->INHERITED::onDrawArc(rect, startAngle, sweepAngle, useCenter, paint); | 168 this->INHERITED::onDrawArc(rect, startAngle, sweepAngle, useCenter, paint); |
| 133 this->restore(); | 169 this->restore(); |
| 134 } | 170 } |
| 135 | 171 |
| 136 void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &pa int) { | 172 void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &pa int) { |
| 173 this->save(); | |
| 137 this->updateMatrix(); | 174 this->updateMatrix(); |
| 138 this->INHERITED::onDrawPath(path, paint); | 175 this->INHERITED::onDrawPath(path, paint); |
| 139 this->restore(); | 176 this->restore(); |
| 140 } | 177 } |
| 141 | 178 |
| 142 void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top, | 179 void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top, |
| 143 const SkPaint *paint) { | 180 const SkPaint *paint) { |
| 181 this->save(); | |
| 144 this->updateMatrix(); | 182 this->updateMatrix(); |
| 145 this->INHERITED::onDrawBitmap(bm, left, top, paint); | 183 this->INHERITED::onDrawBitmap(bm, left, top, paint); |
| 146 this->restore(); | 184 this->restore(); |
| 147 } | 185 } |
| 148 | 186 |
| 149 void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRec t *src, | 187 void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRec t *src, |
| 150 const SkRect &dst, const SkPain t *paint, | 188 const SkRect &dst, const SkPain t *paint, |
| 151 SrcRectConstraint constraint) { | 189 SrcRectConstraint constraint) { |
| 190 this->save(); | |
| 152 this->updateMatrix(); | 191 this->updateMatrix(); |
| 153 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); | 192 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); |
| 154 this->restore(); | 193 this->restore(); |
| 155 } | 194 } |
| 156 | 195 |
| 157 void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRe ct ¢er, | 196 void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRe ct ¢er, |
| 158 const SkRect &dst, const SkPain t *paint) { | 197 const SkRect &dst, const SkPain t *paint) { |
| 198 this->save(); | |
| 159 this->updateMatrix(); | 199 this->updateMatrix(); |
| 160 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); | 200 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); |
| 161 this->restore(); | 201 this->restore(); |
| 162 } | 202 } |
| 163 | 203 |
| 164 void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left, | 204 void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left, |
| 165 SkScalar top, const SkPaint *paint) { | 205 SkScalar top, const SkPaint *paint) { |
| 206 this->save(); | |
| 166 this->updateMatrix(); | 207 this->updateMatrix(); |
| 167 this->INHERITED::onDrawImage(image, left, top, paint); | 208 this->INHERITED::onDrawImage(image, left, top, paint); |
| 168 this->restore(); | 209 this->restore(); |
| 169 } | 210 } |
| 170 | 211 |
| 171 void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRe ct *src, | 212 void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRe ct *src, |
| 172 const SkRect &dst, const SkPaint *paint, | 213 const SkRect &dst, const SkPaint *paint, |
| 173 SrcRectConstraint constraint) { | 214 SrcRectConstraint constraint) { |
| 215 this->save(); | |
| 174 this->updateMatrix(); | 216 this->updateMatrix(); |
| 175 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); | 217 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); |
| 176 this->restore(); | 218 this->restore(); |
| 177 } | 219 } |
| 178 | 220 |
| 179 void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIR ect ¢er, | 221 void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIR ect ¢er, |
| 180 const SkRect &dst, const SkPaint *paint) { | 222 const SkRect &dst, const SkPaint *paint) { |
| 223 this->save(); | |
| 181 this->updateMatrix(); | 224 this->updateMatrix(); |
| 182 this->INHERITED::onDrawImageNine(image, center, dst, paint); | 225 this->INHERITED::onDrawImageNine(image, center, dst, paint); |
| 183 this->restore(); | 226 this->restore(); |
| 184 } | 227 } |
| 185 | 228 |
| 186 | 229 |
| 187 void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount , | 230 void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount , |
| 188 const SkPoint vertices[], const S kPoint texs[], | 231 const SkPoint vertices[], const S kPoint texs[], |
| 189 const SkColor colors[], SkXfermod e *xmode, | 232 const SkColor colors[], SkXfermod e *xmode, |
| 190 const uint16_t indices[], int ind exCount, | 233 const uint16_t indices[], int ind exCount, |
| 191 const SkPaint &paint) { | 234 const SkPaint &paint) { |
| 235 this->save(); | |
| 192 this->updateMatrix(); | 236 this->updateMatrix(); |
| 193 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, | 237 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, |
| 194 xmode, indices, indexCount, paint); | 238 xmode, indices, indexCount, paint); |
| 195 this->restore(); | 239 this->restore(); |
| 196 } | 240 } |
| 197 | 241 |
| 198 void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColo r colors[], | 242 void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColo r colors[], |
| 199 const SkPoint texCoords[], SkXfermod e *xmode, | 243 const SkPoint texCoords[], SkXfermod e *xmode, |
| 200 const SkPaint &paint) { | 244 const SkPaint &paint) { |
| 245 this->save(); | |
| 201 this->updateMatrix(); | 246 this->updateMatrix(); |
| 202 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); | 247 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); |
| 203 this->restore(); | 248 this->restore(); |
| 204 } | 249 } |
| 205 | 250 |
| 206 void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x, | 251 void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x, |
| 207 SkScalar y, const SkPaint &paint) { | 252 SkScalar y, const SkPaint &paint) { |
| 253 this->save(); | |
| 208 this->updateMatrix(); | 254 this->updateMatrix(); |
| 209 this->INHERITED::onDrawText(text, byteLength, x, y, paint); | 255 this->INHERITED::onDrawText(text, byteLength, x, y, paint); |
| 210 this->restore(); | 256 this->restore(); |
| 211 } | 257 } |
| 212 | 258 |
| 213 void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLengt h, | 259 void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLengt h, |
| 214 const SkPoint pos[], const SkPaint &paint) { | 260 const SkPoint pos[], const SkPaint &paint) { |
| 261 this->save(); | |
| 215 this->updateMatrix(); | 262 this->updateMatrix(); |
| 216 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); | 263 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); |
| 217 this->restore(); | 264 this->restore(); |
| 218 } | 265 } |
| 219 | 266 |
| 220 void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLeng th, | 267 void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLeng th, |
| 221 const SkScalar xpos[], | 268 const SkScalar xpos[], |
| 222 SkScalar constY, const SkPaint &p aint) { | 269 SkScalar constY, const SkPaint &p aint) { |
| 270 this->save(); | |
| 223 this->updateMatrix(); | 271 this->updateMatrix(); |
| 224 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); | 272 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); |
| 225 this->restore(); | 273 this->restore(); |
| 226 } | 274 } |
| 227 | 275 |
| 228 void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLe ngth, | 276 void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLe ngth, |
| 229 const SkPath &path, const SkMat rix *matrix, | 277 const SkPath &path, const SkMat rix *matrix, |
| 230 const SkPaint &paint) { | 278 const SkPaint &paint) { |
| 279 this->save(); | |
| 231 this->updateMatrix(); | 280 this->updateMatrix(); |
| 232 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint); | 281 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint); |
| 233 this->restore(); | 282 this->restore(); |
| 234 } | 283 } |
| 235 | 284 |
| 236 void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteL ength, | 285 void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteL ength, |
| 237 const SkRSXform xform[], const SkRect *cull, | 286 const SkRSXform xform[], const SkRect *cull, |
| 238 const SkPaint &paint) { | 287 const SkPaint &paint) { |
| 288 this->save(); | |
| 239 this->updateMatrix(); | 289 this->updateMatrix(); |
| 240 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint); | 290 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint); |
| 241 this->restore(); | 291 this->restore(); |
| 242 } | 292 } |
| 243 | 293 |
| 244 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, | 294 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, |
| 245 const SkPaint &paint) { | 295 const SkPaint &paint) { |
| 296 this->save(); | |
| 246 this->updateMatrix(); | 297 this->updateMatrix(); |
| 247 this->INHERITED::onDrawTextBlob(blob, x, y, paint); | 298 this->INHERITED::onDrawTextBlob(blob, x, y, paint); |
| 248 this->restore(); | 299 this->restore(); |
| 249 } | 300 } |
| 250 | 301 |
| 251 #endif | 302 #endif |
| OLD | NEW |