blob: 978ac455aaee3181e25b9076c0c9de080f111022 [file] [log] [blame]
Robert Phillipsc4039ea2018-03-01 11:36:45 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/text/GrAtlasManager.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrGlyph.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040011#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050013
Herb Derby081e6f32019-01-16 13:46:02 -050014GrAtlasManager::GrAtlasManager(GrProxyProvider* proxyProvider, GrStrikeCache* glyphCache,
Herb Derby3c4d5332018-09-07 15:27:57 -040015 size_t maxTextureBytes,
Cary Clarkaa5f38f2018-09-10 20:28:05 +000016 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing)
Herb Derby3c4d5332018-09-07 15:27:57 -040017 : fAllowMultitexturing{allowMultitexturing}
Herb Derby3c4d5332018-09-07 15:27:57 -040018 , fProxyProvider{proxyProvider}
19 , fCaps{fProxyProvider->refCaps()}
20 , fGlyphCache{glyphCache}
Jim Van Verthf6206f92018-12-14 08:22:24 -050021 , fAtlasConfig{fCaps->maxTextureSize(), maxTextureBytes} { }
Cary Clarkaa5f38f2018-09-10 20:28:05 +000022
Herb Derby3c4d5332018-09-07 15:27:57 -040023GrAtlasManager::~GrAtlasManager() = default;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050024
Robert Phillips42dda082019-05-14 13:29:45 -040025static GrColorType mask_format_to_gr_color_type(GrMaskFormat format) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050026 switch (format) {
27 case kA8_GrMaskFormat:
Robert Phillips42dda082019-05-14 13:29:45 -040028 return GrColorType::kAlpha_8;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050029 case kA565_GrMaskFormat:
Greg Daniel48fec762019-06-18 17:06:43 -040030 return GrColorType::kBGR_565;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050031 case kARGB_GrMaskFormat:
Robert Phillips42dda082019-05-14 13:29:45 -040032 return GrColorType::kRGBA_8888;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050033 default:
34 SkDEBUGFAIL("unsupported GrMaskFormat");
Robert Phillips42dda082019-05-14 13:29:45 -040035 return GrColorType::kAlpha_8;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050036 }
37}
38
Robert Phillipsc4039ea2018-03-01 11:36:45 -050039void GrAtlasManager::freeAll() {
40 for (int i = 0; i < kMaskFormatCount; ++i) {
41 fAtlases[i] = nullptr;
42 }
43}
44
45bool 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 Phillipsd2e9f762018-03-07 11:54:37 -050051GrDrawOpAtlas::ErrorCode GrAtlasManager::addToAtlas(
52 GrResourceProvider* resourceProvider,
Herb Derby081e6f32019-01-16 13:46:02 -050053 GrStrikeCache* glyphCache,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050054 GrTextStrike* strike, GrDrawOpAtlas::AtlasID* id,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050055 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 Phillipsd2e9f762018-03-07 11:54:37 -050059 image, loc);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050060}
61
62void GrAtlasManager::addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater* updater,
63 GrGlyph* glyph,
64 GrDeferredUploadToken token) {
65 SkASSERT(glyph);
Jim Van Verthba98b7d2018-12-05 12:33:43 -050066 if (updater->add(glyph->fID)) {
67 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token);
68 }
Robert Phillipsc4039ea2018-03-01 11:36:45 -050069}
70
71#ifdef SK_DEBUG
Mike Kleinc0bd9f92019-04-23 12:05:21 -050072#include "src/gpu/GrContextPriv.h"
73#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040074#include "src/gpu/GrSurfaceProxy.h"
75#include "src/gpu/GrTextureProxy.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050076
Mike Kleinc0bd9f92019-04-23 12:05:21 -050077#include "include/core/SkBitmap.h"
78#include "include/core/SkImageEncoder.h"
79#include "include/core/SkStream.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050080#include <stdio.h>
81
82/**
83 * Write the contents of the surface proxy to a PNG. Returns true if successful.
84 * @param filename Full path to desired file
85 */
Greg Daniel3912a4b2020-01-14 09:56:04 -050086static bool save_pixels(GrContext* context, GrSurfaceProxyView view, GrColorType colorType,
Brian Salomond6287472019-06-24 15:50:07 -040087 const char* filename) {
Greg Daniel3912a4b2020-01-14 09:56:04 -050088 if (!view.proxy()) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050089 return false;
90 }
91
Brian Salomon9f2b86c2019-10-22 10:37:46 -040092 SkImageInfo ii =
Greg Daniel3912a4b2020-01-14 09:56:04 -050093 SkImageInfo::Make(view.proxy()->dimensions(), kRGBA_8888_SkColorType,
94 kPremul_SkAlphaType);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050095 SkBitmap bm;
96 if (!bm.tryAllocPixels(ii)) {
97 return false;
98 }
99
Greg Daniel3912a4b2020-01-14 09:56:04 -0500100 auto sContext = GrSurfaceContext::Make(context, std::move(view), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -0500101 kUnknown_SkAlphaType, nullptr);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500102 if (!sContext || !sContext->asTextureProxy()) {
103 return false;
104 }
105
Brian Salomon1d435302019-07-01 13:05:28 -0400106 bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0});
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500107 if (!result) {
108 SkDebugf("------ failed to read pixels for %s\n", filename);
109 return false;
110 }
111
112 // remove any previous version of this file
113 remove(filename);
114
115 SkFILEWStream file(filename);
116 if (!file.isValid()) {
117 SkDebugf("------ failed to create file: %s\n", filename);
118 remove(filename); // remove any partial file
119 return false;
120 }
121
122 if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
123 SkDebugf("------ failed to encode %s\n", filename);
124 remove(filename); // remove any partial file
125 return false;
126 }
127
128 return true;
129}
130
131void GrAtlasManager::dump(GrContext* context) const {
132 static int gDumpCount = 0;
133 for (int i = 0; i < kMaskFormatCount; ++i) {
134 if (fAtlases[i]) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500135 const GrSurfaceProxyView* views = fAtlases[i]->getViews();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500136 for (uint32_t pageIdx = 0; pageIdx < fAtlases[i]->numActivePages(); ++pageIdx) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500137 SkASSERT(views[pageIdx].proxy());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500138 SkString filename;
139#ifdef SK_BUILD_FOR_ANDROID
140 filename.printf("/sdcard/fontcache_%d%d%d.png", gDumpCount, i, pageIdx);
141#else
142 filename.printf("fontcache_%d%d%d.png", gDumpCount, i, pageIdx);
143#endif
Brian Salomond6287472019-06-24 15:50:07 -0400144 auto ct = mask_format_to_gr_color_type(AtlasIndexToMaskFormat(i));
Greg Daniel3912a4b2020-01-14 09:56:04 -0500145 save_pixels(context, views[pageIdx], ct, filename.c_str());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500146 }
147 }
148 }
149 ++gDumpCount;
150}
151#endif
152
Brian Salomon2638f3d2019-10-22 12:20:37 -0400153void GrAtlasManager::setAtlasDimensionsToMinimum_ForTesting() {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500154 // Delete any old atlases.
155 // This should be safe to do as long as we are not in the middle of a flush.
156 for (int i = 0; i < kMaskFormatCount; i++) {
157 fAtlases[i] = nullptr;
158 }
Herb Derby3c4d5332018-09-07 15:27:57 -0400159
160 // Set all the atlas sizes to 1x1 plot each.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500161 new (&fAtlasConfig) GrDrawOpAtlasConfig{};
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500162}
163
164bool GrAtlasManager::initAtlas(GrMaskFormat format) {
165 int index = MaskFormatToAtlasIndex(format);
Herb Derby3c4d5332018-09-07 15:27:57 -0400166 if (fAtlases[index] == nullptr) {
Robert Phillips42dda082019-05-14 13:29:45 -0400167 GrColorType grColorType = mask_format_to_gr_color_type(format);
Jim Van Verthf6206f92018-12-14 08:22:24 -0500168 SkISize atlasDimensions = fAtlasConfig.atlasDimensions(format);
169 SkISize plotDimensions = fAtlasConfig.plotDimensions(format);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500170
Robert Phillips0a15cc62019-07-30 12:49:10 -0400171 const GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType,
172 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500173
Herb Derby3c4d5332018-09-07 15:27:57 -0400174 fAtlases[index] = GrDrawOpAtlas::Make(
Robert Phillips42dda082019-05-14 13:29:45 -0400175 fProxyProvider, format, grColorType,
176 atlasDimensions.width(), atlasDimensions.height(),
177 plotDimensions.width(), plotDimensions.height(),
178 fAllowMultitexturing, &GrStrikeCache::HandleEviction, fGlyphCache);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500179 if (!fAtlases[index]) {
180 return false;
181 }
182 }
183 return true;
184}