1/*
2 * Copyright 2021 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 skgpu_ProgramCache_DEFINED
9#define skgpu_ProgramCache_DEFINED
10
11#include <unordered_map>
12#include <string>
13#include <vector>
14#include "experimental/graphite/src/ContextUtils.h"
15#include "include/core/SkRefCnt.h"
16
17namespace skgpu {
18
19class ProgramCache {
20public:
21    ProgramCache();
22
23    static constexpr uint32_t kInvalidProgramID = 0;
24
25    // TODO: this is a bit underspecified. It still needs the rendering technique info.
26    // Additionally, it still needs an entry point to generate the text of the program.
27    class ProgramInfo : public SkRefCnt {
28    public:
29        ProgramInfo(uint32_t uniqueID, Combination c);
30        ~ProgramInfo() override;
31
32        uint32_t id() const { return fID; }
33        Combination combo() const { return fCombination; }
34
35        std::string getMSL() const;
36
37    private:
38        const uint32_t    fID;
39        const Combination fCombination;
40        // TODO: store the rendering technique info from Chris here
41    };
42
43    // TODO: we need the rendering technique info from Chris for this look up
44    sk_sp<ProgramInfo> findOrCreateProgram(Combination);
45
46    sk_sp<ProgramInfo> lookup(uint32_t uniqueID);
47
48    // The number of unique programs in the cache
49    size_t count() const {
50        SkASSERT(fProgramHash.size()+1 == fProgramVector.size());
51        return fProgramHash.size();
52    }
53
54private:
55    struct Hash {
56        size_t operator()(Combination) const;
57    };
58
59    std::unordered_map<Combination, sk_sp<ProgramInfo>, Hash> fProgramHash;
60    std::vector<sk_sp<ProgramInfo>> fProgramVector;
61    // The ProgramInfo's unique ID is only unique w/in a Recorder _not_ globally
62    uint32_t fNextUniqueID = 1;
63};
64
65} // namespace skgpu
66
67#endif // skgpu_ProgramCache_DEFINED
68