| 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 | #include "include/core/SkOverdrawCanvas.h" |
| 9 | |
| 10 | #include "include/core/SkColorFilter.h" |
| 11 | #include "include/core/SkDrawable.h" |
| 12 | #include "include/core/SkPath.h" |
| 13 | #include "include/core/SkRRect.h" |
| 14 | #include "include/core/SkRSXform.h" |
| 15 | #include "include/core/SkTextBlob.h" |
| 16 | #include "include/private/SkTo.h" |
| 17 | #include "src/core/SkDevice.h" |
| 18 | #include "src/core/SkDrawShadowInfo.h" |
| 19 | #include "src/core/SkGlyphRunPainter.h" |
| 20 | #include "src/core/SkImagePriv.h" |
| 21 | #include "src/core/SkLatticeIter.h" |
| 22 | #include "src/core/SkStrikeCache.h" |
| 23 | #include "src/core/SkTextBlobPriv.h" |
| 24 | #include "src/utils/SkPatchUtils.h" |
| 25 | |
| 26 | SkOverdrawCanvas::SkOverdrawCanvas(SkCanvas* canvas) |
| 27 | : INHERITED(canvas->onImageInfo().width(), canvas->onImageInfo().height()) |
| 28 | { |
| 29 | // Non-drawing calls that SkOverdrawCanvas does not override (translate, save, etc.) |
| 30 | // will pass through to the input canvas. |
| 31 | this->addCanvas(canvas); |
| 32 | |
| 33 | static constexpr float kIncrementAlpha[] = { |
| 34 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 35 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 36 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 37 | 0.0f, 0.0f, 0.0f, 0.0f, 1.0f/255, |
| 38 | }; |
| 39 | |
| 40 | fPaint.setAntiAlias(false); |
| 41 | fPaint.setBlendMode(SkBlendMode::kPlus); |
| 42 | fPaint.setColorFilter(SkColorFilters::Matrix(kIncrementAlpha)); |
| 43 | } |
| 44 | |
| 45 | namespace { |
| 46 | class TextDevice : public SkNoPixelsDevice, public SkGlyphRunListPainter::BitmapDevicePainter { |
| 47 | public: |
| 48 | TextDevice(SkCanvas* overdrawCanvas, const SkSurfaceProps& props) |
| 49 | : SkNoPixelsDevice{SkIRect::MakeWH(32767, 32767), props}, |
| 50 | fOverdrawCanvas{overdrawCanvas}, |
| 51 | fPainter{props, kN32_SkColorType, nullptr, SkStrikeCache::GlobalStrikeCache()} {} |
| 52 | |
| 53 | void paintPaths(SkDrawableGlyphBuffer*, SkScalar scale, const SkPaint& paint) const override {} |
| 54 | |
| 55 | void paintMasks(SkDrawableGlyphBuffer* drawables, const SkPaint& paint) const override { |
| 56 | for (auto t : drawables->drawable()) { |
| 57 | SkGlyphVariant glyph; SkPoint pos; |
| 58 | std::tie(glyph, pos) = t; |
| 59 | SkMask mask = glyph.glyph()->mask(pos); |
| 60 | fOverdrawCanvas->drawRect(SkRect::Make(mask.fBounds), SkPaint()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override { |
| 66 | fPainter.drawForBitmapDevice(glyphRunList, fOverdrawCanvas->getTotalMatrix(), this); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | SkCanvas* const fOverdrawCanvas; |
| 71 | SkGlyphRunListPainter fPainter; |
| 72 | }; |
| 73 | } // namespace |
| 74 | |
| 75 | void SkOverdrawCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 76 | const SkPaint& paint) { |
| 77 | SkGlyphRunBuilder b; |
| 78 | SkSurfaceProps props{0, kUnknown_SkPixelGeometry}; |
| 79 | this->getProps(&props); |
| 80 | TextDevice device{this, props}; |
| 81 | |
| 82 | b.drawTextBlob(paint, *blob, {x, y}, &device); |
| 83 | } |
| 84 | |
| 85 | void SkOverdrawCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 86 | const SkPoint texCoords[4], SkBlendMode blendMode, |
| 87 | const SkPaint&) { |
| 88 | fList[0]->onDrawPatch(cubics, colors, texCoords, blendMode, fPaint); |
| 89 | } |
| 90 | |
| 91 | void SkOverdrawCanvas::onDrawPaint(const SkPaint& paint) { |
| 92 | if (0 == paint.getColor() && !paint.getColorFilter() && !paint.getShader()) { |
| 93 | // This is a clear, ignore it. |
| 94 | } else { |
| 95 | fList[0]->onDrawPaint(this->overdrawPaint(paint)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void SkOverdrawCanvas::onDrawBehind(const SkPaint& paint) { |
| 100 | fList[0]->onDrawBehind(this->overdrawPaint(paint)); |
| 101 | } |
| 102 | |
| 103 | void SkOverdrawCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) { |
| 104 | fList[0]->onDrawRect(rect, this->overdrawPaint(paint)); |
| 105 | } |
| 106 | |
| 107 | void SkOverdrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) { |
| 108 | fList[0]->onDrawRegion(region, this->overdrawPaint(paint)); |
| 109 | } |
| 110 | |
| 111 | void SkOverdrawCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) { |
| 112 | fList[0]->onDrawOval(oval, this->overdrawPaint(paint)); |
| 113 | } |
| 114 | |
| 115 | void SkOverdrawCanvas::onDrawArc(const SkRect& arc, SkScalar startAngle, SkScalar sweepAngle, |
| 116 | bool useCenter, const SkPaint& paint) { |
| 117 | fList[0]->onDrawArc(arc, startAngle, sweepAngle, useCenter, this->overdrawPaint(paint)); |
| 118 | } |
| 119 | |
| 120 | void SkOverdrawCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 121 | const SkPaint& paint) { |
| 122 | fList[0]->onDrawDRRect(outer, inner, this->overdrawPaint(paint)); |
| 123 | } |
| 124 | |
| 125 | void SkOverdrawCanvas::onDrawRRect(const SkRRect& rect, const SkPaint& paint) { |
| 126 | fList[0]->onDrawRRect(rect, this->overdrawPaint(paint)); |
| 127 | } |
| 128 | |
| 129 | void SkOverdrawCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint points[], |
| 130 | const SkPaint& paint) { |
| 131 | fList[0]->onDrawPoints(mode, count, points, this->overdrawPaint(paint)); |
| 132 | } |
| 133 | |
| 134 | void SkOverdrawCanvas::onDrawVerticesObject(const SkVertices* vertices, |
| 135 | SkBlendMode blendMode, const SkPaint& paint) { |
| 136 | fList[0]->onDrawVerticesObject(vertices, blendMode, this->overdrawPaint(paint)); |
| 137 | } |
| 138 | |
| 139 | void SkOverdrawCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[], |
| 140 | const SkRect texs[], const SkColor colors[], int count, |
| 141 | SkBlendMode mode, const SkRect* cull, const SkPaint* paint) { |
| 142 | SkPaint* paintPtr = &fPaint; |
| 143 | SkPaint storage; |
| 144 | if (paint) { |
| 145 | storage = this->overdrawPaint(*paint); |
| 146 | paintPtr = &storage; |
| 147 | } |
| 148 | |
| 149 | fList[0]->onDrawAtlas(image, xform, texs, colors, count, mode, cull, paintPtr); |
| 150 | } |
| 151 | |
| 152 | void SkOverdrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { |
| 153 | fList[0]->onDrawPath(path, fPaint); |
| 154 | } |
| 155 | |
| 156 | void SkOverdrawCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint*) { |
| 157 | fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, image->width(), image->height()), fPaint); |
| 158 | } |
| 159 | |
| 160 | void SkOverdrawCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
| 161 | const SkPaint*, SrcRectConstraint) { |
| 162 | fList[0]->onDrawRect(dst, fPaint); |
| 163 | } |
| 164 | |
| 165 | void SkOverdrawCanvas::onDrawImageNine(const SkImage*, const SkIRect&, const SkRect& dst, |
| 166 | const SkPaint*) { |
| 167 | fList[0]->onDrawRect(dst, fPaint); |
| 168 | } |
| 169 | |
| 170 | void SkOverdrawCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice, |
| 171 | const SkRect& dst, const SkPaint*) { |
| 172 | SkIRect bounds; |
| 173 | Lattice latticePlusBounds = lattice; |
| 174 | if (!latticePlusBounds.fBounds) { |
| 175 | bounds = SkIRect::MakeWH(image->width(), image->height()); |
| 176 | latticePlusBounds.fBounds = &bounds; |
| 177 | } |
| 178 | |
| 179 | if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) { |
| 180 | SkLatticeIter iter(latticePlusBounds, dst); |
| 181 | |
| 182 | SkRect dummy, iterDst; |
| 183 | while (iter.next(&dummy, &iterDst)) { |
| 184 | fList[0]->onDrawRect(iterDst, fPaint); |
| 185 | } |
| 186 | } else { |
| 187 | fList[0]->onDrawRect(dst, fPaint); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void SkOverdrawCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) { |
| 192 | drawable->draw(this, matrix); |
| 193 | } |
| 194 | |
| 195 | void SkOverdrawCanvas::onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) { |
| 196 | SkASSERT(false); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | void SkOverdrawCanvas::onDrawAnnotation(const SkRect&, const char[], SkData*) {} |
| 201 | |
| 202 | void SkOverdrawCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) { |
| 203 | SkRect bounds; |
| 204 | SkDrawShadowMetrics::GetLocalBounds(path, rec, this->getTotalMatrix(), &bounds); |
| 205 | fList[0]->onDrawRect(bounds, fPaint); |
| 206 | } |
| 207 | |
| 208 | void SkOverdrawCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], |
| 209 | QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) { |
| 210 | if (clip) { |
| 211 | SkPath path; |
| 212 | path.addPoly(clip, 4, true); |
| 213 | fList[0]->onDrawPath(path, fPaint); |
| 214 | } else { |
| 215 | fList[0]->onDrawRect(rect, fPaint); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void SkOverdrawCanvas::onDrawEdgeAAImageSet(const ImageSetEntry set[], int count, |
| 220 | const SkPoint dstClips[], |
| 221 | const SkMatrix preViewMatrices[], const SkPaint* paint, |
| 222 | SrcRectConstraint constraint) { |
| 223 | int clipIndex = 0; |
| 224 | for (int i = 0; i < count; ++i) { |
| 225 | if (set[i].fMatrixIndex >= 0) { |
| 226 | fList[0]->save(); |
| 227 | fList[0]->concat(preViewMatrices[set[i].fMatrixIndex]); |
| 228 | } |
| 229 | if (set[i].fHasClip) { |
| 230 | SkPath path; |
| 231 | path.addPoly(dstClips + clipIndex, 4, true); |
| 232 | clipIndex += 4; |
| 233 | fList[0]->onDrawPath(path, fPaint); |
| 234 | } else { |
| 235 | fList[0]->onDrawRect(set[i].fDstRect, fPaint); |
| 236 | } |
| 237 | if (set[i].fMatrixIndex >= 0) { |
| 238 | fList[0]->restore(); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | inline SkPaint SkOverdrawCanvas::overdrawPaint(const SkPaint& paint) { |
| 244 | SkPaint newPaint = fPaint; |
| 245 | newPaint.setStyle(paint.getStyle()); |
| 246 | newPaint.setStrokeWidth(paint.getStrokeWidth()); |
| 247 | return newPaint; |
| 248 | } |
| 249 | |