blob: e3bb42fa1fd24230616659eadf18fcf63855900a [file] [log] [blame]
Robert Phillipsa3457b82018-03-08 11:30:12 -05001/*
2 * Copyright 2018 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.
6 */
7
Adlai Holler0ce2c542020-10-06 14:04:35 -04008#include "include/gpu/GrRecordingContext.h"
Robert Phillips576b6a12019-12-06 13:05:49 -05009#include "src/core/SkLRUCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrContextThreadSafeProxyPriv.h"
Robert Phillips576b6a12019-12-06 13:05:49 -050012#include "src/gpu/GrProgramDesc.h"
13#include "src/gpu/GrProgramInfo.h"
Robert Phillips07531a02020-07-15 15:11:09 -040014#include "src/gpu/GrRecordingContextPriv.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050015#include "src/gpu/effects/GrSkSLFP.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050016
17/**
18 * The DDL Context is the one in effect during DDL Recording. It isn't backed by a GrGPU and
19 * cannot allocate any GPU resources.
20 */
Adlai Holler0ce2c542020-10-06 14:04:35 -040021class GrDDLContext final : public GrRecordingContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050022public:
23 GrDDLContext(sk_sp<GrContextThreadSafeProxy> proxy)
Robert Phillips23070582021-03-31 17:04:48 -040024 : INHERITED(std::move(proxy), true) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050025 }
26
Brian Salomon161c8ed2019-11-27 09:12:47 -050027 ~GrDDLContext() override {}
Robert Phillipsa3457b82018-03-08 11:30:12 -050028
Brian Salomon161c8ed2019-11-27 09:12:47 -050029 void abandonContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050030 SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense
31 INHERITED::abandonContext();
32 }
33
Robert Phillips933484f2019-11-26 09:38:55 -050034private:
Robert Phillips576b6a12019-12-06 13:05:49 -050035 // Add to the set of unique program infos required by this DDL
36 void recordProgramInfo(const GrProgramInfo* programInfo) final {
Robert Phillips345af252020-03-04 10:32:28 -050037 if (!programInfo) {
38 return;
39 }
40
Robert Phillips576b6a12019-12-06 13:05:49 -050041 const GrCaps* caps = this->caps();
42
Robert Phillips24e2f6e2020-06-26 08:30:07 -040043 if (this->backend() == GrBackendApi::kMetal ||
Jim Van Verthde175ab2020-06-10 16:12:25 -040044 this->backend() == GrBackendApi::kDirect3D ||
Sean Gilhuly2cdad962020-03-13 11:58:05 -040045 this->backend() == GrBackendApi::kDawn) {
Robert Phillips24e2f6e2020-06-26 08:30:07 -040046 // Currently Metal, Direct3D, and Dawn require a live renderTarget to
Robert Phillipsf6a0b452020-02-18 14:26:46 -050047 // compute the key
Robert Phillips576b6a12019-12-06 13:05:49 -050048 return;
49 }
50
Robert Phillips576b6a12019-12-06 13:05:49 -050051 GrProgramDesc desc = caps->makeDesc(nullptr, *programInfo);
Robert Phillipsf6a0b452020-02-18 14:26:46 -050052 if (!desc.isValid()) {
Robert Phillips576b6a12019-12-06 13:05:49 -050053 return;
54 }
55
56 fProgramInfoMap.add(desc, programInfo);
57 }
58
Robert Phillipsf6a0b452020-02-18 14:26:46 -050059 void detachProgramData(SkTArray<ProgramData>* dst) final {
60 SkASSERT(dst->empty());
Robert Phillips576b6a12019-12-06 13:05:49 -050061
62 fProgramInfoMap.toArray(dst);
63 }
64
65
66private:
67 class ProgramInfoMap : public ::SkNoncopyable {
Robert Phillipsf6a0b452020-02-18 14:26:46 -050068 typedef const GrProgramDesc CacheKey;
Robert Phillips576b6a12019-12-06 13:05:49 -050069 typedef const GrProgramInfo* CacheValue;
70
71 public:
72 // All the programInfo data should be stored in the record-time arena so there is no
73 // need to ref them here or to delete them in the destructor.
74 ProgramInfoMap() : fMap(10) {}
75 ~ProgramInfoMap() {}
76
Robert Phillipsf6a0b452020-02-18 14:26:46 -050077 // TODO: this is doing a lot of reallocating of the ProgramDesc! Once the program descs
78 // are allocated in the record-time area there won't be a problem.
79 void add(CacheKey& desc, const GrProgramInfo* programInfo) {
Robert Phillips576b6a12019-12-06 13:05:49 -050080 SkASSERT(desc.isValid());
81
82 const CacheValue* preExisting = fMap.find(desc);
83 if (preExisting) {
84 return;
85 }
86
87 fMap.insert(desc, programInfo);
88 }
89
Robert Phillipsf6a0b452020-02-18 14:26:46 -050090 void toArray(SkTArray<ProgramData>* dst) {
91 fMap.foreach([dst](CacheKey* programDesc, CacheValue* programInfo) {
92 // TODO: remove this allocation once the program descs are stored
93 // in the record-time arena.
94 dst->emplace_back(std::make_unique<const GrProgramDesc>(*programDesc),
95 *programInfo);
Robert Phillips576b6a12019-12-06 13:05:49 -050096 });
97 }
98
99 private:
100 struct DescHash {
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500101 uint32_t operator()(CacheKey& desc) const {
Robert Phillips576b6a12019-12-06 13:05:49 -0500102 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
103 }
104 };
105
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500106 SkLRUCache<CacheKey, CacheValue, DescHash> fMap;
Robert Phillips576b6a12019-12-06 13:05:49 -0500107 };
108
109 ProgramInfoMap fProgramInfoMap;
110
Adlai Holler0ce2c542020-10-06 14:04:35 -0400111 using INHERITED = GrRecordingContext;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500112};
113
Robert Phillips07531a02020-07-15 15:11:09 -0400114sk_sp<GrRecordingContext> GrRecordingContextPriv::MakeDDL(sk_sp<GrContextThreadSafeProxy> proxy) {
115 sk_sp<GrRecordingContext> context(new GrDDLContext(std::move(proxy)));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500116
Adlai Hollere219d1c2020-06-02 11:23:16 -0400117 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500118 return nullptr;
119 }
120 return context;
121}