Flutter Engine
The Flutter Engine
GrGLGpuProgramCache.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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 <memory>
9
11
19
22 : fProgram(std::move(program)) {}
23
24 Entry(const GrGLPrecompiledProgram& precompiledProgram)
25 : fPrecompiledProgram(precompiledProgram) {}
26
29};
30
31GrGLGpu::ProgramCache::ProgramCache(int runtimeProgramCacheSize)
32 : fMap(runtimeProgramCacheSize) {
33}
34
35GrGLGpu::ProgramCache::~ProgramCache() {}
36
37void GrGLGpu::ProgramCache::abandon() {
38 fMap.foreach([](GrProgramDesc*, std::unique_ptr<Entry>* e) {
39 if ((*e)->fProgram) {
40 (*e)->fProgram->abandon();
41 }
42 });
43
44 this->reset();
45}
46
48 fMap.reset();
49}
50
51sk_sp<GrGLProgram> GrGLGpu::ProgramCache::findOrCreateProgram(GrDirectContext* dContext,
52 const GrProgramInfo& programInfo) {
53 const GrCaps* caps = dContext->priv().caps();
54
55 GrProgramDesc desc = caps->makeDesc(/*renderTarget*/nullptr, programInfo);
56 if (!desc.isValid()) {
57 GrCapsDebugf(caps, "Failed to gl program descriptor!\n");
58 return nullptr;
59 }
60
62 sk_sp<GrGLProgram> tmp = this->findOrCreateProgramImpl(dContext, desc, programInfo, &stat);
63 if (!tmp) {
65 } else {
67 }
68
69 return tmp;
70}
71
72sk_sp<GrGLProgram> GrGLGpu::ProgramCache::findOrCreateProgram(GrDirectContext* dContext,
73 const GrProgramDesc& desc,
74 const GrProgramInfo& programInfo,
76 sk_sp<GrGLProgram> tmp = this->findOrCreateProgramImpl(dContext, desc, programInfo, stat);
77 if (!tmp) {
79 } else {
81 }
82
83 return tmp;
84}
85
86sk_sp<GrGLProgram> GrGLGpu::ProgramCache::findOrCreateProgramImpl(GrDirectContext* dContext,
87 const GrProgramDesc& desc,
88 const GrProgramInfo& programInfo,
90 *stat = Stats::ProgramCacheResult::kHit;
91 std::unique_ptr<Entry>* entry = fMap.find(desc);
92 if (entry && !(*entry)->fProgram) {
93 // We've pre-compiled the GL program, but don't have the GrGLProgram scaffolding
94 const GrGLPrecompiledProgram* precompiledProgram = &((*entry)->fPrecompiledProgram);
95 SkASSERT(precompiledProgram->fProgramID != 0);
96 (*entry)->fProgram = GrGLProgramBuilder::CreateProgram(dContext, desc, programInfo,
97 precompiledProgram);
98 if (!(*entry)->fProgram) {
99 // Should we purge the program ID from the cache at this point?
100 SkDEBUGFAIL("Couldn't create program from precompiled program");
102 return nullptr;
103 }
105 *stat = Stats::ProgramCacheResult::kPartial;
106 } else if (!entry) {
107 // We have a cache miss
108 sk_sp<GrGLProgram> program = GrGLProgramBuilder::CreateProgram(dContext, desc, programInfo);
109 if (!program) {
111 return nullptr;
112 }
114 entry = fMap.insert(desc, std::make_unique<Entry>(std::move(program)));
115 *stat = Stats::ProgramCacheResult::kMiss;
116 }
117
118 return (*entry)->fProgram;
119}
120
121bool GrGLGpu::ProgramCache::precompileShader(GrDirectContext* dContext,
122 const SkData& key,
123 const SkData& data) {
125 if (!GrProgramDesc::BuildFromData(&desc, key.data(), key.size())) {
126 return false;
127 }
128
129 std::unique_ptr<Entry>* entry = fMap.find(desc);
130 if (entry) {
131 // We've already seen/compiled this shader
132 return true;
133 }
134
135 GrGLPrecompiledProgram precompiledProgram;
136 if (!GrGLProgramBuilder::PrecompileProgram(dContext, &precompiledProgram, data)) {
137 return false;
138 }
139
140 fMap.insert(desc, std::make_unique<Entry>(precompiledProgram));
141 return true;
142}
m reset()
#define GrCapsDebugf(caps,...)
Definition: GrTypesPriv.h:490
#define SkDEBUGFAIL(message)
Definition: SkAssert.h:118
#define SkASSERT(cond)
Definition: SkAssert.h:116
const GrCaps * caps() const
Definition: GrCaps.h:57
virtual GrProgramDesc makeDesc(GrRenderTarget *, const GrProgramInfo &, ProgramDescOverrideFlags overrideFlags=ProgramDescOverrideFlags::kNone) const =0
GrDirectContextPriv priv()
static bool PrecompileProgram(GrDirectContext *, GrGLPrecompiledProgram *, const SkData &)
static sk_sp< GrGLProgram > CreateProgram(GrDirectContext *, const GrProgramDesc &, const GrProgramInfo &, const GrGLPrecompiledProgram *=nullptr)
const GrCaps * caps() const
Definition: GrGpu.h:73
Stats fStats
Definition: GrGpu.h:703
static bool BuildFromData(GrProgramDesc *desc, const void *keyData, size_t keyLength)
Definition: GrProgramDesc.h:82
void incNumPreProgramCacheResult(ProgramCacheResult stat)
void incNumInlineProgramCacheResult(ProgramCacheResult stat)
Definition: SkData.h:25
Definition: ref_ptr.h:256
GrGLPrecompiledProgram fPrecompiledProgram
Entry(sk_sp< GrGLProgram > program)
Entry(const GrGLPrecompiledProgram &precompiledProgram)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63