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