Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2010 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 #ifndef GrGlyph_DEFINED
      9 #define GrGlyph_DEFINED
     10 
     11 #include "GrDrawOpAtlas.h"
     12 #include "GrRect.h"
     13 #include "GrTypes.h"
     14 
     15 #include "SkChecksum.h"
     16 #include "SkFixed.h"
     17 #include "SkPath.h"
     18 
     19 /*  Need this to be quad-state:
     20     - complete w/ image
     21     - just metrics
     22     - failed to get image, but has metrics
     23     - failed to get metrics
     24  */
     25 struct GrGlyph {
     26     enum MaskStyle {
     27         kCoverage_MaskStyle,
     28         kDistance_MaskStyle
     29     };
     30 
     31     typedef uint32_t PackedID;
     32 
     33     GrDrawOpAtlas::AtlasID fID;
     34     SkPath*               fPath;
     35     PackedID              fPackedID;
     36     GrMaskFormat          fMaskFormat;
     37     GrIRect16             fBounds;
     38     SkIPoint16            fAtlasLocation;
     39     bool                  fTooLargeForAtlas;
     40 
     41     void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) {
     42         fID = GrDrawOpAtlas::kInvalidAtlasID;
     43         fPath = nullptr;
     44         fPackedID = packed;
     45         fBounds.set(bounds);
     46         fMaskFormat = format;
     47         fAtlasLocation.set(0, 0);
     48         fTooLargeForAtlas = GrDrawOpAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height());
     49     }
     50 
     51     void reset() {
     52         if (fPath) {
     53             delete fPath;
     54             fPath = nullptr;
     55         }
     56     }
     57 
     58     int width() const { return fBounds.width(); }
     59     int height() const { return fBounds.height(); }
     60     bool isEmpty() const { return fBounds.isEmpty(); }
     61     uint16_t glyphID() const { return UnpackID(fPackedID); }
     62     uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); }
     63 
     64     ///////////////////////////////////////////////////////////////////////////
     65 
     66     static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
     67         // two most significant fraction bits from fixed-point
     68         return (pos >> 14) & 3;
     69     }
     70 
     71     static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) {
     72         x = ExtractSubPixelBitsFromFixed(x);
     73         y = ExtractSubPixelBitsFromFixed(y);
     74         int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0;
     75         return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID;
     76     }
     77 
     78     static inline SkFixed UnpackFixedX(PackedID packed) {
     79         return ((packed >> 18) & 3) << 14;
     80     }
     81 
     82     static inline SkFixed UnpackFixedY(PackedID packed) {
     83         return ((packed >> 16) & 3) << 14;
     84     }
     85 
     86     static inline MaskStyle UnpackMaskStyle(PackedID packed) {
     87         return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle;
     88     }
     89 
     90     static inline uint16_t UnpackID(PackedID packed) {
     91         return (uint16_t)packed;
     92     }
     93 
     94     static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
     95         return glyph.fPackedID;
     96     }
     97 
     98     static inline uint32_t Hash(GrGlyph::PackedID key) {
     99         return SkChecksum::Mix(key);
    100     }
    101 };
    102 
    103 #endif
    104