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/SkImagePriv.h" |
22 | #include "src/core/SkLatticeIter.h" |
23 | #include "src/core/SkMarkerStack.h" |
24 | #include "src/core/SkMatrixPriv.h" |
25 | #include "src/core/SkPathPriv.h" |
26 | #include "src/core/SkRasterClip.h" |
27 | #include "src/core/SkSpecialImage.h" |
28 | #include "src/core/SkTLazy.h" |
29 | #include "src/core/SkTextBlobPriv.h" |
30 | #include "src/core/SkUtils.h" |
31 | #include "src/image/SkImage_Base.h" |
32 | #include "src/shaders/SkLocalMatrixShader.h" |
33 | #include "src/utils/SkPatchUtils.h" |
34 | |
35 | SkBaseDevice::SkBaseDevice(const SkImageInfo& info, const SkSurfaceProps& surfaceProps) |
36 | : SkMatrixProvider(/* localToDevice = */ SkMatrix::I()) |
37 | , fInfo(info) |
38 | , fSurfaceProps(surfaceProps) { |
39 | fDeviceToGlobal.reset(); |
40 | fGlobalToDevice.reset(); |
41 | } |
42 | |
43 | void SkBaseDevice::setDeviceCoordinateSystem(const SkMatrix& deviceToGlobal, |
44 | const SkM44& localToDevice, |
45 | int bufferOriginX, |
46 | int bufferOriginY) { |
47 | fDeviceToGlobal = deviceToGlobal; |
48 | fDeviceToGlobal.normalizePerspective(); |
49 | SkAssertResult(deviceToGlobal.invert(&fGlobalToDevice)); |
50 | |
51 | fLocalToDevice = localToDevice; |
52 | fLocalToDevice.normalizePerspective(); |
53 | if (bufferOriginX | bufferOriginY) { |
54 | fDeviceToGlobal.preTranslate(bufferOriginX, bufferOriginY); |
55 | fGlobalToDevice.postTranslate(-bufferOriginX, -bufferOriginY); |
56 | fLocalToDevice.postTranslate(-bufferOriginX, -bufferOriginY); |
57 | } |
58 | fLocalToDevice33 = fLocalToDevice.asM33(); |
59 | } |
60 | |
61 | void SkBaseDevice::setGlobalCTM(const SkM44& ctm) { |
62 | fLocalToDevice = ctm; |
63 | fLocalToDevice.normalizePerspective(); |
64 | if (!fGlobalToDevice.isIdentity()) { |
65 | // Map from the global CTM state to this device's coordinate system. |
66 | fLocalToDevice.postConcat(SkM44(fGlobalToDevice)); |
67 | } |
68 | fLocalToDevice33 = fLocalToDevice.asM33(); |
69 | } |
70 | |
71 | bool SkBaseDevice::isPixelAlignedToGlobal() const { |
72 | return fDeviceToGlobal.isTranslate() && |
73 | SkScalarIsInt(fDeviceToGlobal.getTranslateX()) && |
74 | SkScalarIsInt(fDeviceToGlobal.getTranslateY()); |
75 | } |
76 | |
77 | SkIPoint SkBaseDevice::getOrigin() const { |
78 | // getOrigin() is deprecated, the old origin has been moved into the fDeviceToGlobal matrix. |
79 | // This extracts the origin from the matrix, but asserts that a more complicated coordinate |
80 | // space hasn't been set of the device. This function can be removed once existing use cases |
81 | // have been updated to use the device-to-global matrix instead or have themselves been removed |
82 | // (e.g. Android's device-space clip regions are going away, and are not compatible with the |
83 | // generalized device coordinate system). |
84 | SkASSERT(this->isPixelAlignedToGlobal()); |
85 | return SkIPoint::Make(SkScalarFloorToInt(fDeviceToGlobal.getTranslateX()), |
86 | SkScalarFloorToInt(fDeviceToGlobal.getTranslateY())); |
87 | } |
88 | |
89 | SkMatrix SkBaseDevice::getRelativeTransform(const SkBaseDevice& inputDevice) const { |
90 | // To get the transform from the input's space to this space, transform from the input space to |
91 | // the global space, and then from the global space back to this space. |
92 | return SkMatrix::Concat(fGlobalToDevice, inputDevice.fDeviceToGlobal); |
93 | } |
94 | |
95 | bool SkBaseDevice::getLocalToMarker(uint32_t id, SkM44* localToMarker) const { |
96 | // The marker stack stores CTM snapshots, which are "marker to global" matrices. |
97 | // We ask for the (cached) inverse, which is a "global to marker" matrix. |
98 | SkM44 globalToMarker; |
99 | // ID 0 is special, and refers to the CTM (local-to-global) |
100 | if (fMarkerStack && (id == 0 || fMarkerStack->findMarkerInverse(id, &globalToMarker))) { |
101 | if (localToMarker) { |
102 | // globalToMarker will still be the identity if id is zero |
103 | *localToMarker = globalToMarker * SkM44(fDeviceToGlobal) * fLocalToDevice; |
104 | } |
105 | return true; |
106 | } |
107 | return false; |
108 | } |
109 | |
110 | static inline bool is_int(float x) { |
111 | return x == (float) sk_float_round2int(x); |
112 | } |
113 | |
114 | void SkBaseDevice::drawRegion(const SkRegion& region, const SkPaint& paint) { |
115 | const SkMatrix& localToDevice = this->localToDevice(); |
116 | bool isNonTranslate = localToDevice.getType() & ~(SkMatrix::kTranslate_Mask); |
117 | bool complexPaint = paint.getStyle() != SkPaint::kFill_Style || paint.getMaskFilter() || |
118 | paint.getPathEffect(); |
119 | bool antiAlias = paint.isAntiAlias() && (!is_int(localToDevice.getTranslateX()) || |
120 | !is_int(localToDevice.getTranslateY())); |
121 | if (isNonTranslate || complexPaint || antiAlias) { |
122 | SkPath path; |
123 | region.getBoundaryPath(&path); |
124 | path.setIsVolatile(true); |
125 | return this->drawPath(path, paint, true); |
126 | } |
127 | |
128 | SkRegion::Iterator it(region); |
129 | while (!it.done()) { |
130 | this->drawRect(SkRect::Make(it.rect()), paint); |
131 | it.next(); |
132 | } |
133 | } |
134 | |
135 | void SkBaseDevice::drawArc(const SkRect& oval, SkScalar startAngle, |
136 | SkScalar sweepAngle, bool useCenter, const SkPaint& paint) { |
137 | SkPath path; |
138 | bool isFillNoPathEffect = SkPaint::kFill_Style == paint.getStyle() && !paint.getPathEffect(); |
139 | SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter, |
140 | isFillNoPathEffect); |
141 | this->drawPath(path, paint); |
142 | } |
143 | |
144 | void SkBaseDevice::drawDRRect(const SkRRect& outer, |
145 | const SkRRect& inner, const SkPaint& paint) { |
146 | SkPath path; |
147 | path.addRRect(outer); |
148 | path.addRRect(inner); |
149 | path.setFillType(SkPathFillType::kEvenOdd); |
150 | path.setIsVolatile(true); |
151 | |
152 | this->drawPath(path, paint, true); |
153 | } |
154 | |
155 | void SkBaseDevice::drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
156 | const SkPoint texCoords[4], SkBlendMode bmode, const SkPaint& paint) { |
157 | SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &this->localToDevice()); |
158 | auto vertices = SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height(), |
159 | this->imageInfo().colorSpace()); |
160 | if (vertices) { |
161 | this->drawVertices(vertices.get(), bmode, paint); |
162 | } |
163 | } |
164 | |
165 | void SkBaseDevice::drawImageNine(const SkImage* image, const SkIRect& center, |
166 | const SkRect& dst, const SkPaint& paint) { |
167 | SkLatticeIter iter(image->width(), image->height(), center, dst); |
168 | |
169 | SkRect srcR, dstR; |
170 | while (iter.next(&srcR, &dstR)) { |
171 | this->drawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint); |
172 | } |
173 | } |
174 | |
175 | void SkBaseDevice::drawImageLattice(const SkImage* image, |
176 | const SkCanvas::Lattice& lattice, const SkRect& dst, |
177 | const SkPaint& paint) { |
178 | SkLatticeIter iter(lattice, dst); |
179 | |
180 | SkRect srcR, dstR; |
181 | SkColor c; |
182 | bool isFixedColor = false; |
183 | const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
184 | |
185 | while (iter.next(&srcR, &dstR, &isFixedColor, &c)) { |
186 | if (isFixedColor || (srcR.width() <= 1.0f && srcR.height() <= 1.0f && |
187 | image->readPixels(info, &c, 4, srcR.fLeft, srcR.fTop))) { |
188 | // Fast draw with drawRect, if this is a patch containing a single color |
189 | // or if this is a patch containing a single pixel. |
190 | if (0 != c || !paint.isSrcOver()) { |
191 | SkPaint paintCopy(paint); |
192 | int alpha = SkAlphaMul(SkColorGetA(c), SkAlpha255To256(paint.getAlpha())); |
193 | paintCopy.setColor(SkColorSetA(c, alpha)); |
194 | this->drawRect(dstR, paintCopy); |
195 | } |
196 | } else { |
197 | this->drawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint); |
198 | } |
199 | } |
200 | } |
201 | |
202 | static SkPoint* quad_to_tris(SkPoint tris[6], const SkPoint quad[4]) { |
203 | tris[0] = quad[0]; |
204 | tris[1] = quad[1]; |
205 | tris[2] = quad[2]; |
206 | |
207 | tris[3] = quad[0]; |
208 | tris[4] = quad[2]; |
209 | tris[5] = quad[3]; |
210 | |
211 | return tris + 6; |
212 | } |
213 | |
214 | void SkBaseDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[], |
215 | const SkRect tex[], const SkColor colors[], int quadCount, |
216 | SkBlendMode mode, const SkPaint& paint) { |
217 | const int triCount = quadCount << 1; |
218 | const int vertexCount = triCount * 3; |
219 | uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag; |
220 | if (colors) { |
221 | flags |= SkVertices::kHasColors_BuilderFlag; |
222 | } |
223 | SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, 0, flags); |
224 | |
225 | SkPoint* vPos = builder.positions(); |
226 | SkPoint* vTex = builder.texCoords(); |
227 | SkColor* vCol = builder.colors(); |
228 | for (int i = 0; i < quadCount; ++i) { |
229 | SkPoint tmp[4]; |
230 | xform[i].toQuad(tex[i].width(), tex[i].height(), tmp); |
231 | vPos = quad_to_tris(vPos, tmp); |
232 | |
233 | tex[i].toQuad(tmp); |
234 | vTex = quad_to_tris(vTex, tmp); |
235 | |
236 | if (colors) { |
237 | sk_memset32(vCol, colors[i], 6); |
238 | vCol += 6; |
239 | } |
240 | } |
241 | SkPaint p(paint); |
242 | p.setShader(atlas->makeShader()); |
243 | this->drawVertices(builder.detach().get(), mode, p); |
244 | } |
245 | |
246 | |
247 | void SkBaseDevice::drawEdgeAAQuad(const SkRect& r, const SkPoint clip[4], SkCanvas::QuadAAFlags aa, |
248 | const SkColor4f& color, SkBlendMode mode) { |
249 | SkPaint paint; |
250 | paint.setColor4f(color); |
251 | paint.setBlendMode(mode); |
252 | paint.setAntiAlias(aa == SkCanvas::kAll_QuadAAFlags); |
253 | |
254 | if (clip) { |
255 | // Draw the clip directly as a quad since it's a filled color with no local coords |
256 | SkPath clipPath; |
257 | clipPath.addPoly(clip, 4, true); |
258 | this->drawPath(clipPath, paint); |
259 | } else { |
260 | this->drawRect(r, paint); |
261 | } |
262 | } |
263 | |
264 | void SkBaseDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry images[], int count, |
265 | const SkPoint dstClips[], const SkMatrix preViewMatrices[], |
266 | const SkPaint& paint, |
267 | SkCanvas::SrcRectConstraint constraint) { |
268 | SkASSERT(paint.getStyle() == SkPaint::kFill_Style); |
269 | SkASSERT(!paint.getPathEffect()); |
270 | |
271 | SkPaint entryPaint = paint; |
272 | const SkM44 baseLocalToDevice = this->localToDevice44(); |
273 | int clipIndex = 0; |
274 | for (int i = 0; i < count; ++i) { |
275 | // TODO: Handle per-edge AA. Right now this mirrors the SkiaRenderer component of Chrome |
276 | // which turns off antialiasing unless all four edges should be antialiased. This avoids |
277 | // seaming in tiled composited layers. |
278 | entryPaint.setAntiAlias(images[i].fAAFlags == SkCanvas::kAll_QuadAAFlags); |
279 | entryPaint.setAlphaf(paint.getAlphaf() * images[i].fAlpha); |
280 | |
281 | bool needsRestore = false; |
282 | SkASSERT(images[i].fMatrixIndex < 0 || preViewMatrices); |
283 | if (images[i].fMatrixIndex >= 0) { |
284 | this->save(); |
285 | this->setLocalToDevice(baseLocalToDevice * |
286 | SkM44(preViewMatrices[images[i].fMatrixIndex])); |
287 | needsRestore = true; |
288 | } |
289 | |
290 | SkASSERT(!images[i].fHasClip || dstClips); |
291 | if (images[i].fHasClip) { |
292 | // Since drawImageRect requires a srcRect, the dst clip is implemented as a true clip |
293 | if (!needsRestore) { |
294 | this->save(); |
295 | needsRestore = true; |
296 | } |
297 | SkPath clipPath; |
298 | clipPath.addPoly(dstClips + clipIndex, 4, true); |
299 | this->clipPath(clipPath, SkClipOp::kIntersect, entryPaint.isAntiAlias()); |
300 | clipIndex += 4; |
301 | } |
302 | this->drawImageRect(images[i].fImage.get(), &images[i].fSrcRect, images[i].fDstRect, |
303 | entryPaint, constraint); |
304 | if (needsRestore) { |
305 | this->restoreLocal(baseLocalToDevice); |
306 | } |
307 | } |
308 | } |
309 | |
310 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
311 | |
312 | void SkBaseDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) { |
313 | drawable->draw(canvas, matrix); |
314 | } |
315 | |
316 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
317 | |
318 | void SkBaseDevice::drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&) {} |
319 | sk_sp<SkSpecialImage> SkBaseDevice::makeSpecial(const SkBitmap&) { return nullptr; } |
320 | sk_sp<SkSpecialImage> SkBaseDevice::makeSpecial(const SkImage*) { return nullptr; } |
321 | sk_sp<SkSpecialImage> SkBaseDevice::snapSpecial(const SkIRect&, bool) { return nullptr; } |
322 | sk_sp<SkSpecialImage> SkBaseDevice::snapSpecial() { |
323 | return this->snapSpecial(SkIRect::MakeWH(this->width(), this->height())); |
324 | } |
325 | |
326 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
327 | |
328 | bool SkBaseDevice::readPixels(const SkPixmap& pm, int x, int y) { |
329 | return this->onReadPixels(pm, x, y); |
330 | } |
331 | |
332 | bool SkBaseDevice::writePixels(const SkPixmap& pm, int x, int y) { |
333 | return this->onWritePixels(pm, x, y); |
334 | } |
335 | |
336 | bool SkBaseDevice::onWritePixels(const SkPixmap&, int, int) { |
337 | return false; |
338 | } |
339 | |
340 | bool SkBaseDevice::onReadPixels(const SkPixmap&, int x, int y) { |
341 | return false; |
342 | } |
343 | |
344 | bool SkBaseDevice::accessPixels(SkPixmap* pmap) { |
345 | SkPixmap tempStorage; |
346 | if (nullptr == pmap) { |
347 | pmap = &tempStorage; |
348 | } |
349 | return this->onAccessPixels(pmap); |
350 | } |
351 | |
352 | bool SkBaseDevice::peekPixels(SkPixmap* pmap) { |
353 | SkPixmap tempStorage; |
354 | if (nullptr == pmap) { |
355 | pmap = &tempStorage; |
356 | } |
357 | return this->onPeekPixels(pmap); |
358 | } |
359 | |
360 | ////////////////////////////////////////////////////////////////////////////////////////// |
361 | |
362 | #include "src/core/SkUtils.h" |
363 | |
364 | void SkBaseDevice::drawGlyphRunRSXform(const SkFont& font, const SkGlyphID glyphs[], |
365 | const SkRSXform xform[], int count, SkPoint origin, |
366 | const SkPaint& paint) { |
367 | const SkM44 originalLocalToDevice = this->localToDevice44(); |
368 | if (!originalLocalToDevice.isFinite() || !SkScalarIsFinite(font.getSize()) || |
369 | !SkScalarIsFinite(font.getScaleX()) || |
370 | !SkScalarIsFinite(font.getSkewX())) { |
371 | return; |
372 | } |
373 | |
374 | SkPoint sharedPos{0, 0}; // we're at the origin |
375 | SkGlyphID glyphID; |
376 | SkGlyphRun glyphRun{ |
377 | font, |
378 | SkSpan<const SkPoint>{&sharedPos, 1}, |
379 | SkSpan<const SkGlyphID>{&glyphID, 1}, |
380 | SkSpan<const char>{}, |
381 | SkSpan<const uint32_t>{} |
382 | }; |
383 | |
384 | for (int i = 0; i < count; i++) { |
385 | glyphID = glyphs[i]; |
386 | // now "glyphRun" is pointing at the current glyphID |
387 | |
388 | SkMatrix glyphToDevice; |
389 | glyphToDevice.setRSXform(xform[i]).postTranslate(origin.fX, origin.fY); |
390 | |
391 | // We want to rotate each glyph by the rsxform, but we don't want to rotate "space" |
392 | // (i.e. the shader that cares about the ctm) so we have to undo our little ctm trick |
393 | // with a localmatrixshader so that the shader draws as if there was no change to the ctm. |
394 | SkPaint transformingPaint{paint}; |
395 | auto shader = transformingPaint.getShader(); |
396 | if (shader) { |
397 | SkMatrix inverse; |
398 | if (glyphToDevice.invert(&inverse)) { |
399 | transformingPaint.setShader(shader->makeWithLocalMatrix(inverse)); |
400 | } else { |
401 | transformingPaint.setShader(nullptr); // can't handle this xform |
402 | } |
403 | } |
404 | |
405 | this->setLocalToDevice(originalLocalToDevice * SkM44(glyphToDevice)); |
406 | |
407 | this->drawGlyphRunList(SkGlyphRunList{glyphRun, transformingPaint}); |
408 | } |
409 | this->setLocalToDevice(originalLocalToDevice); |
410 | } |
411 | |
412 | ////////////////////////////////////////////////////////////////////////////////////////// |
413 | |
414 | sk_sp<SkSurface> SkBaseDevice::makeSurface(SkImageInfo const&, SkSurfaceProps const&) { |
415 | return nullptr; |
416 | } |
417 | |
418 | ////////////////////////////////////////////////////////////////////////////////////////// |
419 | |
420 | void SkBaseDevice::LogDrawScaleFactor(const SkMatrix& view, const SkMatrix& srcToDst, |
421 | SkFilterQuality filterQuality) { |
422 | #if SK_HISTOGRAMS_ENABLED |
423 | SkMatrix matrix = SkMatrix::Concat(view, srcToDst); |
424 | enum ScaleFactor { |
425 | kUpscale_ScaleFactor, |
426 | kNoScale_ScaleFactor, |
427 | kDownscale_ScaleFactor, |
428 | kLargeDownscale_ScaleFactor, |
429 | |
430 | kLast_ScaleFactor = kLargeDownscale_ScaleFactor |
431 | }; |
432 | |
433 | float rawScaleFactor = matrix.getMinScale(); |
434 | |
435 | ScaleFactor scaleFactor; |
436 | if (rawScaleFactor < 0.5f) { |
437 | scaleFactor = kLargeDownscale_ScaleFactor; |
438 | } else if (rawScaleFactor < 1.0f) { |
439 | scaleFactor = kDownscale_ScaleFactor; |
440 | } else if (rawScaleFactor > 1.0f) { |
441 | scaleFactor = kUpscale_ScaleFactor; |
442 | } else { |
443 | scaleFactor = kNoScale_ScaleFactor; |
444 | } |
445 | |
446 | switch (filterQuality) { |
447 | case kNone_SkFilterQuality: |
448 | SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.NoneFilterQuality" , scaleFactor, |
449 | kLast_ScaleFactor + 1); |
450 | break; |
451 | case kLow_SkFilterQuality: |
452 | SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.LowFilterQuality" , scaleFactor, |
453 | kLast_ScaleFactor + 1); |
454 | break; |
455 | case kMedium_SkFilterQuality: |
456 | SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.MediumFilterQuality" , scaleFactor, |
457 | kLast_ScaleFactor + 1); |
458 | break; |
459 | case kHigh_SkFilterQuality: |
460 | SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.HighFilterQuality" , scaleFactor, |
461 | kLast_ScaleFactor + 1); |
462 | break; |
463 | } |
464 | |
465 | // Also log filter quality independent scale factor. |
466 | SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.AnyFilterQuality" , scaleFactor, |
467 | kLast_ScaleFactor + 1); |
468 | |
469 | // Also log an overall histogram of filter quality. |
470 | SK_HISTOGRAM_ENUMERATION("FilterQuality" , filterQuality, kLast_SkFilterQuality + 1); |
471 | #endif |
472 | } |
473 | |