Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 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 #ifndef SkShadowPaintFilterCanvas_DEFINED | |
| 9 #define SkShadowPaintFilterCanvas_DEFINED | |
|
jvanverth1
2016/08/02 17:43:33
Does this need to be inside an #ifdef SK_EXPERIMEN
vjiaoblack
2016/08/03 14:04:21
Hmm... I guess it would make sense for it to be
| |
| 10 | |
| 11 #include "SkNWayCanvas.h" | |
| 12 #include "SkTLazy.h" | |
| 13 #include "SkLights.h" | |
| 14 #include "SkPaintFilterCanvas.h" | |
| 15 #include "SkCanvas.h" | |
| 16 | |
| 17 /** \class SkPaintFilterCanvas | |
| 18 | |
| 19 A utility proxy base class for implementing shadow maps. | |
| 20 */ | |
| 21 | |
| 22 /* We override the onFilter method to draw depths into the canvas | |
| 23 * depending on the current draw depth of the canvas, throwing out | |
| 24 * the actual draw color. | |
| 25 */ | |
| 26 class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas { | |
| 27 public: | |
| 28 | |
| 29 sk_sp<SkLights> getLights() { | |
| 30 return INHERITED::getLights(); | |
| 31 } | |
| 32 | |
|
robertphillips
2016/08/02 16:05:06
Will this fit on one line?
vjiaoblack
2016/08/03 14:04:21
Yeah, but I moved some of the code around and this
| |
| 33 SkShadowPaintFilterCanvas(SkCanvas *canvas) | |
| 34 : SkPaintFilterCanvas(canvas) { } | |
| 35 | |
| 36 // TODO use a shader instead | |
| 37 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override { | |
| 38 if (*paint) { | |
| 39 int z = this->getZ(); | |
| 40 SkASSERT(z <= 0xFF && z >= 0x00); | |
| 41 | |
| 42 SkPaint newPaint; | |
| 43 newPaint.setPathEffect(sk_ref_sp<SkPathEffect>((*paint)->getPathEffe ct())); | |
| 44 | |
| 45 SkColor color = 0xFF000000; // init color to opaque black | |
| 46 color |= z; // Put the index into the blue component | |
| 47 newPaint.setColor(color); | |
| 48 | |
| 49 *paint->writable() = newPaint; | |
| 50 } | |
| 51 | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 void onDrawPicture(const SkPicture *picture, const SkMatrix *matrix, const S kPaint *paint) { | |
| 56 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); | |
| 57 if (onFilter(&filteredPaint, kPicture_Type)) | |
| 58 SkCanvas::onDrawPicture(picture, matrix, filteredPaint); | |
| 59 } | |
| 60 | |
| 61 void updateMatrix() { | |
| 62 save(); | |
| 63 | |
|
robertphillips
2016/08/02 16:05:06
Shouldn't this now read something like:
// The us
vjiaoblack
2016/08/03 14:04:21
Done.
| |
| 64 // TODO which light do we set? | |
| 65 if (this->fLights->light(0).type() != SkLights::Light::kAmbient_LightTyp e) { | |
| 66 SkVector3 lightDir = this->fLights->light(0).dir(); | |
| 67 SkScalar x = lightDir.fX * this->getZ(); | |
| 68 SkScalar y = lightDir.fY * this->getZ(); | |
| 69 | |
| 70 this->translate(x, y); | |
| 71 } | |
| 72 | |
| 73 } | |
| 74 | |
|
robertphillips
2016/08/02 16:05:06
What happened to all the overrides?
vjiaoblack
2016/08/03 14:04:21
Currently, I can't have them. (after the refactori
| |
| 75 void onDrawPaint(const SkPaint &paint) { | |
| 76 updateMatrix(); | |
| 77 this->INHERITED::onDrawPaint(paint); | |
| 78 restore(); | |
| 79 } | |
| 80 | |
| 81 void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], | |
| 82 const SkPaint &paint) { | |
| 83 updateMatrix(); | |
| 84 this->INHERITED::onDrawPoints(mode, count, pts, paint); | |
| 85 restore(); | |
| 86 } | |
| 87 | |
| 88 void onDrawRect(const SkRect &rect, const SkPaint &paint) { | |
| 89 updateMatrix(); | |
| 90 this->INHERITED::onDrawRect(rect, paint); | |
| 91 restore(); | |
| 92 } | |
| 93 | |
| 94 void onDrawRRect(const SkRRect &rrect, const SkPaint &paint) { | |
| 95 updateMatrix(); | |
| 96 this->INHERITED::onDrawRRect(rrect, paint); | |
| 97 restore(); | |
| 98 } | |
| 99 | |
| 100 void onDrawDRRect(const SkRRect &outer, const SkRRect &inner, | |
| 101 const SkPaint &paint) { | |
| 102 updateMatrix(); | |
| 103 this->INHERITED::onDrawDRRect(outer, inner, paint); | |
| 104 restore(); | |
| 105 } | |
| 106 | |
| 107 void onDrawOval(const SkRect &rect, const SkPaint &paint) { | |
| 108 updateMatrix(); | |
| 109 this->INHERITED::onDrawOval(rect, paint); | |
| 110 restore(); | |
| 111 } | |
| 112 | |
| 113 void onDrawPath(const SkPath &path, const SkPaint &paint) { | |
| 114 updateMatrix(); | |
| 115 this->INHERITED::onDrawPath(path, paint); | |
| 116 restore(); | |
| 117 } | |
| 118 | |
| 119 void onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top, | |
| 120 const SkPaint *paint) { | |
| 121 updateMatrix(); | |
| 122 this->INHERITED::onDrawBitmap(bm, left, top, paint); | |
| 123 restore(); | |
| 124 } | |
| 125 | |
| 126 void onDrawBitmapRect(const SkBitmap &bm, const SkRect *src, const SkRect &d st, | |
| 127 const SkPaint *paint, SrcRectConstraint constraint) { | |
| 128 updateMatrix(); | |
| 129 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); | |
| 130 restore(); | |
| 131 } | |
| 132 | |
| 133 void onDrawBitmapNine(const SkBitmap &bm, const SkIRect ¢er, | |
| 134 const SkRect &dst, const SkPaint *paint) { | |
| 135 updateMatrix(); | |
| 136 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); | |
| 137 restore(); | |
| 138 } | |
| 139 | |
| 140 void onDrawImage(const SkImage *image, SkScalar left, SkScalar top, | |
| 141 const SkPaint *paint) { | |
| 142 updateMatrix(); | |
| 143 this->INHERITED::onDrawImage(image, left, top, paint); | |
| 144 restore(); | |
| 145 } | |
| 146 | |
| 147 void onDrawImageRect(const SkImage *image, const SkRect *src, | |
| 148 const SkRect &dst, const SkPaint *paint, | |
| 149 SrcRectConstraint constraint) { | |
| 150 updateMatrix(); | |
| 151 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); | |
| 152 restore(); | |
| 153 } | |
| 154 | |
| 155 void onDrawImageNine(const SkImage *image, const SkIRect ¢er, | |
| 156 const SkRect &dst, const SkPaint *paint) { | |
| 157 updateMatrix(); | |
| 158 this->INHERITED::onDrawImageNine(image, center, dst, paint); | |
| 159 restore(); | |
| 160 } | |
| 161 | |
| 162 | |
| 163 void onDrawVertices(VertexMode vmode, int vertexCount, | |
| 164 const SkPoint vertices[], const SkPoint texs[], | |
| 165 const SkColor colors[], SkXfermode *xmode, | |
| 166 const uint16_t indices[], int indexCount, | |
| 167 const SkPaint &paint) { | |
| 168 updateMatrix(); | |
| 169 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colo rs, | |
| 170 xmode, indices, indexCount, paint); | |
| 171 restore(); | |
| 172 } | |
| 173 | |
| 174 void onDrawPatch(const SkPoint cubics[], const SkColor colors[], | |
| 175 const SkPoint texCoords[], SkXfermode *xmode, | |
| 176 const SkPaint &paint) { | |
| 177 updateMatrix(); | |
| 178 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); | |
| 179 restore(); | |
| 180 } | |
| 181 | |
| 182 void onDrawText(const void *text, size_t byteLength, SkScalar x, SkScalar y, | |
| 183 const SkPaint &paint) { | |
| 184 updateMatrix(); | |
| 185 this->INHERITED::onDrawText(text, byteLength, x, y, paint); | |
| 186 restore(); | |
| 187 } | |
| 188 | |
| 189 void onDrawPosText(const void *text, size_t byteLength, const SkPoint pos[], | |
| 190 const SkPaint &paint) { | |
| 191 updateMatrix(); | |
| 192 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); | |
| 193 restore(); | |
| 194 } | |
| 195 | |
| 196 void onDrawPosTextH(const void *text, size_t byteLength, const SkScalar xpos [], | |
| 197 SkScalar constY, const SkPaint &paint) { | |
| 198 updateMatrix(); | |
| 199 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); | |
| 200 restore(); | |
| 201 } | |
| 202 | |
| 203 void onDrawTextOnPath(const void *text, size_t byteLength, const SkPath &pat h, | |
| 204 const SkMatrix *matrix, const SkPaint &paint) { | |
| 205 updateMatrix(); | |
| 206 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint) ; | |
| 207 restore(); | |
| 208 } | |
| 209 | |
| 210 void onDrawTextRSXform(const void *text, size_t byteLength, | |
| 211 const SkRSXform xform[], const SkRect *cull, | |
| 212 const SkPaint &paint) { | |
| 213 updateMatrix(); | |
| 214 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint) ; | |
| 215 restore(); | |
| 216 } | |
| 217 | |
| 218 void onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const Sk Paint &paint) { | |
| 219 updateMatrix(); | |
| 220 this->INHERITED::onDrawTextBlob(blob, x, y, paint); | |
| 221 restore(); | |
| 222 } | |
| 223 | |
| 224 private: | |
| 225 typedef SkPaintFilterCanvas INHERITED; | |
|
robertphillips
2016/08/02 16:05:06
extra '\n' here ?
vjiaoblack
2016/08/03 14:04:21
Done.
| |
| 226 | |
| 227 }; | |
| 228 | |
| 229 | |
| 230 #endif | |
| OLD | NEW |