Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2014 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 "GrPathRendering.h"
      9 #include "SkDescriptor.h"
     10 #include "SkGlyph.h"
     11 #include "SkMatrix.h"
     12 #include "SkTypeface.h"
     13 #include "GrPathRange.h"
     14 
     15 class GlyphGenerator : public GrPathRange::PathGenerator {
     16 public:
     17     GlyphGenerator(const SkTypeface& typeface, const SkDescriptor& desc)
     18         : fScalerContext(typeface.createScalerContext(&desc))
     19 #ifdef SK_DEBUG
     20         , fDesc(desc.copy())
     21 #endif
     22     {}
     23 
     24     virtual ~GlyphGenerator() {
     25 #ifdef SK_DEBUG
     26         SkDescriptor::Free(fDesc);
     27 #endif
     28     }
     29 
     30     int getNumPaths() override {
     31         return fScalerContext->getGlyphCount();
     32     }
     33 
     34     void generatePath(int glyphID, SkPath* out) override {
     35         SkGlyph skGlyph;
     36         skGlyph.initWithGlyphID(glyphID);
     37         fScalerContext->getMetrics(&skGlyph);
     38 
     39         fScalerContext->getPath(skGlyph, out);
     40     }
     41 #ifdef SK_DEBUG
     42     bool isEqualTo(const SkDescriptor& desc) const override {
     43         return fDesc->equals(desc);
     44     }
     45 #endif
     46 private:
     47     const SkAutoTDelete<SkScalerContext> fScalerContext;
     48 #ifdef SK_DEBUG
     49     SkDescriptor* const fDesc;
     50 #endif
     51 };
     52 
     53 GrPathRange* GrPathRendering::createGlyphs(const SkTypeface* typeface,
     54                                            const SkDescriptor* desc,
     55                                            const GrStrokeInfo& stroke) {
     56     if (nullptr == typeface) {
     57         typeface = SkTypeface::GetDefaultTypeface();
     58         SkASSERT(nullptr != typeface);
     59     }
     60 
     61     if (desc) {
     62         SkAutoTUnref<GlyphGenerator> generator(new GlyphGenerator(*typeface, *desc));
     63         return this->createPathRange(generator, stroke);
     64     }
     65 
     66     SkScalerContextRec rec;
     67     memset(&rec, 0, sizeof(rec));
     68     rec.fFontID = typeface->uniqueID();
     69     rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths;
     70     rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1;
     71     // Don't bake stroke information into the glyphs, we'll let the GPU do the stroking.
     72 
     73     SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1));
     74     SkDescriptor*    genericDesc = ad.getDesc();
     75 
     76     genericDesc->init();
     77     genericDesc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
     78     genericDesc->computeChecksum();
     79 
     80     SkAutoTUnref<GlyphGenerator> generator(new GlyphGenerator(*typeface, *genericDesc));
     81     return this->createPathRange(generator, stroke);
     82 }
     83