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) |
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 | |
Robert Phillips | 933484f | 2019-11-26 09:38:55 -0500 | [diff] [blame] | 34 | private: |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 35 | bool init() override { |
| 36 | if (!INHERITED::init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 40 | // 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] | 41 | // splitting. |
| 42 | this->setupDrawingManager(true, true); |
Robert Phillips | 56181ba | 2019-03-08 12:00:45 -0500 | [diff] [blame] | 43 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 47 | // Add to the set of unique program infos required by this DDL |
| 48 | void recordProgramInfo(const GrProgramInfo* programInfo) final { |
Robert Phillips | 345af25 | 2020-03-04 10:32:28 -0500 | [diff] [blame] | 49 | if (!programInfo) { |
| 50 | return; |
| 51 | } |
| 52 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 53 | const GrCaps* caps = this->caps(); |
| 54 | |
Robert Phillips | 24e2f6e | 2020-06-26 08:30:07 -0400 | [diff] [blame] | 55 | if (this->backend() == GrBackendApi::kMetal || |
Jim Van Verth | de175ab | 2020-06-10 16:12:25 -0400 | [diff] [blame] | 56 | this->backend() == GrBackendApi::kDirect3D || |
Sean Gilhuly | 2cdad96 | 2020-03-13 11:58:05 -0400 | [diff] [blame] | 57 | this->backend() == GrBackendApi::kDawn) { |
Robert Phillips | 24e2f6e | 2020-06-26 08:30:07 -0400 | [diff] [blame] | 58 | // Currently Metal, Direct3D, and Dawn require a live renderTarget to |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 59 | // compute the key |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 60 | 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 Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 69 | if (!desc.isValid()) { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
| 73 | fProgramInfoMap.add(desc, programInfo); |
| 74 | } |
| 75 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 76 | void detachProgramData(SkTArray<ProgramData>* dst) final { |
| 77 | SkASSERT(dst->empty()); |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 78 | |
| 79 | fProgramInfoMap.toArray(dst); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | private: |
| 84 | class ProgramInfoMap : public ::SkNoncopyable { |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 85 | typedef const GrProgramDesc CacheKey; |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 86 | 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 Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 94 | // 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 Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 97 | 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 Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 107 | 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 Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 113 | }); |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | struct DescHash { |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 118 | uint32_t operator()(CacheKey& desc) const { |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 119 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 120 | } |
| 121 | }; |
| 122 | |
Robert Phillips | f6a0b45 | 2020-02-18 14:26:46 -0500 | [diff] [blame] | 123 | SkLRUCache<CacheKey, CacheValue, DescHash> fMap; |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | ProgramInfoMap fProgramInfoMap; |
| 127 | |
Adlai Holler | 0ce2c54 | 2020-10-06 14:04:35 -0400 | [diff] [blame^] | 128 | using INHERITED = GrRecordingContext; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 129 | }; |
| 130 | |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 131 | sk_sp<GrRecordingContext> GrRecordingContextPriv::MakeDDL(sk_sp<GrContextThreadSafeProxy> proxy) { |
| 132 | sk_sp<GrRecordingContext> context(new GrDDLContext(std::move(proxy))); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 133 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 134 | if (!context->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 135 | return nullptr; |
| 136 | } |
| 137 | return context; |
| 138 | } |