blob: 9ab1630dd526eaa71a6ed11853d516854af8b9fe [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrGlyph_DEFINED
9#define GrGlyph_DEFINED
10
Brian Salomon903da792016-12-16 14:24:46 -050011#include "GrDrawOpAtlas.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrRect.h"
jvanverth294c3262014-10-10 11:36:12 -070013#include "GrTypes.h"
14
jvanverthe817dbb2014-10-10 08:52:03 -070015#include "SkChecksum.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070016#include "SkFixed.h"
jvanverth294c3262014-10-10 11:36:12 -070017#include "SkPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
reed@google.comac10a2d2010-12-22 21:39:39 +000019/* 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 */
25struct GrGlyph {
jvanvertha7634612015-03-19 06:08:31 -070026 enum MaskStyle {
27 kCoverage_MaskStyle,
28 kDistance_MaskStyle
29 };
mtklein852f15d2016-03-17 10:51:27 -070030
reed@google.comac10a2d2010-12-22 21:39:39 +000031 typedef uint32_t PackedID;
32
Brian Salomon2ee084e2016-12-16 18:59:19 -050033 GrDrawOpAtlas::AtlasID fID;
joshualitt7c3a2f82015-03-31 13:32:05 -070034 SkPath* fPath;
35 PackedID fPackedID;
36 GrMaskFormat fMaskFormat;
37 GrIRect16 fBounds;
38 SkIPoint16 fAtlasLocation;
joshualitt010db532015-04-21 10:07:26 -070039 bool fTooLargeForAtlas;
reed@google.comac10a2d2010-12-22 21:39:39 +000040
jvanverth294c3262014-10-10 11:36:12 -070041 void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) {
Brian Salomon2ee084e2016-12-16 18:59:19 -050042 fID = GrDrawOpAtlas::kInvalidAtlasID;
halcanary96fcdcc2015-08-27 07:41:13 -070043 fPath = nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +000044 fPackedID = packed;
45 fBounds.set(bounds);
jvanverth294c3262014-10-10 11:36:12 -070046 fMaskFormat = format;
reed@google.comac10a2d2010-12-22 21:39:39 +000047 fAtlasLocation.set(0, 0);
Brian Salomon2ee084e2016-12-16 18:59:19 -050048 fTooLargeForAtlas = GrDrawOpAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height());
reed@google.comac10a2d2010-12-22 21:39:39 +000049 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050
mtklein852f15d2016-03-17 10:51:27 -070051 void reset() {
reed@google.comac10a2d2010-12-22 21:39:39 +000052 if (fPath) {
53 delete fPath;
halcanary96fcdcc2015-08-27 07:41:13 -070054 fPath = nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +000055 }
56 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057
reed@google.comac10a2d2010-12-22 21:39:39 +000058 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 Vertheafa64b2017-09-18 10:05:00 -040062 uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); }
reed@google.comac10a2d2010-12-22 21:39:39 +000063
64 ///////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000065
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000066 static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
reed@google.comac10a2d2010-12-22 21:39:39 +000067 // two most significant fraction bits from fixed-point
68 return (pos >> 14) & 3;
69 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070
jvanvertha7634612015-03-19 06:08:31 -070071 static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) {
reed@google.comac10a2d2010-12-22 21:39:39 +000072 x = ExtractSubPixelBitsFromFixed(x);
73 y = ExtractSubPixelBitsFromFixed(y);
jvanvertha7634612015-03-19 06:08:31 -070074 int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0;
75 return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID;
reed@google.comac10a2d2010-12-22 21:39:39 +000076 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000077
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000078 static inline SkFixed UnpackFixedX(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000079 return ((packed >> 18) & 3) << 14;
80 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000082 static inline SkFixed UnpackFixedY(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000083 return ((packed >> 16) & 3) << 14;
84 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085
jvanvertha7634612015-03-19 06:08:31 -070086 static inline MaskStyle UnpackMaskStyle(PackedID packed) {
87 return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle;
88 }
mtklein852f15d2016-03-17 10:51:27 -070089
reed@google.comac10a2d2010-12-22 21:39:39 +000090 static inline uint16_t UnpackID(PackedID packed) {
91 return (uint16_t)packed;
92 }
reed@google.comac10a2d2010-12-22 21:39:39 +000093
jvanverthdd6d2272014-07-22 13:25:26 -070094 static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
95 return glyph.fPackedID;
96 }
97
98 static inline uint32_t Hash(GrGlyph::PackedID key) {
joshualitt010db532015-04-21 10:07:26 -070099 return SkChecksum::Mix(key);
jvanverthdd6d2272014-07-22 13:25:26 -0700100 }
101};
reed@google.comac10a2d2010-12-22 21:39:39 +0000102
103#endif