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/SkAlphaType.h"
11 #include "include/core/SkColorPriv.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkDrawable.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPath.h"
18 #include "include/core/SkPathTypes.h"
19 #include "include/core/SkPixmap.h"
20 #include "include/core/SkRRect.h"
21 #include "include/core/SkRSXform.h"
22 #include "include/core/SkScalar.h"
23 #include "include/core/SkShader.h"
24 #include "include/core/SkSpan.h"
25 #include "include/core/SkSurface.h"
26 #include "include/core/SkTypes.h"
27 #include "include/core/SkVertices.h"
28 #include "include/private/base/SkFloatingPoint.h"
29 #include "include/private/chromium/Slug.h" // IWYU pragma: keep
30 #include "src/core/SkEnumerate.h"
31 #include "src/core/SkImageFilterTypes.h"
32 #include "src/core/SkImageFilter_Base.h"
33 #include "src/core/SkLatticeIter.h"
34 #include "src/core/SkMatrixPriv.h"
35 #include "src/core/SkMemset.h"
36 #include "src/core/SkPathPriv.h"
37 #include "src/core/SkRectPriv.h"
38 #include "src/core/SkScalerContext.h"
39 #include "src/core/SkSpecialImage.h"
40 #include "src/text/GlyphRun.h"
41 #include "src/utils/SkPatchUtils.h"
42
43 #include <cstdint>
44
SkDevice(const SkImageInfo & info,const SkSurfaceProps & surfaceProps)45 SkDevice::SkDevice(const SkImageInfo& info, const SkSurfaceProps& surfaceProps)
46 : fInfo(info)
47 , fSurfaceProps(surfaceProps) {
48 fDeviceToGlobal.setIdentity();
49 fGlobalToDevice.setIdentity();
50 }
51
setDeviceCoordinateSystem(const SkM44 & deviceToGlobal,const SkM44 & globalToDevice,const SkM44 & localToDevice,int bufferOriginX,int bufferOriginY)52 void SkDevice::setDeviceCoordinateSystem(const SkM44& deviceToGlobal,
53 const SkM44& globalToDevice,
54 const SkM44& localToDevice,
55 int bufferOriginX,
56 int bufferOriginY) {
57 fDeviceToGlobal = deviceToGlobal;
58 fDeviceToGlobal.normalizePerspective();
59 fGlobalToDevice = globalToDevice;
60 fGlobalToDevice.normalizePerspective();
61
62 fLocalToDevice = localToDevice;
63 fLocalToDevice.normalizePerspective();
64 if (bufferOriginX | bufferOriginY) {
65 fDeviceToGlobal.preTranslate(bufferOriginX, bufferOriginY);
66 fGlobalToDevice.postTranslate(-bufferOriginX, -bufferOriginY);
67 fLocalToDevice.postTranslate(-bufferOriginX, -bufferOriginY);
68 }
69 fLocalToDevice33 = fLocalToDevice.asM33();
70 fLocalToDeviceDirty = true;
71 }
72
setGlobalCTM(const SkM44 & ctm)73 void SkDevice::setGlobalCTM(const SkM44& ctm) {
74 fLocalToDevice = ctm;
75 fLocalToDevice.normalizePerspective();
76 // Map from the global CTM state to this device's coordinate system.
77 fLocalToDevice.postConcat(fGlobalToDevice);
78 fLocalToDevice33 = fLocalToDevice.asM33();
79 fLocalToDeviceDirty = true;
80 }
81
isPixelAlignedToGlobal() const82 bool SkDevice::isPixelAlignedToGlobal() const {
83 // pixelAligned is set to the identity + integer translation of the device-to-global matrix.
84 // If they are equal then the device is by definition pixel aligned.
85 SkM44 pixelAligned = SkM44();
86 pixelAligned.setRC(0, 3, SkScalarFloorToScalar(fDeviceToGlobal.rc(0, 3)));
87 pixelAligned.setRC(1, 3, SkScalarFloorToScalar(fDeviceToGlobal.rc(1, 3)));
88 return pixelAligned == fDeviceToGlobal;
89 }
90
getOrigin() const91 SkIPoint SkDevice::getOrigin() const {
92 // getOrigin() is deprecated, the old origin has been moved into the fDeviceToGlobal matrix.
93 // This extracts the origin from the matrix, but asserts that a more complicated coordinate
94 // space hasn't been set of the device. This function can be removed once existing use cases
95 // have been updated to use the device-to-global matrix instead or have themselves been removed
96 // (e.g. Android's device-space clip regions are going away, and are not compatible with the
97 // generalized device coordinate system).
98 SkASSERT(this->isPixelAlignedToGlobal());
99 return SkIPoint::Make(SkScalarFloorToInt(fDeviceToGlobal.rc(0, 3)),
100 SkScalarFloorToInt(fDeviceToGlobal.rc(1, 3)));
101 }
102
getRelativeTransform(const SkDevice & dstDevice) const103 SkMatrix SkDevice::getRelativeTransform(const SkDevice& dstDevice) const {
104 // To get the transform from this space to the other device's, transform from our space to
105 // global and then from global to the other device.
106 return (dstDevice.fGlobalToDevice * fDeviceToGlobal).asM33();
107 }
108
is_int(float x)109 static inline bool is_int(float x) {
110 return x == (float) sk_float_round2int(x);
111 }
112
drawRegion(const SkRegion & region,const SkPaint & paint)113 void SkDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
114 const SkMatrix& localToDevice = this->localToDevice();
115 bool isNonTranslate = localToDevice.getType() & ~(SkMatrix::kTranslate_Mask);
116 bool complexPaint = paint.getStyle() != SkPaint::kFill_Style || paint.getMaskFilter() ||
117 paint.getPathEffect();
118 bool antiAlias = paint.isAntiAlias() && (!is_int(localToDevice.getTranslateX()) ||
119 !is_int(localToDevice.getTranslateY()));
120 if (isNonTranslate || complexPaint || antiAlias) {
121 SkPath path;
122 region.getBoundaryPath(&path);
123 path.setIsVolatile(true);
124 return this->drawPath(path, paint, true);
125 }
126
127 SkRegion::Iterator it(region);
128 while (!it.done()) {
129 this->drawRect(SkRect::Make(it.rect()), paint);
130 it.next();
131 }
132 }
133
drawArc(const SkArc & arc,const SkPaint & paint)134 void SkDevice::drawArc(const SkArc& arc, const SkPaint& paint) {
135 SkPath path;
136 bool isFillNoPathEffect = SkPaint::kFill_Style == paint.getStyle() && !paint.getPathEffect();
137 SkPathPriv::CreateDrawArcPath(&path, arc, isFillNoPathEffect);
138 this->drawPath(path, paint);
139 }
140
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)141 void SkDevice::drawDRRect(const SkRRect& outer,
142 const SkRRect& inner, const SkPaint& paint) {
143 SkPath path;
144 path.addRRect(outer);
145 path.addRRect(inner);
146 path.setFillType(SkPathFillType::kEvenOdd);
147 path.setIsVolatile(true);
148
149 this->drawPath(path, paint, true);
150 }
151
drawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],sk_sp<SkBlender> blender,const SkPaint & paint)152 void SkDevice::drawPatch(const SkPoint cubics[12], const SkColor colors[4],
153 const SkPoint texCoords[4], sk_sp<SkBlender> blender,
154 const SkPaint& paint) {
155 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &this->localToDevice());
156 auto vertices = SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height(),
157 this->imageInfo().colorSpace());
158 if (vertices) {
159 this->drawVertices(vertices.get(), std::move(blender), paint);
160 }
161 }
162
drawImageLattice(const SkImage * image,const SkCanvas::Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint & paint)163 void SkDevice::drawImageLattice(const SkImage* image, const SkCanvas::Lattice& lattice,
164 const SkRect& dst, SkFilterMode filter, const SkPaint& paint) {
165 SkLatticeIter iter(lattice, dst);
166
167 SkRect srcR, dstR;
168 SkColor c;
169 bool isFixedColor = false;
170 const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
171
172 while (iter.next(&srcR, &dstR, &isFixedColor, &c)) {
173 // TODO: support this fast-path for GPU images
174 if (isFixedColor || (srcR.width() <= 1.0f && srcR.height() <= 1.0f &&
175 image->readPixels(nullptr, info, &c, 4, srcR.fLeft, srcR.fTop))) {
176 // Fast draw with drawRect, if this is a patch containing a single color
177 // or if this is a patch containing a single pixel.
178 if (0 != c || !paint.isSrcOver()) {
179 SkPaint paintCopy(paint);
180 int alpha = SkAlphaMul(SkColorGetA(c), SkAlpha255To256(paint.getAlpha()));
181 paintCopy.setColor(SkColorSetA(c, alpha));
182 this->drawRect(dstR, paintCopy);
183 }
184 } else {
185 this->drawImageRect(image, &srcR, dstR, SkSamplingOptions(filter), paint,
186 SkCanvas::kStrict_SrcRectConstraint);
187 }
188 }
189 }
190
quad_to_tris(SkPoint tris[6],const SkPoint quad[4])191 static SkPoint* quad_to_tris(SkPoint tris[6], const SkPoint quad[4]) {
192 tris[0] = quad[0];
193 tris[1] = quad[1];
194 tris[2] = quad[2];
195
196 tris[3] = quad[0];
197 tris[4] = quad[2];
198 tris[5] = quad[3];
199
200 return tris + 6;
201 }
202
drawAtlas(const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int quadCount,sk_sp<SkBlender> blender,const SkPaint & paint)203 void SkDevice::drawAtlas(const SkRSXform xform[],
204 const SkRect tex[],
205 const SkColor colors[],
206 int quadCount,
207 sk_sp<SkBlender> blender,
208 const SkPaint& paint) {
209 const int triCount = quadCount << 1;
210 const int vertexCount = triCount * 3;
211 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
212 if (colors) {
213 flags |= SkVertices::kHasColors_BuilderFlag;
214 }
215 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, 0, flags);
216
217 SkPoint* vPos = builder.positions();
218 SkPoint* vTex = builder.texCoords();
219 SkColor* vCol = builder.colors();
220 for (int i = 0; i < quadCount; ++i) {
221 SkPoint tmp[4];
222 xform[i].toQuad(tex[i].width(), tex[i].height(), tmp);
223 vPos = quad_to_tris(vPos, tmp);
224
225 tex[i].toQuad(tmp);
226 vTex = quad_to_tris(vTex, tmp);
227
228 if (colors) {
229 SkOpts::memset32(vCol, colors[i], 6);
230 vCol += 6;
231 }
232 }
233 this->drawVertices(builder.detach().get(), std::move(blender), paint);
234 }
235
drawEdgeAAQuad(const SkRect & r,const SkPoint clip[4],SkCanvas::QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)236 void SkDevice::drawEdgeAAQuad(const SkRect& r, const SkPoint clip[4], SkCanvas::QuadAAFlags aa,
237 const SkColor4f& color, SkBlendMode mode) {
238 SkPaint paint;
239 paint.setColor4f(color);
240 paint.setBlendMode(mode);
241 paint.setAntiAlias(aa == SkCanvas::kAll_QuadAAFlags);
242
243 if (clip) {
244 // Draw the clip directly as a quad since it's a filled color with no local coords
245 SkPath clipPath;
246 clipPath.addPoly(clip, 4, true);
247 this->drawPath(clipPath, paint);
248 } else {
249 this->drawRect(r, paint);
250 }
251 }
252
drawEdgeAAImageSet(const SkCanvas::ImageSetEntry images[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)253 void SkDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry images[], int count,
254 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
255 const SkSamplingOptions& sampling, const SkPaint& paint,
256 SkCanvas::SrcRectConstraint constraint) {
257 SkASSERT(paint.getStyle() == SkPaint::kFill_Style);
258 SkASSERT(!paint.getPathEffect());
259
260 SkPaint entryPaint = paint;
261 const SkM44 baseLocalToDevice = this->localToDevice44();
262 int clipIndex = 0;
263 for (int i = 0; i < count; ++i) {
264 // TODO: Handle per-edge AA. Right now this mirrors the SkiaRenderer component of Chrome
265 // which turns off antialiasing unless all four edges should be antialiased. This avoids
266 // seaming in tiled composited layers.
267 entryPaint.setAntiAlias(images[i].fAAFlags == SkCanvas::kAll_QuadAAFlags);
268 entryPaint.setAlphaf(paint.getAlphaf() * images[i].fAlpha);
269
270 SkASSERT(images[i].fMatrixIndex < 0 || preViewMatrices);
271 if (images[i].fMatrixIndex >= 0) {
272 this->setLocalToDevice(baseLocalToDevice *
273 SkM44(preViewMatrices[images[i].fMatrixIndex]));
274 }
275
276 SkASSERT(!images[i].fHasClip || dstClips);
277 if (images[i].fHasClip) {
278 // Since drawImageRect requires a srcRect, the dst clip is implemented as a true clip
279 this->pushClipStack();
280 SkPath clipPath;
281 clipPath.addPoly(dstClips + clipIndex, 4, true);
282 this->clipPath(clipPath, SkClipOp::kIntersect, entryPaint.isAntiAlias());
283 clipIndex += 4;
284 }
285 this->drawImageRect(images[i].fImage.get(), &images[i].fSrcRect, images[i].fDstRect,
286 sampling, entryPaint, constraint);
287 if (images[i].fHasClip) {
288 this->popClipStack();
289 }
290 if (images[i].fMatrixIndex >= 0) {
291 this->setLocalToDevice(baseLocalToDevice);
292 }
293 }
294 }
295
296 ///////////////////////////////////////////////////////////////////////////////////////////////////
297
drawDrawable(SkCanvas * canvas,SkDrawable * drawable,const SkMatrix * matrix)298 void SkDevice::drawDrawable(SkCanvas* canvas, SkDrawable* drawable, const SkMatrix* matrix) {
299 drawable->draw(canvas, matrix);
300 }
301
302 ///////////////////////////////////////////////////////////////////////////////////////////////////
303
drawSpecial(SkSpecialImage *,const SkMatrix &,const SkSamplingOptions &,const SkPaint &,SkCanvas::SrcRectConstraint)304 void SkDevice::drawSpecial(SkSpecialImage*, const SkMatrix&, const SkSamplingOptions&,
305 const SkPaint&, SkCanvas::SrcRectConstraint) {}
drawCoverageMask(const SkSpecialImage *,const SkMatrix & maskToDevice,const SkSamplingOptions &,const SkPaint &)306 void SkDevice::drawCoverageMask(const SkSpecialImage*, const SkMatrix& maskToDevice,
307 const SkSamplingOptions&, const SkPaint&) {
308 // This shouldn't be reached; SkCanvas will only call this if
309 // useDrawCoverageMaskForMaskFilters() is overridden to return true.
310 SK_ABORT("Must override if useDrawCoverageMaskForMaskFilters() is true");
311 }
312
makeSpecial(const SkBitmap &)313 sk_sp<SkSpecialImage> SkDevice::makeSpecial(const SkBitmap&) { return nullptr; }
makeSpecial(const SkImage *)314 sk_sp<SkSpecialImage> SkDevice::makeSpecial(const SkImage*) { return nullptr; }
snapSpecial(const SkIRect &,bool forceCopy)315 sk_sp<SkSpecialImage> SkDevice::snapSpecial(const SkIRect&, bool forceCopy) { return nullptr; }
snapSpecialScaled(const SkIRect & subset,const SkISize & dstDims)316 sk_sp<SkSpecialImage> SkDevice::snapSpecialScaled(const SkIRect& subset,
317 const SkISize& dstDims) {
318 return nullptr;
319 }
snapSpecial()320 sk_sp<SkSpecialImage> SkDevice::snapSpecial() {
321 return this->snapSpecial(SkIRect::MakeWH(this->width(), this->height()));
322 }
323
createImageFilteringBackend(const SkSurfaceProps & surfaceProps,SkColorType colorType) const324 sk_sp<skif::Backend> SkDevice::createImageFilteringBackend(const SkSurfaceProps& surfaceProps,
325 SkColorType colorType) const {
326 return skif::MakeRasterBackend(surfaceProps, colorType);
327 }
328
drawDevice(SkDevice * device,const SkSamplingOptions & sampling,const SkPaint & paint)329 void SkDevice::drawDevice(SkDevice* device,
330 const SkSamplingOptions& sampling,
331 const SkPaint& paint) {
332 sk_sp<SkSpecialImage> deviceImage = device->snapSpecial();
333 if (deviceImage) {
334 // SkCanvas only calls drawDevice() when there are no filters (so the transform is pixel
335 // aligned). As such it can be drawn without clamping.
336 SkMatrix relativeTransform = device->getRelativeTransform(*this);
337 const bool strict = sampling != SkFilterMode::kNearest ||
338 !relativeTransform.isTranslate() ||
339 !SkScalarIsInt(relativeTransform.getTranslateX()) ||
340 !SkScalarIsInt(relativeTransform.getTranslateY());
341 this->drawSpecial(deviceImage.get(), relativeTransform, sampling, paint,
342 strict ? SkCanvas::kStrict_SrcRectConstraint
343 : SkCanvas::kFast_SrcRectConstraint);
344 }
345 }
346
drawFilteredImage(const skif::Mapping & mapping,SkSpecialImage * src,SkColorType colorType,const SkImageFilter * filter,const SkSamplingOptions & sampling,const SkPaint & paint)347 void SkDevice::drawFilteredImage(const skif::Mapping& mapping,
348 SkSpecialImage* src,
349 SkColorType colorType,
350 const SkImageFilter* filter,
351 const SkSamplingOptions& sampling,
352 const SkPaint& paint) {
353 SkASSERT(!paint.getImageFilter() && !paint.getMaskFilter());
354
355 skif::LayerSpace<SkIRect> targetOutput = mapping.deviceToLayer(
356 skif::DeviceSpace<SkIRect>(this->devClipBounds()));
357
358 if (colorType == kUnknown_SkColorType) {
359 colorType = kRGBA_8888_SkColorType;
360 }
361
362 skif::Stats stats;
363 skif::Context ctx{this->createImageFilteringBackend(src ? src->props() : this->surfaceProps(),
364 colorType),
365 mapping,
366 targetOutput,
367 skif::FilterResult(sk_ref_sp(src)),
368 this->imageInfo().colorSpace(),
369 &stats};
370
371 SkIPoint offset;
372 sk_sp<SkSpecialImage> result = as_IFB(filter)->filterImage(ctx).imageAndOffset(ctx, &offset);
373 stats.reportStats();
374 if (result) {
375 SkMatrix deviceMatrixWithOffset = mapping.layerToDevice();
376 deviceMatrixWithOffset.preTranslate(offset.fX, offset.fY);
377 this->drawSpecial(result.get(), deviceMatrixWithOffset, sampling, paint);
378 }
379 }
380
381 ///////////////////////////////////////////////////////////////////////////////////////////////////
382
accessPixels(SkPixmap * pmap)383 bool SkDevice::accessPixels(SkPixmap* pmap) {
384 SkPixmap tempStorage;
385 if (nullptr == pmap) {
386 pmap = &tempStorage;
387 }
388 return this->onAccessPixels(pmap);
389 }
390
peekPixels(SkPixmap * pmap)391 bool SkDevice::peekPixels(SkPixmap* pmap) {
392 SkPixmap tempStorage;
393 if (nullptr == pmap) {
394 pmap = &tempStorage;
395 }
396 return this->onPeekPixels(pmap);
397 }
398
399 //////////////////////////////////////////////////////////////////////////////////////////
400
make_post_inverse_lm(const SkShader * shader,const SkMatrix & lm)401 static sk_sp<SkShader> make_post_inverse_lm(const SkShader* shader, const SkMatrix& lm) {
402 SkMatrix inverse_lm;
403 if (!shader || !lm.invert(&inverse_lm)) {
404 return nullptr;
405 }
406
407 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) // b/256873449
408 // Legacy impl for old concat order. This does not work for arbitrary shader DAGs (when there is
409 // no single leaf local matrix).
410
411 // LMs pre-compose. In order to push a post local matrix, we peel off any existing local matrix
412 // and set a new local matrix of inverse_lm * prev_local_matrix.
413 SkMatrix prev_local_matrix;
414 const auto nested_shader = as_SB(shader)->makeAsALocalMatrixShader(&prev_local_matrix);
415 if (nested_shader) {
416 // unfurl the shader
417 shader = nested_shader.get();
418 }
419
420 return shader->makeWithLocalMatrix(inverse_lm * prev_local_matrix);
421 #endif
422
423 return shader->makeWithLocalMatrix(inverse_lm);
424 }
425
drawGlyphRunList(SkCanvas * canvas,const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)426 void SkDevice::drawGlyphRunList(SkCanvas* canvas,
427 const sktext::GlyphRunList& 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 sktext::GlyphRunList & glyphRunList,const SkPaint & paint)440 void SkDevice::simplifyGlyphRunRSXFormAndRedraw(SkCanvas* canvas,
441 const sktext::GlyphRunList& glyphRunList,
442 const SkPaint& paint) {
443 for (const sktext::GlyphRun& run : glyphRunList) {
444 if (run.scaledRotations().empty()) {
445 auto subList = glyphRunList.builder()->makeGlyphRunList(run, 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 sktext::GlyphRun 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 sktext::GlyphRunList subList =
476 glyphRunList.builder()->makeGlyphRunList(glyphRun, paint, {0, 0});
477 this->drawGlyphRunList(canvas, subList, invertingPaint);
478 }
479 }
480 }
481 }
482
convertGlyphRunListToSlug(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)483 sk_sp<sktext::gpu::Slug> SkDevice::convertGlyphRunListToSlug(
484 const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
485 return nullptr;
486 }
487
drawSlug(SkCanvas *,const sktext::gpu::Slug *,const SkPaint &)488 void SkDevice::drawSlug(SkCanvas*, const sktext::gpu::Slug*, const SkPaint&) {
489 SK_ABORT("Slug drawing not supported.");
490 }
491
492 //////////////////////////////////////////////////////////////////////////////////////////
493
makeSurface(SkImageInfo const &,SkSurfaceProps const &)494 sk_sp<SkSurface> SkDevice::makeSurface(SkImageInfo const&, SkSurfaceProps const&) {
495 return nullptr;
496 }
497
scalerContextFlags() const498 SkScalerContextFlags SkDevice::scalerContextFlags() const {
499 // If we're doing linear blending, then we can disable the gamma hacks.
500 // Otherwise, leave them on. In either case, we still want the contrast boost:
501 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
502 const SkColorSpace* const cs = fInfo.colorSpace();
503 if (cs && cs->gammaIsLinear()) {
504 return SkScalerContextFlags::kBoostContrast;
505 } else {
506 return SkScalerContextFlags::kFakeGammaAndBoostContrast;
507 }
508 }
509
510 //////////////////////////////////////////////////////////////////////////////////////////
511
SkNoPixelsDevice(const SkIRect & bounds,const SkSurfaceProps & props)512 SkNoPixelsDevice::SkNoPixelsDevice(const SkIRect& bounds, const SkSurfaceProps& props)
513 : SkNoPixelsDevice(bounds, props, nullptr) {}
514
SkNoPixelsDevice(const SkIRect & bounds,const SkSurfaceProps & props,sk_sp<SkColorSpace> colorSpace)515 SkNoPixelsDevice::SkNoPixelsDevice(const SkIRect& bounds, const SkSurfaceProps& props,
516 sk_sp<SkColorSpace> colorSpace)
517 : SkDevice(SkImageInfo::Make(bounds.size(), kUnknown_SkColorType, kUnknown_SkAlphaType,
518 std::move(colorSpace)), props) {
519 // this fails if we enable this assert: DiscardableImageMapTest.GetDiscardableImagesInRectMaxImage
520 //SkASSERT(bounds.width() >= 0 && bounds.height() >= 0);
521
522 this->setOrigin(SkM44(), bounds.left(), bounds.top());
523 fClipStack.emplace_back(this->bounds(), /*isAA=*/false, /*isRect=*/true);
524 }
525
resetForNextPicture(const SkIRect & bounds)526 bool SkNoPixelsDevice::resetForNextPicture(const SkIRect& bounds) {
527 // Resetting should only happen on the root SkNoPixelsDevice, so its device-to-global
528 // transform should be pixel aligned.
529 SkASSERT(this->isPixelAlignedToGlobal());
530 // We can only reset the device as long as its dimensions are not changing.
531 if (bounds.width() != this->width() || bounds.height() != this->height()) {
532 return false;
533 }
534
535 // And the canvas should have restored back to the original save count.
536 SkASSERT(fClipStack.size() == 1 && fClipStack[0].fDeferredSaveCount == 0);
537 // But in the event that the clip was modified w/o a save(), reset the tracking state
538 fClipStack[0].fClipBounds = this->bounds();
539 fClipStack[0].fIsAA = false;
540 fClipStack[0].fIsRect = true;
541
542 this->setOrigin(SkM44(), bounds.left(), bounds.top());
543 return true;
544 }
545
pushClipStack()546 void SkNoPixelsDevice::pushClipStack() {
547 SkASSERT(!fClipStack.empty());
548 fClipStack.back().fDeferredSaveCount++;
549 }
550
popClipStack()551 void SkNoPixelsDevice::popClipStack() {
552 SkASSERT(!fClipStack.empty());
553 if (fClipStack.back().fDeferredSaveCount > 0) {
554 fClipStack.back().fDeferredSaveCount--;
555 } else {
556 fClipStack.pop_back();
557 SkASSERT(!fClipStack.empty());
558 }
559 }
560
writableClip()561 SkNoPixelsDevice::ClipState& SkNoPixelsDevice::writableClip() {
562 SkASSERT(!fClipStack.empty());
563 ClipState& current = fClipStack.back();
564 if (current.fDeferredSaveCount > 0) {
565 current.fDeferredSaveCount--;
566 // Stash current state in case 'current' moves during a resize
567 SkIRect bounds = current.fClipBounds;
568 bool aa = current.fIsAA;
569 bool rect = current.fIsRect;
570 return fClipStack.emplace_back(bounds, aa, rect);
571 } else {
572 return current;
573 }
574 }
575
clipRect(const SkRect & rect,SkClipOp op,bool aa)576 void SkNoPixelsDevice::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
577 this->writableClip().op(op, this->localToDevice44(), rect,
578 aa, /*fillsBounds=*/true);
579 }
580
clipRRect(const SkRRect & rrect,SkClipOp op,bool aa)581 void SkNoPixelsDevice::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
582 this->writableClip().op(op, this->localToDevice44(), rrect.getBounds(),
583 aa, /*fillsBounds=*/rrect.isRect());
584 }
585
clipPath(const SkPath & path,SkClipOp op,bool aa)586 void SkNoPixelsDevice::clipPath(const SkPath& path, SkClipOp op, bool aa) {
587 // Toggle op if the path is inverse filled
588 if (path.isInverseFillType()) {
589 op = (op == SkClipOp::kDifference ? SkClipOp::kIntersect : SkClipOp::kDifference);
590 }
591 this->writableClip().op(op, this->localToDevice44(), path.getBounds(),
592 aa, /*fillsBounds=*/false);
593 }
594
clipRegion(const SkRegion & globalRgn,SkClipOp op)595 void SkNoPixelsDevice::clipRegion(const SkRegion& globalRgn, SkClipOp op) {
596 this->writableClip().op(op, this->globalToDevice(), SkRect::Make(globalRgn.getBounds()),
597 /*isAA=*/false, /*fillsBounds=*/globalRgn.isRect());
598 }
599
onClipShader(sk_sp<SkShader> shader)600 void SkNoPixelsDevice::onClipShader(sk_sp<SkShader> shader) {
601 this->writableClip().fIsRect = false;
602 }
603
replaceClip(const SkIRect & rect)604 void SkNoPixelsDevice::replaceClip(const SkIRect& rect) {
605 SkIRect deviceRect = SkMatrixPriv::MapRect(this->globalToDevice(), SkRect::Make(rect)).round();
606 if (!deviceRect.intersect(this->bounds())) {
607 deviceRect.setEmpty();
608 }
609 auto& clip = this->writableClip();
610 clip.fClipBounds = deviceRect;
611 clip.fIsRect = true;
612 clip.fIsAA = false;
613 }
614
op(SkClipOp op,const SkM44 & transform,const SkRect & bounds,bool isAA,bool fillsBounds)615 void SkNoPixelsDevice::ClipState::op(SkClipOp op, const SkM44& transform, const SkRect& bounds,
616 bool isAA, bool fillsBounds) {
617 const bool isRect = fillsBounds && SkMatrixPriv::IsScaleTranslateAsM33(transform);
618 fIsAA |= isAA;
619
620 SkRect devBounds = bounds.isEmpty() ? SkRect::MakeEmpty()
621 : SkMatrixPriv::MapRect(transform, bounds);
622 if (op == SkClipOp::kIntersect) {
623 if (!fClipBounds.intersect(isAA ? devBounds.roundOut() : devBounds.round())) {
624 fClipBounds.setEmpty();
625 }
626 // A rectangular clip remains rectangular if the intersection is a rect
627 fIsRect &= isRect;
628 } else if (isRect) {
629 // Conservatively, we can leave the clip bounds unchanged and respect the difference op.
630 // But, if we're subtracting out an axis-aligned rectangle that fully spans our existing
631 // clip on an axis, we can shrink the clip bounds.
632 SkASSERT(op == SkClipOp::kDifference);
633 SkIRect difference;
634 if (SkRectPriv::Subtract(fClipBounds, isAA ? devBounds.roundIn() : devBounds.round(),
635 &difference)) {
636 fClipBounds = difference;
637 } else {
638 // The difference couldn't be represented as a rect
639 fIsRect = false;
640 }
641 } else {
642 // A non-rect shape was applied
643 fIsRect = false;
644 }
645 }
646