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