LCOV - code coverage report
Current view: top level - gfx/skia/skia/src/core - SkScalerContext.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 78 457 17.1 %
Date: 2017-07-14 16:53:18 Functions: 11 37 29.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright 2006 The Android Open Source Project
       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 "SkScalerContext.h"
       9             : 
      10             : #include "SkAutoMalloc.h"
      11             : #include "SkAutoPixmapStorage.h"
      12             : #include "SkColorPriv.h"
      13             : #include "SkDescriptor.h"
      14             : #include "SkDraw.h"
      15             : #include "SkGlyph.h"
      16             : #include "SkMakeUnique.h"
      17             : #include "SkMaskFilter.h"
      18             : #include "SkMaskGamma.h"
      19             : #include "SkMatrix22.h"
      20             : #include "SkPathEffect.h"
      21             : #include "SkRasterClip.h"
      22             : #include "SkRasterizer.h"
      23             : #include "SkReadBuffer.h"
      24             : #include "SkStroke.h"
      25             : #include "SkStrokeRec.h"
      26             : #include "SkWriteBuffer.h"
      27             : 
      28             : #define ComputeBWRowBytes(width)        (((unsigned)(width) + 7) >> 3)
      29             : 
      30          58 : void SkGlyph::toMask(SkMask* mask) const {
      31          58 :     SkASSERT(mask);
      32             : 
      33          58 :     mask->fImage = (uint8_t*)fImage;
      34          58 :     mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight);
      35          58 :     mask->fRowBytes = this->rowBytes();
      36          58 :     mask->fFormat = static_cast<SkMask::Format>(fMaskFormat);
      37          58 : }
      38             : 
      39           0 : size_t SkGlyph::computeImageSize() const {
      40           0 :     const size_t size = this->rowBytes() * fHeight;
      41             : 
      42           0 :     switch (fMaskFormat) {
      43             :         case SkMask::k3D_Format:
      44           0 :             return 3 * size;
      45             :         default:
      46           0 :             return size;
      47             :     }
      48             : }
      49             : 
      50          60 : void SkGlyph::zeroMetrics() {
      51          60 :     fAdvanceX = 0;
      52          60 :     fAdvanceY = 0;
      53          60 :     fWidth    = 0;
      54          60 :     fHeight   = 0;
      55          60 :     fTop      = 0;
      56          60 :     fLeft     = 0;
      57          60 :     fRsbDelta = 0;
      58          60 :     fLsbDelta = 0;
      59          60 : }
      60             : 
      61             : ///////////////////////////////////////////////////////////////////////////////
      62             : 
      63             : #ifdef SK_DEBUG
      64             :     #define DUMP_RECx
      65             : #endif
      66             : 
      67           2 : SkScalerContext::SkScalerContext(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects,
      68           2 :                                  const SkDescriptor* desc)
      69           2 :     : fRec(*static_cast<const Rec*>(desc->findEntry(kRec_SkDescriptorTag, nullptr)))
      70             : 
      71           2 :     , fTypeface(std::move(typeface))
      72           2 :     , fPathEffect(sk_ref_sp(effects.fPathEffect))
      73           2 :     , fMaskFilter(sk_ref_sp(effects.fMaskFilter))
      74           2 :     , fRasterizer(sk_ref_sp(effects.fRasterizer))
      75             :       // Initialize based on our settings. Subclasses can also force this.
      76           2 :     , fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != nullptr || fRasterizer != nullptr)
      77             : 
      78           2 :     , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec))
      79             :     , fPreBlendForFilter(fMaskFilter ? SkScalerContext::GetMaskPreBlend(fRec)
      80          16 :                                      : SkMaskGamma::PreBlend())
      81             : {
      82             : #ifdef DUMP_REC
      83             :     desc->assertChecksum();
      84             :     SkDebugf("SkScalerContext checksum %x count %d length %d\n",
      85             :              desc->getChecksum(), desc->getCount(), desc->getLength());
      86             :     SkDebugf(" textsize %g prescale %g preskew %g post [%g %g %g %g]\n",
      87             :         rec->fTextSize, rec->fPreScaleX, rec->fPreSkewX, rec->fPost2x2[0][0],
      88             :         rec->fPost2x2[0][1], rec->fPost2x2[1][0], rec->fPost2x2[1][1]);
      89             :     SkDebugf("  frame %g miter %g hints %d framefill %d format %d join %d cap %d\n",
      90             :         rec->fFrameWidth, rec->fMiterLimit, rec->fHints, rec->fFrameAndFill,
      91             :         rec->fMaskFormat, rec->fStrokeJoin, rec->fStrokeCap);
      92             :     SkDebugf("  pathEffect %x maskFilter %x\n",
      93             :              desc->findEntry(kPathEffect_SkDescriptorTag, nullptr),
      94             :         desc->findEntry(kMaskFilter_SkDescriptorTag, nullptr));
      95             : #endif
      96           2 : }
      97             : 
      98           0 : SkScalerContext::~SkScalerContext() {}
      99             : 
     100           0 : void SkScalerContext::getAdvance(SkGlyph* glyph) {
     101             :     // mark us as just having a valid advance
     102           0 :     glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE;
     103             :     // we mark the format before making the call, in case the impl
     104             :     // internally ends up calling its generateMetrics, which is OK
     105             :     // albeit slower than strictly necessary
     106           0 :     generateAdvance(glyph);
     107           0 : }
     108             : 
     109          60 : void SkScalerContext::getMetrics(SkGlyph* glyph) {
     110          60 :     generateMetrics(glyph);
     111             : 
     112             :     // for now we have separate cache entries for devkerning on and off
     113             :     // in the future we might share caches, but make our measure/draw
     114             :     // code make the distinction. Thus we zap the values if the caller
     115             :     // has not asked for them.
     116          60 :     if ((fRec.fFlags & SkScalerContext::kDevKernText_Flag) == 0) {
     117             :         // no devkern, so zap the fields
     118          60 :         glyph->fLsbDelta = glyph->fRsbDelta = 0;
     119             :     }
     120             : 
     121             :     // if either dimension is empty, zap the image bounds of the glyph
     122          60 :     if (0 == glyph->fWidth || 0 == glyph->fHeight) {
     123           2 :         glyph->fWidth   = 0;
     124           2 :         glyph->fHeight  = 0;
     125           2 :         glyph->fTop     = 0;
     126           2 :         glyph->fLeft    = 0;
     127           2 :         glyph->fMaskFormat = 0;
     128           2 :         return;
     129             :     }
     130             : 
     131          58 :     if (fGenerateImageFromPath) {
     132           0 :         SkPath      devPath, fillPath;
     133             :         SkMatrix    fillToDevMatrix;
     134             : 
     135           0 :         this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix);
     136             : 
     137           0 :         if (fRasterizer) {
     138           0 :             SkMask  mask;
     139             : 
     140           0 :             if (fRasterizer->rasterize(fillPath, fillToDevMatrix, nullptr,
     141             :                                        fMaskFilter.get(), &mask,
     142             :                                        SkMask::kJustComputeBounds_CreateMode)) {
     143           0 :                 glyph->fLeft    = mask.fBounds.fLeft;
     144           0 :                 glyph->fTop     = mask.fBounds.fTop;
     145           0 :                 glyph->fWidth   = SkToU16(mask.fBounds.width());
     146           0 :                 glyph->fHeight  = SkToU16(mask.fBounds.height());
     147             :             } else {
     148           0 :                 goto SK_ERROR;
     149             :             }
     150             :         } else {
     151             :             // just use devPath
     152           0 :             const SkIRect ir = devPath.getBounds().roundOut();
     153             : 
     154           0 :             if (ir.isEmpty() || !ir.is16Bit()) {
     155           0 :                 goto SK_ERROR;
     156             :             }
     157           0 :             glyph->fLeft    = ir.fLeft;
     158           0 :             glyph->fTop     = ir.fTop;
     159           0 :             glyph->fWidth   = SkToU16(ir.width());
     160           0 :             glyph->fHeight  = SkToU16(ir.height());
     161             : 
     162           0 :             if (glyph->fWidth > 0) {
     163           0 :                 switch (fRec.fMaskFormat) {
     164             :                 case SkMask::kLCD16_Format:
     165           0 :                     glyph->fWidth += 2;
     166           0 :                     glyph->fLeft -= 1;
     167           0 :                     break;
     168             :                 default:
     169           0 :                     break;
     170             :                 }
     171             :             }
     172             :         }
     173             :     }
     174             : 
     175          58 :     if (SkMask::kARGB32_Format != glyph->fMaskFormat) {
     176          58 :         glyph->fMaskFormat = fRec.fMaskFormat;
     177             :     }
     178             : 
     179             :     // If we are going to create the mask, then we cannot keep the color
     180          58 :     if ((fGenerateImageFromPath || fMaskFilter) &&
     181           0 :             SkMask::kARGB32_Format == glyph->fMaskFormat) {
     182           0 :         glyph->fMaskFormat = SkMask::kA8_Format;
     183             :     }
     184             : 
     185          58 :     if (fMaskFilter) {
     186           0 :         SkMask      src, dst;
     187             :         SkMatrix    matrix;
     188             : 
     189           0 :         glyph->toMask(&src);
     190           0 :         fRec.getMatrixFrom2x2(&matrix);
     191             : 
     192           0 :         src.fImage = nullptr;  // only want the bounds from the filter
     193           0 :         if (fMaskFilter->filterMask(&dst, src, matrix, nullptr)) {
     194           0 :             if (dst.fBounds.isEmpty() || !dst.fBounds.is16Bit()) {
     195           0 :                 goto SK_ERROR;
     196             :             }
     197           0 :             SkASSERT(dst.fImage == nullptr);
     198           0 :             glyph->fLeft    = dst.fBounds.fLeft;
     199           0 :             glyph->fTop     = dst.fBounds.fTop;
     200           0 :             glyph->fWidth   = SkToU16(dst.fBounds.width());
     201           0 :             glyph->fHeight  = SkToU16(dst.fBounds.height());
     202           0 :             glyph->fMaskFormat = dst.fFormat;
     203             :         }
     204             :     }
     205          58 :     return;
     206             : 
     207             : SK_ERROR:
     208             :     // draw nothing 'cause we failed
     209           0 :     glyph->fLeft    = 0;
     210           0 :     glyph->fTop     = 0;
     211           0 :     glyph->fWidth   = 0;
     212           0 :     glyph->fHeight  = 0;
     213             :     // put a valid value here, in case it was earlier set to
     214             :     // MASK_FORMAT_JUST_ADVANCE
     215           0 :     glyph->fMaskFormat = fRec.fMaskFormat;
     216             : }
     217             : 
     218             : #define SK_SHOW_TEXT_BLIT_COVERAGE 0
     219             : 
     220           0 : static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) {
     221           0 :     uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage;
     222           0 :     unsigned rowBytes = mask.fRowBytes;
     223             : 
     224           0 :     for (int y = mask.fBounds.height() - 1; y >= 0; --y) {
     225           0 :         for (int x = mask.fBounds.width() - 1; x >= 0; --x) {
     226           0 :             dst[x] = lut[dst[x]];
     227             :         }
     228           0 :         dst += rowBytes;
     229             :     }
     230           0 : }
     231             : 
     232             : template<bool APPLY_PREBLEND>
     233           0 : static void pack4xHToLCD16(const SkPixmap& src, const SkMask& dst,
     234             :                            const SkMaskGamma::PreBlend& maskPreBlend) {
     235             : #define SAMPLES_PER_PIXEL 4
     236             : #define LCD_PER_PIXEL 3
     237           0 :     SkASSERT(kAlpha_8_SkColorType == src.colorType());
     238           0 :     SkASSERT(SkMask::kLCD16_Format == dst.fFormat);
     239             : 
     240           0 :     const int sample_width = src.width();
     241           0 :     const int height = src.height();
     242             : 
     243           0 :     uint16_t* dstP = (uint16_t*)dst.fImage;
     244           0 :     size_t dstRB = dst.fRowBytes;
     245             :     // An N tap FIR is defined by
     246             :     // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N]
     247             :     // or
     248             :     // out[n] = sum(i, 0, N, coeff[i]*x[n-i])
     249             : 
     250             :     // The strategy is to use one FIR (different coefficients) for each of r, g, and b.
     251             :     // This means using every 4th FIR output value of each FIR and discarding the rest.
     252             :     // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'.
     253             :     // (For r and b this is technically incorrect, but the coeffs outside round to zero anyway.)
     254             : 
     255             :     // These are in some fixed point repesentation.
     256             :     // Adding up to more than one simulates ink spread.
     257             :     // For implementation reasons, these should never add up to more than two.
     258             : 
     259             :     // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast').
     260             :     // Calculated using tools/generate_fir_coeff.py
     261             :     // With this one almost no fringing is ever seen, but it is imperceptibly blurry.
     262             :     // The lcd smoothed text is almost imperceptibly different from gray,
     263             :     // but is still sharper on small stems and small rounded corners than gray.
     264             :     // This also seems to be about as wide as one can get and only have a three pixel kernel.
     265             :     // TODO: caculate these at runtime so parameters can be adjusted (esp contrast).
     266             :     static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = {
     267             :         //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted.
     268             :         { 0x03, 0x0b, 0x1c, 0x33,  0x40, 0x39, 0x24, 0x10,  0x05, 0x01, 0x00, 0x00, },
     269             :         //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric
     270             :         { 0x00, 0x02, 0x08, 0x16,  0x2b, 0x3d, 0x3d, 0x2b,  0x16, 0x08, 0x02, 0x00, },
     271             :         //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted.
     272             :         { 0x00, 0x00, 0x01, 0x05,  0x10, 0x24, 0x39, 0x40,  0x33, 0x1c, 0x0b, 0x03, },
     273             :     };
     274             : 
     275           0 :     for (int y = 0; y < height; ++y) {
     276           0 :         const uint8_t* srcP = src.addr8(0, y);
     277             : 
     278             :         // TODO: this fir filter implementation is straight forward, but slow.
     279             :         // It should be possible to make it much faster.
     280           0 :         for (int sample_x = -4, pixel_x = 0; sample_x < sample_width + 4; sample_x += 4, ++pixel_x) {
     281           0 :             int fir[LCD_PER_PIXEL] = { 0 };
     282           0 :             for (int sample_index = SkMax32(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4)
     283           0 :                 ; sample_index < SkMin32(sample_x + 8, sample_width)
     284             :                 ; ++sample_index, ++coeff_index)
     285             :             {
     286           0 :                 int sample_value = srcP[sample_index];
     287           0 :                 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) {
     288           0 :                     fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value;
     289             :                 }
     290             :             }
     291           0 :             for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) {
     292           0 :                 fir[subpxl_index] /= 0x100;
     293           0 :                 fir[subpxl_index] = SkMin32(fir[subpxl_index], 255);
     294             :             }
     295             : 
     296           0 :             U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(fir[0], maskPreBlend.fR);
     297           0 :             U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(fir[1], maskPreBlend.fG);
     298           0 :             U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(fir[2], maskPreBlend.fB);
     299             : #if SK_SHOW_TEXT_BLIT_COVERAGE
     300             :             r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10);
     301             : #endif
     302           0 :             dstP[pixel_x] = SkPack888ToRGB16(r, g, b);
     303             :         }
     304           0 :         dstP = (uint16_t*)((char*)dstP + dstRB);
     305             :     }
     306           0 : }
     307             : 
     308           0 : static inline int convert_8_to_1(unsigned byte) {
     309           0 :     SkASSERT(byte <= 0xFF);
     310           0 :     return byte >> 7;
     311             : }
     312             : 
     313           0 : static uint8_t pack_8_to_1(const uint8_t alpha[8]) {
     314           0 :     unsigned bits = 0;
     315           0 :     for (int i = 0; i < 8; ++i) {
     316           0 :         bits <<= 1;
     317           0 :         bits |= convert_8_to_1(alpha[i]);
     318             :     }
     319           0 :     return SkToU8(bits);
     320             : }
     321             : 
     322           0 : static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) {
     323           0 :     const int height = mask.fBounds.height();
     324           0 :     const int width = mask.fBounds.width();
     325           0 :     const int octs = width >> 3;
     326           0 :     const int leftOverBits = width & 7;
     327             : 
     328           0 :     uint8_t* dst = mask.fImage;
     329           0 :     const int dstPad = mask.fRowBytes - SkAlign8(width)/8;
     330           0 :     SkASSERT(dstPad >= 0);
     331             : 
     332           0 :     SkASSERT(width >= 0);
     333           0 :     SkASSERT(srcRB >= (size_t)width);
     334           0 :     const size_t srcPad = srcRB - width;
     335             : 
     336           0 :     for (int y = 0; y < height; ++y) {
     337           0 :         for (int i = 0; i < octs; ++i) {
     338           0 :             *dst++ = pack_8_to_1(src);
     339           0 :             src += 8;
     340             :         }
     341           0 :         if (leftOverBits > 0) {
     342           0 :             unsigned bits = 0;
     343           0 :             int shift = 7;
     344           0 :             for (int i = 0; i < leftOverBits; ++i, --shift) {
     345           0 :                 bits |= convert_8_to_1(*src++) << shift;
     346             :             }
     347           0 :             *dst++ = bits;
     348             :         }
     349           0 :         src += srcPad;
     350           0 :         dst += dstPad;
     351             :     }
     352           0 : }
     353             : 
     354           0 : static void generateMask(const SkMask& mask, const SkPath& path,
     355             :                          const SkMaskGamma::PreBlend& maskPreBlend) {
     356           0 :     SkPaint paint;
     357             : 
     358           0 :     int srcW = mask.fBounds.width();
     359           0 :     int srcH = mask.fBounds.height();
     360           0 :     int dstW = srcW;
     361           0 :     int dstH = srcH;
     362           0 :     int dstRB = mask.fRowBytes;
     363             : 
     364             :     SkMatrix matrix;
     365           0 :     matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
     366           0 :                         -SkIntToScalar(mask.fBounds.fTop));
     367             : 
     368           0 :     paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat);
     369           0 :     switch (mask.fFormat) {
     370             :         case SkMask::kBW_Format:
     371           0 :             dstRB = 0;  // signals we need a copy
     372           0 :             break;
     373             :         case SkMask::kA8_Format:
     374           0 :             break;
     375             :         case SkMask::kLCD16_Format:
     376             :             // TODO: trigger off LCD orientation
     377           0 :             dstW = 4*dstW - 8;
     378           0 :             matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft + 1),
     379           0 :                                 -SkIntToScalar(mask.fBounds.fTop));
     380           0 :             matrix.postScale(SkIntToScalar(4), SK_Scalar1);
     381           0 :             dstRB = 0;  // signals we need a copy
     382           0 :             break;
     383             :         default:
     384           0 :             SkDEBUGFAIL("unexpected mask format");
     385             :     }
     386             : 
     387           0 :     SkRasterClip clip;
     388           0 :     clip.setRect(SkIRect::MakeWH(dstW, dstH));
     389             : 
     390           0 :     const SkImageInfo info = SkImageInfo::MakeA8(dstW, dstH);
     391           0 :     SkAutoPixmapStorage dst;
     392             : 
     393           0 :     if (0 == dstRB) {
     394           0 :         if (!dst.tryAlloc(info)) {
     395             :             // can't allocate offscreen, so empty the mask and return
     396           0 :             sk_bzero(mask.fImage, mask.computeImageSize());
     397           0 :             return;
     398             :         }
     399             :     } else {
     400           0 :         dst.reset(info, mask.fImage, dstRB);
     401             :     }
     402           0 :     sk_bzero(dst.writable_addr(), dst.getSafeSize());
     403             : 
     404           0 :     SkDraw  draw;
     405           0 :     draw.fDst   = dst;
     406           0 :     draw.fRC    = &clip;
     407           0 :     draw.fMatrix = &matrix;
     408           0 :     draw.drawPath(path, paint);
     409             : 
     410           0 :     switch (mask.fFormat) {
     411             :         case SkMask::kBW_Format:
     412           0 :             packA8ToA1(mask, dst.addr8(0, 0), dst.rowBytes());
     413           0 :             break;
     414             :         case SkMask::kA8_Format:
     415           0 :             if (maskPreBlend.isApplicable()) {
     416           0 :                 applyLUTToA8Mask(mask, maskPreBlend.fG);
     417             :             }
     418           0 :             break;
     419             :         case SkMask::kLCD16_Format:
     420           0 :             if (maskPreBlend.isApplicable()) {
     421           0 :                 pack4xHToLCD16<true>(dst, mask, maskPreBlend);
     422             :             } else {
     423           0 :                 pack4xHToLCD16<false>(dst, mask, maskPreBlend);
     424             :             }
     425           0 :             break;
     426             :         default:
     427           0 :             break;
     428             :     }
     429             : }
     430             : 
     431           0 : static void extract_alpha(const SkMask& dst,
     432             :                           const SkPMColor* srcRow, size_t srcRB) {
     433           0 :     int width = dst.fBounds.width();
     434           0 :     int height = dst.fBounds.height();
     435           0 :     int dstRB = dst.fRowBytes;
     436           0 :     uint8_t* dstRow = dst.fImage;
     437             : 
     438           0 :     for (int y = 0; y < height; ++y) {
     439           0 :         for (int x = 0; x < width; ++x) {
     440           0 :             dstRow[x] = SkGetPackedA32(srcRow[x]);
     441             :         }
     442             :         // zero any padding on each row
     443           0 :         for (int x = width; x < dstRB; ++x) {
     444           0 :             dstRow[x] = 0;
     445             :         }
     446           0 :         dstRow += dstRB;
     447           0 :         srcRow = (const SkPMColor*)((const char*)srcRow + srcRB);
     448             :     }
     449           0 : }
     450             : 
     451          58 : void SkScalerContext::getImage(const SkGlyph& origGlyph) {
     452          58 :     const SkGlyph*  glyph = &origGlyph;
     453          58 :     SkGlyph         tmpGlyph;
     454             : 
     455             :     // in case we need to call generateImage on a mask-format that is different
     456             :     // (i.e. larger) than what our caller allocated by looking at origGlyph.
     457         116 :     SkAutoMalloc tmpGlyphImageStorage;
     458             : 
     459             :     // If we are going to draw-from-path, then we cannot generate color, since
     460             :     // the path only makes a mask. This case should have been caught up in
     461             :     // generateMetrics().
     462          58 :     SkASSERT(!fGenerateImageFromPath ||
     463             :              SkMask::kARGB32_Format != origGlyph.fMaskFormat);
     464             : 
     465          58 :     if (fMaskFilter) {   // restore the prefilter bounds
     466           0 :         tmpGlyph.initWithGlyphID(origGlyph.getPackedID());
     467             : 
     468             :         // need the original bounds, sans our maskfilter
     469           0 :         SkMaskFilter* mf = fMaskFilter.release();   // temp disable
     470           0 :         this->getMetrics(&tmpGlyph);
     471           0 :         fMaskFilter = sk_sp<SkMaskFilter>(mf);      // restore
     472             : 
     473             :         // we need the prefilter bounds to be <= filter bounds
     474           0 :         SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth);
     475           0 :         SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight);
     476             : 
     477           0 :         if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) {
     478           0 :             tmpGlyph.fImage = origGlyph.fImage;
     479             :         } else {
     480           0 :             tmpGlyphImageStorage.reset(tmpGlyph.computeImageSize());
     481           0 :             tmpGlyph.fImage = tmpGlyphImageStorage.get();
     482             :         }
     483           0 :         glyph = &tmpGlyph;
     484             :     }
     485             : 
     486          58 :     if (fGenerateImageFromPath) {
     487           0 :         SkPath      devPath, fillPath;
     488             :         SkMatrix    fillToDevMatrix;
     489           0 :         SkMask      mask;
     490             : 
     491           0 :         this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix);
     492           0 :         glyph->toMask(&mask);
     493             : 
     494           0 :         if (fRasterizer) {
     495           0 :             mask.fFormat = SkMask::kA8_Format;
     496           0 :             sk_bzero(glyph->fImage, mask.computeImageSize());
     497             : 
     498           0 :             if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, nullptr,
     499             :                                         fMaskFilter.get(), &mask,
     500             :                                         SkMask::kJustRenderImage_CreateMode)) {
     501           0 :                 return;
     502             :             }
     503           0 :             if (fPreBlend.isApplicable()) {
     504           0 :                 applyLUTToA8Mask(mask, fPreBlend.fG);
     505             :             }
     506             :         } else {
     507           0 :             SkASSERT(SkMask::kARGB32_Format != mask.fFormat);
     508           0 :             generateMask(mask, devPath, fPreBlend);
     509             :         }
     510             :     } else {
     511          58 :         generateImage(*glyph);
     512             :     }
     513             : 
     514          58 :     if (fMaskFilter) {
     515           0 :         SkMask      srcM, dstM;
     516             :         SkMatrix    matrix;
     517             : 
     518             :         // the src glyph image shouldn't be 3D
     519           0 :         SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat);
     520             : 
     521           0 :         SkAutoSMalloc<32*32> a8storage;
     522           0 :         glyph->toMask(&srcM);
     523           0 :         if (SkMask::kARGB32_Format == srcM.fFormat) {
     524             :             // now we need to extract the alpha-channel from the glyph's image
     525             :             // and copy it into a temp buffer, and then point srcM at that temp.
     526           0 :             srcM.fFormat = SkMask::kA8_Format;
     527           0 :             srcM.fRowBytes = SkAlign4(srcM.fBounds.width());
     528           0 :             size_t size = srcM.computeImageSize();
     529           0 :             a8storage.reset(size);
     530           0 :             srcM.fImage = (uint8_t*)a8storage.get();
     531           0 :             extract_alpha(srcM,
     532           0 :                           (const SkPMColor*)glyph->fImage, glyph->rowBytes());
     533             :         }
     534             : 
     535           0 :         fRec.getMatrixFrom2x2(&matrix);
     536             : 
     537           0 :         if (fMaskFilter->filterMask(&dstM, srcM, matrix, nullptr)) {
     538           0 :             int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width());
     539           0 :             int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height());
     540           0 :             int dstRB = origGlyph.rowBytes();
     541           0 :             int srcRB = dstM.fRowBytes;
     542             : 
     543           0 :             const uint8_t* src = (const uint8_t*)dstM.fImage;
     544           0 :             uint8_t* dst = (uint8_t*)origGlyph.fImage;
     545             : 
     546           0 :             if (SkMask::k3D_Format == dstM.fFormat) {
     547             :                 // we have to copy 3 times as much
     548           0 :                 height *= 3;
     549             :             }
     550             : 
     551             :             // clean out our glyph, since it may be larger than dstM
     552             :             //sk_bzero(dst, height * dstRB);
     553             : 
     554           0 :             while (--height >= 0) {
     555           0 :                 memcpy(dst, src, width);
     556           0 :                 src += srcRB;
     557           0 :                 dst += dstRB;
     558             :             }
     559           0 :             SkMask::FreeImage(dstM.fImage);
     560             : 
     561           0 :             if (fPreBlendForFilter.isApplicable()) {
     562           0 :                 applyLUTToA8Mask(srcM, fPreBlendForFilter.fG);
     563             :             }
     564             :         }
     565             :     }
     566             : }
     567             : 
     568           0 : void SkScalerContext::getPath(SkPackedGlyphID glyphID, SkPath* path) {
     569           0 :     this->internalGetPath(glyphID, nullptr, path, nullptr);
     570           0 : }
     571             : 
     572           2 : void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* fm) {
     573           2 :     SkASSERT(fm);
     574           2 :     this->generateFontMetrics(fm);
     575           2 : }
     576             : 
     577           0 : SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) {
     578           0 :     return 0;
     579             : }
     580             : 
     581             : ///////////////////////////////////////////////////////////////////////////////
     582             : 
     583           0 : void SkScalerContext::internalGetPath(SkPackedGlyphID glyphID, SkPath* fillPath,
     584             :                                       SkPath* devPath, SkMatrix* fillToDevMatrix) {
     585           0 :     SkPath  path;
     586           0 :     generatePath(glyphID.code(), &path);
     587             : 
     588           0 :     if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
     589           0 :         SkFixed dx = glyphID.getSubXFixed();
     590           0 :         SkFixed dy = glyphID.getSubYFixed();
     591           0 :         if (dx | dy) {
     592           0 :             path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy));
     593             :         }
     594             :     }
     595             : 
     596           0 :     if (fRec.fFrameWidth > 0 || fPathEffect != nullptr) {
     597             :         // need the path in user-space, with only the point-size applied
     598             :         // so that our stroking and effects will operate the same way they
     599             :         // would if the user had extracted the path themself, and then
     600             :         // called drawPath
     601           0 :         SkPath      localPath;
     602             :         SkMatrix    matrix, inverse;
     603             : 
     604           0 :         fRec.getMatrixFrom2x2(&matrix);
     605           0 :         if (!matrix.invert(&inverse)) {
     606             :             // assume fillPath and devPath are already empty.
     607           0 :             return;
     608             :         }
     609           0 :         path.transform(inverse, &localPath);
     610             :         // now localPath is only affected by the paint settings, and not the canvas matrix
     611             : 
     612           0 :         SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
     613             : 
     614           0 :         if (fRec.fFrameWidth > 0) {
     615           0 :             rec.setStrokeStyle(fRec.fFrameWidth,
     616           0 :                                SkToBool(fRec.fFlags & kFrameAndFill_Flag));
     617             :             // glyphs are always closed contours, so cap type is ignored,
     618             :             // so we just pass something.
     619           0 :             rec.setStrokeParams((SkPaint::Cap)fRec.fStrokeCap,
     620           0 :                                 (SkPaint::Join)fRec.fStrokeJoin,
     621           0 :                                 fRec.fMiterLimit);
     622             :         }
     623             : 
     624           0 :         if (fPathEffect) {
     625           0 :             SkPath effectPath;
     626           0 :             if (fPathEffect->filterPath(&effectPath, localPath, &rec, nullptr)) {
     627           0 :                 localPath.swap(effectPath);
     628             :             }
     629             :         }
     630             : 
     631           0 :         if (rec.needToApply()) {
     632           0 :             SkPath strokePath;
     633           0 :             if (rec.applyToPath(&strokePath, localPath)) {
     634           0 :                 localPath.swap(strokePath);
     635             :             }
     636             :         }
     637             : 
     638             :         // now return stuff to the caller
     639           0 :         if (fillToDevMatrix) {
     640           0 :             *fillToDevMatrix = matrix;
     641             :         }
     642           0 :         if (devPath) {
     643           0 :             localPath.transform(matrix, devPath);
     644             :         }
     645           0 :         if (fillPath) {
     646           0 :             fillPath->swap(localPath);
     647             :         }
     648             :     } else {   // nothing tricky to do
     649           0 :         if (fillToDevMatrix) {
     650           0 :             fillToDevMatrix->reset();
     651             :         }
     652           0 :         if (devPath) {
     653           0 :             if (fillPath == nullptr) {
     654           0 :                 devPath->swap(path);
     655             :             } else {
     656           0 :                 *devPath = path;
     657             :             }
     658             :         }
     659             : 
     660           0 :         if (fillPath) {
     661           0 :             fillPath->swap(path);
     662             :         }
     663             :     }
     664             : 
     665           0 :     if (devPath) {
     666           0 :         devPath->updateBoundsCache();
     667             :     }
     668           0 :     if (fillPath) {
     669           0 :         fillPath->updateBoundsCache();
     670             :     }
     671             : }
     672             : 
     673             : 
     674           2 : void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const {
     675           2 :     dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0,
     676           2 :                 fPost2x2[1][0], fPost2x2[1][1], 0,
     677           2 :                 0,              0,              1);
     678           2 : }
     679             : 
     680           2 : void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const {
     681           2 :     SkPaint::SetTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX);
     682           2 : }
     683             : 
     684           2 : void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const {
     685           2 :     this->getLocalMatrix(m);
     686             : 
     687             :     //  now concat the device matrix
     688             :     SkMatrix    deviceMatrix;
     689           2 :     this->getMatrixFrom2x2(&deviceMatrix);
     690           2 :     m->postConcat(deviceMatrix);
     691           2 : }
     692             : 
     693           0 : bool SkScalerContextRec::computeMatrices(PreMatrixScale preMatrixScale, SkVector* s, SkMatrix* sA,
     694             :                                          SkMatrix* GsA, SkMatrix* G_inv, SkMatrix* A_out)
     695             : {
     696             :     // A is the 'total' matrix.
     697             :     SkMatrix A;
     698           0 :     this->getSingleMatrix(&A);
     699             : 
     700             :     // The caller may find the 'total' matrix useful when dealing directly with EM sizes.
     701           0 :     if (A_out) {
     702           0 :         *A_out = A;
     703             :     }
     704             : 
     705             :     // If the 'total' matrix is singular, set the 'scale' to something finite and zero the matrices.
     706             :     // All underlying ports have issues with zero text size, so use the matricies to zero.
     707             : 
     708             :     // Map the vectors [0,1], [1,0], [1,1] and [1,-1] (the EM) through the 'total' matrix.
     709             :     // If the length of one of these vectors is less than 1/256 then an EM filling square will
     710             :     // never affect any pixels.
     711           0 :     SkVector diag[4] = { { A.getScaleX()               ,                 A.getSkewY() },
     712           0 :                          {                 A.getSkewX(), A.getScaleY()                },
     713           0 :                          { A.getScaleX() + A.getSkewX(), A.getScaleY() + A.getSkewY() },
     714           0 :                          { A.getScaleX() - A.getSkewX(), A.getScaleY() - A.getSkewY() }, };
     715           0 :     if (diag[0].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero ||
     716           0 :         diag[1].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero ||
     717           0 :         diag[2].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero ||
     718           0 :         diag[3].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero)
     719             :     {
     720           0 :         s->fX = SK_Scalar1;
     721           0 :         s->fY = SK_Scalar1;
     722           0 :         sA->setScale(0, 0);
     723           0 :         if (GsA) {
     724           0 :             GsA->setScale(0, 0);
     725             :         }
     726           0 :         if (G_inv) {
     727           0 :             G_inv->reset();
     728             :         }
     729           0 :         return false;
     730             :     }
     731             : 
     732             :     // GA is the matrix A with rotation removed.
     733             :     SkMatrix GA;
     734           0 :     bool skewedOrFlipped = A.getSkewX() || A.getSkewY() || A.getScaleX() < 0 || A.getScaleY() < 0;
     735           0 :     if (skewedOrFlipped) {
     736             :         // h is where A maps the horizontal baseline.
     737           0 :         SkPoint h = SkPoint::Make(SK_Scalar1, 0);
     738           0 :         A.mapPoints(&h, 1);
     739             : 
     740             :         // G is the Givens Matrix for A (rotational matrix where GA[0][1] == 0).
     741             :         SkMatrix G;
     742           0 :         SkComputeGivensRotation(h, &G);
     743             : 
     744           0 :         GA = G;
     745           0 :         GA.preConcat(A);
     746             : 
     747             :         // The 'remainingRotation' is G inverse, which is fairly simple since G is 2x2 rotational.
     748           0 :         if (G_inv) {
     749           0 :             G_inv->setAll(
     750           0 :                 G.get(SkMatrix::kMScaleX), -G.get(SkMatrix::kMSkewX), G.get(SkMatrix::kMTransX),
     751           0 :                 -G.get(SkMatrix::kMSkewY), G.get(SkMatrix::kMScaleY), G.get(SkMatrix::kMTransY),
     752           0 :                 G.get(SkMatrix::kMPersp0), G.get(SkMatrix::kMPersp1), G.get(SkMatrix::kMPersp2));
     753             :         }
     754             :     } else {
     755           0 :         GA = A;
     756           0 :         if (G_inv) {
     757           0 :             G_inv->reset();
     758             :         }
     759             :     }
     760             : 
     761             :     // At this point, given GA, create s.
     762           0 :     switch (preMatrixScale) {
     763             :         case kFull_PreMatrixScale: {
     764           0 :             SkScalar xScale = SkScalarAbs(GA.get(SkMatrix::kMScaleX));
     765           0 :             SkScalar yScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
     766           0 :             if (xScale <= SK_ScalarNearlyZero) {
     767           0 :                 xScale = SK_Scalar1;
     768             :             }
     769           0 :             if (yScale <= SK_ScalarNearlyZero) {
     770           0 :                 yScale = SK_Scalar1;
     771             :             }
     772           0 :             s->fX = xScale;
     773           0 :             s->fY = yScale;
     774           0 :             break;
     775             :         }
     776             :         case kVertical_PreMatrixScale: {
     777           0 :             SkScalar yScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
     778           0 :             if (yScale <= SK_ScalarNearlyZero) {
     779           0 :                 yScale = SK_Scalar1;
     780             :             }
     781           0 :             s->fX = yScale;
     782           0 :             s->fY = yScale;
     783           0 :             break;
     784             :         }
     785             :         case kVerticalInteger_PreMatrixScale: {
     786           0 :             SkScalar realYScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
     787           0 :             SkScalar intYScale = SkScalarRoundToScalar(realYScale);
     788           0 :             if (intYScale == 0) {
     789           0 :                 intYScale = SK_Scalar1;
     790             :             }
     791           0 :             s->fX = intYScale;
     792           0 :             s->fY = intYScale;
     793           0 :             break;
     794             :         }
     795             :     }
     796             : 
     797             :     // The 'remaining' matrix sA is the total matrix A without the scale.
     798           0 :     if (!skewedOrFlipped && (
     799           0 :             (kFull_PreMatrixScale == preMatrixScale) ||
     800           0 :             (kVertical_PreMatrixScale == preMatrixScale && A.getScaleX() == A.getScaleY())))
     801             :     {
     802             :         // If GA == A and kFull_PreMatrixScale, sA is identity.
     803             :         // If GA == A and kVertical_PreMatrixScale and A.scaleX == A.scaleY, sA is identity.
     804           0 :         sA->reset();
     805           0 :     } else if (!skewedOrFlipped && kVertical_PreMatrixScale == preMatrixScale) {
     806             :         // If GA == A and kVertical_PreMatrixScale, sA.scaleY is SK_Scalar1.
     807           0 :         sA->reset();
     808           0 :         sA->setScaleX(A.getScaleX() / s->fY);
     809             :     } else {
     810             :         // TODO: like kVertical_PreMatrixScale, kVerticalInteger_PreMatrixScale with int scales.
     811           0 :         *sA = A;
     812           0 :         sA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY));
     813             :     }
     814             : 
     815             :     // The 'remainingWithoutRotation' matrix GsA is the non-rotational part of A without the scale.
     816           0 :     if (GsA) {
     817           0 :         *GsA = GA;
     818             :          // G is rotational so reorders with the scale.
     819           0 :         GsA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY));
     820             :     }
     821             : 
     822           0 :     return true;
     823             : }
     824             : 
     825          21 : SkAxisAlignment SkScalerContext::computeAxisAlignmentForHText() {
     826             :     // Why fPost2x2 can be used here.
     827             :     // getSingleMatrix multiplies in getLocalMatrix, which consists of
     828             :     // * fTextSize (a scale, which has no effect)
     829             :     // * fPreScaleX (a scale in x, which has no effect)
     830             :     // * fPreSkewX (has no effect, but would on vertical text alignment).
     831             :     // In other words, making the text bigger, stretching it along the
     832             :     // horizontal axis, or fake italicizing it does not move the baseline.
     833             : 
     834          21 :     if (0 == fRec.fPost2x2[1][0]) {
     835             :         // The x axis is mapped onto the x axis.
     836          21 :         return kX_SkAxisAlignment;
     837             :     }
     838           0 :     if (0 == fRec.fPost2x2[0][0]) {
     839             :         // The x axis is mapped onto the y axis.
     840           0 :         return kY_SkAxisAlignment;
     841             :     }
     842           0 :     return kNone_SkAxisAlignment;
     843             : }
     844             : 
     845             : ///////////////////////////////////////////////////////////////////////////////
     846             : 
     847           0 : class SkScalerContext_Empty : public SkScalerContext {
     848             : public:
     849           0 :     SkScalerContext_Empty(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects,
     850             :                           const SkDescriptor* desc)
     851           0 :         : SkScalerContext(std::move(typeface), effects, desc) {}
     852             : 
     853             : protected:
     854           0 :     unsigned generateGlyphCount() override {
     855           0 :         return 0;
     856             :     }
     857           0 :     uint16_t generateCharToGlyph(SkUnichar uni) override {
     858           0 :         return 0;
     859             :     }
     860           0 :     void generateAdvance(SkGlyph* glyph) override {
     861           0 :         glyph->zeroMetrics();
     862           0 :     }
     863           0 :     void generateMetrics(SkGlyph* glyph) override {
     864           0 :         glyph->zeroMetrics();
     865           0 :     }
     866           0 :     void generateImage(const SkGlyph& glyph) override {}
     867           0 :     void generatePath(SkGlyphID glyph, SkPath* path) override {}
     868           0 :     void generateFontMetrics(SkPaint::FontMetrics* metrics) override {
     869           0 :         if (metrics) {
     870           0 :             sk_bzero(metrics, sizeof(*metrics));
     871             :         }
     872           0 :     }
     873             : };
     874             : 
     875             : extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc);
     876             : 
     877           2 : std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext(
     878             :     const SkScalerContextEffects& effects, const SkDescriptor* desc, bool allowFailure) const
     879             : {
     880           2 :     std::unique_ptr<SkScalerContext> c(this->onCreateScalerContext(effects, desc));
     881           2 :     if (!c && !allowFailure) {
     882           0 :         c = skstd::make_unique<SkScalerContext_Empty>(sk_ref_sp(const_cast<SkTypeface*>(this)),
     883           0 :                                                       effects, desc);
     884             :     }
     885           2 :     return c;
     886             : }

Generated by: LCOV version 1.13