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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrCaps.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 9 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrDistanceFieldGenFromVector.h" |
| 11 | #include "src/gpu/text/GrAtlasManager.h" |
| 12 | #include "src/gpu/text/GrStrikeCache.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 13 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/core/SkAutoMalloc.h" |
| 15 | #include "src/core/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}, |
Mike Klein | 65e8417 | 2019-02-13 12:25:55 -0500 | [diff] [blame] | 20 | GrMaskFormatBytesPerPixel(kA565_GrMaskFormat))) { } |
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) { |
Herb Derby | f5e5f19 | 2019-05-07 15:39:01 -0400 | [diff] [blame] | 42 | GrStrikeCache* grStrikeCache = reinterpret_cast<GrStrikeCache*>(ptr); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 43 | |
Herb Derby | f5e5f19 | 2019-05-07 15:39:01 -0400 | [diff] [blame] | 44 | StrikeHash::Iter iter(&grStrikeCache->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 |
Herb Derby | f5e5f19 | 2019-05-07 15:39:01 -0400 | [diff] [blame] | 51 | if (strike != grStrikeCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) { |
| 52 | grStrikeCache->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) { |
Herb Derby | 3e8e34e | 2019-06-20 15:16:13 -0400 | [diff] [blame] | 86 | SkASSERT(glyph.width() == width); |
| 87 | SkASSERT(glyph.height() == height); |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 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. |
Herb Derby | 3721688 | 2019-06-13 17:24:24 -0400 | [diff] [blame] | 132 | if (glyph.maskFormat() == SkMask::kBW_Format) { |
bsalomon | c2878e2 | 2016-05-17 13:18:03 -0700 | [diff] [blame] | 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 | |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 175 | void GrTextStrike::removeID(GrDrawOpAtlas::AtlasID id) { |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 176 | SkTDynamicHash<GrGlyph, SkPackedGlyphID>::Iter iter(&fCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 177 | while (!iter.done()) { |
| 178 | if (id == (*iter).fID) { |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 179 | (*iter).fID = GrDrawOpAtlas::kInvalidAtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 180 | fAtlasedGlyphs--; |
| 181 | SkASSERT(fAtlasedGlyphs >= 0); |
| 182 | } |
| 183 | ++iter; |
| 184 | } |
| 185 | } |
| 186 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 187 | GrDrawOpAtlas::ErrorCode GrTextStrike::addGlyphToAtlas( |
| 188 | GrResourceProvider* resourceProvider, |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 189 | GrDeferredUploadTarget* target, |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 190 | GrStrikeCache* glyphCache, |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 191 | GrAtlasManager* fullAtlasManager, |
| 192 | GrGlyph* glyph, |
Herb Derby | f5e5f19 | 2019-05-07 15:39:01 -0400 | [diff] [blame] | 193 | SkStrike* skStrikeCache, |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 194 | GrMaskFormat expectedMaskFormat, |
| 195 | bool isScaledGlyph) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 196 | SkASSERT(glyph); |
Herb Derby | f5e5f19 | 2019-05-07 15:39:01 -0400 | [diff] [blame] | 197 | SkASSERT(skStrikeCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 198 | SkASSERT(fCache.find(glyph->fPackedID)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 199 | |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 200 | expectedMaskFormat = fullAtlasManager->resolveMaskFormat(expectedMaskFormat); |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 201 | int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 202 | int width = glyph->width(); |
| 203 | int height = glyph->height(); |
| 204 | int rowBytes = width * bytesPerPixel; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 205 | |
| 206 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
Herb Derby | 37e21f6 | 2018-12-19 19:56:02 -0500 | [diff] [blame] | 207 | bool isSDFGlyph = GrGlyph::kDistance_MaskStyle == glyph->maskStyle(); |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 208 | bool addPad = isScaledGlyph && !isSDFGlyph; |
| 209 | if (addPad) { |
| 210 | width += 2; |
| 211 | rowBytes += 2*bytesPerPixel; |
| 212 | size += 2 * rowBytes; |
| 213 | height += 2; |
| 214 | size += 2 * (height + 2) * bytesPerPixel; |
| 215 | } |
joshualitt | 29f8679 | 2015-05-29 08:06:48 -0700 | [diff] [blame] | 216 | SkAutoSMalloc<1024> storage(size); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 217 | |
Herb Derby | 3e8e34e | 2019-06-20 15:16:13 -0400 | [diff] [blame] | 218 | SkGlyph* skGlyph = skStrikeCache->glyph(glyph->fPackedID); |
Jim Van Verth | d401da6 | 2018-05-03 10:40:30 -0400 | [diff] [blame] | 219 | void* dataPtr = storage.get(); |
| 220 | if (addPad) { |
| 221 | sk_bzero(dataPtr, size); |
| 222 | dataPtr = (char*)(dataPtr) + rowBytes + bytesPerPixel; |
| 223 | } |
Herb Derby | 3e8e34e | 2019-06-20 15:16:13 -0400 | [diff] [blame] | 224 | if (!get_packed_glyph_image(skStrikeCache, *skGlyph, glyph->width(), glyph->height(), |
Jim Van Verth | d401da6 | 2018-05-03 10:40:30 -0400 | [diff] [blame] | 225 | rowBytes, expectedMaskFormat, |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 226 | dataPtr, glyphCache->getMasks())) { |
Jim Van Verth | d401da6 | 2018-05-03 10:40:30 -0400 | [diff] [blame] | 227 | return GrDrawOpAtlas::ErrorCode::kError; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 230 | GrDrawOpAtlas::ErrorCode result = fullAtlasManager->addToAtlas( |
| 231 | resourceProvider, glyphCache, this, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 232 | &glyph->fID, target, expectedMaskFormat, |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 233 | width, height, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 234 | storage.get(), &glyph->fAtlasLocation); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 235 | if (GrDrawOpAtlas::ErrorCode::kSucceeded == result) { |
Jim Van Verth | cf838c7 | 2018-03-05 14:40:36 -0500 | [diff] [blame] | 236 | if (addPad) { |
| 237 | glyph->fAtlasLocation.fX += 1; |
| 238 | glyph->fAtlasLocation.fY += 1; |
| 239 | } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 240 | SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != glyph->fID); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 241 | fAtlasedGlyphs++; |
| 242 | } |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 243 | return result; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 244 | } |