Home | History | Annotate | Download | only in core
      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 #include "SkColorFilter.h"
     10 #include "SkDraw.h"
     11 #include "SkDrawFilter.h"
     12 #include "SkImageFilter.h"
     13 #include "SkImageFilterCache.h"
     14 #include "SkImagePriv.h"
     15 #include "SkImage_Base.h"
     16 #include "SkLatticeIter.h"
     17 #include "SkPatchUtils.h"
     18 #include "SkPathMeasure.h"
     19 #include "SkPathPriv.h"
     20 #include "SkRSXform.h"
     21 #include "SkRasterClip.h"
     22 #include "SkShader.h"
     23 #include "SkSpecialImage.h"
     24 #include "SkTLazy.h"
     25 #include "SkTextBlobRunIterator.h"
     26 #include "SkTextToPathIter.h"
     27 #include "SkUtils.h"
     28 #include "SkVertices.h"
     29 
     30 SkBaseDevice::SkBaseDevice(const SkImageInfo& info, const SkSurfaceProps& surfaceProps)
     31     : fInfo(info)
     32     , fSurfaceProps(surfaceProps)
     33 {
     34     fOrigin.setZero();
     35     fCTM.reset();
     36 }
     37 
     38 void SkBaseDevice::setOrigin(const SkMatrix& globalCTM, int x, int y) {
     39     fOrigin.set(x, y);
     40     fCTM = globalCTM;
     41     fCTM.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
     42 }
     43 
     44 void SkBaseDevice::setGlobalCTM(const SkMatrix& ctm) {
     45     fCTM = ctm;
     46     if (fOrigin.fX | fOrigin.fY) {
     47         fCTM.postTranslate(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
     48     }
     49 }
     50 
     51 bool SkBaseDevice::clipIsWideOpen() const {
     52     if (kRect_ClipType == this->onGetClipType()) {
     53         SkRegion rgn;
     54         this->onAsRgnClip(&rgn);
     55         SkASSERT(rgn.isRect());
     56         return rgn.getBounds() == SkIRect::MakeWH(this->width(), this->height());
     57     } else {
     58         return false;
     59     }
     60 }
     61 
     62 SkPixelGeometry SkBaseDevice::CreateInfo::AdjustGeometry(const SkImageInfo& info,
     63                                                          TileUsage tileUsage,
     64                                                          SkPixelGeometry geo,
     65                                                          bool preserveLCDText) {
     66     switch (tileUsage) {
     67         case kPossible_TileUsage:
     68             // (we think) for compatibility with old clients, we assume this layer can support LCD
     69             // even though they may not have marked it as opaque... seems like we should update
     70             // our callers (reed/robertphilips).
     71             break;
     72         case kNever_TileUsage:
     73             if (!preserveLCDText) {
     74                 geo = kUnknown_SkPixelGeometry;
     75             }
     76             break;
     77     }
     78     return geo;
     79 }
     80 
     81 static inline bool is_int(float x) {
     82     return x == (float) sk_float_round2int(x);
     83 }
     84 
     85 void SkBaseDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
     86     const SkMatrix& ctm = this->ctm();
     87     bool isNonTranslate = ctm.getType() & ~(SkMatrix::kTranslate_Mask);
     88     bool complexPaint = paint.getStyle() != SkPaint::kFill_Style || paint.getMaskFilter() ||
     89                         paint.getPathEffect();
     90     bool antiAlias = paint.isAntiAlias() && (!is_int(ctm.getTranslateX()) ||
     91                                              !is_int(ctm.getTranslateY()));
     92     if (isNonTranslate || complexPaint || antiAlias) {
     93         SkPath path;
     94         region.getBoundaryPath(&path);
     95         return this->drawPath(path, paint, nullptr, false);
     96     }
     97 
     98     SkRegion::Iterator it(region);
     99     while (!it.done()) {
    100         this->drawRect(SkRect::Make(it.rect()), paint);
    101         it.next();
    102     }
    103 }
    104 
    105 void SkBaseDevice::drawArc(const SkRect& oval, SkScalar startAngle,
    106                            SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
    107     SkPath path;
    108     bool isFillNoPathEffect = SkPaint::kFill_Style == paint.getStyle() && !paint.getPathEffect();
    109     SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
    110                                   isFillNoPathEffect);
    111     this->drawPath(path, paint);
    112 }
    113 
    114 void SkBaseDevice::drawDRRect(const SkRRect& outer,
    115                               const SkRRect& inner, const SkPaint& paint) {
    116     SkPath path;
    117     path.addRRect(outer);
    118     path.addRRect(inner);
    119     path.setFillType(SkPath::kEvenOdd_FillType);
    120     path.setIsVolatile(true);
    121 
    122     const SkMatrix* preMatrix = nullptr;
    123     const bool pathIsMutable = true;
    124     this->drawPath(path, paint, preMatrix, pathIsMutable);
    125 }
    126 
    127 void SkBaseDevice::drawPatch(const SkPoint cubics[12], const SkColor colors[4],
    128                              const SkPoint texCoords[4], SkBlendMode bmode,
    129                              bool interpColorsLinearly, const SkPaint& paint) {
    130     SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &this->ctm());
    131     auto vertices = SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height(),
    132                                                interpColorsLinearly);
    133     if (vertices) {
    134         this->drawVertices(vertices.get(), bmode, paint);
    135     }
    136 }
    137 
    138 void SkBaseDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
    139                                 const SkPaint &paint, SkDrawFilter* drawFilter) {
    140 
    141     SkPaint runPaint = paint;
    142 
    143     SkTextBlobRunIterator it(blob);
    144     for (;!it.done(); it.next()) {
    145         size_t textLen = it.glyphCount() * sizeof(uint16_t);
    146         const SkPoint& offset = it.offset();
    147         // applyFontToPaint() always overwrites the exact same attributes,
    148         // so it is safe to not re-seed the paint for this reason.
    149         it.applyFontToPaint(&runPaint);
    150 
    151         if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
    152             // A false return from filter() means we should abort the current draw.
    153             runPaint = paint;
    154             continue;
    155         }
    156 
    157         runPaint.setFlags(this->filterTextFlags(runPaint));
    158 
    159         switch (it.positioning()) {
    160         case SkTextBlob::kDefault_Positioning:
    161             this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint);
    162             break;
    163         case SkTextBlob::kHorizontal_Positioning:
    164             this->drawPosText(it.glyphs(), textLen, it.pos(), 1,
    165                               SkPoint::Make(x, y + offset.y()), runPaint);
    166             break;
    167         case SkTextBlob::kFull_Positioning:
    168             this->drawPosText(it.glyphs(), textLen, it.pos(), 2,
    169                               SkPoint::Make(x, y), runPaint);
    170             break;
    171         default:
    172             SkFAIL("unhandled positioning mode");
    173         }
    174 
    175         if (drawFilter) {
    176             // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
    177             runPaint = paint;
    178         }
    179     }
    180 }
    181 
    182 void SkBaseDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y,
    183                              const SkPaint& paint) {
    184     SkBitmap bm;
    185     if (as_IB(image)->getROPixels(&bm, this->imageInfo().colorSpace())) {
    186         this->drawBitmap(bm, x, y, paint);
    187     }
    188 }
    189 
    190 void SkBaseDevice::drawImageRect(const SkImage* image, const SkRect* src,
    191                                  const SkRect& dst, const SkPaint& paint,
    192                                  SkCanvas::SrcRectConstraint constraint) {
    193     SkBitmap bm;
    194     if (as_IB(image)->getROPixels(&bm, this->imageInfo().colorSpace())) {
    195         this->drawBitmapRect(bm, src, dst, paint, constraint);
    196     }
    197 }
    198 
    199 void SkBaseDevice::drawImageNine(const SkImage* image, const SkIRect& center,
    200                                  const SkRect& dst, const SkPaint& paint) {
    201     SkLatticeIter iter(image->width(), image->height(), center, dst);
    202 
    203     SkRect srcR, dstR;
    204     while (iter.next(&srcR, &dstR)) {
    205         this->drawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint);
    206     }
    207 }
    208 
    209 void SkBaseDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
    210                                   const SkRect& dst, const SkPaint& paint) {
    211     SkLatticeIter iter(bitmap.width(), bitmap.height(), center, dst);
    212 
    213     SkRect srcR, dstR;
    214     while (iter.next(&srcR, &dstR)) {
    215         this->drawBitmapRect(bitmap, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint);
    216     }
    217 }
    218 
    219 void SkBaseDevice::drawImageLattice(const SkImage* image,
    220                                     const SkCanvas::Lattice& lattice, const SkRect& dst,
    221                                     const SkPaint& paint) {
    222     SkLatticeIter iter(lattice, dst);
    223 
    224     SkRect srcR, dstR;
    225     while (iter.next(&srcR, &dstR)) {
    226         this->drawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint);
    227     }
    228 }
    229 
    230 void SkBaseDevice::drawBitmapLattice(const SkBitmap& bitmap,
    231                                      const SkCanvas::Lattice& lattice, const SkRect& dst,
    232                                      const SkPaint& paint) {
    233     SkLatticeIter iter(lattice, dst);
    234 
    235     SkRect srcR, dstR;
    236     while (iter.next(&srcR, &dstR)) {
    237         this->drawBitmapRect(bitmap, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint);
    238     }
    239 }
    240 
    241 static SkPoint* quad_to_tris(SkPoint tris[6], const SkPoint quad[4]) {
    242     tris[0] = quad[0];
    243     tris[1] = quad[1];
    244     tris[2] = quad[2];
    245 
    246     tris[3] = quad[0];
    247     tris[4] = quad[2];
    248     tris[5] = quad[3];
    249 
    250     return tris + 6;
    251 }
    252 
    253 void SkBaseDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
    254                              const SkRect tex[], const SkColor colors[], int quadCount,
    255                              SkBlendMode mode, const SkPaint& paint) {
    256     const int triCount = quadCount << 1;
    257     const int vertexCount = triCount * 3;
    258     uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
    259     if (colors) {
    260         flags |= SkVertices::kHasColors_BuilderFlag;
    261     }
    262     SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, 0, flags);
    263 
    264     SkPoint* vPos = builder.positions();
    265     SkPoint* vTex = builder.texCoords();
    266     SkColor* vCol = builder.colors();
    267     for (int i = 0; i < quadCount; ++i) {
    268         SkPoint tmp[4];
    269         xform[i].toQuad(tex[i].width(), tex[i].height(), tmp);
    270         vPos = quad_to_tris(vPos, tmp);
    271 
    272         tex[i].toQuad(tmp);
    273         vTex = quad_to_tris(vTex, tmp);
    274 
    275         if (colors) {
    276             sk_memset32(vCol, colors[i], 6);
    277             vCol += 6;
    278         }
    279     }
    280     SkPaint p(paint);
    281     p.setShader(atlas->makeShader());
    282     this->drawVertices(builder.detach().get(), mode, p);
    283 }
    284 
    285 ///////////////////////////////////////////////////////////////////////////////////////////////////
    286 
    287 void SkBaseDevice::drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&,
    288                                SkImage*, const SkMatrix&) {}
    289 sk_sp<SkSpecialImage> SkBaseDevice::makeSpecial(const SkBitmap&) { return nullptr; }
    290 sk_sp<SkSpecialImage> SkBaseDevice::makeSpecial(const SkImage*) { return nullptr; }
    291 sk_sp<SkSpecialImage> SkBaseDevice::snapSpecial() { return nullptr; }
    292 
    293 ///////////////////////////////////////////////////////////////////////////////////////////////////
    294 
    295 bool SkBaseDevice::readPixels(const SkImageInfo& info, void* dstP, size_t rowBytes, int x, int y) {
    296     return this->onReadPixels(info, dstP, rowBytes, x, y);
    297 }
    298 
    299 bool SkBaseDevice::writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
    300                                int x, int y) {
    301     return this->onWritePixels(info, pixels, rowBytes, x, y);
    302 }
    303 
    304 bool SkBaseDevice::onWritePixels(const SkImageInfo&, const void*, size_t, int, int) {
    305     return false;
    306 }
    307 
    308 bool SkBaseDevice::onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) {
    309     return false;
    310 }
    311 
    312 bool SkBaseDevice::accessPixels(SkPixmap* pmap) {
    313     SkPixmap tempStorage;
    314     if (nullptr == pmap) {
    315         pmap = &tempStorage;
    316     }
    317     return this->onAccessPixels(pmap);
    318 }
    319 
    320 bool SkBaseDevice::peekPixels(SkPixmap* pmap) {
    321     SkPixmap tempStorage;
    322     if (nullptr == pmap) {
    323         pmap = &tempStorage;
    324     }
    325     return this->onPeekPixels(pmap);
    326 }
    327 
    328 //////////////////////////////////////////////////////////////////////////////////////////
    329 
    330 static void morphpoints(SkPoint dst[], const SkPoint src[], int count,
    331                         SkPathMeasure& meas, const SkMatrix& matrix) {
    332     SkMatrix::MapXYProc proc = matrix.getMapXYProc();
    333 
    334     for (int i = 0; i < count; i++) {
    335         SkPoint pos;
    336         SkVector tangent;
    337 
    338         proc(matrix, src[i].fX, src[i].fY, &pos);
    339         SkScalar sx = pos.fX;
    340         SkScalar sy = pos.fY;
    341 
    342         if (!meas.getPosTan(sx, &pos, &tangent)) {
    343             // set to 0 if the measure failed, so that we just set dst == pos
    344             tangent.set(0, 0);
    345         }
    346 
    347         /*  This is the old way (that explains our approach but is way too slow
    348          SkMatrix    matrix;
    349          SkPoint     pt;
    350 
    351          pt.set(sx, sy);
    352          matrix.setSinCos(tangent.fY, tangent.fX);
    353          matrix.preTranslate(-sx, 0);
    354          matrix.postTranslate(pos.fX, pos.fY);
    355          matrix.mapPoints(&dst[i], &pt, 1);
    356          */
    357         dst[i].set(pos.fX - tangent.fY * sy, pos.fY + tangent.fX * sy);
    358     }
    359 }
    360 
    361 /*  TODO
    362 
    363  Need differentially more subdivisions when the follow-path is curvy. Not sure how to
    364  determine that, but we need it. I guess a cheap answer is let the caller tell us,
    365  but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
    366  */
    367 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
    368                       const SkMatrix& matrix) {
    369     SkPath::Iter    iter(src, false);
    370     SkPoint         srcP[4], dstP[3];
    371     SkPath::Verb    verb;
    372 
    373     while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
    374         switch (verb) {
    375             case SkPath::kMove_Verb:
    376                 morphpoints(dstP, srcP, 1, meas, matrix);
    377                 dst->moveTo(dstP[0]);
    378                 break;
    379             case SkPath::kLine_Verb:
    380                 // turn lines into quads to look bendy
    381                 srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
    382                 srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
    383                 morphpoints(dstP, srcP, 2, meas, matrix);
    384                 dst->quadTo(dstP[0], dstP[1]);
    385                 break;
    386             case SkPath::kQuad_Verb:
    387                 morphpoints(dstP, &srcP[1], 2, meas, matrix);
    388                 dst->quadTo(dstP[0], dstP[1]);
    389                 break;
    390             case SkPath::kCubic_Verb:
    391                 morphpoints(dstP, &srcP[1], 3, meas, matrix);
    392                 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
    393                 break;
    394             case SkPath::kClose_Verb:
    395                 dst->close();
    396                 break;
    397             default:
    398                 SkDEBUGFAIL("unknown verb");
    399                 break;
    400         }
    401     }
    402 }
    403 
    404 void SkBaseDevice::drawTextOnPath(const void* text, size_t byteLength,
    405                                   const SkPath& follow, const SkMatrix* matrix,
    406                                   const SkPaint& paint) {
    407     SkASSERT(byteLength == 0 || text != nullptr);
    408 
    409     // nothing to draw
    410     if (text == nullptr || byteLength == 0) {
    411         return;
    412     }
    413 
    414     SkTextToPathIter    iter((const char*)text, byteLength, paint, true);
    415     SkPathMeasure       meas(follow, false);
    416     SkScalar            hOffset = 0;
    417 
    418     // need to measure first
    419     if (paint.getTextAlign() != SkPaint::kLeft_Align) {
    420         SkScalar pathLen = meas.getLength();
    421         if (paint.getTextAlign() == SkPaint::kCenter_Align) {
    422             pathLen = SkScalarHalf(pathLen);
    423         }
    424         hOffset += pathLen;
    425     }
    426 
    427     const SkPath*   iterPath;
    428     SkScalar        xpos;
    429     SkMatrix        scaledMatrix;
    430     SkScalar        scale = iter.getPathScale();
    431 
    432     scaledMatrix.setScale(scale, scale);
    433 
    434     while (iter.next(&iterPath, &xpos)) {
    435         if (iterPath) {
    436             SkPath      tmp;
    437             SkMatrix    m(scaledMatrix);
    438 
    439             tmp.setIsVolatile(true);
    440             m.postTranslate(xpos + hOffset, 0);
    441             if (matrix) {
    442                 m.postConcat(*matrix);
    443             }
    444             morphpath(&tmp, *iterPath, meas, m);
    445             this->drawPath(tmp, iter.getPaint(), nullptr, true);
    446         }
    447     }
    448 }
    449 
    450 #include "SkUtils.h"
    451 typedef int (*CountTextProc)(const char* text);
    452 static int count_utf16(const char* text) {
    453     const uint16_t* prev = (uint16_t*)text;
    454     (void)SkUTF16_NextUnichar(&prev);
    455     return SkToInt((const char*)prev - text);
    456 }
    457 static int return_4(const char* text) { return 4; }
    458 static int return_2(const char* text) { return 2; }
    459 
    460 void SkBaseDevice::drawTextRSXform(const void* text, size_t len,
    461                                    const SkRSXform xform[], const SkPaint& paint) {
    462     CountTextProc proc = nullptr;
    463     switch (paint.getTextEncoding()) {
    464         case SkPaint::kUTF8_TextEncoding:
    465             proc = SkUTF8_CountUTF8Bytes;
    466             break;
    467         case SkPaint::kUTF16_TextEncoding:
    468             proc = count_utf16;
    469             break;
    470         case SkPaint::kUTF32_TextEncoding:
    471             proc = return_4;
    472             break;
    473         case SkPaint::kGlyphID_TextEncoding:
    474             proc = return_2;
    475             break;
    476     }
    477 
    478     SkMatrix localM, currM;
    479     const void* stopText = (const char*)text + len;
    480     while ((const char*)text < (const char*)stopText) {
    481         localM.setRSXform(*xform++);
    482         currM.setConcat(this->ctm(), localM);
    483         SkAutoDeviceCTMRestore adc(this, currM);
    484 
    485         int subLen = proc((const char*)text);
    486         this->drawText(text, subLen, 0, 0, paint);
    487         text = (const char*)text + subLen;
    488     }
    489 }
    490 
    491 //////////////////////////////////////////////////////////////////////////////////////////
    492 
    493 uint32_t SkBaseDevice::filterTextFlags(const SkPaint& paint) const {
    494     uint32_t flags = paint.getFlags();
    495 
    496     if (!paint.isLCDRenderText() || !paint.isAntiAlias()) {
    497         return flags;
    498     }
    499 
    500     if (kUnknown_SkPixelGeometry == fSurfaceProps.pixelGeometry()
    501         || this->onShouldDisableLCD(paint)) {
    502 
    503         flags &= ~SkPaint::kLCDRenderText_Flag;
    504         flags |= SkPaint::kGenA8FromLCD_Flag;
    505     }
    506 
    507     return flags;
    508 }
    509 
    510 sk_sp<SkSurface> SkBaseDevice::makeSurface(SkImageInfo const&, SkSurfaceProps const&) {
    511     return nullptr;
    512 }
    513 
    514 //////////////////////////////////////////////////////////////////////////////////////////
    515 
    516 void SkBaseDevice::LogDrawScaleFactor(const SkMatrix& matrix, SkFilterQuality filterQuality) {
    517 #if SK_HISTOGRAMS_ENABLED
    518     enum ScaleFactor {
    519         kUpscale_ScaleFactor,
    520         kNoScale_ScaleFactor,
    521         kDownscale_ScaleFactor,
    522         kLargeDownscale_ScaleFactor,
    523 
    524         kLast_ScaleFactor = kLargeDownscale_ScaleFactor
    525     };
    526 
    527     float rawScaleFactor = matrix.getMinScale();
    528 
    529     ScaleFactor scaleFactor;
    530     if (rawScaleFactor < 0.5f) {
    531         scaleFactor = kLargeDownscale_ScaleFactor;
    532     } else if (rawScaleFactor < 1.0f) {
    533         scaleFactor = kDownscale_ScaleFactor;
    534     } else if (rawScaleFactor > 1.0f) {
    535         scaleFactor = kUpscale_ScaleFactor;
    536     } else {
    537         scaleFactor = kNoScale_ScaleFactor;
    538     }
    539 
    540     switch (filterQuality) {
    541         case kNone_SkFilterQuality:
    542             SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.NoneFilterQuality", scaleFactor,
    543                                      kLast_ScaleFactor + 1);
    544             break;
    545         case kLow_SkFilterQuality:
    546             SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.LowFilterQuality", scaleFactor,
    547                                      kLast_ScaleFactor + 1);
    548             break;
    549         case kMedium_SkFilterQuality:
    550             SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.MediumFilterQuality", scaleFactor,
    551                                      kLast_ScaleFactor + 1);
    552             break;
    553         case kHigh_SkFilterQuality:
    554             SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.HighFilterQuality", scaleFactor,
    555                                      kLast_ScaleFactor + 1);
    556             break;
    557     }
    558 
    559     // Also log filter quality independent scale factor.
    560     SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.AnyFilterQuality", scaleFactor,
    561                              kLast_ScaleFactor + 1);
    562 
    563     // Also log an overall histogram of filter quality.
    564     SK_HISTOGRAM_ENUMERATION("FilterQuality", filterQuality, kLast_SkFilterQuality + 1);
    565 #endif
    566 }
    567 
    568