reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrGlyph_DEFINED |
| 9 | #define GrGlyph_DEFINED |
| 10 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 11 | #include "GrBatchAtlas.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 12 | #include "GrRect.h" |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 13 | #include "GrTypes.h" |
| 14 | |
jvanverth | e817dbb | 2014-10-10 08:52:03 -0700 | [diff] [blame] | 15 | #include "SkChecksum.h" |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 16 | #include "SkPath.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 18 | class GrPlot; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 { |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 27 | enum MaskStyle { |
| 28 | kCoverage_MaskStyle, |
| 29 | kDistance_MaskStyle |
| 30 | }; |
| 31 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | typedef uint32_t PackedID; |
| 33 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 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; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 42 | bool fTooLargeForAtlas; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 44 | void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 45 | fID = GrBatchAtlas::kInvalidAtlasID; |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 46 | fPlot = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | fPath = NULL; |
| 48 | fPackedID = packed; |
| 49 | fBounds.set(bounds); |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 50 | fMaskFormat = format; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | fAtlasLocation.set(0, 0); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 52 | fTooLargeForAtlas = GrBatchAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 53 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 54 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | void free() { |
| 56 | if (fPath) { |
| 57 | delete fPath; |
| 58 | fPath = NULL; |
| 59 | } |
| 60 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 61 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | /////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 69 | static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | // two most significant fraction bits from fixed-point |
| 71 | return (pos >> 14) & 3; |
| 72 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 73 | |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 74 | static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | x = ExtractSubPixelBitsFromFixed(x); |
| 76 | y = ExtractSubPixelBitsFromFixed(y); |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 77 | int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0; |
| 78 | return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 80 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 81 | static inline SkFixed UnpackFixedX(PackedID packed) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 82 | return ((packed >> 18) & 3) << 14; |
| 83 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 85 | static inline SkFixed UnpackFixedY(PackedID packed) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | return ((packed >> 16) & 3) << 14; |
| 87 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 88 | |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 89 | static inline MaskStyle UnpackMaskStyle(PackedID packed) { |
| 90 | return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle; |
| 91 | } |
| 92 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | static inline uint16_t UnpackID(PackedID packed) { |
| 94 | return (uint16_t)packed; |
| 95 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 96 | |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 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) { |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 102 | return SkChecksum::Mix(key); |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 103 | } |
| 104 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | |
| 106 | #endif |