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