18#if defined(GRAPHITE_TEST_UTILS)
24#define ASSERT_SINGLE_OWNER SKGPU_ASSERT_SINGLE_OWNER(fSingleOwner)
34 , fSingleOwner(singleOwner) {
36 fProxyCache = std::make_unique<ProxyCache>(recorderID);
60 fProxyCache->purgeAll();
63 this->processReturnedResources();
65 while (fNonpurgeableResources.
size()) {
66 Resource* back = *(fNonpurgeableResources.
end() - 1);
68 this->removeFromNonpurgeableArray(back);
72 while (fPurgeableQueue.
count()) {
75 this->removeFromPurgeableQueue(top);
100 this->processReturnedResources();
108 this->setResourceTimestamp(
resource, this->getNextTimestamp());
111 this->addToNonpurgeableArray(
resource);
120 fBudgetedBytes +=
resource->gpuMemorySize();
123 this->purgeAsNeeded();
137 if (this->processReturnedResources()) {
150 fBudgetedBytes -=
resource->gpuMemorySize();
152 SkDEBUGCODE(
resource->fNonShareableInCache =
false;)
157 this->refAndMakeResourceMRU(
resource);
171 this->purgeAsNeeded();
180 if (this->inPurgeableQueue(
resource)) {
182 this->removeFromPurgeableQueue(
resource);
183 this->addToNonpurgeableArray(
resource);
187 this->setResourceTimestamp(
resource, this->getNextTimestamp());
204 if (
resource->shouldDeleteASAP() == Resource::DeleteASAP::kNo &&
221 if (*
resource->accessReturnIndex() >= 0) {
226 fReturnQueue[*
resource->accessReturnIndex()].second = removedRef;
231 for (
auto& nextResource : fReturnQueue) {
236 fReturnQueue.push_back(std::make_pair(
resource, removedRef));
237 *
resource->accessReturnIndex() = fReturnQueue.
size() - 1;
242bool ResourceCache::processReturnedResources() {
247 ReturnQueue tempQueue;
253 tempQueue = fReturnQueue;
254 fReturnQueue.clear();
255 for (
auto& nextResource : tempQueue) {
258 *
resource->accessReturnIndex() = -1;
262 if (tempQueue.empty()) {
269 for (
auto& nextResource : tempQueue) {
286 if (*
resource->accessCacheIndex() != -1) {
309 SkDEBUGCODE(
resource->fNonShareableInCache =
true;)
313 fBudgetedBytes +=
resource->gpuMemorySize();
335 this->setResourceTimestamp(
resource, this->getNextTimestamp());
337 this->removeFromNonpurgeableArray(
resource);
339 if (
resource->shouldDeleteASAP() == Resource::DeleteASAP::kYes) {
348void ResourceCache::addToNonpurgeableArray(Resource*
resource) {
349 int index = fNonpurgeableResources.
size();
351 *
resource->accessCacheIndex() = index;
354void ResourceCache::removeFromNonpurgeableArray(Resource*
resource) {
355 int* index =
resource->accessCacheIndex();
358 Resource*
tail = *(fNonpurgeableResources.
end() - 1);
360 fNonpurgeableResources[*index] =
tail;
361 *
tail->accessCacheIndex() = *index;
366void ResourceCache::removeFromPurgeableQueue(Resource*
resource) {
374bool ResourceCache::inPurgeableQueue(Resource*
resource)
const {
376 int index = *
resource->accessCacheIndex();
377 if (index < fPurgeableQueue.
count() && fPurgeableQueue.
at(index) ==
resource) {
383void ResourceCache::purgeResource(Resource*
resource) {
391 if (
resource->shouldDeleteASAP() == Resource::DeleteASAP::kNo) {
393 this->removeFromPurgeableQueue(
resource);
398 fBudgetedBytes -=
resource->gpuMemorySize();
402void ResourceCache::purgeAsNeeded() {
405 if (this->overbudget() && fProxyCache) {
406 fProxyCache->freeUniquelyHeld();
409 this->processReturnedResources();
411 while (this->overbudget() && fPurgeableQueue.
count()) {
416 if (
resource->timestamp() == kMaxTimestamp) {
443 fProxyCache->purgeProxiesNotUsedSince(purgeTime);
445 this->processReturnedResources();
449 if (fPurgeableQueue.
count() &&
451 fPurgeableQueue.
peek()->lastAccessTime() >= *purgeTime) {
456 fPurgeableQueue.
sort();
460 for (
int i = 0;
i < fPurgeableQueue.
count();
i++) {
463 const skgpu::StdSteadyClock::time_point resourceTime =
resource->lastAccessTime();
464 if (purgeTime && resourceTime >= *purgeTime) {
469 if (
resource->gpuMemorySize() > 0) {
476 for (
int i = 0;
i < nonZeroSizedResources.
size();
i++) {
477 this->purgeResource(nonZeroSizedResources[
i]);
483 this->purgeAsNeeded();
486uint32_t ResourceCache::getNextTimestamp() {
490 if (fTimestamp == kMaxTimestamp) {
498 sortedPurgeableResources.
reserve(fPurgeableQueue.
count());
500 while (fPurgeableQueue.
count()) {
501 *sortedPurgeableResources.
append() = fPurgeableQueue.
peek();
502 fPurgeableQueue.
pop();
512 while (currP < sortedPurgeableResources.
size() &&
513 currNP < fNonpurgeableResources.
size()) {
514 uint32_t tsP = sortedPurgeableResources[currP]->timestamp();
515 uint32_t tsNP = fNonpurgeableResources[currNP]->timestamp();
518 this->setResourceTimestamp(sortedPurgeableResources[currP++], fTimestamp++);
521 *fNonpurgeableResources[currNP]->accessCacheIndex() = currNP;
522 this->setResourceTimestamp(fNonpurgeableResources[currNP++], fTimestamp++);
527 while (currP < sortedPurgeableResources.
size()) {
528 this->setResourceTimestamp(sortedPurgeableResources[currP++], fTimestamp++);
530 while (currNP < fNonpurgeableResources.
size()) {
531 *fNonpurgeableResources[currNP]->accessCacheIndex() = currNP;
532 this->setResourceTimestamp(fNonpurgeableResources[currNP++], fTimestamp++);
536 for (
int i = 0;
i < sortedPurgeableResources.
size(); ++
i) {
537 fPurgeableQueue.
insert(sortedPurgeableResources[
i]);
550void ResourceCache::setResourceTimestamp(Resource*
resource, uint32_t timestamp) {
552 if (
resource->gpuMemorySize() == 0) {
553 timestamp = kMaxTimestamp;
559 for (
int i = 0;
i < fNonpurgeableResources.
size(); ++
i) {
560 fNonpurgeableResources[
i]->dumpMemoryStatistics(traceMemoryDump);
562 for (
int i = 0;
i < fPurgeableQueue.
count(); ++
i) {
563 fPurgeableQueue.
at(
i)->dumpMemoryStatistics(traceMemoryDump);
577bool ResourceCache::CompareTimestamp(Resource*
const&
a, Resource*
const&
b) {
578 return a->timestamp() <
b->timestamp();
581int* ResourceCache::AccessResourceIndex(Resource*
const& res) {
582 return res->accessCacheIndex();
586void ResourceCache::validate()
const {
590 if (~mask && (gRandom.
nextU() & mask)) {
597 size_t fBudgetedBytes;
598 const ResourceMap* fResourceMap;
601 memset(
this, 0,
sizeof(*
this));
602 fResourceMap = &
cache->fResourceMap;
621 if (
resource->isUsableAsScratch()) {
637 fBudgetedBytes +=
resource->gpuMemorySize();
640 if (
resource->gpuMemorySize() == 0) {
650 fResourceMap.foreach([&](
const Resource&
resource) {
670 for (
int i = 0;
i < fNonpurgeableResources.
size(); ++
i) {
671 SkASSERT(*fNonpurgeableResources[
i]->accessCacheIndex() ==
i);
672 SkASSERT(!fNonpurgeableResources[
i]->wasDestroyed());
673 SkASSERT(!this->inPurgeableQueue(fNonpurgeableResources[
i]));
674 stats.update(fNonpurgeableResources[
i]);
676 bool firstPurgeableIsSizeZero =
false;
677 for (
int i = 0;
i < fPurgeableQueue.
count(); ++
i) {
679 firstPurgeableIsSizeZero = (fPurgeableQueue.
at(0)->gpuMemorySize() == 0);
681 if (firstPurgeableIsSizeZero) {
685 SkASSERT(fPurgeableQueue.
at(
i)->gpuMemorySize() == 0);
688 SkASSERT(*fPurgeableQueue.
at(
i)->accessCacheIndex() ==
i);
690 stats.update(fPurgeableQueue.
at(
i));
697bool ResourceCache::isInCache(
const Resource*
resource)
const {
698 int index = *
resource->accessCacheIndex();
702 if (index < fPurgeableQueue.
count() && fPurgeableQueue.
at(index) ==
resource) {
705 if (index < fNonpurgeableResources.
size() && fNonpurgeableResources[index] ==
resource) {
708 SkDEBUGFAIL(
"Resource index should be -1 or the resource should be in the cache.");
714#if defined(GRAPHITE_TEST_UTILS)
716int ResourceCache::numFindableResources()
const {
717 return fResourceMap.count();
720void ResourceCache::setMaxBudget(
size_t bytes) {
722 this->processReturnedResources();
723 this->purgeAsNeeded();
726Resource* ResourceCache::topOfPurgeableQueue() {
727 if (!fPurgeableQueue.
count()) {
730 return fPurgeableQueue.
peek();
733void ResourceCache::visitTextures(
734 const std::function<
void(
const Texture*,
bool purgeable)>& func)
const {
735 for (
int i = 0;
i < fNonpurgeableResources.
size(); ++
i) {
736 if (
const Texture* tex = fNonpurgeableResources[
i]->asTexture()) {
740 for (
int i = 0;
i < fPurgeableQueue.
count(); ++
i) {
741 if (
const Texture* tex = fPurgeableQueue.
at(
i)->asTexture()) {
#define ASSERT_SINGLE_OWNER
#define SkDEBUGFAIL(message)
static int SkNextPow2(int value)
static SkString resource(SkPDFResourceType type, int index)
sk_sp< T > sk_ref_sp(T *obj)
void SkTQSort(T *begin, T *end, const C &lessThan)
constexpr uint32_t SkToU32(S x)
#define TRACE_EVENT_SCOPE_THREAD
static constexpr uint32_t SK_InvalidGenID
int find(const char substring[]) const
bool returnResource(Resource *, LastRemovedRef)
~ResourceCache() override
ResourceCache(const ResourceCache &)=delete
void purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime)
static sk_sp< ResourceCache > Make(SingleOwner *, uint32_t recorderID, size_t maxBytes)
void dumpMemoryStatistics(SkTraceMemoryDump *traceMemoryDump) const
int getResourceCount() const
void insertResource(Resource *)
Resource * findAndRefResource(const GraphiteResourceKey &key, skgpu::Budgeted)
bool wasDestroyed() const
const GraphiteResourceKey & key() const
Dart_NativeFunction function
static uint32_t Hash(uint32_t key)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
#define TRACE_EVENT0(category_group, name)
#define TRACE_EVENT_INSTANT0(category_group, name)
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
#define TRACE_EVENT_INSTANT1(category_group, name, arg1_name, arg1_val)