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 | |
Adlai Holler | 0ce2c54 | 2020-10-06 14:04:35 -0400 | [diff] [blame] | 8 | #include "include/gpu/GrRecordingContext.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 | */ |
Adlai Holler | 0ce2c54 | 2020-10-06 14:04:35 -0400 | [diff] [blame] | 21 | class GrDDLContext final : public GrRecordingContext { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 22 | public: |
| 23 | GrDDLContext(sk_sp<GrContextThreadSafeProxy> proxy) |
Robert Phillips | 2307058 | 2021-03-31 17:04:48 -0400 | [diff] [blame] | 24 | : INHERITED(std::move(proxy), true) { |
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 | |
Robert Phillips | 933484f | 2019-11-26 09:38:55 -0500 | [diff] [blame] | 34 | private: |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 35 | // Add to the set of unique program infos required by this DDL |
| 36 | void recordProgramInfo(const GrProgramInfo* programInfo) final { |
Robert Phillips | 345af25 | 2020-03-04 10:32:28 -0500 | [diff] [blame] | 37 | if (!programInfo) { |
| 38 | return; |
| 39 | } |
| 40 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 41 | const GrCaps* caps = this->caps(); |
| 42 | |
Robert Phillips | 24e2f6e | 2020-06-26 08:30:07 -0400 | [diff] [blame] | 43 | if (this->backend() == GrBackendApi::kMetal || |
Jim Van Verth | de175ab | 2020-06-10 16:12:25 -0400 | [diff] [blame] | 44 | this->backend() == GrBackendApi::kDirect3D || |
Sean Gilhuly | 2cdad96 | 2020-03-13 11:58:05 -0400 | [diff] [blame] | 45 | this->backend() == GrBackendApi::kDawn) { |
Robert Phillips | 24e2f6e | 2020-06-26 08:30:07 -0400 | [diff] [blame] | 46 | // Currently Metal, Direct3D, and Dawn require a live renderTarget to |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 47 | // compute the key |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 51 | GrProgramDesc desc = caps->makeDesc(nullptr, *programInfo); |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 52 | if (!desc.isValid()) { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
| 56 | fProgramInfoMap.add(desc, programInfo); |
| 57 | } |
| 58 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 59 | void detachProgramData(SkTArray<ProgramData>* dst) final { |
| 60 | SkASSERT(dst->empty()); |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 61 | |
| 62 | fProgramInfoMap.toArray(dst); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | private: |
| 67 | class ProgramInfoMap : public ::SkNoncopyable { |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 68 | typedef const GrProgramDesc CacheKey; |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 69 | 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 Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 77 | // 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 Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 80 | 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 Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 90 | 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 Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 96 | }); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | struct DescHash { |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 101 | uint32_t operator()(CacheKey& desc) const { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 102 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 103 | } |
| 104 | }; |
| 105 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 106 | SkLRUCache<CacheKey, CacheValue, DescHash> fMap; |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | ProgramInfoMap fProgramInfoMap; |
| 110 | |
Adlai Holler | 0ce2c54 | 2020-10-06 14:04:35 -0400 | [diff] [blame] | 111 | using INHERITED = GrRecordingContext; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 112 | }; |
| 113 | |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 114 | sk_sp<GrRecordingContext> GrRecordingContextPriv::MakeDDL(sk_sp<GrContextThreadSafeProxy> proxy) { |
| 115 | sk_sp<GrRecordingContext> context(new GrDDLContext(std::move(proxy))); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 116 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 117 | if (!context->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 118 | return nullptr; |
| 119 | } |
| 120 | return context; |
| 121 | } |