blob: 55e925f09d120a06d455bdc71140be5c1ae5167c [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
joshualitt7c3a2f82015-03-31 13:32:05 -070011#include "GrBatchAtlas.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"
jvanverth294c3262014-10-10 11:36:12 -070016#include "SkPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000018class GrPlot;
reed@google.comac10a2d2010-12-22 21:39:39 +000019
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 */
26struct GrGlyph {
jvanvertha7634612015-03-19 06:08:31 -070027 enum MaskStyle {
28 kCoverage_MaskStyle,
29 kDistance_MaskStyle
30 };
31
reed@google.comac10a2d2010-12-22 21:39:39 +000032 typedef uint32_t PackedID;
33
joshualitt7c3a2f82015-03-31 13:32:05 -070034 GrBatchAtlas::AtlasID fID;
joshualitt7c3a2f82015-03-31 13:32:05 -070035 SkPath* fPath;
36 PackedID fPackedID;
37 GrMaskFormat fMaskFormat;
38 GrIRect16 fBounds;
39 SkIPoint16 fAtlasLocation;
joshualitt010db532015-04-21 10:07:26 -070040 bool fTooLargeForAtlas;
reed@google.comac10a2d2010-12-22 21:39:39 +000041
jvanverth294c3262014-10-10 11:36:12 -070042 void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) {
joshualitt7c3a2f82015-03-31 13:32:05 -070043 fID = GrBatchAtlas::kInvalidAtlasID;
Tom Hudson2880df22015-10-29 09:55:42 -040044 fPath = nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +000045 fPackedID = packed;
46 fBounds.set(bounds);
jvanverth294c3262014-10-10 11:36:12 -070047 fMaskFormat = format;
reed@google.comac10a2d2010-12-22 21:39:39 +000048 fAtlasLocation.set(0, 0);
joshualitt010db532015-04-21 10:07:26 -070049 fTooLargeForAtlas = GrBatchAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height());
reed@google.comac10a2d2010-12-22 21:39:39 +000050 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@google.comac10a2d2010-12-22 21:39:39 +000052 void free() {
53 if (fPath) {
54 delete fPath;
Tom Hudson2880df22015-10-29 09:55:42 -040055 fPath = nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +000056 }
57 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000058
reed@google.comac10a2d2010-12-22 21:39:39 +000059 int width() const { return fBounds.width(); }
60 int height() const { return fBounds.height(); }
61 bool isEmpty() const { return fBounds.isEmpty(); }
62 uint16_t glyphID() const { return UnpackID(fPackedID); }
63
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 }
89
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