joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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. |
| 6 | */ |
| 7 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 8 | #include "GrStrikeCache.h" |
Khushal | fa8ff09 | 2018-06-06 17:46:38 -0700 | [diff] [blame] | 9 | #include "GrAtlasManager.h" |
| 10 | #include "GrCaps.h" |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 11 | #include "GrColor.h" |
Khushal | fa8ff09 | 2018-06-06 17:46:38 -0700 | [diff] [blame] | 12 | #include "GrDistanceFieldGenFromVector.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 13 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 14 | #include "SkAutoMalloc.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 15 | #include "SkDistanceFieldGen.h" |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 16 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 17 | GrStrikeCache::GrStrikeCache(const GrCaps* caps, size_t maxTextureBytes) |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 18 | : fPreserveStrike(nullptr) |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 19 | , f565Masks(SkMasks::CreateMasks({0xF800, 0x07E0, 0x001F, 0}, |
Herb Derby | 96519f2 | 2018-09-12 17:15:26 -0400 | [diff] [blame] | 20 | GrMaskFormatBytesPerPixel(kA565_GrMaskFormat) * 8)) { } |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 21 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 22 | GrStrikeCache::~GrStrikeCache() { |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 23 | StrikeHash::Iter iter(&fCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 24 | while (!iter.done()) { |
joshualitt | a5f1d5a | 2015-05-22 13:09:57 -0700 | [diff] [blame] | 25 | (*iter).fIsAbandoned = true; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 26 | (*iter).unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 27 | ++iter; |
| 28 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 31 | void GrStrikeCache::freeAll() { |
bsalomon | c5fd5c4 | 2016-05-17 11:58:24 -0700 | [diff] [blame] | 32 | StrikeHash::Iter iter(&fCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 33 | while (!iter.done()) { |
joshualitt | a5f1d5a | 2015-05-22 13:09:57 -0700 | [diff] [blame] | 34 | (*iter).fIsAbandoned = true; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 35 | (*iter).unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 36 | ++iter; |
| 37 | } |
| 38 | fCache.rewind(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 41 | void GrStrikeCache::HandleEviction(GrDrawOpAtlas::AtlasID id, void* ptr) { |
| 42 | GrStrikeCache* glyphCache = reinterpret_cast<GrStrikeCache*>(ptr); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 43 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 44 | StrikeHash::Iter iter(&glyphCache->fCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 45 | for (; !iter.done(); ++iter) { |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 46 | GrTextStrike* strike = &*iter; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 47 | strike->removeID(id); |
| 48 | |
| 49 | // clear out any empty strikes. We will preserve the strike whose call to addToAtlas |
| 50 | // triggered the eviction |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 51 | if (strike != glyphCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) { |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 52 | glyphCache->fCache.remove(GrTextStrike::GetKey(*strike)); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 53 | strike->fIsAbandoned = true; |
| 54 | strike->unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 59 | // expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to |
| 60 | // A8, RGB565, or RGBA8888. |
| 61 | template <typename INT_TYPE> |
| 62 | static void expand_bits(INT_TYPE* dst, |
| 63 | const uint8_t* src, |
| 64 | int width, |
| 65 | int height, |
| 66 | int dstRowBytes, |
| 67 | int srcRowBytes) { |
| 68 | for (int i = 0; i < height; ++i) { |
| 69 | int rowWritesLeft = width; |
| 70 | const uint8_t* s = src; |
| 71 | INT_TYPE* d = dst; |
| 72 | while (rowWritesLeft > 0) { |
| 73 | unsigned mask = *s++; |
| 74 | for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) { |
| 75 | *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0; |
| 76 | } |
| 77 | } |
| 78 | dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes); |
| 79 | src += srcRowBytes; |
| 80 | } |
| 81 | } |
| 82 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 83 | static bool get_packed_glyph_image(SkStrike* cache, const SkGlyph& glyph, int width, |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 84 | int height, int dstRB, GrMaskFormat expectedMaskFormat, |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 85 | void* dst, const SkMasks& masks) { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 86 | SkASSERT(glyph.fWidth == width); |
| 87 | SkASSERT(glyph.fHeight == height); |
| 88 | const void* src = cache->findImage(glyph); |
| 89 | if (nullptr == src) { |
| 90 | return false; |
| 91 | } |
| 92 | |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 93 | // Convert if the glyph uses a 565 mask format since it is using LCD text rendering but the |
| 94 | // expected format is 8888 (will happen on macOS with Metal since that combination does not |
| 95 | // support 565). |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 96 | if (kA565_GrMaskFormat == GrGlyph::FormatFromSkGlyph(glyph) && |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 97 | kARGB_GrMaskFormat == expectedMaskFormat) { |
| 98 | const int a565Bpp = GrMaskFormatBytesPerPixel(kA565_GrMaskFormat); |
| 99 | const int argbBpp = GrMaskFormatBytesPerPixel(kARGB_GrMaskFormat); |
| 100 | for (int y = 0; y < height; y++) { |
| 101 | for (int x = 0; x < width; x++) { |
| 102 | uint16_t color565 = 0; |
| 103 | memcpy(&color565, src, a565Bpp); |
| 104 | uint32_t colorRGBA = GrColorPackRGBA(masks.getRed(color565), |
| 105 | masks.getGreen(color565), |
| 106 | masks.getBlue(color565), |
| 107 | 0xFF); |
| 108 | memcpy(dst, &colorRGBA, argbBpp); |
| 109 | src = (char*)src + a565Bpp; |
| 110 | dst = (char*)dst + argbBpp; |
| 111 | } |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 116 | // crbug:510931 |
| 117 | // Retrieving the image from the cache can actually change the mask format. This case is very |
| 118 | // uncommon so for now we just draw a clear box for these glyphs. |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 119 | if (GrGlyph::FormatFromSkGlyph(glyph) != expectedMaskFormat) { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 120 | const int bpp = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
| 121 | for (int y = 0; y < height; y++) { |
| 122 | sk_bzero(dst, width * bpp); |
| 123 | dst = (char*)dst + dstRB; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | int srcRB = glyph.rowBytes(); |
| 129 | // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to |
| 130 | // check the glyph's format, not the strike's format, and to be able to convert to any of the |
| 131 | // GrMaskFormats. |
| 132 | if (SkMask::kBW_Format == glyph.fMaskFormat) { |
| 133 | // expand bits to our mask type |
| 134 | const uint8_t* bits = reinterpret_cast<const uint8_t*>(src); |
| 135 | switch (expectedMaskFormat) { |
| 136 | case kA8_GrMaskFormat:{ |
| 137 | uint8_t* bytes = reinterpret_cast<uint8_t*>(dst); |
| 138 | expand_bits(bytes, bits, width, height, dstRB, srcRB); |
| 139 | break; |
| 140 | } |
| 141 | case kA565_GrMaskFormat: { |
| 142 | uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst); |
| 143 | expand_bits(rgb565, bits, width, height, dstRB, srcRB); |
| 144 | break; |
| 145 | } |
| 146 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 147 | SK_ABORT("Invalid GrMaskFormat"); |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 148 | } |
| 149 | } else if (srcRB == dstRB) { |
| 150 | memcpy(dst, src, dstRB * height); |
| 151 | } else { |
| 152 | const int bbp = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
| 153 | for (int y = 0; y < height; y++) { |
| 154 | memcpy(dst, src, width * bbp); |
| 155 | src = (const char*)src + srcRB; |
| 156 | dst = (char*)dst + dstRB; |
| 157 | } |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 162 | /////////////////////////////////////////////////////////////////////////////// |
| 163 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 164 | /* |
| 165 | The text strike is specific to a given font/style/matrix setup, which is |
| 166 | represented by the GrHostFontScaler object we are given in getGlyph(). |
| 167 | |
| 168 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
| 169 | atlas and a position within that texture. |
| 170 | */ |
| 171 | |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 172 | GrTextStrike::GrTextStrike(const SkDescriptor& key) |
Herbert Derby | 83ea522 | 2018-11-28 14:39:29 -0500 | [diff] [blame] | 173 | : fFontScalerKey(key) {} |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 174 | |
Herb Derby | 4c35be0 | 2018-12-20 14:03:47 -0500 | [diff] [blame] | 175 | GrGlyph* GrTextStrike::generateGlyph(const SkGlyph& skGlyph) { |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 176 | GrGlyph* grGlyph = fAlloc.make<GrGlyph>(skGlyph); |
| 177 | fCache.add(grGlyph); |
| 178 | return grGlyph; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 181 | void GrTextStrike::removeID(GrDrawOpAtlas::AtlasID id) { |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 182 | SkTDynamicHash<GrGlyph, SkPackedGlyphID>::Iter iter(&fCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 183 | while (!iter.done()) { |
| 184 | if (id == (*iter).fID) { |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 185 | (*iter).fID = GrDrawOpAtlas::kInvalidAtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 186 | fAtlasedGlyphs--; |
| 187 | SkASSERT(fAtlasedGlyphs >= 0); |
| 188 | } |
| 189 | ++iter; |
| 190 | } |
| 191 | } |
| 192 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 193 | GrDrawOpAtlas::ErrorCode GrTextStrike::addGlyphToAtlas( |
| 194 | GrResourceProvider* resourceProvider, |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 195 | GrDeferredUploadTarget* target, |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 196 | GrStrikeCache* glyphCache, |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 197 | GrAtlasManager* fullAtlasManager, |
| 198 | GrGlyph* glyph, |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 199 | SkStrike* cache, |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 200 | GrMaskFormat expectedMaskFormat, |
| 201 | bool isScaledGlyph) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 202 | SkASSERT(glyph); |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 203 | SkASSERT(cache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 204 | SkASSERT(fCache.find(glyph->fPackedID)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 205 | |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 206 | expectedMaskFormat = fullAtlasManager->resolveMaskFormat(expectedMaskFormat); |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 207 | int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 208 | int width = glyph->width(); |
| 209 | int height = glyph->height(); |
| 210 | int rowBytes = width * bytesPerPixel; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 211 | |
| 212 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
Herb Derby | 37e21f6 | 2018-12-19 19:56:02 -0500 | [diff] [blame] | 213 | bool isSDFGlyph = GrGlyph::kDistance_MaskStyle == glyph->maskStyle(); |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 214 | bool addPad = isScaledGlyph && !isSDFGlyph; |
| 215 | if (addPad) { |
| 216 | width += 2; |
| 217 | rowBytes += 2*bytesPerPixel; |
| 218 | size += 2 * rowBytes; |
| 219 | height += 2; |
| 220 | size += 2 * (height + 2) * bytesPerPixel; |
| 221 | } |
joshualitt | 29f8679 | 2015-05-29 08:06:48 -0700 | [diff] [blame] | 222 | SkAutoSMalloc<1024> storage(size); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 223 | |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 224 | const SkGlyph& skGlyph = GrToSkGlyph(cache, glyph->fPackedID); |
Jim Van Verth | d401da6 | 2018-05-03 10:40:30 -0400 | [diff] [blame] | 225 | void* dataPtr = storage.get(); |
| 226 | if (addPad) { |
| 227 | sk_bzero(dataPtr, size); |
| 228 | dataPtr = (char*)(dataPtr) + rowBytes + bytesPerPixel; |
| 229 | } |
| 230 | if (!get_packed_glyph_image(cache, skGlyph, glyph->width(), glyph->height(), |
| 231 | rowBytes, expectedMaskFormat, |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 232 | dataPtr, glyphCache->getMasks())) { |
Jim Van Verth | d401da6 | 2018-05-03 10:40:30 -0400 | [diff] [blame] | 233 | return GrDrawOpAtlas::ErrorCode::kError; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 236 | GrDrawOpAtlas::ErrorCode result = fullAtlasManager->addToAtlas( |
| 237 | resourceProvider, glyphCache, this, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 238 | &glyph->fID, target, expectedMaskFormat, |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 239 | width, height, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 240 | storage.get(), &glyph->fAtlasLocation); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 241 | if (GrDrawOpAtlas::ErrorCode::kSucceeded == result) { |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 242 | if (addPad) { |
| 243 | glyph->fAtlasLocation.fX += 1; |
| 244 | glyph->fAtlasLocation.fY += 1; |
| 245 | } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 246 | SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != glyph->fID); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 247 | fAtlasedGlyphs++; |
| 248 | } |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 249 | return result; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 250 | } |