1 /*
2 * Copyright 2011 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/SkDevice.h"
9
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkDrawable.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkPathMeasure.h"
14 #include "include/core/SkRSXform.h"
15 #include "include/core/SkShader.h"
16 #include "include/core/SkVertices.h"
17 #include "include/private/SkTo.h"
18 #include "src/core/SkDraw.h"
19 #include "src/core/SkGlyphRun.h"
20 #include "src/core/SkImageFilterCache.h"
21 #include "src/core/SkImageFilter_Base.h"
22 #include "src/core/SkImagePriv.h"
23 #include "src/core/SkLatticeIter.h"
24 #include "src/core/SkMatrixPriv.h"
25 #include "src/core/SkOpts.h"
26 #include "src/core/SkPathPriv.h"
27 #include "src/core/SkRasterClip.h"
28 #include "src/core/SkRectPriv.h"
29 #include "src/core/SkSpecialImage.h"
30 #include "src/core/SkTLazy.h"
31 #include "src/core/SkTextBlobPriv.h"
32 #include "src/image/SkImage_Base.h"
33 #include "src/shaders/SkLocalMatrixShader.h"
34 #include "src/utils/SkPatchUtils.h"
35 #if SK_SUPPORT_GPU
36 #include "include/private/chromium/GrSlug.h"
37 #endif
38
SkBaseDevice(const SkImageInfo & info,const SkSurfaceProps & surfaceProps)39 SkBaseDevice::SkBaseDevice(const SkImageInfo& info, const SkSurfaceProps& surfaceProps)
40 : SkMatrixProvider(/* localToDevice = */ SkMatrix::I())
41 , fInfo(info)
42 , fSurfaceProps(surfaceProps) {
43 fDeviceToGlobal.setIdentity();
44 fGlobalToDevice.setIdentity();
45 }
46
setDeviceCoordinateSystem(const SkM44 & deviceToGlobal,const SkM44 & localToDevice,int bufferOriginX,int bufferOriginY)47 bool SkBaseDevice::setDeviceCoordinateSystem(const SkM44& deviceToGlobal,
48 const SkM44& localToDevice,
49 int bufferOriginX,
50 int bufferOriginY) {
51 fDeviceToGlobal = deviceToGlobal;
52 fDeviceToGlobal.normalizePerspective();
53 if (!fDeviceToGlobal.invert(&fGlobalToDevice)) {
54 return false;
55 }
56
57 fLocalToDevice = localToDevice;
58 fLocalToDevice.normalizePerspective();
59 if (bufferOriginX | bufferOriginY) {
60 fDeviceToGlobal.preTranslate(bufferOriginX, bufferOriginY);
61 fGlobalToDevice.postTranslate(-bufferOriginX, -bufferOriginY);
62 fLocalToDevice.postTranslate(-bufferOriginX, -bufferOriginY);
63 }
64 fLocalToDevice33 = fLocalToDevice.asM33();
65 return true;
66 }
67
setGlobalCTM(const SkM44 & ctm)68 void SkBaseDevice::setGlobalCTM(const SkM44& ctm) {
69 fLocalToDevice = ctm;
70 fLocalToDevice.normalizePerspective();
71 // Map from the global CTM state to this device's coordinate system.
72 fLocalToDevice.postConcat(fGlobalToDevice);
73 fLocalToDevice33 = fLocalToDevice.asM33();
74 }
75
isPixelAlignedToGlobal() const76 bool SkBaseDevice::isPixelAlignedToGlobal() const {
77 // pixelAligned is set to the identity + integer translation of the device-to-global matrix.
78 // If they are equal then the device is by definition pixel aligned.
79 SkM44 pixelAligned = SkM44();
80 pixelAligned.setRC(0, 3, SkScalarFloorToScalar(fDeviceToGlobal.rc(0, 3)));
81 pixelAligned.setRC(1, 3, SkScalarFloorToScalar(fDeviceToGlobal.rc(1, 3)));
82 return pixelAligned == fDeviceToGlobal;
83 }
84
getOrigin() const85 SkIPoint SkBaseDevice::getOrigin() const {
86 // getOrigin() is deprecated, the old origin has been moved into the fDeviceToGlobal matrix.
87 // This extracts the origin from the matrix, but asserts that a more complicated coordinate
88 // space hasn't been set of the device. This function can be removed once existing use cases
89 // have been updated to use the device-to-global matrix instead or have themselves been removed
90 // (e.g. Android's device-space clip regions are going away, and are not compatible with the
91 // generalized device coordinate system).
92 SkASSERT(this->isPixelAlignedToGlobal());
93 return SkIPoint::Make(SkScalarFloorToInt(fDeviceToGlobal.rc(0, 3)),
94 SkScalarFloorToInt(fDeviceToGlobal.rc(1, 3)));
95 }
96
getRelativeTransform(const SkBaseDevice & dstDevice) const97 SkMatrix SkBaseDevice::getRelativeTransform(const SkBaseDevice& dstDevice) const {
98 // To get the transform from this space to the other device's, transform from our space to
99 // global and then from global to the other device.
100 return (dstDevice.fGlobalToDevice * fDeviceToGlobal).asM33();
101 }
102
is_int(float x)103 static inline bool is_int(float x) {
104 return x == (float) sk_float_round2int(x);
105 }
106
drawRegion(const SkRegion & region,const SkPaint & paint)107 void SkBaseDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
108 const SkMatrix& localToDevice = this->localToDevice();
109 bool isNonTranslate = localToDevice.getType() & ~(SkMatrix::kTranslate_Mask);
110 bool complexPaint = paint.getStyle() != SkPaint::kFill_Style || paint.getMaskFilter() ||
111 paint.getPathEffect();
112 bool antiAlias = paint.isAntiAlias() && (!is_int(localToDevice.getTranslateX()) ||
113 !is_int(localToDevice.getTranslateY()));
114 if (isNonTranslate || complexPaint || antiAlias) {
115 SkPath path;
116 region.getBoundaryPath(&path);
117 path.setIsVolatile(true);
118 return this->drawPath(path, paint, true);
119 }
120
121 SkRegion::Iterator it(region);
122 while (!it.done()) {
123 this->drawRect(SkRect::Make(it.rect()), paint);
124 it.next();
125 }
126 }
127
drawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)128 void SkBaseDevice::drawArc(const SkRect& oval, SkScalar startAngle,
129 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
130 SkPath path;
131 bool isFillNoPathEffect = SkPaint::kFill_Style == paint.getStyle() && !paint.getPathEffect();
132 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
133 isFillNoPathEffect);
134 this->drawPath(path, paint);
135 }
136
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)137 void SkBaseDevice::drawDRRect(const SkRRect& outer,
138 const SkRRect& inner, const SkPaint& paint) {
139 SkPath path;
140 path.addRRect(outer);
141 path.addRRect(inner);
142 path.setFillType(SkPathFillType::kEvenOdd);
143 path.setIsVolatile(true);
144
145 this->drawPath(path, paint, true);
146 }
147
drawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],sk_sp<SkBlender> blender,const SkPaint & paint)148 void SkBaseDevice::drawPatch(const SkPoint cubics[12], const SkColor colors[4],
149 const SkPoint texCoords[4], sk_sp<SkBlender> blender,
150 const SkPaint& paint) {
151 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &this->localToDevice());
152 auto vertices = SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height(),
153 this->imageInfo().colorSpace());
154 if (vertices) {
155 this->drawVertices(vertices.get(), std::move(blender), paint);
156 }
157 }
158
drawImageLattice(const SkImage * image,const SkCanvas::Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint & paint)159 void SkBaseDevice::drawImageLattice(const SkImage* image, const SkCanvas::Lattice& lattice,
160 const SkRect& dst, SkFilterMode filter, const SkPaint& paint) {
161 SkLatticeIter iter(lattice, dst);
162
163 SkRect srcR, dstR;
164 SkColor c;
165 bool isFixedColor = false;
166 const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
167
168 while (iter.next(&srcR, &dstR, &isFixedColor, &c)) {
169 // TODO: support this fast-path for GPU images
170 if (isFixedColor || (srcR.width() <= 1.0f && srcR.height() <= 1.0f &&
171 image->readPixels(nullptr, info, &c, 4, srcR.fLeft, srcR.fTop))) {
172 // Fast draw with drawRect, if this is a patch containing a single color
173 // or if this is a patch containing a single pixel.
174 if (0 != c || !paint.isSrcOver()) {
175 SkPaint paintCopy(paint);
176 int alpha = SkAlphaMul(SkColorGetA(c), SkAlpha255To256(paint.getAlpha()));
177 paintCopy.setColor(SkColorSetA(c, alpha));
178 this->drawRect(dstR, paintCopy);
179 }
180 } else {
181 this->drawImageRect(image, &srcR, dstR, SkSamplingOptions(filter), paint,
182 SkCanvas::kStrict_SrcRectConstraint);
183 }
184 }
185 }
186
quad_to_tris(SkPoint tris[6],const SkPoint quad[4])187 static SkPoint* quad_to_tris(SkPoint tris[6], const SkPoint quad[4]) {
188 tris[0] = quad[0];
189 tris[1] = quad[1];
190 tris[2] = quad[2];
191
192 tris[3] = quad[0];
193 tris[4] = quad[2];
194 tris[5] = quad[3];
195
196 return tris + 6;
197 }
198
drawAtlas(const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int quadCount,sk_sp<SkBlender> blender,const SkPaint & paint)199 void SkBaseDevice::drawAtlas(const SkRSXform xform[],
200 const SkRect tex[],
201 const SkColor colors[],
202 int quadCount,
203 sk_sp<SkBlender> blender,
204 const SkPaint& paint) {
205 const int triCount = quadCount << 1;
206 const int vertexCount = triCount * 3;
207 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
208 if (colors) {
209 flags |= SkVertices::kHasColors_BuilderFlag;
210 }
211 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, 0, flags);
212
213 SkPoint* vPos = builder.positions();
214 SkPoint* vTex = builder.texCoords();
215 SkColor* vCol = builder.colors();
216 for (int i = 0; i < quadCount; ++i) {
217 SkPoint tmp[4];
218 xform[i].toQuad(tex[i].width(), tex[i].height(), tmp);
219 vPos = quad_to_tris(vPos, tmp);
220
221 tex[i].toQuad(tmp);
222 vTex = quad_to_tris(vTex, tmp);
223
224 if (colors) {
225 sk_memset32(vCol, colors[i], 6);
226 vCol += 6;
227 }
228 }
229 this->drawVertices(builder.detach().get(), std::move(blender), paint);
230 }
231
drawEdgeAAQuad(const SkRect & r,const SkPoint clip[4],SkCanvas::QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)232 void SkBaseDevice::drawEdgeAAQuad(const SkRect& r, const SkPoint clip[4], SkCanvas::QuadAAFlags aa,
233 const SkColor4f& color, SkBlendMode mode) {
234 SkPaint paint;
235 paint.setColor4f(color);
236 paint.setBlendMode(mode);
237 paint.setAntiAlias(aa == SkCanvas::kAll_QuadAAFlags);
238
239 if (clip) {
240 // Draw the clip directly as a quad since it's a filled color with no local coords
241 SkPath clipPath;
242 clipPath.addPoly(clip, 4, true);
243 this->drawPath(clipPath, paint);
244 } else {
245 this->drawRect(r, paint);
246 }
247 }
248
drawEdgeAAImageSet(const SkCanvas::ImageSetEntry images[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)249 void SkBaseDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry images[], int count,
250 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
251 const SkSamplingOptions& sampling, const SkPaint& paint,
252 SkCanvas::SrcRectConstraint constraint) {
253 SkASSERT(paint.getStyle() == SkPaint::kFill_Style);
254 SkASSERT(!paint.getPathEffect());
255
256 SkPaint entryPaint = paint;
257 const SkM44 baseLocalToDevice = this->localToDevice44();
258 int clipIndex = 0;
259 for (int i = 0; i < count; ++i) {
260 // TODO: Handle per-edge AA. Right now this mirrors the SkiaRenderer component of Chrome
261 // which turns off antialiasing unless all four edges should be antialiased. This avoids
262 // seaming in tiled composited layers.
263 entryPaint.setAntiAlias(images[i].fAAFlags == SkCanvas::kAll_QuadAAFlags);
264 entryPaint.setAlphaf(paint.getAlphaf() * images[i].fAlpha);
265
266 bool needsRestore = false;
267 SkASSERT(images[i].fMatrixIndex < 0 || preViewMatrices);
268 if (images[i].fMatrixIndex >= 0) {
269 this->save();
270 this->setLocalToDevice(baseLocalToDevice *
271 SkM44(preViewMatrices[images[i].fMatrixIndex]));
272 needsRestore = true;
273 }
274
275 SkASSERT(!images[i].fHasClip || dstClips);
276 if (images[i].fHasClip) {
277 // Since drawImageRect requires a srcRect, the dst clip is implemented as a true clip
278 if (!needsRestore) {
279 this->save();
280 needsRestore = true;
281 }
282 SkPath clipPath;
283 clipPath.addPoly(dstClips + clipIndex, 4, true);
284 this->clipPath(clipPath, SkClipOp::kIntersect, entryPaint.isAntiAlias());
285 clipIndex += 4;
286 }
287 this->drawImageRect(images[i].fImage.get(), &images[i].fSrcRect, images[i].fDstRect,
288 sampling, entryPaint, constraint);
289 if (needsRestore) {
290 this->restoreLocal(baseLocalToDevice);
291 }
292 }
293 }
294
295 ///////////////////////////////////////////////////////////////////////////////////////////////////
296
drawDrawable(SkCanvas * canvas,SkDrawable * drawable,const SkMatrix * matrix)297 void SkBaseDevice::drawDrawable(SkCanvas* canvas, SkDrawable* drawable, const SkMatrix* matrix) {
298 drawable->draw(canvas, matrix);
299 }
300
301 ///////////////////////////////////////////////////////////////////////////////////////////////////
302
drawSpecial(SkSpecialImage *,const SkMatrix &,const SkSamplingOptions &,const SkPaint &)303 void SkBaseDevice::drawSpecial(SkSpecialImage*, const SkMatrix&, const SkSamplingOptions&,
304 const SkPaint&) {}
makeSpecial(const SkBitmap &)305 sk_sp<SkSpecialImage> SkBaseDevice::makeSpecial(const SkBitmap&) { return nullptr; }
makeSpecial(const SkImage *)306 sk_sp<SkSpecialImage> SkBaseDevice::makeSpecial(const SkImage*) { return nullptr; }
snapSpecial(const SkIRect &,bool)307 sk_sp<SkSpecialImage> SkBaseDevice::snapSpecial(const SkIRect&, bool) { return nullptr; }
snapSpecial()308 sk_sp<SkSpecialImage> SkBaseDevice::snapSpecial() {
309 return this->snapSpecial(SkIRect::MakeWH(this->width(), this->height()));
310 }
311
drawDevice(SkBaseDevice * device,const SkSamplingOptions & sampling,const SkPaint & paint)312 void SkBaseDevice::drawDevice(SkBaseDevice* device, const SkSamplingOptions& sampling,
313 const SkPaint& paint) {
314 sk_sp<SkSpecialImage> deviceImage = device->snapSpecial();
315 if (deviceImage) {
316 this->drawSpecial(deviceImage.get(), device->getRelativeTransform(*this), sampling, paint);
317 }
318 }
319
drawFilteredImage(const skif::Mapping & mapping,SkSpecialImage * src,const SkImageFilter * filter,const SkSamplingOptions & sampling,const SkPaint & paint)320 void SkBaseDevice::drawFilteredImage(const skif::Mapping& mapping, SkSpecialImage* src,
321 const SkImageFilter* filter, const SkSamplingOptions& sampling,
322 const SkPaint& paint) {
323 SkASSERT(!paint.getImageFilter() && !paint.getMaskFilter());
324
325 skif::LayerSpace<SkIRect> targetOutput = mapping.deviceToLayer(
326 skif::DeviceSpace<SkIRect>(this->devClipBounds()));
327
328 // FIXME If the saved layer (so src) was created to use F16, should we do all image filtering
329 // in F16 and then only flatten to the destination color encoding at the end?
330 // Currently, this context converts everything to the dst color type ASAP.
331 SkColorType colorType = this->imageInfo().colorType();
332 if (colorType == kUnknown_SkColorType) {
333 colorType = kRGBA_8888_SkColorType;
334 }
335
336 // getImageFilterCache returns a bare image filter cache pointer that must be ref'ed until the
337 // filter's filterImage(ctx) function returns.
338 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
339 skif::Context ctx(mapping, targetOutput, cache.get(), colorType, this->imageInfo().colorSpace(),
340 skif::FilterResult(sk_ref_sp(src)));
341
342 SkIPoint offset;
343 sk_sp<SkSpecialImage> result = as_IFB(filter)->filterImage(ctx).imageAndOffset(&offset);
344 if (result) {
345 SkMatrix deviceMatrixWithOffset = mapping.deviceMatrix();
346 deviceMatrixWithOffset.preTranslate(offset.fX, offset.fY);
347 this->drawSpecial(result.get(), deviceMatrixWithOffset, sampling, paint);
348 }
349 }
350
351 ///////////////////////////////////////////////////////////////////////////////////////////////////
352
readPixels(const SkPixmap & pm,int x,int y)353 bool SkBaseDevice::readPixels(const SkPixmap& pm, int x, int y) {
354 return this->onReadPixels(pm, x, y);
355 }
356
writePixels(const SkPixmap & pm,int x,int y)357 bool SkBaseDevice::writePixels(const SkPixmap& pm, int x, int y) {
358 return this->onWritePixels(pm, x, y);
359 }
360
onWritePixels(const SkPixmap &,int,int)361 bool SkBaseDevice::onWritePixels(const SkPixmap&, int, int) {
362 return false;
363 }
364
onReadPixels(const SkPixmap &,int x,int y)365 bool SkBaseDevice::onReadPixels(const SkPixmap&, int x, int y) {
366 return false;
367 }
368
accessPixels(SkPixmap * pmap)369 bool SkBaseDevice::accessPixels(SkPixmap* pmap) {
370 SkPixmap tempStorage;
371 if (nullptr == pmap) {
372 pmap = &tempStorage;
373 }
374 return this->onAccessPixels(pmap);
375 }
376
peekPixels(SkPixmap * pmap)377 bool SkBaseDevice::peekPixels(SkPixmap* pmap) {
378 SkPixmap tempStorage;
379 if (nullptr == pmap) {
380 pmap = &tempStorage;
381 }
382 return this->onPeekPixels(pmap);
383 }
384
385 //////////////////////////////////////////////////////////////////////////////////////////
386
387 #include "src/core/SkUtils.h"
388
389
390 // TODO: This does not work for arbitrary shader DAGs (when there is no single leaf local matrix).
391 // What we really need is proper post-LM plumbing for shaders.
make_post_inverse_lm(const SkShader * shader,const SkMatrix & m)392 static sk_sp<SkShader> make_post_inverse_lm(const SkShader* shader, const SkMatrix& m) {
393 SkMatrix inverse;
394 if (!shader || !m.invert(&inverse)) {
395 return nullptr;
396 }
397
398 // Normal LMs pre-compose. In order to push a post local matrix, we shoot for
399 // something along these lines (where all new components are pre-composed):
400 //
401 // new_lm X current_lm == current_lm X inv(current_lm) X new_lm X current_lm
402 //
403 // We also have two sources of local matrices:
404 // - the actual shader lm
405 // - outer lms applied via SkLocalMatrixShader
406
407 SkMatrix outer_lm;
408 const auto nested_shader = as_SB(shader)->makeAsALocalMatrixShader(&outer_lm);
409 if (nested_shader) {
410 // unfurl the shader
411 shader = nested_shader.get();
412 } else {
413 outer_lm.reset();
414 }
415
416 const auto lm = *as_SB(shader)->totalLocalMatrix(nullptr);
417 SkMatrix lm_inv;
418 if (!lm.invert(&lm_inv)) {
419 return nullptr;
420 }
421
422 // Note: since we unfurled the shader above, we don't need to apply an outer_lm inverse
423 return shader->makeWithLocalMatrix(lm_inv * inverse * lm * outer_lm);
424 }
425
drawGlyphRunList(SkCanvas * canvas,const SkGlyphRunList & glyphRunList,const SkPaint & paint)426 void SkBaseDevice::drawGlyphRunList(SkCanvas* canvas,
427 const SkGlyphRunList& glyphRunList,
428 const SkPaint& paint) {
429 if (!this->localToDevice().isFinite()) {
430 return;
431 }
432
433 if (!glyphRunList.hasRSXForm()) {
434 this->onDrawGlyphRunList(canvas, glyphRunList, paint);
435 } else {
436 this->simplifyGlyphRunRSXFormAndRedraw(canvas, glyphRunList, paint);
437 }
438 }
439
simplifyGlyphRunRSXFormAndRedraw(SkCanvas * canvas,const SkGlyphRunList & glyphRunList,const SkPaint & paint)440 void SkBaseDevice::simplifyGlyphRunRSXFormAndRedraw(SkCanvas* canvas,
441 const SkGlyphRunList& glyphRunList,
442 const SkPaint& paint) {
443 for (const SkGlyphRun& run : glyphRunList) {
444 if (run.scaledRotations().empty()) {
445 SkGlyphRunList subList{run, run.sourceBounds(paint), {0, 0}};
446 this->drawGlyphRunList(canvas, subList, paint);
447 } else {
448 SkPoint origin = glyphRunList.origin();
449 SkPoint sharedPos{0, 0}; // we're at the origin
450 SkGlyphID sharedGlyphID;
451 SkGlyphRun glyphRun {
452 run.font(),
453 SkSpan<const SkPoint>{&sharedPos, 1},
454 SkSpan<const SkGlyphID>{&sharedGlyphID, 1},
455 SkSpan<const char>{},
456 SkSpan<const uint32_t>{},
457 SkSpan<const SkVector>{}
458 };
459
460 for (auto [i, glyphID, pos] : SkMakeEnumerate(run.source())) {
461 sharedGlyphID = glyphID;
462 auto [scos, ssin] = run.scaledRotations()[i];
463 SkRSXform rsxForm = SkRSXform::Make(scos, ssin, pos.x(), pos.y());
464 SkMatrix glyphToLocal;
465 glyphToLocal.setRSXform(rsxForm).postTranslate(origin.x(), origin.y());
466
467 // We want to rotate each glyph by the rsxform, but we don't want to rotate "space"
468 // (i.e. the shader that cares about the ctm) so we have to undo our little ctm
469 // trick with a localmatrixshader so that the shader draws as if there was no
470 // change to the ctm.
471 SkPaint invertingPaint{paint};
472 invertingPaint.setShader(make_post_inverse_lm(paint.getShader(), glyphToLocal));
473 SkAutoCanvasRestore acr(canvas, true);
474 canvas->concat(SkM44(glyphToLocal));
475 SkGlyphRunList subList{glyphRun, glyphRun.sourceBounds(paint), {0, 0}};
476 this->drawGlyphRunList(canvas, subList, invertingPaint);
477 }
478 }
479 }
480 }
481
482 #if SK_SUPPORT_GPU
convertGlyphRunListToSlug(const SkGlyphRunList & glyphRunList,const SkPaint & paint)483 sk_sp<GrSlug> SkBaseDevice::convertGlyphRunListToSlug(
484 const SkGlyphRunList& glyphRunList,
485 const SkPaint& paint) {
486 return nullptr;
487 }
488
drawSlug(SkCanvas *,GrSlug *)489 void SkBaseDevice::drawSlug(SkCanvas*, GrSlug*) {
490 SK_ABORT("GrSlug drawing not supported.");
491 }
492 #endif
493
494 //////////////////////////////////////////////////////////////////////////////////////////
495
makeSurface(SkImageInfo const &,SkSurfaceProps const &)496 sk_sp<SkSurface> SkBaseDevice::makeSurface(SkImageInfo const&, SkSurfaceProps const&) {
497 return nullptr;
498 }
499
500 //////////////////////////////////////////////////////////////////////////////////////////
501
onSave()502 void SkNoPixelsDevice::onSave() {
503 SkASSERT(!fClipStack.empty());
504 fClipStack.back().fDeferredSaveCount++;
505 }
506
onRestore()507 void SkNoPixelsDevice::onRestore() {
508 SkASSERT(!fClipStack.empty());
509 if (fClipStack.back().fDeferredSaveCount > 0) {
510 fClipStack.back().fDeferredSaveCount--;
511 } else {
512 fClipStack.pop_back();
513 SkASSERT(!fClipStack.empty());
514 }
515 }
516
writableClip()517 SkNoPixelsDevice::ClipState& SkNoPixelsDevice::writableClip() {
518 SkASSERT(!fClipStack.empty());
519 ClipState& current = fClipStack.back();
520 if (current.fDeferredSaveCount > 0) {
521 current.fDeferredSaveCount--;
522 // Stash current state in case 'current' moves during a resize
523 SkIRect bounds = current.fClipBounds;
524 bool aa = current.fIsAA;
525 bool rect = current.fIsRect;
526 return fClipStack.emplace_back(bounds, aa, rect);
527 } else {
528 return current;
529 }
530 }
531
onClipRect(const SkRect & rect,SkClipOp op,bool aa)532 void SkNoPixelsDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) {
533 this->writableClip().op(op, this->localToDevice44(), rect,
534 aa, /*fillsBounds=*/true);
535 }
536
onClipRRect(const SkRRect & rrect,SkClipOp op,bool aa)537 void SkNoPixelsDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
538 this->writableClip().op(op, this->localToDevice44(), rrect.getBounds(),
539 aa, /*fillsBounds=*/rrect.isRect());
540 }
541
onClipPath(const SkPath & path,SkClipOp op,bool aa)542 void SkNoPixelsDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) {
543 // Toggle op if the path is inverse filled
544 if (path.isInverseFillType()) {
545 op = (op == SkClipOp::kDifference ? SkClipOp::kIntersect : SkClipOp::kDifference);
546 }
547 this->writableClip().op(op, this->localToDevice44(), path.getBounds(),
548 aa, /*fillsBounds=*/false);
549 }
550
onClipRegion(const SkRegion & globalRgn,SkClipOp op)551 void SkNoPixelsDevice::onClipRegion(const SkRegion& globalRgn, SkClipOp op) {
552 this->writableClip().op(op, this->globalToDevice(), SkRect::Make(globalRgn.getBounds()),
553 /*isAA=*/false, /*fillsBounds=*/globalRgn.isRect());
554 }
555
onClipShader(sk_sp<SkShader> shader)556 void SkNoPixelsDevice::onClipShader(sk_sp<SkShader> shader) {
557 this->writableClip().fIsRect = false;
558 }
559
onReplaceClip(const SkIRect & rect)560 void SkNoPixelsDevice::onReplaceClip(const SkIRect& rect) {
561 SkIRect deviceRect = SkMatrixPriv::MapRect(this->globalToDevice(), SkRect::Make(rect)).round();
562 if (!deviceRect.intersect(this->bounds())) {
563 deviceRect.setEmpty();
564 }
565 auto& clip = this->writableClip();
566 clip.fClipBounds = deviceRect;
567 clip.fIsRect = true;
568 clip.fIsAA = false;
569 }
570
onGetClipType() const571 SkBaseDevice::ClipType SkNoPixelsDevice::onGetClipType() const {
572 const auto& clip = this->clip();
573 if (clip.fClipBounds.isEmpty()) {
574 return ClipType::kEmpty;
575 } else if (clip.fIsRect) {
576 return ClipType::kRect;
577 } else {
578 return ClipType::kComplex;
579 }
580 }
581
op(SkClipOp op,const SkM44 & transform,const SkRect & bounds,bool isAA,bool fillsBounds)582 void SkNoPixelsDevice::ClipState::op(SkClipOp op, const SkM44& transform, const SkRect& bounds,
583 bool isAA, bool fillsBounds) {
584 const bool isRect = fillsBounds && SkMatrixPriv::IsScaleTranslateAsM33(transform);
585 fIsAA |= isAA;
586
587 SkRect devBounds = bounds.isEmpty() ? SkRect::MakeEmpty()
588 : SkMatrixPriv::MapRect(transform, bounds);
589 if (op == SkClipOp::kIntersect) {
590 if (!fClipBounds.intersect(isAA ? devBounds.roundOut() : devBounds.round())) {
591 fClipBounds.setEmpty();
592 }
593 // A rectangular clip remains rectangular if the intersection is a rect
594 fIsRect &= isRect;
595 } else if (isRect) {
596 // Conservatively, we can leave the clip bounds unchanged and respect the difference op.
597 // But, if we're subtracting out an axis-aligned rectangle that fully spans our existing
598 // clip on an axis, we can shrink the clip bounds.
599 SkASSERT(op == SkClipOp::kDifference);
600 SkIRect difference;
601 if (SkRectPriv::Subtract(fClipBounds, isAA ? devBounds.roundIn() : devBounds.round(),
602 &difference)) {
603 fClipBounds = difference;
604 } else {
605 // The difference couldn't be represented as a rect
606 fIsRect = false;
607 }
608 } else {
609 // A non-rect shape was applied
610 fIsRect = false;
611 }
612 }
613