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" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrSkSLFPFactoryCache.h" |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 16 | #include "src/gpu/effects/GrSkSLFP.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * The DDL Context is the one in effect during DDL Recording. It isn't backed by a GrGPU and |
| 20 | * cannot allocate any GPU resources. |
| 21 | */ |
Jim Van Verth | 861ac61 | 2019-11-27 09:41:43 -0500 | [diff] [blame] | 22 | class GrDDLContext final : public GrContext { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 23 | public: |
| 24 | GrDDLContext(sk_sp<GrContextThreadSafeProxy> proxy) |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 25 | : INHERITED(proxy->backend(), proxy->priv().options(), proxy->priv().contextID()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 26 | fThreadSafeProxy = std::move(proxy); |
| 27 | } |
| 28 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 29 | ~GrDDLContext() override {} |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 30 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 31 | void abandonContext() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 32 | SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense |
| 33 | INHERITED::abandonContext(); |
| 34 | } |
| 35 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 36 | void releaseResourcesAndAbandonContext() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 37 | SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense |
| 38 | INHERITED::releaseResourcesAndAbandonContext(); |
| 39 | } |
| 40 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 41 | void freeGpuResources() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 42 | SkASSERT(0); // freeing resources in a DDL Recorder doesn't make a whole lot of sense |
| 43 | INHERITED::freeGpuResources(); |
| 44 | } |
| 45 | |
Robert Phillips | 933484f | 2019-11-26 09:38:55 -0500 | [diff] [blame] | 46 | private: |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 47 | // TODO: Here we're pretending this isn't derived from GrContext. Switch this to be derived from |
| 48 | // GrRecordingContext! |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 49 | GrContext* asDirectContext() override { return nullptr; } |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 50 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 51 | bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) override { |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 52 | SkASSERT(caps && FPFactoryCache); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 53 | SkASSERT(fThreadSafeProxy); // should've been set in the ctor |
| 54 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 55 | if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 59 | // 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] | 60 | // splitting. |
| 61 | this->setupDrawingManager(true, true); |
Robert Phillips | 56181ba | 2019-03-08 12:00:45 -0500 | [diff] [blame] | 62 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 63 | SkASSERT(this->caps()); |
| 64 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
Brian Salomon | 161c8ed | 2019-11-27 09:12:47 -0500 | [diff] [blame] | 68 | GrAtlasManager* onGetAtlasManager() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 69 | SkASSERT(0); // the DDL Recorders should never invoke this |
| 70 | return nullptr; |
| 71 | } |
| 72 | |
Robert Phillips | 576b6a1 | 2019-12-06 13:05:49 -0500 | [diff] [blame^] | 73 | // Add to the set of unique program infos required by this DDL |
| 74 | void recordProgramInfo(const GrProgramInfo* programInfo) final { |
| 75 | const GrCaps* caps = this->caps(); |
| 76 | |
| 77 | if (this->backend() == GrBackendApi::kVulkan) { |
| 78 | // Currently, Vulkan requires a live renderTarget to compute the key |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (programInfo->requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) { |
| 83 | // Sample locations require a live renderTarget to compute the key |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | GrProgramDesc desc = caps->makeDesc(nullptr, *programInfo); |
| 88 | if (desc.isValid()) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | fProgramInfoMap.add(desc, programInfo); |
| 93 | } |
| 94 | |
| 95 | void detachProgramInfos(SkTDArray<const GrProgramInfo*>* dst) final { |
| 96 | SkASSERT(dst->isEmpty()); |
| 97 | |
| 98 | fProgramInfoMap.toArray(dst); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | private: |
| 103 | class ProgramInfoMap : public ::SkNoncopyable { |
| 104 | typedef const GrProgramInfo* CacheValue; |
| 105 | |
| 106 | public: |
| 107 | // All the programInfo data should be stored in the record-time arena so there is no |
| 108 | // need to ref them here or to delete them in the destructor. |
| 109 | ProgramInfoMap() : fMap(10) {} |
| 110 | ~ProgramInfoMap() {} |
| 111 | |
| 112 | void add(const GrProgramDesc& desc, const GrProgramInfo* programInfo) { |
| 113 | SkASSERT(desc.isValid()); |
| 114 | |
| 115 | const CacheValue* preExisting = fMap.find(desc); |
| 116 | if (preExisting) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | fMap.insert(desc, programInfo); |
| 121 | } |
| 122 | |
| 123 | void toArray(SkTDArray<const GrProgramInfo*>* dst) { |
| 124 | fMap.foreach([dst](CacheValue* programInfo) { |
| 125 | dst->push_back(*programInfo); |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | struct DescHash { |
| 131 | uint32_t operator()(const GrProgramDesc& desc) const { |
| 132 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | SkLRUCache<GrProgramDesc, CacheValue, DescHash> fMap; |
| 137 | }; |
| 138 | |
| 139 | ProgramInfoMap fProgramInfoMap; |
| 140 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 141 | typedef GrContext INHERITED; |
| 142 | }; |
| 143 | |
Kevin Lubick | b5502b2 | 2018-03-12 10:17:06 -0400 | [diff] [blame] | 144 | sk_sp<GrContext> GrContextPriv::MakeDDL(const sk_sp<GrContextThreadSafeProxy>& proxy) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 145 | sk_sp<GrContext> context(new GrDDLContext(proxy)); |
| 146 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 147 | if (!context->init(proxy->priv().refCaps(), proxy->priv().fpFactoryCache())) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 148 | return nullptr; |
| 149 | } |
| 150 | return context; |
| 151 | } |