| 1 |
/* |
| 2 |
* Copyright 2018 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 "src/core/SkCanvasPriv.h" |
| 9 |
|
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorFilter.h" |
| 13 |
#include "include/core/SkImageFilter.h" |
| 14 |
#include "include/core/SkMaskFilter.h" |
| 15 |
#include "include/core/SkMatrix.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkShader.h" |
| 19 |
#include "include/private/base/SkAlign.h" |
| 20 |
#include "include/private/base/SkAssert.h" |
| 21 |
#include "include/private/base/SkTo.h" |
| 22 |
#include "src/base/SkAutoMalloc.h" |
| 23 |
#include "src/core/SkMaskFilterBase.h" |
| 24 |
#include "src/core/SkReadBuffer.h" |
| 25 |
#include "src/core/SkWriteBuffer.h" |
| 26 |
#include "src/core/SkWriter32.h" |
| 27 |
|
| 28 |
#include <limits> |
| 29 |
#include <utility> |
| 30 |
#include <cstdint> |
| 31 |
|
| 32 |
SkAutoCanvasMatrixPaint::SkAutoCanvasMatrixPaint(SkCanvas* canvas, const SkMatrix* matrix, |
| 33 |
const SkPaint* paint, const SkRect& bounds) |
| 34 |
: fCanvas(canvas) |
| 35 |
, fSaveCount(canvas->getSaveCount()) { |
| 36 |
if (paint) { |
| 37 |
SkRect newBounds = bounds; |
| 38 |
if (matrix) { |
| 39 |
matrix->mapRect(&newBounds); |
| 40 |
} |
| 41 |
canvas->saveLayer(&newBounds, paint); |
| 42 |
} else if (matrix) { |
| 43 |
canvas->save(); |
| 44 |
} |
| 45 |
|
| 46 |
if (matrix) { |
| 47 |
canvas->concat(*matrix); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
| 52 |
fCanvas->restoreToCount(fSaveCount); |
| 53 |
} |
| 54 |
|
| 55 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 56 |
|
| 57 |
bool SkCanvasPriv::ReadLattice(SkReadBuffer& buffer, SkCanvas::Lattice* lattice) { |
| 58 |
lattice->fXCount = buffer.readInt(); |
| 59 |
if (lattice->fXCount < 0) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
lattice->fXDivs = buffer.skipT<int32_t>(lattice->fXCount); |
| 63 |
lattice->fYCount = buffer.readInt(); |
| 64 |
if (lattice->fYCount < 0) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
lattice->fYDivs = buffer.skipT<int32_t>(lattice->fYCount); |
| 68 |
int flagCount = buffer.readInt(); |
| 69 |
lattice->fRectTypes = nullptr; |
| 70 |
lattice->fColors = nullptr; |
| 71 |
// flagCount was serialized as (xCount + 1) * (yCount + 1) if there were rect types and colors, |
| 72 |
// so if that isn't still the case the buffer is invalid. |
| 73 |
if (flagCount) { |
| 74 |
if (flagCount < 0 || (uint64_t) flagCount != ((uint64_t) lattice->fXCount + 1) * |
| 75 |
((uint64_t) lattice->fYCount + 1)) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
lattice->fRectTypes = buffer.skipT<SkCanvas::Lattice::RectType>(flagCount); |
| 79 |
lattice->fColors = buffer.skipT<SkColor>(flagCount); |
| 80 |
} |
| 81 |
lattice->fBounds = buffer.skipT<SkIRect>(); |
| 82 |
return buffer.isValid(); |
| 83 |
} |
| 84 |
|
| 85 |
size_t SkCanvasPriv::WriteLattice(void* buffer, const SkCanvas::Lattice& lattice) { |
| 86 |
int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0; |
| 87 |
|
| 88 |
const size_t size = (1 + lattice.fXCount + 1 + lattice.fYCount + 1) * sizeof(int32_t) + |
| 89 |
SkAlign4(flagCount * sizeof(SkCanvas::Lattice::RectType)) + |
| 90 |
SkAlign4(flagCount * sizeof(SkColor)) + |
| 91 |
sizeof(SkIRect); |
| 92 |
|
| 93 |
if (buffer) { |
| 94 |
SkWriter32 writer(buffer, size); |
| 95 |
writer.write32(lattice.fXCount); |
| 96 |
writer.write(lattice.fXDivs, lattice.fXCount * sizeof(uint32_t)); |
| 97 |
writer.write32(lattice.fYCount); |
| 98 |
writer.write(lattice.fYDivs, lattice.fYCount * sizeof(uint32_t)); |
| 99 |
writer.write32(flagCount); |
| 100 |
writer.writePad(lattice.fRectTypes, flagCount * sizeof(uint8_t)); |
| 101 |
writer.write(lattice.fColors, flagCount * sizeof(SkColor)); |
| 102 |
SkASSERT(lattice.fBounds); |
| 103 |
writer.write(lattice.fBounds, sizeof(SkIRect)); |
| 104 |
SkASSERT(writer.bytesWritten() == size); |
| 105 |
} |
| 106 |
return size; |
| 107 |
} |
| 108 |
|
| 109 |
void SkCanvasPriv::WriteLattice(SkWriteBuffer& buffer, const SkCanvas::Lattice& lattice) { |
| 110 |
const size_t size = WriteLattice(nullptr, lattice); |
| 111 |
SkAutoSMalloc<1024> storage(size); |
| 112 |
WriteLattice(storage.get(), lattice); |
| 113 |
buffer.writePad32(storage.get(), size); |
| 114 |
} |
| 115 |
|
| 116 |
void SkCanvasPriv::GetDstClipAndMatrixCounts(const SkCanvas::ImageSetEntry set[], int count, |
| 117 |
int* totalDstClipCount, int* totalMatrixCount) { |
| 118 |
int dstClipCount = 0; |
| 119 |
int maxMatrixIndex = -1; |
| 120 |
for (int i = 0; i < count; ++i) { |
| 121 |
dstClipCount += 4 * set[i].fHasClip; |
| 122 |
if (set[i].fMatrixIndex > maxMatrixIndex) { |
| 123 |
maxMatrixIndex = set[i].fMatrixIndex; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
*totalDstClipCount = dstClipCount; |
| 128 |
*totalMatrixCount = maxMatrixIndex + 1; |
| 129 |
} |
| 130 |
|
| 131 |
// Attempts to convert an image filter to its equivalent color filter, which if possible, modifies |
| 132 |
// the paint to compose the image filter's color filter into the paint's color filter slot. Returns |
| 133 |
// true if the paint has been modified. Requires the paint to have an image filter. |
| 134 |
bool SkCanvasPriv::ImageToColorFilter(SkPaint* paint) { |
| 135 |
SkASSERT(SkToBool(paint) && paint->getImageFilter()); |
| 136 |
|
| 137 |
// An image filter logically runs after any mask filter and the src-over blending against the |
| 138 |
// layer's transparent black initial content. Moving the image filter (as a color filter) into |
| 139 |
// the color filter slot causes it to run before the mask filter or blending. |
| 140 |
// |
| 141 |
// Src-over blending against transparent black is a no-op, so skipping the layer and drawing the |
| 142 |
// output of the color filter-image filter with the original blender is valid. |
| 143 |
// |
| 144 |
// If there's also a mask filter on the paint, it will operate on an alpha-only layer that's |
| 145 |
// then shaded with the paint's effects. Moving the CF-IF into the paint's color filter slot |
| 146 |
// will mean that the CF-IF operates on the output of the original CF *before* it's combined |
| 147 |
// with the coverage value. Under normal circumstances the CF-IF evaluates the color after |
| 148 |
// coverage has been multiplied into the alpha channel. |
| 149 |
// |
| 150 |
// Some color filters may behave the same, e.g. cf(color)*coverage == cf(color*coverage), but |
| 151 |
// that's hard to detect so we disable the optimization when both image filters and mask filters |
| 152 |
// are present. |
| 153 |
if (paint->getMaskFilter()) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
SkColorFilter* imgCFPtr; |
| 158 |
if (!paint->getImageFilter()->asAColorFilter(&imgCFPtr)) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
sk_sp<SkColorFilter> imgCF(imgCFPtr); |
| 162 |
|
| 163 |
SkColorFilter* paintCF = paint->getColorFilter(); |
| 164 |
if (paintCF) { |
| 165 |
// The paint has both a colorfilter(paintCF) and an imagefilter-that-is-a-colorfilter(imgCF) |
| 166 |
// and we need to combine them into a single colorfilter. |
| 167 |
imgCF = imgCF->makeComposed(sk_ref_sp(paintCF)); |
| 168 |
} |
| 169 |
|
| 170 |
paint->setColorFilter(std::move(imgCF)); |
| 171 |
paint->setImageFilter(nullptr); |
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
AutoLayerForImageFilter::AutoLayerForImageFilter(SkCanvas* canvas, |
| 176 |
const SkPaint& paint, |
| 177 |
const SkRect* rawBounds, |
| 178 |
bool skipMaskFilterLayer) |
| 179 |
: fPaint(paint) |
| 180 |
, fCanvas(canvas) |
| 181 |
, fTempLayersForFilters(0) { |
| 182 |
SkDEBUGCODE(fSaveCount = canvas->getSaveCount();) |
| 183 |
|
| 184 |
// Depending on the original paint, this will add 0, 1, or 2 layers that apply the |
| 185 |
// filter effects to a temporary layer that rasterized the remaining effects. Image filters |
| 186 |
// are applied to the result of any mask filter, so its layer is added first in the stack. |
| 187 |
// |
| 188 |
// If present on the original paint, the image filter layer's restore paint steals the blender |
| 189 |
// and the image filter so that the draw's paint will never have an image filter. |
| 190 |
if (fPaint.getImageFilter() && !SkCanvasPriv::ImageToColorFilter(&fPaint)) { |
| 191 |
this->addImageFilterLayer(rawBounds); |
| 192 |
} |
| 193 |
|
| 194 |
// If present on the original paint, the mask filter layer's restore paint steals all shading |
| 195 |
// effects and the draw's paint shading is updated to draw a solid opaque color (thus encoding |
| 196 |
// coverage into the alpha channel). The draw's paint preserves all geometric effects that have |
| 197 |
// to be applied before the mask filter. The layer's restore paint adds an image filter |
| 198 |
// representing the mask filter. |
| 199 |
if (fPaint.getMaskFilter() && !skipMaskFilterLayer) { |
| 200 |
this->addMaskFilterLayer(rawBounds); |
| 201 |
} |
| 202 |
|
| 203 |
// When the original paint has both an image filter and a mask filter, this will create two |
| 204 |
// internal layers and perform two restores when finished. This actually creates one fewer |
| 205 |
// offscreen passes compared to directly composing the mask filter's output with an |
| 206 |
// SkImageFilters::Shader node and passing that into the rest of the image filter. |
| 207 |
} |
| 208 |
|
| 209 |
AutoLayerForImageFilter::~AutoLayerForImageFilter() { |
| 210 |
for (int i = 0; i < fTempLayersForFilters; ++i) { |
| 211 |
fCanvas->fSaveCount -= 1; |
| 212 |
fCanvas->internalRestore(); |
| 213 |
} |
| 214 |
// Negative save count occurs when this layer was moved. |
| 215 |
SkASSERT(fSaveCount < 0 || fCanvas->getSaveCount() == fSaveCount); |
| 216 |
} |
| 217 |
|
| 218 |
AutoLayerForImageFilter::AutoLayerForImageFilter(AutoLayerForImageFilter&& other) { |
| 219 |
*this = std::move(other); |
| 220 |
} |
| 221 |
|
| 222 |
AutoLayerForImageFilter& AutoLayerForImageFilter::operator=(AutoLayerForImageFilter&& other) { |
| 223 |
fPaint = std::move(other.fPaint); |
| 224 |
fCanvas = other.fCanvas; |
| 225 |
fTempLayersForFilters = other.fTempLayersForFilters; |
| 226 |
SkDEBUGCODE(fSaveCount = other.fSaveCount;) |
| 227 |
|
| 228 |
other.fTempLayersForFilters = 0; |
| 229 |
SkDEBUGCODE(other.fSaveCount = -1;) |
| 230 |
|
| 231 |
return *this; |
| 232 |
} |
| 233 |
|
| 234 |
void AutoLayerForImageFilter::addImageFilterLayer(const SkRect* drawBounds) { |
| 235 |
// Shouldn't be adding a layer if there was no image filter to begin with. |
| 236 |
SkASSERT(fPaint.getImageFilter()); |
| 237 |
|
| 238 |
// The restore paint for an image filter layer simply takes the image filter and blending off |
| 239 |
// the original paint. The blending is applied post image filter because otherwise it'd be |
| 240 |
// applied with the new layer's transparent dst and not be very interesting. |
| 241 |
SkPaint restorePaint; |
| 242 |
restorePaint.setImageFilter(fPaint.refImageFilter()); |
| 243 |
restorePaint.setBlender(fPaint.refBlender()); |
| 244 |
|
| 245 |
// Remove the restorePaint fields from our "working" paint, leaving all other shading and |
| 246 |
// geometry effects to be rendered into the layer. If there happens to be a mask filter, this |
| 247 |
// paint will still trigger a second layer for that filter. |
| 248 |
fPaint.setImageFilter(nullptr); |
| 249 |
fPaint.setBlendMode(SkBlendMode::kSrcOver); |
| 250 |
|
| 251 |
this->addLayer(restorePaint, drawBounds, /*coverageOnly=*/false); |
| 252 |
} |
| 253 |
|
| 254 |
void AutoLayerForImageFilter::addMaskFilterLayer(const SkRect* drawBounds) { |
| 255 |
// Shouldn't be adding a layer if there was no mask filter to begin with. |
| 256 |
SkASSERT(fPaint.getMaskFilter()); |
| 257 |
|
| 258 |
// Image filters are evaluated after mask filters so any filter should have been converted to |
| 259 |
// a layer and removed from fPaint already. |
| 260 |
SkASSERT(!fPaint.getImageFilter()); |
| 261 |
|
| 262 |
// TODO: Eventually all SkMaskFilters will implement this method so this can switch to an assert |
| 263 |
auto [maskFilterAsImageFilter, appliesShading] = as_MFB( |
| 264 |
fPaint.getMaskFilter())->asImageFilter(fCanvas->getTotalMatrix(), fPaint); |
| 265 |
if (!maskFilterAsImageFilter) { |
| 266 |
// This is a legacy mask filter that can be handled by raster and Ganesh directly, but will |
| 267 |
// be ignored by Graphite. Return now, leaving the paint with the mask filter so that the |
| 268 |
// underlying SkDevice can handle it if it will. |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
// The restore paint for the coverage layer takes over all shading effects that had been on the |
| 273 |
// original paint, which will be applied to the alpha-only output image from the mask filter |
| 274 |
// converted to an image filter. |
| 275 |
// If we know our mask filter will affect shading, we don't want to add the original shading |
| 276 |
// into the restore paint. |
| 277 |
SkPaint restorePaint; |
| 278 |
if (!appliesShading) { |
| 279 |
restorePaint.setColor4f(fPaint.getColor4f()); |
| 280 |
restorePaint.setShader(fPaint.refShader()); |
| 281 |
restorePaint.setColorFilter(fPaint.refColorFilter()); |
| 282 |
restorePaint.setDither(fPaint.isDither()); |
| 283 |
} |
| 284 |
restorePaint.setBlender(fPaint.refBlender()); |
| 285 |
restorePaint.setImageFilter(maskFilterAsImageFilter); |
| 286 |
|
| 287 |
// Remove all shading effects from the "working" paint so that the layer's alpha channel |
| 288 |
// will correspond to the coverage. This leaves the original style and AA settings that |
| 289 |
// contribute to coverage (including any path effect). |
| 290 |
fPaint.setColor4f(SkColors::kWhite); |
| 291 |
fPaint.setShader(nullptr); |
| 292 |
fPaint.setColorFilter(nullptr); |
| 293 |
fPaint.setMaskFilter(nullptr); |
| 294 |
fPaint.setDither(false); |
| 295 |
fPaint.setBlendMode(SkBlendMode::kSrcOver); |
| 296 |
|
| 297 |
this->addLayer(restorePaint, drawBounds, /*coverageOnly=*/!appliesShading); |
| 298 |
} |
| 299 |
|
| 300 |
void AutoLayerForImageFilter::addLayer(const SkPaint& restorePaint, |
| 301 |
const SkRect* drawBounds, |
| 302 |
bool coverageOnly) { |
| 303 |
SkRect storage; |
| 304 |
const SkRect* contentBounds = nullptr; |
| 305 |
if (drawBounds && fPaint.canComputeFastBounds()) { |
| 306 |
// The content bounds will include all paint outsets except for those that have been |
| 307 |
// extracted into 'restorePaint' or a previously added layer. |
| 308 |
contentBounds = &fPaint.computeFastBounds(*drawBounds, &storage); |
| 309 |
} |
| 310 |
|
| 311 |
fCanvas->fSaveCount += 1; |
| 312 |
fCanvas->internalSaveLayer(SkCanvas::SaveLayerRec(contentBounds, &restorePaint), |
| 313 |
SkCanvas::kFullLayer_SaveLayerStrategy, |
| 314 |
coverageOnly); |
| 315 |
fTempLayersForFilters += 1; |
| 316 |
} |