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