blob: ce6dbbf097233d9a728c4fb668e71c4dd129aad1 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContext.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 */
Jim Van Verth861ac612019-11-27 09:41:43 -050021class GrDDLContext final : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050022public:
23 GrDDLContext(sk_sp<GrContextThreadSafeProxy> proxy)
Adlai Hollere219d1c2020-06-02 11:23:16 -040024 : INHERITED(std::move(proxy)) {
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
Brian Salomon161c8ed2019-11-27 09:12:47 -050034 void releaseResourcesAndAbandonContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050035 SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense
36 INHERITED::releaseResourcesAndAbandonContext();
37 }
38
Brian Salomon161c8ed2019-11-27 09:12:47 -050039 void freeGpuResources() override {
Robert Phillips9977ab92020-06-19 15:01:47 -040040 // freeing resources in a DDL Recorder doesn't make a whole lot of sense but some of
41 // our tests do it anyways
Robert Phillipsa3457b82018-03-08 11:30:12 -050042 }
43
Robert Phillips933484f2019-11-26 09:38:55 -050044private:
Robert Phillipsa41c6852019-02-07 10:44:10 -050045 // TODO: Here we're pretending this isn't derived from GrContext. Switch this to be derived from
46 // GrRecordingContext!
Robert Phillips44333c52020-06-30 13:28:00 -040047 GrDirectContext* asDirectContext() override { return nullptr; }
Robert Phillipsa41c6852019-02-07 10:44:10 -050048
Adlai Hollere219d1c2020-06-02 11:23:16 -040049 bool init() override {
50 if (!INHERITED::init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050051 return false;
52 }
53
Greg Danielf41b2bd2019-08-22 16:19:24 -040054 // DDL contexts/drawing managers always sort the oplists and attempt to reduce opsTask
Robert Phillips6db27c22019-05-01 10:43:56 -040055 // splitting.
56 this->setupDrawingManager(true, true);
Robert Phillips56181ba2019-03-08 12:00:45 -050057
Robert Phillipsa3457b82018-03-08 11:30:12 -050058 return true;
59 }
60
Brian Salomon161c8ed2019-11-27 09:12:47 -050061 GrAtlasManager* onGetAtlasManager() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050062 SkASSERT(0); // the DDL Recorders should never invoke this
63 return nullptr;
64 }
65
Robert Phillips5edf5102020-08-10 16:30:36 -040066 GrSmallPathAtlasMgr* onGetSmallPathAtlasMgr() override {
67 SkASSERT(0); // DDL recorders should never invoke this
68 return nullptr;
69 }
70
Robert Phillips576b6a12019-12-06 13:05:49 -050071 // Add to the set of unique program infos required by this DDL
72 void recordProgramInfo(const GrProgramInfo* programInfo) final {
Robert Phillips345af252020-03-04 10:32:28 -050073 if (!programInfo) {
74 return;
75 }
76
Robert Phillips576b6a12019-12-06 13:05:49 -050077 const GrCaps* caps = this->caps();
78
Robert Phillips24e2f6e2020-06-26 08:30:07 -040079 if (this->backend() == GrBackendApi::kMetal ||
Jim Van Verthde175ab2020-06-10 16:12:25 -040080 this->backend() == GrBackendApi::kDirect3D ||
Sean Gilhuly2cdad962020-03-13 11:58:05 -040081 this->backend() == GrBackendApi::kDawn) {
Robert Phillips24e2f6e2020-06-26 08:30:07 -040082 // Currently Metal, Direct3D, and Dawn require a live renderTarget to
Robert Phillipsf6a0b452020-02-18 14:26:46 -050083 // compute the key
Robert Phillips576b6a12019-12-06 13:05:49 -050084 return;
85 }
86
87 if (programInfo->requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) {
88 // Sample locations require a live renderTarget to compute the key
89 return;
90 }
91
92 GrProgramDesc desc = caps->makeDesc(nullptr, *programInfo);
Robert Phillipsf6a0b452020-02-18 14:26:46 -050093 if (!desc.isValid()) {
Robert Phillips576b6a12019-12-06 13:05:49 -050094 return;
95 }
96
97 fProgramInfoMap.add(desc, programInfo);
98 }
99
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500100 void detachProgramData(SkTArray<ProgramData>* dst) final {
101 SkASSERT(dst->empty());
Robert Phillips576b6a12019-12-06 13:05:49 -0500102
103 fProgramInfoMap.toArray(dst);
104 }
105
106
107private:
108 class ProgramInfoMap : public ::SkNoncopyable {
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500109 typedef const GrProgramDesc CacheKey;
Robert Phillips576b6a12019-12-06 13:05:49 -0500110 typedef const GrProgramInfo* CacheValue;
111
112 public:
113 // All the programInfo data should be stored in the record-time arena so there is no
114 // need to ref them here or to delete them in the destructor.
115 ProgramInfoMap() : fMap(10) {}
116 ~ProgramInfoMap() {}
117
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500118 // TODO: this is doing a lot of reallocating of the ProgramDesc! Once the program descs
119 // are allocated in the record-time area there won't be a problem.
120 void add(CacheKey& desc, const GrProgramInfo* programInfo) {
Robert Phillips576b6a12019-12-06 13:05:49 -0500121 SkASSERT(desc.isValid());
122
123 const CacheValue* preExisting = fMap.find(desc);
124 if (preExisting) {
125 return;
126 }
127
128 fMap.insert(desc, programInfo);
129 }
130
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500131 void toArray(SkTArray<ProgramData>* dst) {
132 fMap.foreach([dst](CacheKey* programDesc, CacheValue* programInfo) {
133 // TODO: remove this allocation once the program descs are stored
134 // in the record-time arena.
135 dst->emplace_back(std::make_unique<const GrProgramDesc>(*programDesc),
136 *programInfo);
Robert Phillips576b6a12019-12-06 13:05:49 -0500137 });
138 }
139
140 private:
141 struct DescHash {
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500142 uint32_t operator()(CacheKey& desc) const {
Robert Phillips576b6a12019-12-06 13:05:49 -0500143 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
144 }
145 };
146
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500147 SkLRUCache<CacheKey, CacheValue, DescHash> fMap;
Robert Phillips576b6a12019-12-06 13:05:49 -0500148 };
149
150 ProgramInfoMap fProgramInfoMap;
151
Robert Phillipsa3457b82018-03-08 11:30:12 -0500152 typedef GrContext INHERITED;
153};
154
Robert Phillips07531a02020-07-15 15:11:09 -0400155sk_sp<GrRecordingContext> GrRecordingContextPriv::MakeDDL(sk_sp<GrContextThreadSafeProxy> proxy) {
156 sk_sp<GrRecordingContext> context(new GrDDLContext(std::move(proxy)));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500157
Adlai Hollere219d1c2020-06-02 11:23:16 -0400158 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500159 return nullptr;
160 }
161 return context;
162}