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