blob: c73c48223344d060893efe387d0e837c786295ae [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 PackedID fPackedID;
35 GrMaskFormat fMaskFormat;
36 GrIRect16 fBounds;
37 SkIPoint16 fAtlasLocation;
reed@google.comac10a2d2010-12-22 21:39:39 +000038
jvanverth294c3262014-10-10 11:36:12 -070039 void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) {
Brian Salomon2ee084e2016-12-16 18:59:19 -050040 fID = GrDrawOpAtlas::kInvalidAtlasID;
reed@google.comac10a2d2010-12-22 21:39:39 +000041 fPackedID = packed;
42 fBounds.set(bounds);
jvanverth294c3262014-10-10 11:36:12 -070043 fMaskFormat = format;
reed@google.comac10a2d2010-12-22 21:39:39 +000044 fAtlasLocation.set(0, 0);
45 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046
Herb Derby4a823962018-08-02 15:52:30 -040047 void reset() { }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048
reed@google.comac10a2d2010-12-22 21:39:39 +000049 int width() const { return fBounds.width(); }
50 int height() const { return fBounds.height(); }
51 bool isEmpty() const { return fBounds.isEmpty(); }
52 uint16_t glyphID() const { return UnpackID(fPackedID); }
Jim Van Vertheafa64b2017-09-18 10:05:00 -040053 uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); }
reed@google.comac10a2d2010-12-22 21:39:39 +000054
55 ///////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000056
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000057 static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
reed@google.comac10a2d2010-12-22 21:39:39 +000058 // two most significant fraction bits from fixed-point
59 return (pos >> 14) & 3;
60 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000061
jvanvertha7634612015-03-19 06:08:31 -070062 static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) {
reed@google.comac10a2d2010-12-22 21:39:39 +000063 x = ExtractSubPixelBitsFromFixed(x);
64 y = ExtractSubPixelBitsFromFixed(y);
jvanvertha7634612015-03-19 06:08:31 -070065 int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0;
66 return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID;
reed@google.comac10a2d2010-12-22 21:39:39 +000067 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000069 static inline SkFixed UnpackFixedX(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000070 return ((packed >> 18) & 3) << 14;
71 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000073 static inline SkFixed UnpackFixedY(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000074 return ((packed >> 16) & 3) << 14;
75 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000076
jvanvertha7634612015-03-19 06:08:31 -070077 static inline MaskStyle UnpackMaskStyle(PackedID packed) {
78 return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle;
79 }
mtklein852f15d2016-03-17 10:51:27 -070080
reed@google.comac10a2d2010-12-22 21:39:39 +000081 static inline uint16_t UnpackID(PackedID packed) {
82 return (uint16_t)packed;
83 }
reed@google.comac10a2d2010-12-22 21:39:39 +000084
jvanverthdd6d2272014-07-22 13:25:26 -070085 static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
86 return glyph.fPackedID;
87 }
88
89 static inline uint32_t Hash(GrGlyph::PackedID key) {
joshualitt010db532015-04-21 10:07:26 -070090 return SkChecksum::Mix(key);
jvanverthdd6d2272014-07-22 13:25:26 -070091 }
92};
reed@google.comac10a2d2010-12-22 21:39:39 +000093
94#endif