Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/gpu/GrContext.h" |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 9 | #include "src/core/SkLRUCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrCaps.h" |
| 11 | #include "src/gpu/GrContextPriv.h" |
| 12 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrProgramDesc.h" |
| 14 | #include "src/gpu/GrProgramInfo.h" |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 15 | #include "src/gpu/effects/GrSkSLFP.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 16 | |
| 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 Verth | 861ac61 | 2019-11-27 09:41:43 -0500 | [diff] [blame] | 21 | class GrDDLContext final : public GrContext { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 22 | public: |
| 23 | GrDDLContext(sk_sp<GrContextThreadSafeProxy> proxy) |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 24 | : INHERITED(proxy->backend(), proxy->priv().options(), proxy->priv().contextID()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 25 | fThreadSafeProxy = std::move(proxy); |
| 26 | } |
| 27 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 28 | ~GrDDLContext() override {} |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 29 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 30 | void abandonContext() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 31 | SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense |
| 32 | INHERITED::abandonContext(); |
| 33 | } |
| 34 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 35 | void releaseResourcesAndAbandonContext() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 36 | SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense |
| 37 | INHERITED::releaseResourcesAndAbandonContext(); |
| 38 | } |
| 39 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 40 | void freeGpuResources() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 41 | SkASSERT(0); // freeing resources in a DDL Recorder doesn't make a whole lot of sense |
| 42 | INHERITED::freeGpuResources(); |
| 43 | } |
| 44 | |
Robert Phillips | 933484f | 2019-11-26 09:38:55 -0500 | [diff] [blame] | 45 | private: |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 46 | // TODO: Here we're pretending this isn't derived from GrContext. Switch this to be derived from |
| 47 | // GrRecordingContext! |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 48 | GrContext* asDirectContext() override { return nullptr; } |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 49 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 50 | bool init(sk_sp<const GrCaps> caps) override { |
| 51 | SkASSERT(caps); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 52 | SkASSERT(fThreadSafeProxy); // should've been set in the ctor |
| 53 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 54 | if (!INHERITED::init(std::move(caps))) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 58 | // DDL contexts/drawing managers always sort the oplists and attempt to reduce opsTask |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 59 | // splitting. |
| 60 | this->setupDrawingManager(true, true); |
Robert Phillips | 56181ba | 2019-03-08 12:00:45 -0500 | [diff] [blame] | 61 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 62 | SkASSERT(this->caps()); |
| 63 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 67 | GrAtlasManager* onGetAtlasManager() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 68 | SkASSERT(0); // the DDL Recorders should never invoke this |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 72 | // Add to the set of unique program infos required by this DDL |
| 73 | void recordProgramInfo(const GrProgramInfo* programInfo) final { |
Robert Phillips | 345af25 | 2020-03-04 10:32:28 -0500 | [diff] [blame] | 74 | if (!programInfo) { |
| 75 | return; |
| 76 | } |
| 77 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 78 | const GrCaps* caps = this->caps(); |
| 79 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 80 | if (this->backend() == GrBackendApi::kVulkan || |
Sean Gilhuly | 2cdad96 | 2020-03-13 11:58:05 -0400 | [diff] [blame^] | 81 | this->backend() == GrBackendApi::kMetal || |
| 82 | this->backend() == GrBackendApi::kDawn) { |
| 83 | // Currently, Vulkan, Metal and Dawn require a live renderTarget to |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 84 | // compute the key |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
| 88 | if (programInfo->requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) { |
| 89 | // Sample locations require a live renderTarget to compute the key |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | GrProgramDesc desc = caps->makeDesc(nullptr, *programInfo); |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 94 | if (!desc.isValid()) { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
| 98 | fProgramInfoMap.add(desc, programInfo); |
| 99 | } |
| 100 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 101 | void detachProgramData(SkTArray<ProgramData>* dst) final { |
| 102 | SkASSERT(dst->empty()); |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 103 | |
| 104 | fProgramInfoMap.toArray(dst); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | private: |
| 109 | class ProgramInfoMap : public ::SkNoncopyable { |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 110 | typedef const GrProgramDesc CacheKey; |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 111 | typedef const GrProgramInfo* CacheValue; |
| 112 | |
| 113 | public: |
| 114 | // All the programInfo data should be stored in the record-time arena so there is no |
| 115 | // need to ref them here or to delete them in the destructor. |
| 116 | ProgramInfoMap() : fMap(10) {} |
| 117 | ~ProgramInfoMap() {} |
| 118 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 119 | // TODO: this is doing a lot of reallocating of the ProgramDesc! Once the program descs |
| 120 | // are allocated in the record-time area there won't be a problem. |
| 121 | void add(CacheKey& desc, const GrProgramInfo* programInfo) { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 122 | SkASSERT(desc.isValid()); |
| 123 | |
| 124 | const CacheValue* preExisting = fMap.find(desc); |
| 125 | if (preExisting) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | fMap.insert(desc, programInfo); |
| 130 | } |
| 131 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 132 | void toArray(SkTArray<ProgramData>* dst) { |
| 133 | fMap.foreach([dst](CacheKey* programDesc, CacheValue* programInfo) { |
| 134 | // TODO: remove this allocation once the program descs are stored |
| 135 | // in the record-time arena. |
| 136 | dst->emplace_back(std::make_unique<const GrProgramDesc>(*programDesc), |
| 137 | *programInfo); |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 138 | }); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | struct DescHash { |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 143 | uint32_t operator()(CacheKey& desc) const { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 144 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 145 | } |
| 146 | }; |
| 147 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 148 | SkLRUCache<CacheKey, CacheValue, DescHash> fMap; |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | ProgramInfoMap fProgramInfoMap; |
| 152 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 153 | typedef GrContext INHERITED; |
| 154 | }; |
| 155 | |
Kevin Lubick | b5502b2 | 2018-03-12 10:17:06 -0400 | [diff] [blame] | 156 | sk_sp<GrContext> GrContextPriv::MakeDDL(const sk_sp<GrContextThreadSafeProxy>& proxy) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 157 | sk_sp<GrContext> context(new GrDDLContext(proxy)); |
| 158 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 159 | if (!context->init(proxy->priv().refCaps())) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 160 | return nullptr; |
| 161 | } |
| 162 | return context; |
| 163 | } |