blob: 26dad6b1d060cee23161b3364b5e28e2578aaeac [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 Phillipsc4039ea2018-03-01 11:36:45 -050025void GrAtlasManager::freeAll() {
26 for (int i = 0; i < kMaskFormatCount; ++i) {
27 fAtlases[i] = nullptr;
28 }
29}
30
31bool GrAtlasManager::hasGlyph(GrGlyph* glyph) {
32 SkASSERT(glyph);
33 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
34}
35
36// add to texture atlas that matches this format
Robert Phillipsd2e9f762018-03-07 11:54:37 -050037GrDrawOpAtlas::ErrorCode GrAtlasManager::addToAtlas(
38 GrResourceProvider* resourceProvider,
Herb Derby081e6f32019-01-16 13:46:02 -050039 GrStrikeCache* glyphCache,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050040 GrTextStrike* strike, GrDrawOpAtlas::AtlasID* id,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050041 GrDeferredUploadTarget* target, GrMaskFormat format,
42 int width, int height, const void* image, SkIPoint16* loc) {
43 glyphCache->setStrikeToPreserve(strike);
44 return this->getAtlas(format)->addToAtlas(resourceProvider, id, target, width, height,
Robert Phillipsd2e9f762018-03-07 11:54:37 -050045 image, loc);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050046}
47
48void GrAtlasManager::addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater* updater,
49 GrGlyph* glyph,
50 GrDeferredUploadToken token) {
51 SkASSERT(glyph);
Jim Van Verthba98b7d2018-12-05 12:33:43 -050052 if (updater->add(glyph->fID)) {
53 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token);
54 }
Robert Phillipsc4039ea2018-03-01 11:36:45 -050055}
56
57#ifdef SK_DEBUG
Mike Kleinc0bd9f92019-04-23 12:05:21 -050058#include "src/gpu/GrContextPriv.h"
59#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040060#include "src/gpu/GrSurfaceProxy.h"
61#include "src/gpu/GrTextureProxy.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050062
Mike Kleinc0bd9f92019-04-23 12:05:21 -050063#include "include/core/SkBitmap.h"
64#include "include/core/SkImageEncoder.h"
65#include "include/core/SkStream.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050066#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 Daniel3912a4b2020-01-14 09:56:04 -050072static bool save_pixels(GrContext* context, GrSurfaceProxyView view, GrColorType colorType,
Brian Salomond6287472019-06-24 15:50:07 -040073 const char* filename) {
Greg Daniel3912a4b2020-01-14 09:56:04 -050074 if (!view.proxy()) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050075 return false;
76 }
77
Brian Salomon9f2b86c2019-10-22 10:37:46 -040078 SkImageInfo ii =
Greg Daniel3912a4b2020-01-14 09:56:04 -050079 SkImageInfo::Make(view.proxy()->dimensions(), kRGBA_8888_SkColorType,
80 kPremul_SkAlphaType);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050081 SkBitmap bm;
82 if (!bm.tryAllocPixels(ii)) {
83 return false;
84 }
85
Greg Daniel3912a4b2020-01-14 09:56:04 -050086 auto sContext = GrSurfaceContext::Make(context, std::move(view), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -050087 kUnknown_SkAlphaType, nullptr);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050088 if (!sContext || !sContext->asTextureProxy()) {
89 return false;
90 }
91
Brian Salomon1d435302019-07-01 13:05:28 -040092 bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0});
Robert Phillipsc4039ea2018-03-01 11:36:45 -050093 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
117void GrAtlasManager::dump(GrContext* context) const {
118 static int gDumpCount = 0;
119 for (int i = 0; i < kMaskFormatCount; ++i) {
120 if (fAtlases[i]) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500121 const GrSurfaceProxyView* views = fAtlases[i]->getViews();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500122 for (uint32_t pageIdx = 0; pageIdx < fAtlases[i]->numActivePages(); ++pageIdx) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500123 SkASSERT(views[pageIdx].proxy());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500124 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 Daniel7c165a42020-01-22 12:22:36 -0500130 auto ct = GrMaskFormatToColorType(AtlasIndexToMaskFormat(i));
Greg Daniel3912a4b2020-01-14 09:56:04 -0500131 save_pixels(context, views[pageIdx], ct, filename.c_str());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500132 }
133 }
134 }
135 ++gDumpCount;
136}
137#endif
138
Brian Salomon2638f3d2019-10-22 12:20:37 -0400139void GrAtlasManager::setAtlasDimensionsToMinimum_ForTesting() {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500140 // 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 Derby3c4d5332018-09-07 15:27:57 -0400145
146 // Set all the atlas sizes to 1x1 plot each.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500147 new (&fAtlasConfig) GrDrawOpAtlasConfig{};
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500148}
149
150bool GrAtlasManager::initAtlas(GrMaskFormat format) {
151 int index = MaskFormatToAtlasIndex(format);
Herb Derby3c4d5332018-09-07 15:27:57 -0400152 if (fAtlases[index] == nullptr) {
Greg Daniel7c165a42020-01-22 12:22:36 -0500153 GrColorType grColorType = GrMaskFormatToColorType(format);
Jim Van Verthf6206f92018-12-14 08:22:24 -0500154 SkISize atlasDimensions = fAtlasConfig.atlasDimensions(format);
155 SkISize plotDimensions = fAtlasConfig.plotDimensions(format);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500156
Robert Phillips0a15cc62019-07-30 12:49:10 -0400157 const GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType,
158 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500159
Herb Derby3c4d5332018-09-07 15:27:57 -0400160 fAtlases[index] = GrDrawOpAtlas::Make(
Robert Phillips42dda082019-05-14 13:29:45 -0400161 fProxyProvider, format, grColorType,
162 atlasDimensions.width(), atlasDimensions.height(),
163 plotDimensions.width(), plotDimensions.height(),
Herb Derby1a496c52020-01-22 17:26:56 -0500164 fAllowMultitexturing, fGlyphCache);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500165 if (!fAtlases[index]) {
166 return false;
167 }
168 }
169 return true;
170}