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