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 | |
Brian Salomon | 903da79 | 2016-12-16 14:24:46 -0500 | [diff] [blame] | 11 | #include "GrDrawOpAtlas.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" |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 16 | #include "SkFixed.h" |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 17 | #include "SkPath.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 { |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 26 | enum MaskStyle { |
| 27 | kCoverage_MaskStyle, |
| 28 | kDistance_MaskStyle |
| 29 | }; |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 30 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | typedef uint32_t PackedID; |
| 32 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 33 | GrDrawOpAtlas::AtlasID fID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 34 | SkPath* fPath; |
| 35 | PackedID fPackedID; |
| 36 | GrMaskFormat fMaskFormat; |
| 37 | GrIRect16 fBounds; |
| 38 | SkIPoint16 fAtlasLocation; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 39 | bool fTooLargeForAtlas; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 41 | void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) { |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 42 | fID = GrDrawOpAtlas::kInvalidAtlasID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | fPath = nullptr; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | fPackedID = packed; |
| 45 | fBounds.set(bounds); |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 46 | fMaskFormat = format; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | fAtlasLocation.set(0, 0); |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 48 | fTooLargeForAtlas = GrDrawOpAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 50 | |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 51 | void reset() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 52 | if (fPath) { |
| 53 | delete fPath; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | fPath = nullptr; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 57 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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); } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 62 | uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | |
| 64 | /////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 65 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 66 | static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | // two most significant fraction bits from fixed-point |
| 68 | return (pos >> 14) & 3; |
| 69 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 70 | |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 71 | 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] | 72 | x = ExtractSubPixelBitsFromFixed(x); |
| 73 | y = ExtractSubPixelBitsFromFixed(y); |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 74 | int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0; |
| 75 | return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 76 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 77 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 78 | static inline SkFixed UnpackFixedX(PackedID packed) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | return ((packed >> 18) & 3) << 14; |
| 80 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 82 | static inline SkFixed UnpackFixedY(PackedID packed) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | return ((packed >> 16) & 3) << 14; |
| 84 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 85 | |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 86 | static inline MaskStyle UnpackMaskStyle(PackedID packed) { |
| 87 | return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle; |
| 88 | } |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 89 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | static inline uint16_t UnpackID(PackedID packed) { |
| 91 | return (uint16_t)packed; |
| 92 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 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) { |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 99 | return SkChecksum::Mix(key); |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 100 | } |
| 101 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | |
| 103 | #endif |