blob: e74b02d4805a6264f5ea9c957ba2f4373a62c19c [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);
Herb Derby4d721712020-01-24 14:31:16 -050033 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fPlotLocator);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050034}
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,
Herb Derby4d721712020-01-24 14:31:16 -050040 GrTextStrike* strike, GrDrawOpAtlas::PlotLocator* plotLocator,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050041 GrDeferredUploadTarget* target, GrMaskFormat format,
42 int width, int height, const void* image, SkIPoint16* loc) {
Herb Derby4d721712020-01-24 14:31:16 -050043 return this->getAtlas(format)->addToAtlas(
44 resourceProvider, plotLocator, target, width, height, image, loc);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050045}
46
47void GrAtlasManager::addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater* updater,
48 GrGlyph* glyph,
49 GrDeferredUploadToken token) {
50 SkASSERT(glyph);
Herb Derby4d721712020-01-24 14:31:16 -050051 if (updater->add(glyph->fPlotLocator)) {
52 this->getAtlas(glyph->fMaskFormat)->setLastUseToken(glyph->fPlotLocator, token);
Jim Van Verthba98b7d2018-12-05 12:33:43 -050053 }
Robert Phillipsc4039ea2018-03-01 11:36:45 -050054}
55
56#ifdef SK_DEBUG
Mike Kleinc0bd9f92019-04-23 12:05:21 -050057#include "src/gpu/GrContextPriv.h"
58#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040059#include "src/gpu/GrSurfaceProxy.h"
60#include "src/gpu/GrTextureProxy.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050061
Mike Kleinc0bd9f92019-04-23 12:05:21 -050062#include "include/core/SkBitmap.h"
63#include "include/core/SkImageEncoder.h"
64#include "include/core/SkStream.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050065#include <stdio.h>
66
67/**
68 * Write the contents of the surface proxy to a PNG. Returns true if successful.
69 * @param filename Full path to desired file
70 */
Greg Daniel3912a4b2020-01-14 09:56:04 -050071static bool save_pixels(GrContext* context, GrSurfaceProxyView view, GrColorType colorType,
Brian Salomond6287472019-06-24 15:50:07 -040072 const char* filename) {
Greg Daniel3912a4b2020-01-14 09:56:04 -050073 if (!view.proxy()) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050074 return false;
75 }
76
Brian Salomon9f2b86c2019-10-22 10:37:46 -040077 SkImageInfo ii =
Greg Daniel3912a4b2020-01-14 09:56:04 -050078 SkImageInfo::Make(view.proxy()->dimensions(), kRGBA_8888_SkColorType,
79 kPremul_SkAlphaType);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050080 SkBitmap bm;
81 if (!bm.tryAllocPixels(ii)) {
82 return false;
83 }
84
Greg Daniel3912a4b2020-01-14 09:56:04 -050085 auto sContext = GrSurfaceContext::Make(context, std::move(view), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -050086 kUnknown_SkAlphaType, nullptr);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050087 if (!sContext || !sContext->asTextureProxy()) {
88 return false;
89 }
90
Brian Salomon1d435302019-07-01 13:05:28 -040091 bool result = sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0});
Robert Phillipsc4039ea2018-03-01 11:36:45 -050092 if (!result) {
93 SkDebugf("------ failed to read pixels for %s\n", filename);
94 return false;
95 }
96
97 // remove any previous version of this file
98 remove(filename);
99
100 SkFILEWStream file(filename);
101 if (!file.isValid()) {
102 SkDebugf("------ failed to create file: %s\n", filename);
103 remove(filename); // remove any partial file
104 return false;
105 }
106
107 if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
108 SkDebugf("------ failed to encode %s\n", filename);
109 remove(filename); // remove any partial file
110 return false;
111 }
112
113 return true;
114}
115
116void GrAtlasManager::dump(GrContext* context) const {
117 static int gDumpCount = 0;
118 for (int i = 0; i < kMaskFormatCount; ++i) {
119 if (fAtlases[i]) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500120 const GrSurfaceProxyView* views = fAtlases[i]->getViews();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500121 for (uint32_t pageIdx = 0; pageIdx < fAtlases[i]->numActivePages(); ++pageIdx) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500122 SkASSERT(views[pageIdx].proxy());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500123 SkString filename;
124#ifdef SK_BUILD_FOR_ANDROID
125 filename.printf("/sdcard/fontcache_%d%d%d.png", gDumpCount, i, pageIdx);
126#else
127 filename.printf("fontcache_%d%d%d.png", gDumpCount, i, pageIdx);
128#endif
Greg Daniel7c165a42020-01-22 12:22:36 -0500129 auto ct = GrMaskFormatToColorType(AtlasIndexToMaskFormat(i));
Greg Daniel3912a4b2020-01-14 09:56:04 -0500130 save_pixels(context, views[pageIdx], ct, filename.c_str());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500131 }
132 }
133 }
134 ++gDumpCount;
135}
136#endif
137
Brian Salomon2638f3d2019-10-22 12:20:37 -0400138void GrAtlasManager::setAtlasDimensionsToMinimum_ForTesting() {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500139 // Delete any old atlases.
140 // This should be safe to do as long as we are not in the middle of a flush.
141 for (int i = 0; i < kMaskFormatCount; i++) {
142 fAtlases[i] = nullptr;
143 }
Herb Derby3c4d5332018-09-07 15:27:57 -0400144
145 // Set all the atlas sizes to 1x1 plot each.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500146 new (&fAtlasConfig) GrDrawOpAtlasConfig{};
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500147}
148
149bool GrAtlasManager::initAtlas(GrMaskFormat format) {
150 int index = MaskFormatToAtlasIndex(format);
Herb Derby3c4d5332018-09-07 15:27:57 -0400151 if (fAtlases[index] == nullptr) {
Greg Daniel7c165a42020-01-22 12:22:36 -0500152 GrColorType grColorType = GrMaskFormatToColorType(format);
Jim Van Verthf6206f92018-12-14 08:22:24 -0500153 SkISize atlasDimensions = fAtlasConfig.atlasDimensions(format);
154 SkISize plotDimensions = fAtlasConfig.plotDimensions(format);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500155
Robert Phillips0a15cc62019-07-30 12:49:10 -0400156 const GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType,
157 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500158
Herb Derby3c4d5332018-09-07 15:27:57 -0400159 fAtlases[index] = GrDrawOpAtlas::Make(
Robert Phillips42dda082019-05-14 13:29:45 -0400160 fProxyProvider, format, grColorType,
161 atlasDimensions.width(), atlasDimensions.height(),
162 plotDimensions.width(), plotDimensions.height(),
Herb Derbya90ed952020-01-28 15:55:58 -0500163 this, fAllowMultitexturing, nullptr);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500164 if (!fAtlases[index]) {
165 return false;
166 }
167 }
168 return true;
169}