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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrProgramDesc.h" |
| 13 | #include "src/gpu/GrProgramInfo.h" |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrRecordingContextPriv.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) |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 24 | : INHERITED(std::move(proxy)) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 25 | } |
| 26 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 27 | ~GrDDLContext() override {} |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 28 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 29 | void abandonContext() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 30 | SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense |
| 31 | INHERITED::abandonContext(); |
| 32 | } |
| 33 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 34 | void releaseResourcesAndAbandonContext() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 35 | SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense |
| 36 | INHERITED::releaseResourcesAndAbandonContext(); |
| 37 | } |
| 38 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 39 | void freeGpuResources() override { |
Robert Phillips | 9977ab9 | 2020-06-19 15:01:47 -0400 | [diff] [blame] | 40 | // freeing resources in a DDL Recorder doesn't make a whole lot of sense but some of |
| 41 | // our tests do it anyways |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Robert Phillips | 933484f | 2019-11-26 09:38:55 -0500 | [diff] [blame] | 44 | private: |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 45 | // TODO: Here we're pretending this isn't derived from GrContext. Switch this to be derived from |
| 46 | // GrRecordingContext! |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 47 | GrDirectContext* asDirectContext() override { return nullptr; } |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 48 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 49 | bool init() override { |
| 50 | if (!INHERITED::init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 54 | // 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] | 55 | // splitting. |
| 56 | this->setupDrawingManager(true, true); |
Robert Phillips | 56181ba | 2019-03-08 12:00:45 -0500 | [diff] [blame] | 57 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 61 | GrAtlasManager* onGetAtlasManager() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 62 | SkASSERT(0); // the DDL Recorders should never invoke this |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 66 | GrSmallPathAtlasMgr* onGetSmallPathAtlasMgr() override { |
| 67 | SkASSERT(0); // DDL recorders should never invoke this |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 71 | // Add to the set of unique program infos required by this DDL |
| 72 | void recordProgramInfo(const GrProgramInfo* programInfo) final { |
Robert Phillips | 345af25 | 2020-03-04 10:32:28 -0500 | [diff] [blame] | 73 | if (!programInfo) { |
| 74 | return; |
| 75 | } |
| 76 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 77 | const GrCaps* caps = this->caps(); |
| 78 | |
Robert Phillips | 24e2f6e | 2020-06-26 08:30:07 -0400 | [diff] [blame] | 79 | if (this->backend() == GrBackendApi::kMetal || |
Jim Van Verth | de175ab | 2020-06-10 16:12:25 -0400 | [diff] [blame] | 80 | this->backend() == GrBackendApi::kDirect3D || |
Sean Gilhuly | 2cdad96 | 2020-03-13 11:58:05 -0400 | [diff] [blame] | 81 | this->backend() == GrBackendApi::kDawn) { |
Robert Phillips | 24e2f6e | 2020-06-26 08:30:07 -0400 | [diff] [blame] | 82 | // Currently Metal, Direct3D, and Dawn require a live renderTarget to |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 83 | // compute the key |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 84 | 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 Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 93 | if (!desc.isValid()) { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
| 97 | fProgramInfoMap.add(desc, programInfo); |
| 98 | } |
| 99 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 100 | void detachProgramData(SkTArray<ProgramData>* dst) final { |
| 101 | SkASSERT(dst->empty()); |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 102 | |
| 103 | fProgramInfoMap.toArray(dst); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | private: |
| 108 | class ProgramInfoMap : public ::SkNoncopyable { |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 109 | typedef const GrProgramDesc CacheKey; |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 110 | 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 Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 118 | // 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 Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 121 | 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 Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 131 | 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 Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 137 | }); |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | struct DescHash { |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 142 | uint32_t operator()(CacheKey& desc) const { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 143 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 144 | } |
| 145 | }; |
| 146 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 147 | SkLRUCache<CacheKey, CacheValue, DescHash> fMap; |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | ProgramInfoMap fProgramInfoMap; |
| 151 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 152 | typedef GrContext INHERITED; |
| 153 | }; |
| 154 | |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 155 | sk_sp<GrRecordingContext> GrRecordingContextPriv::MakeDDL(sk_sp<GrContextThreadSafeProxy> proxy) { |
| 156 | sk_sp<GrRecordingContext> context(new GrDDLContext(std::move(proxy))); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 157 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 158 | if (!context->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 159 | return nullptr; |
| 160 | } |
| 161 | return context; |
| 162 | } |