Debian Codegraph @

src/gpu/ganesh/GrDrawOpAtlas.cpp

raw
1 /*
2 * Copyright 2015 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 "src/gpu/ganesh/GrDrawOpAtlas.h"
9
10 #include "include/core/SkRect.h"
11 #include "include/gpu/GpuTypes.h"
12 #include "include/gpu/ganesh/GrTypes.h"
13 #include "include/private/base/SkMath.h"
14 #include "include/private/base/SkPoint_impl.h"
15 #include "include/private/base/SkTArray.h"
16 #include "include/private/base/SkTPin.h"
17 #include "include/private/gpu/ganesh/GrTypesPriv.h"
18 #include "src/base/SkMathPriv.h"
19 #include "src/core/SkTraceEvent.h"
20 #include "src/gpu/SkBackingFit.h"
21 #include "src/gpu/Swizzle.h"
22 #include "src/gpu/ganesh/GrBackendUtils.h"
23 #include "src/gpu/ganesh/GrCaps.h"
24 #include "src/gpu/ganesh/GrProxyProvider.h"
25 #include "src/gpu/ganesh/GrSurfaceProxy.h"
26 #include "src/gpu/ganesh/GrTextureProxy.h"
27
28 #include <algorithm>
29 #include <functional>
30 #include <memory>
31 #include <tuple>
32 #include <utility>
33
34 using namespace skia_private;
35
36 using AtlasLocator = skgpu::AtlasLocator;
37 using EvictionCallback = skgpu::PlotEvictionCallback;
38 using GenerationCounter = skgpu::AtlasGenerationCounter;
39 using MaskFormat = skgpu::MaskFormat;
40 using Plot = skgpu::Plot;
41 using PlotList = skgpu::PlotList;
42 using PlotLocator = skgpu::PlotLocator;
43
44 #if defined(DUMP_ATLAS_DATA)
45 static const constexpr bool kDumpAtlasData = true;
46 #else
47 static const constexpr bool kDumpAtlasData = false;
48 #endif
49
50 #ifdef SK_DEBUG
51 void GrDrawOpAtlas::validate(const AtlasLocator& atlasLocator) const {
52 // Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle
53 int numPlotsX = fTextureWidth / fPlotWidth;
54 int numPlotsY = fTextureHeight / fPlotHeight;
55
56 int plotIndex = atlasLocator.plotIndex();
57 auto topLeft = atlasLocator.topLeft();
58 int plotX = topLeft.x() / fPlotWidth;
59 int plotY = topLeft.y() / fPlotHeight;
60 SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1));
61 }
62 #endif
63
64 // When proxy allocation is deferred until flush time the proxies acting as atlases require
65 // special handling. This is because the usage that can be determined from the ops themselves
66 // isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
67 // atlases. Extending the usage interval of any op that uses an atlas to the start of the
68 // flush (as is done for proxies that are used for sw-generated masks) also won't work because
69 // the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
70 // must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
71 // (which calls this method).
72 void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
73 for (uint32_t i = 0; i < fNumActivePages; ++i) {
74 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
75 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
76 }
77 }
78
79 std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
80 const GrBackendFormat& format,
81 SkColorType colorType, size_t bpp, int width,
82 int height, int plotWidth, int plotHeight,
83 GenerationCounter* generationCounter,
84 AllowMultitexturing allowMultitexturing,
85 EvictionCallback* evictor,
86 std::string_view label) {
87 if (!format.isValid()) {
88 return nullptr;
89 }
90
91 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType, bpp,
92 width, height, plotWidth, plotHeight,
93 generationCounter,
94 allowMultitexturing, label));
95 if (!atlas->createPages(proxyProvider, generationCounter) || !atlas->getViews()[0].proxy()) {
96 return nullptr;
97 }
98
99 if (evictor != nullptr) {
100 atlas->fEvictionCallbacks.emplace_back(evictor);
101 }
102 return atlas;
103 }
104
105 ///////////////////////////////////////////////////////////////////////////////
106
107 GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
108 SkColorType colorType, size_t bpp, int width, int height,
109 int plotWidth, int plotHeight, GenerationCounter* generationCounter,
110 AllowMultitexturing allowMultitexturing, std::string_view label)
111 : fFormat(format)
112 , fColorType(colorType)
113 , fBytesPerPixel(bpp)
114 , fTextureWidth(width)
115 , fTextureHeight(height)
116 , fPlotWidth(plotWidth)
117 , fPlotHeight(plotHeight)
118 , fLabel(label)
119 , fGenerationCounter(generationCounter)
120 , fAtlasGeneration(fGenerationCounter->next())
121 , fPrevFlushToken(skgpu::Token::InvalidToken())
122 , fFlushesSinceLastUse(0)
123 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ?
124 PlotLocator::kMaxMultitexturePages : 1)
125 , fNumActivePages(0) {
126 int numPlotsX = width/plotWidth;
127 int numPlotsY = height/plotHeight;
128 SkASSERT(numPlotsX * numPlotsY <= PlotLocator::kMaxPlots);
129 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
130 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
131
132 fNumPlots = numPlotsX * numPlotsY;
133 }
134
135 inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
136 for (EvictionCallback* evictor : fEvictionCallbacks) {
137 evictor->evict(plotLocator);
138 }
139
140 fAtlasGeneration = fGenerationCounter->next();
141 }
142
143 void GrDrawOpAtlas::uploadPlotToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
144 GrTextureProxy* proxy,
145 Plot* plot) {
146 SkASSERT(proxy && proxy->peekTexture());
147 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
148
149 const void* dataPtr;
150 SkIRect rect;
151 std::tie(dataPtr, rect) = plot->prepareForUpload();
152
153 writePixels(proxy,
154 rect,
155 SkColorTypeToGrColorType(fColorType),
156 dataPtr,
157 fBytesPerPixel*fPlotWidth);
158 }
159
160 inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
161 AtlasLocator* atlasLocator, Plot* plot) {
162 uint32_t pageIdx = plot->pageIndex();
163 if (pageIdx >= fNumActivePages) {
164 return false;
165 }
166 this->makeMRU(plot, pageIdx);
167
168 // If our most recent upload has already occurred then we have to insert a new
169 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
170 // This new update will piggy back on that previously scheduled update.
171 if (plot->lastUploadToken() < target->tokenTracker()->nextFlushToken()) {
172 // With c+14 we could move sk_sp into lamba to only ref once.
173 sk_sp<Plot> plotsp(SkRef(plot));
174
175 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
176 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
177
178 skgpu::Token lastUploadToken = target->addASAPUpload(
179 [this, plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
180 this->uploadPlotToTexture(writePixels, proxy, plotsp.get());
181 });
182 plot->setLastUploadToken(lastUploadToken);
183 }
184 atlasLocator->updatePlotLocator(plot->plotLocator());
185 SkDEBUGCODE(this->validate(*atlasLocator);)
186 return true;
187 }
188
189 bool GrDrawOpAtlas::uploadToPage(unsigned int pageIdx, GrDeferredUploadTarget* target, int width,
190 int height, const void* image, AtlasLocator* atlasLocator) {
191 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
192
193 // look through all allocated plots for one we can share, in Most Recently Refed order
194 PlotList::Iter plotIter;
195 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
196
197 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
198 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
199 plot->bpp());
200
201 if (plot->addSubImage(width, height, image, atlasLocator)) {
202 return this->updatePlot(target, atlasLocator, plot);
203 }
204 }
205
206 return false;
207 }
208
209 // Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
210 //
211 // This value is somewhat arbitrary -- the idea is to keep it low enough that
212 // a page with unused plots will get removed reasonably quickly, but allow it
213 // to hang around for a bit in case it's needed. The assumption is that flushes
214 // are rare; i.e., we are not continually refreshing the frame.
215 static constexpr auto kPlotRecentlyUsedCount = 32;
216 static constexpr auto kAtlasRecentlyUsedCount = 128;
217
218 GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
219 GrDeferredUploadTarget* target,
220 int width, int height, const void* image,
221 AtlasLocator* atlasLocator) {
222 if (width > fPlotWidth || height > fPlotHeight) {
223 return ErrorCode::kError;
224 }
225
226 // Look through each page to see if we can upload without having to flush
227 // We prioritize this upload to the first pages, not the most recently used, to make it easier
228 // to remove unused pages in reverse page order.
229 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
230 if (this->uploadToPage(pageIdx, target, width, height, image, atlasLocator)) {
231 return ErrorCode::kSucceeded;
232 }
233 }
234
235 // If the above fails, then see if the least recently used plot per page has already been
236 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
237 // We wait until we've grown to the full number of pages to begin evicting already flushed
238 // plots so that we can maximize the opportunity for reuse.
239 // As before we prioritize this upload to the first pages, not the most recently used.
240 if (fNumActivePages == this->maxPages()) {
241 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
242 Plot* plot = fPages[pageIdx].fPlotList.tail();
243 SkASSERT(plot);
244 if (plot->lastUseToken() < target->tokenTracker()->nextFlushToken()) {
245 this->processEvictionAndResetRects(plot);
246 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
247 plot->bpp());
248 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, atlasLocator);
249 SkASSERT(verify);
250 if (!this->updatePlot(target, atlasLocator, plot)) {
251 return ErrorCode::kError;
252 }
253 return ErrorCode::kSucceeded;
254 }
255 }
256 } else {
257 // If we haven't activated all the available pages, try to create a new one and add to it
258 if (!this->activateNewPage(resourceProvider)) {
259 return ErrorCode::kError;
260 }
261
262 if (this->uploadToPage(fNumActivePages-1, target, width, height, image, atlasLocator)) {
263 return ErrorCode::kSucceeded;
264 } else {
265 // If we fail to upload to a newly activated page then something has gone terribly
266 // wrong - return an error
267 return ErrorCode::kError;
268 }
269 }
270
271 if (!fNumActivePages) {
272 return ErrorCode::kError;
273 }
274
275 // Try to find a plot that we can perform an inline upload to.
276 // We prioritize this upload in reverse order of pages to counterbalance the order above.
277 Plot* plot = nullptr;
278 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
279 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
280 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
281 plot = currentPlot;
282 break;
283 }
284 }
285
286 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
287 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
288 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
289 // continue past this branch and prepare an inline upload that will occur after the enqueued
290 // draw which references the plot's pre-upload content.
291 if (!plot) {
292 return ErrorCode::kTryAgain;
293 }
294
295 this->processEviction(plot->plotLocator());
296 int pageIdx = plot->pageIndex();
297 fPages[pageIdx].fPlotList.remove(plot);
298 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
299 newPlot = plot->clone();
300
301 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
302 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
303 newPlot->bpp());
304 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, atlasLocator);
305 SkASSERT(verify);
306
307 // Note that this plot will be uploaded inline with the draws whereas the
308 // one it displaced most likely was uploaded ASAP.
309 // With c++14 we could move sk_sp into lambda to only ref once.
310 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
311
312 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
313 SkASSERT(proxy && proxy->isInstantiated());
314
315 skgpu::Token lastUploadToken = target->addInlineUpload(
316 [this, plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
317 this->uploadPlotToTexture(writePixels, proxy, plotsp.get());
318 });
319 newPlot->setLastUploadToken(lastUploadToken);
320
321 atlasLocator->updatePlotLocator(newPlot->plotLocator());
322 SkDEBUGCODE(this->validate(*atlasLocator);)
323
324 return ErrorCode::kSucceeded;
325 }
326
327 void GrDrawOpAtlas::compact(skgpu::Token startTokenForNextFlush) {
328 if (fNumActivePages < 1) {
329 fPrevFlushToken = startTokenForNextFlush;
330 return;
331 }
332
333 // For all plots, reset number of flushes since used if used this frame.
334 PlotList::Iter plotIter;
335 bool atlasUsedThisFlush = false;
336 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
337 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
338 while (Plot* plot = plotIter.get()) {
339 // Reset number of flushes since used
340 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
341 plot->resetFlushesSinceLastUsed();
342 atlasUsedThisFlush = true;
343 }
344
345 plotIter.next();
346 }
347 }
348
349 if (atlasUsedThisFlush) {
350 fFlushesSinceLastUse = 0;
351 } else {
352 ++fFlushesSinceLastUse;
353 }
354
355 // We only try to compact if the atlas was used in the recently completed flush or
356 // hasn't been used in a long time.
357 // This is to handle the case where a lot of text or path rendering has occurred but then just
358 // a blinking cursor is drawn.
359 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
360 TArray<Plot*> availablePlots;
361 uint32_t lastPageIndex = fNumActivePages - 1;
362
363 // For all plots but the last one, update number of flushes since used, and check to see
364 // if there are any in the first pages that the last page can safely upload to.
365 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
366 if constexpr (kDumpAtlasData) {
367 SkDebugf("page %u: ", pageIndex);
368 }
369
370 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
371 while (Plot* plot = plotIter.get()) {
372 // Update number of flushes since plot was last used
373 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
374 // to avoid deleting everything when we return to text drawing in the blinking
375 // cursor case
376 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
377 plot->incFlushesSinceLastUsed();
378 }
379
380 if constexpr (kDumpAtlasData) {
381 SkDebugf("%d ", plot->flushesSinceLastUsed());
382 }
383
384 // Count plots we can potentially upload to in all pages except the last one
385 // (the potential compactee).
386 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
387 availablePlots.push_back() = plot;
388 }
389
390 plotIter.next();
391 }
392
393 if constexpr (kDumpAtlasData) {
394 SkDebugf("\n");
395 }
396 }
397
398 // Count recently used plots in the last page and evict any that are no longer in use.
399 // Since we prioritize uploading to the first pages, this will eventually
400 // clear out usage of this page unless we have a large need.
401 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
402 unsigned int usedPlots = 0;
403 if constexpr (kDumpAtlasData) {
404 SkDebugf("page %u: ", lastPageIndex);
405 }
406
407 while (Plot* plot = plotIter.get()) {
408 // Update number of flushes since plot was last used
409 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
410 plot->incFlushesSinceLastUsed();
411 }
412
413 if constexpr (kDumpAtlasData) {
414 SkDebugf("%d ", plot->flushesSinceLastUsed());
415 }
416
417 // If this plot was used recently
418 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
419 usedPlots++;
420 } else if (plot->lastUseToken() != skgpu::Token::InvalidToken()) {
421 // otherwise if aged out just evict it.
422 this->processEvictionAndResetRects(plot);
423 }
424 plotIter.next();
425 }
426
427 if constexpr (kDumpAtlasData) {
428 SkDebugf("\n");
429 }
430
431 // If recently used plots in the last page are using less than a quarter of the page, try
432 // to evict them if there's available space in earlier pages. Since we prioritize uploading
433 // to the first pages, this will eventually clear out usage of this page unless we have a
434 // large need.
435 if (!availablePlots.empty() && usedPlots && usedPlots <= fNumPlots / 4) {
436 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
437 while (Plot* plot = plotIter.get()) {
438 // If this plot was used recently
439 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
440 // See if there's room in an earlier page and if so evict.
441 // We need to be somewhat harsh here so that a handful of plots that are
442 // consistently in use don't end up locking the page in memory.
443 if (!availablePlots.empty()) {
444 this->processEvictionAndResetRects(plot);
445 this->processEvictionAndResetRects(availablePlots.back());
446 availablePlots.pop_back();
447 --usedPlots;
448 }
449 if (!usedPlots || availablePlots.empty()) {
450 break;
451 }
452 }
453 plotIter.next();
454 }
455 }
456
457 // If none of the plots in the last page have been used recently, delete it.
458 if (!usedPlots) {
459 if constexpr (kDumpAtlasData) {
460 SkDebugf("delete %u\n", fNumActivePages - 1);
461 }
462
463 this->deactivateLastPage();
464 fFlushesSinceLastUse = 0;
465 }
466 }
467
468 fPrevFlushToken = startTokenForNextFlush;
469 }
470
471 bool GrDrawOpAtlas::createPages(
472 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
473 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
474
475 SkISize dims = {fTextureWidth, fTextureHeight};
476
477 int numPlotsX = fTextureWidth/fPlotWidth;
478 int numPlotsY = fTextureHeight/fPlotHeight;
479
480 GrColorType grColorType = SkColorTypeToGrColorType(fColorType);
481
482 for (uint32_t i = 0; i < this->maxPages(); ++i) {
483 skgpu::Swizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, grColorType);
484 if (GrColorTypeIsAlphaOnly(grColorType)) {
485 swizzle = skgpu::Swizzle::Concat(swizzle, skgpu::Swizzle("aaaa"));
486 }
487 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(fFormat,
488 dims,
489 GrRenderable::kNo,
490 1,
491 skgpu::Mipmapped::kNo,
492 SkBackingFit::kExact,
493 skgpu::Budgeted::kYes,
494 GrProtected::kNo,
495 fLabel,
496 GrInternalSurfaceFlags::kNone,
497 GrSurfaceProxy::UseAllocator::kNo);
498 if (!proxy) {
499 return false;
500 }
501 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
502
503 // set up allocated plots
504 fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
505
506 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
507 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
508 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
509 uint32_t plotIndex = r * numPlotsX + c;
510 currPlot->reset(new Plot(
511 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType,
512 fBytesPerPixel));
513
514 // build LRU list
515 fPages[i].fPlotList.addToHead(currPlot->get());
516 ++currPlot;
517 }
518 }
519
520 }
521
522 return true;
523 }
524
525 bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
526 SkASSERT(fNumActivePages < this->maxPages());
527
528 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
529 return false;
530 }
531
532 if constexpr (kDumpAtlasData) {
533 SkDebugf("activated page#: %u\n", fNumActivePages);
534 }
535
536 ++fNumActivePages;
537 return true;
538 }
539
540 inline void GrDrawOpAtlas::deactivateLastPage() {
541 SkASSERT(fNumActivePages);
542
543 uint32_t lastPageIndex = fNumActivePages - 1;
544
545 int numPlotsX = fTextureWidth/fPlotWidth;
546 int numPlotsY = fTextureHeight/fPlotHeight;
547
548 fPages[lastPageIndex].fPlotList.reset();
549 for (int r = 0; r < numPlotsY; ++r) {
550 for (int c = 0; c < numPlotsX; ++c) {
551 uint32_t plotIndex = r * numPlotsX + c;
552
553 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
554 currPlot->resetRects(/*freeData=*/false);
555 currPlot->resetFlushesSinceLastUsed();
556
557 // rebuild the LRU list
558 SkDEBUGCODE(currPlot->resetListPtrs());
559 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
560 }
561 }
562
563 // remove ref to the backing texture
564 fViews[lastPageIndex].proxy()->deinstantiate();
565 --fNumActivePages;
566 }
567
568 GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
569 static const SkISize kARGBDimensions[] = {
570 {256, 256}, // maxBytes < 2^19
571 {512, 256}, // 2^19 <= maxBytes < 2^20
572 {512, 512}, // 2^20 <= maxBytes < 2^21
573 {1024, 512}, // 2^21 <= maxBytes < 2^22
574 {1024, 1024}, // 2^22 <= maxBytes < 2^23
575 {2048, 1024}, // 2^23 <= maxBytes
576 };
577
578 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
579 maxBytes >>= 18;
580 // Take the floor of the log to get the index
581 int index = maxBytes > 0
582 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, std::size(kARGBDimensions) - 1)
583 : 0;
584
585 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
586 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
587 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
588 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
589 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
590 }
591
592 SkISize GrDrawOpAtlasConfig::atlasDimensions(MaskFormat type) const {
593 if (MaskFormat::kA8 == type) {
594 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
595 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
596 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
597 } else {
598 return fARGBDimensions;
599 }
600 }
601
602 SkISize GrDrawOpAtlasConfig::plotDimensions(MaskFormat type) const {
603 if (MaskFormat::kA8 == type) {
604 SkISize atlasDimensions = this->atlasDimensions(type);
605 // For A8 we want to grow the plots at larger texture sizes to accept more of the
606 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
607 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
608
609 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
610 // and 256x256 plots otherwise.
611 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
612 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
613
614 return { plotWidth, plotHeight };
615 } else {
616 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
617 return { 256, 256 };
618 }
619 }