blob: 851640a7561946d72a18b51bd25fc697efafa243 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
jvanverth39edf762014-12-22 11:44:19 -08008#include "GrGLGpu.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
joshualitt47bb3822014-10-07 16:43:25 -070010#include "builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrProcessor.h"
egdaniel5d8f69f2016-09-07 07:24:12 -070012#include "GrProgramDesc.h"
egdaniel8a4c1032014-09-16 07:18:54 -070013#include "GrGLPathRendering.h"
egdaniel64c47282015-11-13 06:54:19 -080014#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070015#include "glsl/GrGLSLProgramDataManager.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000016#include "SkTSearch.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000018#ifdef PROGRAM_CACHE_STATS
halcanary4e44efe2016-08-04 10:47:16 -070019// Display program cache usage
20static const bool c_DisplayCache{false};
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000021#endif
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000022
egdaniel018fb622015-10-28 07:26:40 -070023typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000024
bsalomon861e1032014-12-16 07:33:49 -080025struct GrGLGpu::ProgramCache::Entry {
Robert Phillips752e08b2018-06-22 09:48:38 -040026 Entry(sk_sp<GrGLProgram> program) : fProgram(std::move(program)) {}
halcanary9d524f22016-03-29 09:03:52 -070027
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050028 sk_sp<GrGLProgram> fProgram;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000029};
junov@google.comf93e7172011-03-31 21:26:24 +000030
bsalomon861e1032014-12-16 07:33:49 -080031GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu)
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050032 : fMap(kMaxEntries)
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000033 , fGpu(gpu)
jvanverth@google.com94878772013-03-12 16:00:54 +000034#ifdef PROGRAM_CACHE_STATS
35 , fTotalRequests(0)
36 , fCacheMisses(0)
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000037 , fHashMisses(0)
jvanverth@google.com94878772013-03-12 16:00:54 +000038#endif
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050039{}
jvanverth@google.com94878772013-03-12 16:00:54 +000040
bsalomon861e1032014-12-16 07:33:49 -080041GrGLGpu::ProgramCache::~ProgramCache() {
jvanverth@google.com94878772013-03-12 16:00:54 +000042 // dump stats
43#ifdef PROGRAM_CACHE_STATS
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000044 if (c_DisplayCache) {
45 SkDebugf("--- Program Cache ---\n");
46 SkDebugf("Total requests: %d\n", fTotalRequests);
47 SkDebugf("Cache misses: %d\n", fCacheMisses);
48 SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ?
49 100.f * fCacheMisses / fTotalRequests :
50 0.f);
51 int cacheHits = fTotalRequests - fCacheMisses;
52 SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f);
53 SkDebugf("---------------------\n");
54 }
jvanverth@google.com94878772013-03-12 16:00:54 +000055#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000056}
junov@google.comf93e7172011-03-31 21:26:24 +000057
egdaniel22281c12016-03-23 13:49:40 -070058void GrGLGpu::ProgramCache::abandon() {
joshualitt8fd844f2015-12-02 13:36:47 -080059#ifdef PROGRAM_CACHE_STATS
60 fTotalRequests = 0;
61 fCacheMisses = 0;
62 fHashMisses = 0;
63#endif
Robert Phillips752e08b2018-06-22 09:48:38 -040064
65 fMap.foreach([](std::unique_ptr<Entry>* e) {
66 (*e)->fProgram->abandon();
67 });
68 fMap.reset();
joshualitt8fd844f2015-12-02 13:36:47 -080069}
70
Greg Daniel7a82edf2018-12-04 10:54:34 -050071GrGLProgram* GrGLGpu::ProgramCache::refProgram(GrGLGpu* gpu,
bsalomon2eda5b32016-09-21 10:53:24 -070072 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050073 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -040074 const GrPipeline& pipeline,
bsalomon2eda5b32016-09-21 10:53:24 -070075 bool isPoints) {
jvanverth@google.com94878772013-03-12 16:00:54 +000076#ifdef PROGRAM_CACHE_STATS
77 ++fTotalRequests;
78#endif
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000079
egdaniel0e1853c2016-03-17 11:35:45 -070080 // Get GrGLProgramDesc
egdaniel5d8f69f2016-09-07 07:24:12 -070081 GrProgramDesc desc;
Greg Daniel7a82edf2018-12-04 10:54:34 -050082 if (!GrProgramDesc::Build(&desc, primProc, isPoints, pipeline, gpu)) {
egdaniel0e1853c2016-03-17 11:35:45 -070083 GrCapsDebugf(gpu->caps(), "Failed to gl program descriptor!\n");
84 return nullptr;
85 }
egdaniel5d8f69f2016-09-07 07:24:12 -070086 desc.finalize();
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050087 std::unique_ptr<Entry>* entry = fMap.find(desc);
88 if (!entry) {
Ethan Nicholas38657112017-02-09 17:01:22 -050089 // Didn't find an origin-independent version, check with the specific origin
Robert Phillips2890fbf2017-07-26 15:48:41 -040090 GrSurfaceOrigin origin = pipeline.proxy()->origin();
Robert Phillips7f861922018-01-30 13:13:42 +000091 desc.setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(origin));
Ethan Nicholas38657112017-02-09 17:01:22 -050092 desc.finalize();
93 entry = fMap.find(desc);
94 }
95 if (!entry) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000096 // We have a cache miss
jvanverth@google.com94878772013-03-12 16:00:54 +000097#ifdef PROGRAM_CACHE_STATS
98 ++fCacheMisses;
99#endif
Greg Daniel9a51a862018-11-30 10:18:14 -0500100 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(primProc, primProcProxies,
101 pipeline, &desc, fGpu);
halcanary96fcdcc2015-08-27 07:41:13 -0700102 if (nullptr == program) {
103 return nullptr;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000104 }
Ethan Nicholas1b9924f2016-12-15 15:28:42 -0500105 entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(sk_sp<GrGLProgram>(program))));
junov@google.comf93e7172011-03-31 21:26:24 +0000106 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000107
Ethan Nicholas1b9924f2016-12-15 15:28:42 -0500108 return SkRef((*entry)->fProgram.get());
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000109}