Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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/text/GrAtlasManager.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrGlyph.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/text/GrStrikeCache.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 13 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 14 | GrAtlasManager::GrAtlasManager(GrProxyProvider* proxyProvider, GrStrikeCache* glyphCache, |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 15 | size_t maxTextureBytes, |
Cary Clark | aa5f38f | 2018-09-10 20:28:05 +0000 | [diff] [blame] | 16 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing) |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 17 | : fAllowMultitexturing{allowMultitexturing} |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 18 | , fProxyProvider{proxyProvider} |
| 19 | , fCaps{fProxyProvider->refCaps()} |
| 20 | , fGlyphCache{glyphCache} |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 21 | , fAtlasConfig{fCaps->maxTextureSize(), maxTextureBytes} { } |
Cary Clark | aa5f38f | 2018-09-10 20:28:05 +0000 | [diff] [blame] | 22 | |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 23 | GrAtlasManager::~GrAtlasManager() = default; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 24 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 25 | void GrAtlasManager::freeAll() { |
| 26 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 27 | fAtlases[i] = nullptr; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | bool GrAtlasManager::hasGlyph(GrGlyph* glyph) { |
| 32 | SkASSERT(glyph); |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame^] | 33 | return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fPlotLocator); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // add to texture atlas that matches this format |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 37 | GrDrawOpAtlas::ErrorCode GrAtlasManager::addToAtlas( |
| 38 | GrResourceProvider* resourceProvider, |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 39 | GrStrikeCache* glyphCache, |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame^] | 40 | GrTextStrike* strike, GrDrawOpAtlas::PlotLocator* plotLocator, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 41 | GrDeferredUploadTarget* target, GrMaskFormat format, |
| 42 | int width, int height, const void* image, SkIPoint16* loc) { |
| 43 | glyphCache->setStrikeToPreserve(strike); |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame^] | 44 | return this->getAtlas(format)->addToAtlas( |
| 45 | resourceProvider, plotLocator, target, width, height, image, loc); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void GrAtlasManager::addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater* updater, |
| 49 | GrGlyph* glyph, |
| 50 | GrDeferredUploadToken token) { |
| 51 | SkASSERT(glyph); |
Herb Derby | 4d72171 | 2020-01-24 14:31:16 -0500 | [diff] [blame^] | 52 | if (updater->add(glyph->fPlotLocator)) { |
| 53 | this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fPlotLocator, token); |
Jim Van Verth | ba98b7d | 2018-12-05 12:33:43 -0500 | [diff] [blame] | 54 | } |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | #ifdef SK_DEBUG |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 58 | #include "src/gpu/GrContextPriv.h" |
| 59 | #include "src/gpu/GrSurfaceContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 60 | #include "src/gpu/GrSurfaceProxy.h" |
| 61 | #include "src/gpu/GrTextureProxy.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 62 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 63 | #include "include/core/SkBitmap.h" |
| 64 | #include "include/core/SkImageEncoder.h" |
| 65 | #include "include/core/SkStream.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 66 | #include <stdio.h> |
| 67 | |
| 68 | /** |
| 69 | * Write the contents of the surface proxy to a PNG. Returns true if successful. |
| 70 | * @param filename Full path to desired file |
| 71 | */ |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 72 | static bool save_pixels(GrContext* context, GrSurfaceProxyView view, GrColorType colorType, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 73 | const char* filename) { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 74 | if (!view.proxy()) { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 78 | SkImageInfo ii = |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 79 | SkImageInfo::Make(view.proxy()->dimensions(), kRGBA_8888_SkColorType, |
| 80 | kPremul_SkAlphaType); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 81 | SkBitmap bm; |
| 82 | if (!bm.tryAllocPixels(ii)) { |
| 83 | return false; |
| 84 | } |
| 85 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 86 | auto sContext = GrSurfaceContext::Make(context, std::move(view), colorType, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 87 | kUnknown_SkAlphaType, nullptr); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 88 | if (!sContext || !sContext->asTextureProxy()) { |
| 89 | return false; |
| 90 | } |
| 91 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 92 | bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0}); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 93 | if (!result) { |
| 94 | SkDebugf("------ failed to read pixels for %s\n", filename); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | // remove any previous version of this file |
| 99 | remove(filename); |
| 100 | |
| 101 | SkFILEWStream file(filename); |
| 102 | if (!file.isValid()) { |
| 103 | SkDebugf("------ failed to create file: %s\n", filename); |
| 104 | remove(filename); // remove any partial file |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) { |
| 109 | SkDebugf("------ failed to encode %s\n", filename); |
| 110 | remove(filename); // remove any partial file |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | void GrAtlasManager::dump(GrContext* context) const { |
| 118 | static int gDumpCount = 0; |
| 119 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 120 | if (fAtlases[i]) { |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 121 | const GrSurfaceProxyView* views = fAtlases[i]->getViews(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 122 | for (uint32_t pageIdx = 0; pageIdx < fAtlases[i]->numActivePages(); ++pageIdx) { |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 123 | SkASSERT(views[pageIdx].proxy()); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 124 | SkString filename; |
| 125 | #ifdef SK_BUILD_FOR_ANDROID |
| 126 | filename.printf("/sdcard/fontcache_%d%d%d.png", gDumpCount, i, pageIdx); |
| 127 | #else |
| 128 | filename.printf("fontcache_%d%d%d.png", gDumpCount, i, pageIdx); |
| 129 | #endif |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 130 | auto ct = GrMaskFormatToColorType(AtlasIndexToMaskFormat(i)); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 131 | save_pixels(context, views[pageIdx], ct, filename.c_str()); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | ++gDumpCount; |
| 136 | } |
| 137 | #endif |
| 138 | |
Brian Salomon | 2638f3d | 2019-10-22 12:20:37 -0400 | [diff] [blame] | 139 | void GrAtlasManager::setAtlasDimensionsToMinimum_ForTesting() { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 140 | // Delete any old atlases. |
| 141 | // This should be safe to do as long as we are not in the middle of a flush. |
| 142 | for (int i = 0; i < kMaskFormatCount; i++) { |
| 143 | fAtlases[i] = nullptr; |
| 144 | } |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 145 | |
| 146 | // Set all the atlas sizes to 1x1 plot each. |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 147 | new (&fAtlasConfig) GrDrawOpAtlasConfig{}; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | bool GrAtlasManager::initAtlas(GrMaskFormat format) { |
| 151 | int index = MaskFormatToAtlasIndex(format); |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 152 | if (fAtlases[index] == nullptr) { |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 153 | GrColorType grColorType = GrMaskFormatToColorType(format); |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 154 | SkISize atlasDimensions = fAtlasConfig.atlasDimensions(format); |
| 155 | SkISize plotDimensions = fAtlasConfig.plotDimensions(format); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 156 | |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 157 | const GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, |
| 158 | GrRenderable::kNo); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 159 | |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 160 | fAtlases[index] = GrDrawOpAtlas::Make( |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 161 | fProxyProvider, format, grColorType, |
| 162 | atlasDimensions.width(), atlasDimensions.height(), |
| 163 | plotDimensions.width(), plotDimensions.height(), |
Herb Derby | 1a496c5 | 2020-01-22 17:26:56 -0500 | [diff] [blame] | 164 | fAllowMultitexturing, fGlyphCache); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 165 | if (!fAtlases[index]) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | return true; |
| 170 | } |