blob: 6ae06a4341c70fab3f0c02bcba900c015996a950 [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"
11#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050012
Herb Derby081e6f32019-01-16 13:46:02 -050013GrAtlasManager::GrAtlasManager(GrProxyProvider* proxyProvider, GrStrikeCache* glyphCache,
Herb Derby3c4d5332018-09-07 15:27:57 -040014 size_t maxTextureBytes,
Cary Clarkaa5f38f2018-09-10 20:28:05 +000015 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing)
Herb Derby3c4d5332018-09-07 15:27:57 -040016 : fAllowMultitexturing{allowMultitexturing}
Herb Derby3c4d5332018-09-07 15:27:57 -040017 , fProxyProvider{proxyProvider}
18 , fCaps{fProxyProvider->refCaps()}
19 , fGlyphCache{glyphCache}
Jim Van Verthf6206f92018-12-14 08:22:24 -050020 , fAtlasConfig{fCaps->maxTextureSize(), maxTextureBytes} { }
Cary Clarkaa5f38f2018-09-10 20:28:05 +000021
Herb Derby3c4d5332018-09-07 15:27:57 -040022GrAtlasManager::~GrAtlasManager() = default;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050023
Robert Phillips42dda082019-05-14 13:29:45 -040024static GrColorType mask_format_to_gr_color_type(GrMaskFormat format) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050025 switch (format) {
26 case kA8_GrMaskFormat:
Robert Phillips42dda082019-05-14 13:29:45 -040027 return GrColorType::kAlpha_8;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050028 case kA565_GrMaskFormat:
Greg Daniel48fec762019-06-18 17:06:43 -040029 return GrColorType::kBGR_565;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050030 case kARGB_GrMaskFormat:
Robert Phillips42dda082019-05-14 13:29:45 -040031 return GrColorType::kRGBA_8888;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050032 default:
33 SkDEBUGFAIL("unsupported GrMaskFormat");
Robert Phillips42dda082019-05-14 13:29:45 -040034 return GrColorType::kAlpha_8;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050035 }
36}
37
Robert Phillipsc4039ea2018-03-01 11:36:45 -050038void GrAtlasManager::freeAll() {
39 for (int i = 0; i < kMaskFormatCount; ++i) {
40 fAtlases[i] = nullptr;
41 }
42}
43
44bool GrAtlasManager::hasGlyph(GrGlyph* glyph) {
45 SkASSERT(glyph);
46 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
47}
48
49// add to texture atlas that matches this format
Robert Phillipsd2e9f762018-03-07 11:54:37 -050050GrDrawOpAtlas::ErrorCode GrAtlasManager::addToAtlas(
51 GrResourceProvider* resourceProvider,
Herb Derby081e6f32019-01-16 13:46:02 -050052 GrStrikeCache* glyphCache,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050053 GrTextStrike* strike, GrDrawOpAtlas::AtlasID* id,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050054 GrDeferredUploadTarget* target, GrMaskFormat format,
55 int width, int height, const void* image, SkIPoint16* loc) {
56 glyphCache->setStrikeToPreserve(strike);
57 return this->getAtlas(format)->addToAtlas(resourceProvider, id, target, width, height,
Robert Phillipsd2e9f762018-03-07 11:54:37 -050058 image, loc);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050059}
60
61void GrAtlasManager::addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater* updater,
62 GrGlyph* glyph,
63 GrDeferredUploadToken token) {
64 SkASSERT(glyph);
Jim Van Verthba98b7d2018-12-05 12:33:43 -050065 if (updater->add(glyph->fID)) {
66 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fID, token);
67 }
Robert Phillipsc4039ea2018-03-01 11:36:45 -050068}
69
70#ifdef SK_DEBUG
Mike Kleinc0bd9f92019-04-23 12:05:21 -050071#include "src/gpu/GrContextPriv.h"
72#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040073#include "src/gpu/GrSurfaceProxy.h"
74#include "src/gpu/GrTextureProxy.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050075
Mike Kleinc0bd9f92019-04-23 12:05:21 -050076#include "include/core/SkBitmap.h"
77#include "include/core/SkImageEncoder.h"
78#include "include/core/SkStream.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050079#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 */
Brian Salomond6287472019-06-24 15:50:07 -040085static bool save_pixels(GrContext* context, GrSurfaceProxy* sProxy, GrColorType colorType,
86 const char* filename) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050087 if (!sProxy) {
88 return false;
89 }
90
91 SkImageInfo ii = SkImageInfo::Make(sProxy->width(), sProxy->height(),
92 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
93 SkBitmap bm;
94 if (!bm.tryAllocPixels(ii)) {
95 return false;
96 }
97
Brian Salomond6287472019-06-24 15:50:07 -040098 sk_sp<GrSurfaceContext> sContext(context->priv().makeWrappedSurfaceContext(
99 sk_ref_sp(sProxy), colorType, kUnknown_SkAlphaType));
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500100 if (!sContext || !sContext->asTextureProxy()) {
101 return false;
102 }
103
Brian Salomon1d435302019-07-01 13:05:28 -0400104 bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0});
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500105 if (!result) {
106 SkDebugf("------ failed to read pixels for %s\n", filename);
107 return false;
108 }
109
110 // remove any previous version of this file
111 remove(filename);
112
113 SkFILEWStream file(filename);
114 if (!file.isValid()) {
115 SkDebugf("------ failed to create file: %s\n", filename);
116 remove(filename); // remove any partial file
117 return false;
118 }
119
120 if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
121 SkDebugf("------ failed to encode %s\n", filename);
122 remove(filename); // remove any partial file
123 return false;
124 }
125
126 return true;
127}
128
129void GrAtlasManager::dump(GrContext* context) const {
130 static int gDumpCount = 0;
131 for (int i = 0; i < kMaskFormatCount; ++i) {
132 if (fAtlases[i]) {
133 const sk_sp<GrTextureProxy>* proxies = fAtlases[i]->getProxies();
134 for (uint32_t pageIdx = 0; pageIdx < fAtlases[i]->numActivePages(); ++pageIdx) {
135 SkASSERT(proxies[pageIdx]);
136 SkString filename;
137#ifdef SK_BUILD_FOR_ANDROID
138 filename.printf("/sdcard/fontcache_%d%d%d.png", gDumpCount, i, pageIdx);
139#else
140 filename.printf("fontcache_%d%d%d.png", gDumpCount, i, pageIdx);
141#endif
Brian Salomond6287472019-06-24 15:50:07 -0400142 auto ct = mask_format_to_gr_color_type(AtlasIndexToMaskFormat(i));
143 save_pixels(context, proxies[pageIdx].get(), ct, filename.c_str());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500144 }
145 }
146 }
147 ++gDumpCount;
148}
149#endif
150
Herb Derby3c4d5332018-09-07 15:27:57 -0400151void GrAtlasManager::setAtlasSizesToMinimum_ForTesting() {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500152 // Delete any old atlases.
153 // This should be safe to do as long as we are not in the middle of a flush.
154 for (int i = 0; i < kMaskFormatCount; i++) {
155 fAtlases[i] = nullptr;
156 }
Herb Derby3c4d5332018-09-07 15:27:57 -0400157
158 // Set all the atlas sizes to 1x1 plot each.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500159 new (&fAtlasConfig) GrDrawOpAtlasConfig{};
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500160}
161
162bool GrAtlasManager::initAtlas(GrMaskFormat format) {
163 int index = MaskFormatToAtlasIndex(format);
Herb Derby3c4d5332018-09-07 15:27:57 -0400164 if (fAtlases[index] == nullptr) {
Robert Phillips42dda082019-05-14 13:29:45 -0400165 GrColorType grColorType = mask_format_to_gr_color_type(format);
Jim Van Verthf6206f92018-12-14 08:22:24 -0500166 SkISize atlasDimensions = fAtlasConfig.atlasDimensions(format);
167 SkISize plotDimensions = fAtlasConfig.plotDimensions(format);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500168
Robert Phillips0a15cc62019-07-30 12:49:10 -0400169 const GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType,
170 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500171
Herb Derby3c4d5332018-09-07 15:27:57 -0400172 fAtlases[index] = GrDrawOpAtlas::Make(
Robert Phillips42dda082019-05-14 13:29:45 -0400173 fProxyProvider, format, grColorType,
174 atlasDimensions.width(), atlasDimensions.height(),
175 plotDimensions.width(), plotDimensions.height(),
176 fAllowMultitexturing, &GrStrikeCache::HandleEviction, fGlyphCache);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500177 if (!fAtlases[index]) {
178 return false;
179 }
180 }
181 return true;
182}