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