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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/gl/GrGLGpu.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
Brian Osman | 172bb44 | 2019-09-06 10:16:02 -0400 | [diff] [blame^] | 10 | #include "include/gpu/GrContextOptions.h" |
| 11 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrProcessor.h" |
| 13 | #include "src/gpu/GrProgramDesc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/gl/builders/GrGLProgramBuilder.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 16 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 17 | struct GrGLGpu::ProgramCache::Entry { |
Robert Phillips | 752e08b | 2018-06-22 09:48:38 -0400 | [diff] [blame] | 18 | Entry(sk_sp<GrGLProgram> program) : fProgram(std::move(program)) {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 19 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 20 | sk_sp<GrGLProgram> fProgram; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 21 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 22 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 23 | GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu) |
Brian Osman | 172bb44 | 2019-09-06 10:16:02 -0400 | [diff] [blame^] | 24 | : fMap(gpu->getContext()->priv().options().fRuntimeProgramCacheSize) |
Brian Osman | e9d9a32 | 2019-09-03 13:59:36 -0400 | [diff] [blame] | 25 | , fGpu(gpu) {} |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 26 | |
Brian Osman | e9d9a32 | 2019-09-03 13:59:36 -0400 | [diff] [blame] | 27 | GrGLGpu::ProgramCache::~ProgramCache() {} |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 28 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 29 | void GrGLGpu::ProgramCache::abandon() { |
Brian Osman | 0b8bb88 | 2019-04-12 11:47:19 -0400 | [diff] [blame] | 30 | fMap.foreach([](std::unique_ptr<Entry>* e) { |
| 31 | (*e)->fProgram->abandon(); |
| 32 | }); |
| 33 | |
| 34 | this->reset(); |
| 35 | } |
| 36 | |
| 37 | void GrGLGpu::ProgramCache::reset() { |
Robert Phillips | 752e08b | 2018-06-22 09:48:38 -0400 | [diff] [blame] | 38 | fMap.reset(); |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 41 | GrGLProgram* GrGLGpu::ProgramCache::refProgram(GrGLGpu* gpu, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 42 | GrRenderTarget* renderTarget, |
| 43 | GrSurfaceOrigin origin, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 44 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 45 | const GrTextureProxy* const primProcProxies[], |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 46 | const GrPipeline& pipeline, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 47 | bool isPoints) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 48 | // Get GrGLProgramDesc |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 49 | GrProgramDesc desc; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 50 | if (!GrProgramDesc::Build(&desc, renderTarget, primProc, isPoints, pipeline, gpu)) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 51 | GrCapsDebugf(gpu->caps(), "Failed to gl program descriptor!\n"); |
| 52 | return nullptr; |
| 53 | } |
Brian Osman | e11dfd3 | 2019-07-23 10:29:41 -0400 | [diff] [blame] | 54 | // If we knew the shader won't depend on origin, we could skip this (and use the same program |
| 55 | // for both origins). Instrumenting all fragment processors would be difficult and error prone. |
| 56 | desc.setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(origin)); |
| 57 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 58 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
| 59 | if (!entry) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 60 | // We have a cache miss |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 61 | GrGLProgram* program = GrGLProgramBuilder::CreateProgram(renderTarget, origin, |
| 62 | primProc, primProcProxies, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 63 | pipeline, &desc, fGpu); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | if (nullptr == program) { |
| 65 | return nullptr; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 66 | } |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 67 | 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] | 68 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 69 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 70 | return SkRef((*entry)->fProgram.get()); |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 71 | } |