blob: 5005b74543ac4eb5f38cf42f3786308c201f71ac [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
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000019class GrPlot;
reed@google.comac10a2d2010-12-22 21:39:39 +000020
21/* Need this to be quad-state:
22 - complete w/ image
23 - just metrics
24 - failed to get image, but has metrics
25 - failed to get metrics
26 */
27struct GrGlyph {
jvanvertha7634612015-03-19 06:08:31 -070028 enum MaskStyle {
29 kCoverage_MaskStyle,
30 kDistance_MaskStyle
31 };
mtklein852f15d2016-03-17 10:51:27 -070032
reed@google.comac10a2d2010-12-22 21:39:39 +000033 typedef uint32_t PackedID;
34
Brian Salomon2ee084e2016-12-16 18:59:19 -050035 GrDrawOpAtlas::AtlasID fID;
joshualitt7c3a2f82015-03-31 13:32:05 -070036 SkPath* fPath;
37 PackedID fPackedID;
38 GrMaskFormat fMaskFormat;
39 GrIRect16 fBounds;
40 SkIPoint16 fAtlasLocation;
joshualitt010db532015-04-21 10:07:26 -070041 bool fTooLargeForAtlas;
reed@google.comac10a2d2010-12-22 21:39:39 +000042
jvanverth294c3262014-10-10 11:36:12 -070043 void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) {
Brian Salomon2ee084e2016-12-16 18:59:19 -050044 fID = GrDrawOpAtlas::kInvalidAtlasID;
halcanary96fcdcc2015-08-27 07:41:13 -070045 fPath = nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +000046 fPackedID = packed;
47 fBounds.set(bounds);
jvanverth294c3262014-10-10 11:36:12 -070048 fMaskFormat = format;
reed@google.comac10a2d2010-12-22 21:39:39 +000049 fAtlasLocation.set(0, 0);
Brian Salomon2ee084e2016-12-16 18:59:19 -050050 fTooLargeForAtlas = GrDrawOpAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height());
reed@google.comac10a2d2010-12-22 21:39:39 +000051 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052
mtklein852f15d2016-03-17 10:51:27 -070053 void reset() {
reed@google.comac10a2d2010-12-22 21:39:39 +000054 if (fPath) {
55 delete fPath;
halcanary96fcdcc2015-08-27 07:41:13 -070056 fPath = nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +000057 }
58 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000059
reed@google.comac10a2d2010-12-22 21:39:39 +000060 int width() const { return fBounds.width(); }
61 int height() const { return fBounds.height(); }
62 bool isEmpty() const { return fBounds.isEmpty(); }
63 uint16_t glyphID() const { return UnpackID(fPackedID); }
64
65 ///////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000067 static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
reed@google.comac10a2d2010-12-22 21:39:39 +000068 // two most significant fraction bits from fixed-point
69 return (pos >> 14) & 3;
70 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
jvanvertha7634612015-03-19 06:08:31 -070072 static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) {
reed@google.comac10a2d2010-12-22 21:39:39 +000073 x = ExtractSubPixelBitsFromFixed(x);
74 y = ExtractSubPixelBitsFromFixed(y);
jvanvertha7634612015-03-19 06:08:31 -070075 int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0;
76 return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID;
reed@google.comac10a2d2010-12-22 21:39:39 +000077 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000078
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000079 static inline SkFixed UnpackFixedX(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000080 return ((packed >> 18) & 3) << 14;
81 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000083 static inline SkFixed UnpackFixedY(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000084 return ((packed >> 16) & 3) << 14;
85 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
jvanvertha7634612015-03-19 06:08:31 -070087 static inline MaskStyle UnpackMaskStyle(PackedID packed) {
88 return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle;
89 }
mtklein852f15d2016-03-17 10:51:27 -070090
reed@google.comac10a2d2010-12-22 21:39:39 +000091 static inline uint16_t UnpackID(PackedID packed) {
92 return (uint16_t)packed;
93 }
reed@google.comac10a2d2010-12-22 21:39:39 +000094
jvanverthdd6d2272014-07-22 13:25:26 -070095 static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
96 return glyph.fPackedID;
97 }
98
99 static inline uint32_t Hash(GrGlyph::PackedID key) {
joshualitt010db532015-04-21 10:07:26 -0700100 return SkChecksum::Mix(key);
jvanverthdd6d2272014-07-22 13:25:26 -0700101 }
102};
reed@google.comac10a2d2010-12-22 21:39:39 +0000103
104#endif