blob: 279dd033cd47dd77e452469a7deb2199b77c3f4b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/gl/GrGLGpu.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
Brian Osman172bb442019-09-06 10:16:02 -040010#include "include/gpu/GrContextOptions.h"
11#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrProcessor.h"
13#include "src/gpu/GrProgramDesc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/gl/builders/GrGLProgramBuilder.h"
15#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000016
bsalomon861e1032014-12-16 07:33:49 -080017struct GrGLGpu::ProgramCache::Entry {
Robert Phillips752e08b2018-06-22 09:48:38 -040018 Entry(sk_sp<GrGLProgram> program) : fProgram(std::move(program)) {}
halcanary9d524f22016-03-29 09:03:52 -070019
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050020 sk_sp<GrGLProgram> fProgram;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000021};
junov@google.comf93e7172011-03-31 21:26:24 +000022
bsalomon861e1032014-12-16 07:33:49 -080023GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu)
Brian Osman172bb442019-09-06 10:16:02 -040024 : fMap(gpu->getContext()->priv().options().fRuntimeProgramCacheSize)
Brian Osmane9d9a322019-09-03 13:59:36 -040025 , fGpu(gpu) {}
jvanverth@google.com94878772013-03-12 16:00:54 +000026
Brian Osmane9d9a322019-09-03 13:59:36 -040027GrGLGpu::ProgramCache::~ProgramCache() {}
junov@google.comf93e7172011-03-31 21:26:24 +000028
egdaniel22281c12016-03-23 13:49:40 -070029void GrGLGpu::ProgramCache::abandon() {
Brian Osman0b8bb882019-04-12 11:47:19 -040030 fMap.foreach([](std::unique_ptr<Entry>* e) {
31 (*e)->fProgram->abandon();
32 });
33
34 this->reset();
35}
36
37void GrGLGpu::ProgramCache::reset() {
Robert Phillips752e08b2018-06-22 09:48:38 -040038 fMap.reset();
joshualitt8fd844f2015-12-02 13:36:47 -080039}
40
Greg Daniel7a82edf2018-12-04 10:54:34 -050041GrGLProgram* GrGLGpu::ProgramCache::refProgram(GrGLGpu* gpu,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050042 GrRenderTarget* renderTarget,
43 GrSurfaceOrigin origin,
bsalomon2eda5b32016-09-21 10:53:24 -070044 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050045 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -040046 const GrPipeline& pipeline,
bsalomon2eda5b32016-09-21 10:53:24 -070047 bool isPoints) {
egdaniel0e1853c2016-03-17 11:35:45 -070048 // Get GrGLProgramDesc
egdaniel5d8f69f2016-09-07 07:24:12 -070049 GrProgramDesc desc;
Chris Daltond7291ba2019-03-07 14:17:03 -070050 if (!GrProgramDesc::Build(&desc, renderTarget, primProc, isPoints, pipeline, gpu)) {
egdaniel0e1853c2016-03-17 11:35:45 -070051 GrCapsDebugf(gpu->caps(), "Failed to gl program descriptor!\n");
52 return nullptr;
53 }
Brian Osmane11dfd32019-07-23 10:29:41 -040054 // 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 Nicholas1b9924f2016-12-15 15:28:42 -050058 std::unique_ptr<Entry>* entry = fMap.find(desc);
59 if (!entry) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000060 // We have a cache miss
Robert Phillipsd0fe8752019-01-31 14:13:59 -050061 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(renderTarget, origin,
62 primProc, primProcProxies,
Greg Daniel9a51a862018-11-30 10:18:14 -050063 pipeline, &desc, fGpu);
halcanary96fcdcc2015-08-27 07:41:13 -070064 if (nullptr == program) {
65 return nullptr;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000066 }
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050067 entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(sk_sp<GrGLProgram>(program))));
junov@google.comf93e7172011-03-31 21:26:24 +000068 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000069
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050070 return SkRef((*entry)->fProgram.get());
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000071}