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 | |
| 8 | #include "GrAtlasManager.h" |
| 9 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 10 | #include "GrGlyph.h" |
| 11 | #include "GrGlyphCache.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 12 | |
Cary Clark | aa5f38f | 2018-09-10 20:28:05 +0000 | [diff] [blame] | 13 | GrAtlasManager::GrAtlasManager(GrProxyProvider* proxyProvider, GrGlyphCache* glyphCache, |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame^] | 14 | size_t maxTextureBytes, |
Cary Clark | aa5f38f | 2018-09-10 20:28:05 +0000 | [diff] [blame] | 15 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing) |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame^] | 16 | : fAllowMultitexturing{allowMultitexturing} |
| 17 | , fGlyphSizeLimit{SkGlyphCacheCommon::kSkSideTooBigForAtlas} |
| 18 | , fProxyProvider{proxyProvider} |
| 19 | , fCaps{fProxyProvider->refCaps()} |
| 20 | , fGlyphCache{glyphCache} |
| 21 | , fAtlasConfigs{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 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 25 | static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 26 | switch (format) { |
| 27 | case kA8_GrMaskFormat: |
| 28 | return kAlpha_8_GrPixelConfig; |
| 29 | case kA565_GrMaskFormat: |
| 30 | return kRGB_565_GrPixelConfig; |
| 31 | case kARGB_GrMaskFormat: |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 32 | return kRGBA_8888_GrPixelConfig; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 33 | default: |
| 34 | SkDEBUGFAIL("unsupported GrMaskFormat"); |
| 35 | return kAlpha_8_GrPixelConfig; |
| 36 | } |
| 37 | } |
| 38 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 39 | void GrAtlasManager::freeAll() { |
| 40 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 41 | fAtlases[i] = nullptr; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | bool GrAtlasManager::hasGlyph(GrGlyph* glyph) { |
| 46 | SkASSERT(glyph); |
| 47 | return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID); |
| 48 | } |
| 49 | |
| 50 | // add to texture atlas that matches this format |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 51 | GrDrawOpAtlas::ErrorCode GrAtlasManager::addToAtlas( |
| 52 | GrResourceProvider* resourceProvider, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 53 | GrGlyphCache* glyphCache, |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 54 | GrTextStrike* strike, GrDrawOpAtlas::AtlasID* id, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 55 | GrDeferredUploadTarget* target, GrMaskFormat format, |
| 56 | int width, int height, const void* image, SkIPoint16* loc) { |
| 57 | glyphCache->setStrikeToPreserve(strike); |
| 58 | return this->getAtlas(format)->addToAtlas(resourceProvider, id, target, width, height, |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 59 | image, loc); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void GrAtlasManager::addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater* updater, |
| 63 | GrGlyph* glyph, |
| 64 | GrDeferredUploadToken token) { |
| 65 | SkASSERT(glyph); |
| 66 | updater->add(glyph->fID); |
| 67 | this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token); |
| 68 | } |
| 69 | |
| 70 | #ifdef SK_DEBUG |
| 71 | #include "GrContextPriv.h" |
| 72 | #include "GrSurfaceProxy.h" |
| 73 | #include "GrSurfaceContext.h" |
| 74 | #include "GrTextureProxy.h" |
| 75 | |
| 76 | #include "SkBitmap.h" |
| 77 | #include "SkImageEncoder.h" |
| 78 | #include "SkStream.h" |
| 79 | #include <stdio.h> |
| 80 | |
| 81 | /** |
| 82 | * Write the contents of the surface proxy to a PNG. Returns true if successful. |
| 83 | * @param filename Full path to desired file |
| 84 | */ |
| 85 | static bool save_pixels(GrContext* context, GrSurfaceProxy* sProxy, const char* filename) { |
| 86 | if (!sProxy) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | SkImageInfo ii = SkImageInfo::Make(sProxy->width(), sProxy->height(), |
| 91 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 92 | SkBitmap bm; |
| 93 | if (!bm.tryAllocPixels(ii)) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext( |
| 98 | sk_ref_sp(sProxy))); |
| 99 | if (!sContext || !sContext->asTextureProxy()) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0); |
| 104 | if (!result) { |
| 105 | SkDebugf("------ failed to read pixels for %s\n", filename); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // remove any previous version of this file |
| 110 | remove(filename); |
| 111 | |
| 112 | SkFILEWStream file(filename); |
| 113 | if (!file.isValid()) { |
| 114 | SkDebugf("------ failed to create file: %s\n", filename); |
| 115 | remove(filename); // remove any partial file |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) { |
| 120 | SkDebugf("------ failed to encode %s\n", filename); |
| 121 | remove(filename); // remove any partial file |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | void GrAtlasManager::dump(GrContext* context) const { |
| 129 | static int gDumpCount = 0; |
| 130 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 131 | if (fAtlases[i]) { |
| 132 | const sk_sp<GrTextureProxy>* proxies = fAtlases[i]->getProxies(); |
| 133 | for (uint32_t pageIdx = 0; pageIdx < fAtlases[i]->numActivePages(); ++pageIdx) { |
| 134 | SkASSERT(proxies[pageIdx]); |
| 135 | SkString filename; |
| 136 | #ifdef SK_BUILD_FOR_ANDROID |
| 137 | filename.printf("/sdcard/fontcache_%d%d%d.png", gDumpCount, i, pageIdx); |
| 138 | #else |
| 139 | filename.printf("fontcache_%d%d%d.png", gDumpCount, i, pageIdx); |
| 140 | #endif |
| 141 | |
| 142 | save_pixels(context, proxies[pageIdx].get(), filename.c_str()); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | ++gDumpCount; |
| 147 | } |
| 148 | #endif |
| 149 | |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame^] | 150 | void GrAtlasManager::setAtlasSizesToMinimum_ForTesting() { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 151 | // Delete any old atlases. |
| 152 | // This should be safe to do as long as we are not in the middle of a flush. |
| 153 | for (int i = 0; i < kMaskFormatCount; i++) { |
| 154 | fAtlases[i] = nullptr; |
| 155 | } |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame^] | 156 | |
| 157 | // Set all the atlas sizes to 1x1 plot each. |
| 158 | new (&fAtlasConfigs) GrDrawOpAtlasConfig{}; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool GrAtlasManager::initAtlas(GrMaskFormat format) { |
| 162 | int index = MaskFormatToAtlasIndex(format); |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame^] | 163 | if (fAtlases[index] == nullptr) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 164 | GrPixelConfig config = mask_format_to_pixel_config(format); |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame^] | 165 | SkISize atlasDimensions = fAtlasConfigs.atlasDimensions(format); |
| 166 | SkISize numPlots = fAtlasConfigs.numPlots(format); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 167 | |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame^] | 168 | fAtlases[index] = GrDrawOpAtlas::Make( |
| 169 | fProxyProvider, config, atlasDimensions.width(), atlasDimensions.height(), |
| 170 | numPlots.width(), numPlots.height(), fAllowMultitexturing, |
| 171 | &GrGlyphCache::HandleEviction, fGlyphCache); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 172 | if (!fAtlases[index]) { |
| 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | return true; |
| 177 | } |