| 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 | |
| 8 | #include "src/codec/SkMasks.h" |
| 9 | #include "src/core/SkArenaAlloc.h" |
| 10 | #include "src/core/SkAutoMalloc.h" |
| 11 | #include "src/core/SkDistanceFieldGen.h" |
| 12 | #include "src/core/SkStrikeSpec.h" |
| 13 | #include "src/gpu/GrCaps.h" |
| 14 | #include "src/gpu/GrColor.h" |
| 15 | #include "src/gpu/GrDistanceFieldGenFromVector.h" |
| 16 | #include "src/gpu/text/GrAtlasManager.h" |
| 17 | #include "src/gpu/text/GrStrikeCache.h" |
| 18 | |
| 19 | GrStrikeCache::~GrStrikeCache() { |
| 20 | this->freeAll(); |
| 21 | } |
| 22 | |
| 23 | void GrStrikeCache::freeAll() { |
| 24 | fCache.reset(); |
| 25 | } |
| 26 | |
| 27 | // expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to |
| 28 | // A8, RGB565, or RGBA8888. |
| 29 | template <typename INT_TYPE> |
| 30 | static void expand_bits(INT_TYPE* dst, |
| 31 | const uint8_t* src, |
| 32 | int width, |
| 33 | int height, |
| 34 | int dstRowBytes, |
| 35 | int srcRowBytes) { |
| 36 | for (int i = 0; i < height; ++i) { |
| 37 | int rowWritesLeft = width; |
| 38 | const uint8_t* s = src; |
| 39 | INT_TYPE* d = dst; |
| 40 | while (rowWritesLeft > 0) { |
| 41 | unsigned mask = *s++; |
| 42 | for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) { |
| 43 | *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0; |
| 44 | } |
| 45 | } |
| 46 | dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes); |
| 47 | src += srcRowBytes; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void get_packed_glyph_image( |
| 52 | const SkGlyph& glyph, int dstRB, GrMaskFormat expectedMaskFormat, void* dst) { |
| 53 | const int width = glyph.width(); |
| 54 | const int height = glyph.height(); |
| 55 | const void* src = glyph.image(); |
| 56 | SkASSERT(src != nullptr); |
| 57 | |
| 58 | GrMaskFormat grMaskFormat = GrGlyph::FormatFromSkGlyph(glyph.maskFormat()); |
| 59 | if (grMaskFormat == expectedMaskFormat) { |
| 60 | int srcRB = glyph.rowBytes(); |
| 61 | // Notice this comparison is with the glyphs raw mask format, and not its GrMaskFormat. |
| 62 | if (glyph.maskFormat() != SkMask::kBW_Format) { |
| 63 | if (srcRB != dstRB) { |
| 64 | const int bbp = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
| 65 | for (int y = 0; y < height; y++) { |
| 66 | memcpy(dst, src, width * bbp); |
| 67 | src = (const char*) src + srcRB; |
| 68 | dst = (char*) dst + dstRB; |
| 69 | } |
| 70 | } else { |
| 71 | memcpy(dst, src, dstRB * height); |
| 72 | } |
| 73 | } else { |
| 74 | // Handle 8-bit format by expanding the mask to the expected format. |
| 75 | const uint8_t* bits = reinterpret_cast<const uint8_t*>(src); |
| 76 | switch (expectedMaskFormat) { |
| 77 | case kA8_GrMaskFormat: { |
| 78 | uint8_t* bytes = reinterpret_cast<uint8_t*>(dst); |
| 79 | expand_bits(bytes, bits, width, height, dstRB, srcRB); |
| 80 | break; |
| 81 | } |
| 82 | case kA565_GrMaskFormat: { |
| 83 | uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst); |
| 84 | expand_bits(rgb565, bits, width, height, dstRB, srcRB); |
| 85 | break; |
| 86 | } |
| 87 | default: |
| 88 | SK_ABORT("Invalid GrMaskFormat" ); |
| 89 | } |
| 90 | } |
| 91 | } else if (grMaskFormat == kA565_GrMaskFormat && expectedMaskFormat == kARGB_GrMaskFormat) { |
| 92 | // Convert if the glyph uses a 565 mask format since it is using LCD text rendering |
| 93 | // but the expected format is 8888 (will happen on macOS with Metal since that |
| 94 | // combination does not support 565). |
| 95 | static constexpr SkMasks masks{ |
| 96 | {0b1111'1000'0000'0000, 11, 5}, // Red |
| 97 | {0b0000'0111'1110'0000, 5, 6}, // Green |
| 98 | {0b0000'0000'0001'1111, 0, 5}, // Blue |
| 99 | {0, 0, 0} // Alpha |
| 100 | }; |
| 101 | const int a565Bpp = GrMaskFormatBytesPerPixel(kA565_GrMaskFormat); |
| 102 | const int argbBpp = GrMaskFormatBytesPerPixel(kARGB_GrMaskFormat); |
| 103 | for (int y = 0; y < height; y++) { |
| 104 | for (int x = 0; x < width; x++) { |
| 105 | uint16_t color565 = 0; |
| 106 | memcpy(&color565, src, a565Bpp); |
| 107 | uint32_t colorRGBA = GrColorPackRGBA(masks.getRed(color565), |
| 108 | masks.getGreen(color565), |
| 109 | masks.getBlue(color565), |
| 110 | 0xFF); |
| 111 | memcpy(dst, &colorRGBA, argbBpp); |
| 112 | src = (char*)src + a565Bpp; |
| 113 | dst = (char*)dst + argbBpp; |
| 114 | } |
| 115 | } |
| 116 | } else { |
| 117 | // crbug:510931 |
| 118 | // Retrieving the image from the cache can actually change the mask format. This case is |
| 119 | // very uncommon so for now we just draw a clear box for these glyphs. |
| 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 | } |
| 126 | } |
| 127 | |
| 128 | /////////////////////////////////////////////////////////////////////////////// |
| 129 | |
| 130 | /* |
| 131 | The text strike is specific to a given font/style/matrix setup, which is |
| 132 | represented by the GrHostFontScaler object we are given in getGlyph(). |
| 133 | |
| 134 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
| 135 | atlas and a position within that texture. |
| 136 | */ |
| 137 | |
| 138 | GrTextStrike::GrTextStrike(const SkDescriptor& key) |
| 139 | : fFontScalerKey(key) {} |
| 140 | |
| 141 | GrDrawOpAtlas::ErrorCode GrTextStrike::addGlyphToAtlas(const SkGlyph& skGlyph, |
| 142 | GrMaskFormat expectedMaskFormat, |
| 143 | bool needsPadding, |
| 144 | GrResourceProvider* resourceProvider, |
| 145 | GrDeferredUploadTarget* target, |
| 146 | GrAtlasManager* fullAtlasManager, |
| 147 | GrGlyph* grGlyph) { |
| 148 | SkASSERT(grGlyph != nullptr); |
| 149 | SkASSERT(fCache.findOrNull(grGlyph->fPackedID)); |
| 150 | SkASSERT(skGlyph.image() != nullptr); |
| 151 | |
| 152 | expectedMaskFormat = fullAtlasManager->resolveMaskFormat(expectedMaskFormat); |
| 153 | int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
| 154 | |
| 155 | SkDEBUGCODE(bool isSDFGlyph = skGlyph.maskFormat() == SkMask::kSDF_Format;) |
| 156 | SkASSERT(!needsPadding || !isSDFGlyph); |
| 157 | |
| 158 | // Add 1 pixel padding around grGlyph if needed. |
| 159 | const int width = needsPadding ? skGlyph.width() + 2 : skGlyph.width(); |
| 160 | const int height = needsPadding ? skGlyph.height() + 2 : skGlyph.height(); |
| 161 | int rowBytes = width * bytesPerPixel; |
| 162 | size_t size = height * rowBytes; |
| 163 | |
| 164 | // Temporary storage for normalizing grGlyph image. |
| 165 | SkAutoSMalloc<1024> storage(size); |
| 166 | void* dataPtr = storage.get(); |
| 167 | if (needsPadding) { |
| 168 | sk_bzero(dataPtr, size); |
| 169 | // Advance in one row and one column. |
| 170 | dataPtr = (char*)(dataPtr) + rowBytes + bytesPerPixel; |
| 171 | } |
| 172 | |
| 173 | get_packed_glyph_image(skGlyph, rowBytes, expectedMaskFormat, dataPtr); |
| 174 | |
| 175 | return fullAtlasManager->addToAtlas(resourceProvider, target, expectedMaskFormat, width, height, |
| 176 | storage.get(), &grGlyph->fAtlasLocator); |
| 177 | } |
| 178 | |
| 179 | GrGlyph* GrTextStrike::getGlyph(SkPackedGlyphID packedGlyphID) { |
| 180 | GrGlyph* grGlyph = fCache.findOrNull(packedGlyphID); |
| 181 | if (grGlyph == nullptr) { |
| 182 | grGlyph = fAlloc.make<GrGlyph>(packedGlyphID); |
| 183 | fCache.set(grGlyph); |
| 184 | } |
| 185 | return grGlyph; |
| 186 | } |
| 187 | |
| 188 | |