blob: bffb68988f360237a676ce5af2520a1d3818fdcd [file] [log] [blame]
Jim Van Verth8b932b12020-03-24 14:58:03 -04001/*
2 * Copyright 2020 Google LLC
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
Jim Van Verth6ec56882020-03-26 11:47:26 -04008#ifndef GrD3DTextureResource_DEFINED
9#define GrD3DTextureResource_DEFINED
Jim Van Verth8b932b12020-03-24 14:58:03 -040010
11#include "include/core/SkTypes.h"
12#include "include/gpu/GrBackendSurface.h"
Jim Van Verth9aa9a682020-04-01 10:13:48 -040013#include "include/gpu/d3d/GrD3DTypes.h"
Jim Van Verth8b932b12020-03-24 14:58:03 -040014#include "include/private/GrTypesPriv.h"
15#include "src/gpu/GrManagedResource.h"
Jim Van Verth8b932b12020-03-24 14:58:03 -040016#include "src/gpu/d3d/GrD3DResourceState.h"
17
18class GrD3DGpu;
19
Jim Van Verth6ec56882020-03-26 11:47:26 -040020class GrD3DTextureResource : SkNoncopyable {
Jim Van Verth8b932b12020-03-24 14:58:03 -040021private:
22 class Resource;
23
24public:
Greg Danielc9624d52020-04-13 15:36:31 -040025 static const unsigned int kDefaultQualityLevel = 0;
26
Jim Van Verthaa90dad2020-03-30 15:00:39 -040027 GrD3DTextureResource(const GrD3DTextureResourceInfo& info, sk_sp<GrD3DResourceState> state)
Jim Van Verth8b932b12020-03-24 14:58:03 -040028 : fInfo(info)
29 , fState(std::move(state))
Jim Van Verth6ec56882020-03-26 11:47:26 -040030 , fResource(new Resource(fInfo.fResource)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -040031 // gr_cp will implicitly ref the ID3D12Resource for us, so we don't need to worry about
32 // whether it's borrowed or not
Jim Van Verth8b932b12020-03-24 14:58:03 -040033 }
Jim Van Verth6ec56882020-03-26 11:47:26 -040034 virtual ~GrD3DTextureResource();
Jim Van Verth8b932b12020-03-24 14:58:03 -040035
Jim Van Verthaa90dad2020-03-30 15:00:39 -040036 ID3D12Resource* d3dResource() const {
Jim Van Verth8b932b12020-03-24 14:58:03 -040037 SkASSERT(fResource);
Jim Van Verth9aa9a682020-04-01 10:13:48 -040038 return fInfo.fResource.get();
Jim Van Verth8b932b12020-03-24 14:58:03 -040039 }
40 DXGI_FORMAT dxgiFormat() const { return fInfo.fFormat; }
41 GrBackendFormat getBackendFormat() const {
42 return GrBackendFormat::MakeDxgi(this->dxgiFormat());
43 }
Greg Daniela5a6b322020-04-23 12:52:27 -040044 sk_sp<Resource> resource() const {
Jim Van Verthaa90dad2020-03-30 15:00:39 -040045 SkASSERT(fResource);
Greg Daniela5a6b322020-04-23 12:52:27 -040046 return fResource;
Jim Van Verthaa90dad2020-03-30 15:00:39 -040047 }
Jim Van Verth8b932b12020-03-24 14:58:03 -040048 uint32_t mipLevels() const { return fInfo.fLevelCount; }
Jim Van Verth8b932b12020-03-24 14:58:03 -040049
50 sk_sp<GrD3DResourceState> grD3DResourceState() const { return fState; }
51
52 D3D12_RESOURCE_STATES currentState() const {
53 return fState->getResourceState();
54 }
55
56 void setResourceState(const GrD3DGpu* gpu, D3D12_RESOURCE_STATES newResourceState);
57
Greg Danielc9624d52020-04-13 15:36:31 -040058 unsigned int sampleQualityLevel() const { return fInfo.fSampleQualityLevel; }
59
Jim Van Verth8b932b12020-03-24 14:58:03 -040060 // This simply updates our tracking of the resourceState and does not actually do any gpu work.
61 // Externally, primarily used for implicit changes in resourceState due to certain GPU commands.
Jim Van Verthc632aa62020-04-17 16:58:20 -040062 void updateResourceState(D3D12_RESOURCE_STATES newState) {
Jim Van Verth8b932b12020-03-24 14:58:03 -040063 SkASSERT(fResource);
64 fState->setResourceState(newState);
Jim Van Verth8b932b12020-03-24 14:58:03 -040065 }
66
Jim Van Verth2b9f53e2020-04-14 11:47:34 -040067 static bool InitTextureResourceInfo(GrD3DGpu* gpu, const D3D12_RESOURCE_DESC& desc,
68 D3D12_RESOURCE_STATES initialState, GrProtected,
Jim Van Verth280d4f72020-05-04 10:04:24 -040069 D3D12_CLEAR_VALUE*, GrD3DTextureResourceInfo*);
Jim Van Verth8b932b12020-03-24 14:58:03 -040070
71 void setResourceRelease(sk_sp<GrRefCntedCallback> releaseHelper);
72
73protected:
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040074 void releaseResource(GrD3DGpu* gpu);
Jim Van Verth8b932b12020-03-24 14:58:03 -040075
Greg Daniela5a6b322020-04-23 12:52:27 -040076 void addResourceIdleProc(GrTexture* owningTexture, sk_sp<GrRefCntedCallback> idleProc) {
77 if (fResource) {
78 fResource->addIdleProc(owningTexture, std::move(idleProc));
79 }
80 }
81 void resetResourceIdleProcs() {
82 SkASSERT(fResource);
83 fResource->resetIdleProcs();
84 }
85 bool resourceIsQueuedForWorkOnGpu() const {
86 SkASSERT(fResource);
87 return fResource->isQueuedForWorkOnGpu();
88 }
89 int resourceIdleProcCnt() const {
90 SkASSERT(fResource);
91 return fResource->idleProcCnt();
92 }
93 sk_sp<GrRefCntedCallback> resourceIdleProc(int i) const {
94 SkASSERT(fResource);
95 return fResource->idleProc(i);
96 }
97
Jim Van Verth6ec56882020-03-26 11:47:26 -040098 GrD3DTextureResourceInfo fInfo;
Jim Van Verth8b932b12020-03-24 14:58:03 -040099 sk_sp<GrD3DResourceState> fState;
Jim Van Verth8b932b12020-03-24 14:58:03 -0400100
101private:
102 class Resource : public GrTextureResource {
103 public:
104 explicit Resource()
105 : fResource(nullptr) {
106 }
107
Jim Van Verth9aa9a682020-04-01 10:13:48 -0400108 Resource(const gr_cp<ID3D12Resource>& textureResource)
Jim Van Verth8b932b12020-03-24 14:58:03 -0400109 : fResource(textureResource) {
Jim Van Verth8b932b12020-03-24 14:58:03 -0400110 }
111
112 ~Resource() override {}
113
114#ifdef SK_TRACE_MANAGED_RESOURCES
115 void dumpInfo() const override {
Jim Van Verth9aa9a682020-04-01 10:13:48 -0400116 SkDebugf("GrD3DTextureResource: %d (%d refs)\n", fResource.get(), this->getRefCnt());
Jim Van Verth8b932b12020-03-24 14:58:03 -0400117 }
118#endif
119
120 private:
121 void freeGPUData() const override;
122
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400123 mutable gr_cp<ID3D12Resource> fResource;
Jim Van Verth8b932b12020-03-24 14:58:03 -0400124
125 typedef GrTextureResource INHERITED;
126 };
127
128 sk_sp<Resource> fResource;
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400129
130 friend class GrD3DRenderTarget;
Jim Van Verth8b932b12020-03-24 14:58:03 -0400131};
132
133#endif