blob: 180fcb09f79855a79fc0f11fef917ad09f59e09c [file] [log] [blame]
Ethan Nicholas00543112018-07-31 09:44:36 -04001/*
2 * Copyright 2018 Google LLC
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#ifndef GrSkSLFPFactoryCache_DEFINED
9#define GrSkSLFPFactoryCache_DEFINED
10
Ethan Nicholas00543112018-07-31 09:44:36 -040011#include <vector>
Robert Phillips7f11fb52019-12-03 13:35:19 -050012#include "include/core/SkRefCnt.h"
13#include "src/sksl/ir/SkSLProgram.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040014
15class GrSkSLFPFactory;
16
17// This is a cache used by GrSkSLFP to retain GrSkSLFPFactory instances, so we don't have to
18// re-process the SkSL source code every time we create a GrSkSLFP instance.
19// For thread safety, it is important that GrSkSLFP only interact with the cache from methods that
20// are only called from within the rendering thread, like onCreateGLSLInstance and
21// onGetGLSLProcessorKey.
Mike Klein408ef212018-10-30 15:23:00 +000022class GrSkSLFPFactoryCache : public SkNVRefCnt<GrSkSLFPFactoryCache> {
Ethan Nicholas00543112018-07-31 09:44:36 -040023public:
Robert Phillips7f11fb52019-12-03 13:35:19 -050024 GrSkSLFPFactoryCache(sk_sp<const GrShaderCaps> shaderCaps) : fShaderCaps(shaderCaps) {}
25 ~GrSkSLFPFactoryCache();
26
27 sk_sp<GrSkSLFPFactory> findOrCreate(int index, const char* name, const char* skSL,
28 SkSL::Program::Kind kind);
29
30private:
Ethan Nicholas00543112018-07-31 09:44:36 -040031 // Returns a factory by its numeric index, or null if no such factory exists. Indices are
32 // allocated by GrSkSLFP::NewIndex().
33 sk_sp<GrSkSLFPFactory> get(int index);
34
35 // Stores a new factory with the given index.
36 void set(int index, sk_sp<GrSkSLFPFactory> factory);
37
Robert Phillips7f11fb52019-12-03 13:35:19 -050038 mutable SkMutex fCacheMutex;
Ethan Nicholas00543112018-07-31 09:44:36 -040039
Robert Phillips7f11fb52019-12-03 13:35:19 -050040 std::vector<sk_sp<GrSkSLFPFactory>> fFactories;
41 sk_sp<const GrShaderCaps> fShaderCaps;
Ethan Nicholas00543112018-07-31 09:44:36 -040042};
43
44#endif