blob: 183bc8fa4d88d57f810493d247644d429019a6e3 [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)
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
Robert Phillips933484f2019-11-26 09:38:55 -050034private:
Adlai Hollere219d1c2020-06-02 11:23:16 -040035 bool init() override {
36 if (!INHERITED::init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050037 return false;
38 }
39
Greg Danielf41b2bd2019-08-22 16:19:24 -040040 // DDL contexts/drawing managers always sort the oplists and attempt to reduce opsTask
Robert Phillips6db27c22019-05-01 10:43:56 -040041 // splitting.
42 this->setupDrawingManager(true, true);
Robert Phillips56181ba2019-03-08 12:00:45 -050043
Robert Phillipsa3457b82018-03-08 11:30:12 -050044 return true;
45 }
46
Robert Phillips576b6a12019-12-06 13:05:49 -050047 // Add to the set of unique program infos required by this DDL
48 void recordProgramInfo(const GrProgramInfo* programInfo) final {
Robert Phillips345af252020-03-04 10:32:28 -050049 if (!programInfo) {
50 return;
51 }
52
Robert Phillips576b6a12019-12-06 13:05:49 -050053 const GrCaps* caps = this->caps();
54
Robert Phillips24e2f6e2020-06-26 08:30:07 -040055 if (this->backend() == GrBackendApi::kMetal ||
Jim Van Verthde175ab2020-06-10 16:12:25 -040056 this->backend() == GrBackendApi::kDirect3D ||
Sean Gilhuly2cdad962020-03-13 11:58:05 -040057 this->backend() == GrBackendApi::kDawn) {
Robert Phillips24e2f6e2020-06-26 08:30:07 -040058 // Currently Metal, Direct3D, and Dawn require a live renderTarget to
Robert Phillipsf6a0b452020-02-18 14:26:46 -050059 // compute the key
Robert Phillips576b6a12019-12-06 13:05:49 -050060 return;
61 }
62
63 if (programInfo->requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) {
64 // Sample locations require a live renderTarget to compute the key
65 return;
66 }
67
68 GrProgramDesc desc = caps->makeDesc(nullptr, *programInfo);
Robert Phillipsf6a0b452020-02-18 14:26:46 -050069 if (!desc.isValid()) {
Robert Phillips576b6a12019-12-06 13:05:49 -050070 return;
71 }
72
73 fProgramInfoMap.add(desc, programInfo);
74 }
75
Robert Phillipsf6a0b452020-02-18 14:26:46 -050076 void detachProgramData(SkTArray<ProgramData>* dst) final {
77 SkASSERT(dst->empty());
Robert Phillips576b6a12019-12-06 13:05:49 -050078
79 fProgramInfoMap.toArray(dst);
80 }
81
82
83private:
84 class ProgramInfoMap : public ::SkNoncopyable {
Robert Phillipsf6a0b452020-02-18 14:26:46 -050085 typedef const GrProgramDesc CacheKey;
Robert Phillips576b6a12019-12-06 13:05:49 -050086 typedef const GrProgramInfo* CacheValue;
87
88 public:
89 // All the programInfo data should be stored in the record-time arena so there is no
90 // need to ref them here or to delete them in the destructor.
91 ProgramInfoMap() : fMap(10) {}
92 ~ProgramInfoMap() {}
93
Robert Phillipsf6a0b452020-02-18 14:26:46 -050094 // TODO: this is doing a lot of reallocating of the ProgramDesc! Once the program descs
95 // are allocated in the record-time area there won't be a problem.
96 void add(CacheKey& desc, const GrProgramInfo* programInfo) {
Robert Phillips576b6a12019-12-06 13:05:49 -050097 SkASSERT(desc.isValid());
98
99 const CacheValue* preExisting = fMap.find(desc);
100 if (preExisting) {
101 return;
102 }
103
104 fMap.insert(desc, programInfo);
105 }
106
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500107 void toArray(SkTArray<ProgramData>* dst) {
108 fMap.foreach([dst](CacheKey* programDesc, CacheValue* programInfo) {
109 // TODO: remove this allocation once the program descs are stored
110 // in the record-time arena.
111 dst->emplace_back(std::make_unique<const GrProgramDesc>(*programDesc),
112 *programInfo);
Robert Phillips576b6a12019-12-06 13:05:49 -0500113 });
114 }
115
116 private:
117 struct DescHash {
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500118 uint32_t operator()(CacheKey& desc) const {
Robert Phillips576b6a12019-12-06 13:05:49 -0500119 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
120 }
121 };
122
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500123 SkLRUCache<CacheKey, CacheValue, DescHash> fMap;
Robert Phillips576b6a12019-12-06 13:05:49 -0500124 };
125
126 ProgramInfoMap fProgramInfoMap;
127
Adlai Holler0ce2c542020-10-06 14:04:35 -0400128 using INHERITED = GrRecordingContext;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500129};
130
Robert Phillips07531a02020-07-15 15:11:09 -0400131sk_sp<GrRecordingContext> GrRecordingContextPriv::MakeDDL(sk_sp<GrContextThreadSafeProxy> proxy) {
132 sk_sp<GrRecordingContext> context(new GrDDLContext(std::move(proxy)));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500133
Adlai Hollere219d1c2020-06-02 11:23:16 -0400134 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500135 return nullptr;
136 }
137 return context;
138}