blob: 8750c763e528999da3f7376aafe99424f2f024f1 [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 // 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;
joshualitt010db532015-04-21 10:07:26 -070042 bool fTooLargeForAtlas;
reed@google.comac10a2d2010-12-22 21:39:39 +000043
jvanverth294c3262014-10-10 11:36:12 -070044 void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) {
joshualitt7c3a2f82015-03-31 13:32:05 -070045 fID = GrBatchAtlas::kInvalidAtlasID;
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000046 fPlot = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000047 fPath = NULL;
48 fPackedID = packed;
49 fBounds.set(bounds);
jvanverth294c3262014-10-10 11:36:12 -070050 fMaskFormat = format;
reed@google.comac10a2d2010-12-22 21:39:39 +000051 fAtlasLocation.set(0, 0);
joshualitt010db532015-04-21 10:07:26 -070052 fTooLargeForAtlas = GrBatchAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height());
reed@google.comac10a2d2010-12-22 21:39:39 +000053 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000054
reed@google.comac10a2d2010-12-22 21:39:39 +000055 void free() {
56 if (fPath) {
57 delete fPath;
58 fPath = NULL;
59 }
60 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000061
reed@google.comac10a2d2010-12-22 21:39:39 +000062 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.comfbfcd562012-08-23 18:09:54 +000068
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000069 static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
reed@google.comac10a2d2010-12-22 21:39:39 +000070 // two most significant fraction bits from fixed-point
71 return (pos >> 14) & 3;
72 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
jvanvertha7634612015-03-19 06:08:31 -070074 static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) {
reed@google.comac10a2d2010-12-22 21:39:39 +000075 x = ExtractSubPixelBitsFromFixed(x);
76 y = ExtractSubPixelBitsFromFixed(y);
jvanvertha7634612015-03-19 06:08:31 -070077 int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0;
78 return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID;
reed@google.comac10a2d2010-12-22 21:39:39 +000079 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000081 static inline SkFixed UnpackFixedX(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000082 return ((packed >> 18) & 3) << 14;
83 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000084
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000085 static inline SkFixed UnpackFixedY(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000086 return ((packed >> 16) & 3) << 14;
87 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
jvanvertha7634612015-03-19 06:08:31 -070089 static inline MaskStyle UnpackMaskStyle(PackedID packed) {
90 return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle;
91 }
92
reed@google.comac10a2d2010-12-22 21:39:39 +000093 static inline uint16_t UnpackID(PackedID packed) {
94 return (uint16_t)packed;
95 }
reed@google.comac10a2d2010-12-22 21:39:39 +000096
jvanverthdd6d2272014-07-22 13:25:26 -070097 static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
98 return glyph.fPackedID;
99 }
100
101 static inline uint32_t Hash(GrGlyph::PackedID key) {
joshualitt010db532015-04-21 10:07:26 -0700102 return SkChecksum::Mix(key);
jvanverthdd6d2272014-07-22 13:25:26 -0700103 }
104};
reed@google.comac10a2d2010-12-22 21:39:39 +0000105
106#endif