junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 8 | #include "GrGLGpu.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 10 | #include "builders/GrGLProgramBuilder.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "GrProcessor.h" |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 12 | #include "GrProgramDesc.h" |
egdaniel | 8a4c103 | 2014-09-16 07:18:54 -0700 | [diff] [blame] | 13 | #include "GrGLPathRendering.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLProgramDataManager.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 16 | #include "SkTSearch.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 18 | #ifdef PROGRAM_CACHE_STATS |
halcanary | 4e44efe | 2016-08-04 10:47:16 -0700 | [diff] [blame] | 19 | // Display program cache usage |
| 20 | static const bool c_DisplayCache{false}; |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 21 | #endif |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 22 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 23 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 24 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 25 | struct GrGLGpu::ProgramCache::Entry { |
Robert Phillips | 752e08b | 2018-06-22 09:48:38 -0400 | [diff] [blame] | 26 | Entry(sk_sp<GrGLProgram> program) : fProgram(std::move(program)) {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 27 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 28 | sk_sp<GrGLProgram> fProgram; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 29 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 30 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 31 | GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu) |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 32 | : fMap(kMaxEntries) |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 33 | , fGpu(gpu) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 34 | #ifdef PROGRAM_CACHE_STATS |
| 35 | , fTotalRequests(0) |
| 36 | , fCacheMisses(0) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 37 | , fHashMisses(0) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 38 | #endif |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 39 | {} |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 40 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 41 | GrGLGpu::ProgramCache::~ProgramCache() { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 42 | // dump stats |
| 43 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 44 | 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.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 55 | #endif |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 56 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 57 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 58 | void GrGLGpu::ProgramCache::abandon() { |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 59 | #ifdef PROGRAM_CACHE_STATS |
| 60 | fTotalRequests = 0; |
| 61 | fCacheMisses = 0; |
| 62 | fHashMisses = 0; |
| 63 | #endif |
Robert Phillips | 752e08b | 2018-06-22 09:48:38 -0400 | [diff] [blame] | 64 | |
| 65 | fMap.foreach([](std::unique_ptr<Entry>* e) { |
| 66 | (*e)->fProgram->abandon(); |
| 67 | }); |
| 68 | fMap.reset(); |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 71 | GrGLProgram* GrGLGpu::ProgramCache::refProgram(GrGLGpu* gpu, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 72 | GrRenderTarget* renderTarget, |
| 73 | GrSurfaceOrigin origin, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 74 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 75 | const GrTextureProxy* const primProcProxies[], |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 76 | const GrPipeline& pipeline, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 77 | bool isPoints) { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 78 | #ifdef PROGRAM_CACHE_STATS |
| 79 | ++fTotalRequests; |
| 80 | #endif |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 81 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 82 | // Get GrGLProgramDesc |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 83 | GrProgramDesc desc; |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 84 | if (!GrProgramDesc::Build(&desc, renderTarget->config(), primProc, isPoints, pipeline, gpu)) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 85 | GrCapsDebugf(gpu->caps(), "Failed to gl program descriptor!\n"); |
| 86 | return nullptr; |
| 87 | } |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 88 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
| 89 | if (!entry) { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 90 | // Didn't find an origin-independent version, check with the specific origin |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 91 | desc.setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(origin)); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 92 | entry = fMap.find(desc); |
| 93 | } |
| 94 | if (!entry) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 95 | // We have a cache miss |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 96 | #ifdef PROGRAM_CACHE_STATS |
| 97 | ++fCacheMisses; |
| 98 | #endif |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 99 | GrGLProgram* program = GrGLProgramBuilder::CreateProgram(renderTarget, origin, |
| 100 | primProc, primProcProxies, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 101 | pipeline, &desc, fGpu); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 102 | if (nullptr == program) { |
| 103 | return nullptr; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 104 | } |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 105 | entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(sk_sp<GrGLProgram>(program)))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 106 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 107 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 108 | return SkRef((*entry)->fProgram.get()); |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 109 | } |