xref: /aosp_15_r20/external/skia/src/gpu/ganesh/d3d/GrD3DAttachment.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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 
8 #include "src/gpu/ganesh/d3d/GrD3DAttachment.h"
9 
10 #include "src/gpu/ganesh/d3d/GrD3DGpu.h"
11 
GrD3DAttachment(GrD3DGpu * gpu,SkISize dimensions,UsageFlags supportedUsages,DXGI_FORMAT format,const D3D12_RESOURCE_DESC & desc,const GrD3DTextureResourceInfo & info,sk_sp<GrD3DResourceState> state,const GrD3DDescriptorHeap::CPUHandle & view,std::string_view label)12 GrD3DAttachment::GrD3DAttachment(GrD3DGpu* gpu,
13                                  SkISize dimensions,
14                                  UsageFlags supportedUsages,
15                                  DXGI_FORMAT format,
16                                  const D3D12_RESOURCE_DESC& desc,
17                                  const GrD3DTextureResourceInfo& info,
18                                  sk_sp<GrD3DResourceState> state,
19                                  const GrD3DDescriptorHeap::CPUHandle& view,
20                                  std::string_view label)
21         : GrAttachment(gpu,
22                        dimensions,
23                        supportedUsages,
24                        desc.SampleDesc.Count,
25                        skgpu::Mipmapped::kNo,
26                        GrProtected::kNo,
27                        label)
28         , GrD3DTextureResource(info, state)
29         , fView(view)
30         , fFormat(format) {
31     this->registerWithCache(skgpu::Budgeted::kYes);
32 }
33 
MakeStencil(GrD3DGpu * gpu,SkISize dimensions,int sampleCnt,DXGI_FORMAT format)34 sk_sp<GrD3DAttachment> GrD3DAttachment::MakeStencil(GrD3DGpu* gpu,
35                                                     SkISize dimensions,
36                                                     int sampleCnt,
37                                                     DXGI_FORMAT format) {
38     D3D12_RESOURCE_DESC resourceDesc = {};
39     resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
40     resourceDesc.Alignment = 0;  // default alignment
41     resourceDesc.Width = dimensions.width();
42     resourceDesc.Height = dimensions.height();
43     resourceDesc.DepthOrArraySize = 1;
44     resourceDesc.MipLevels = 1;
45     resourceDesc.Format = format;
46     resourceDesc.SampleDesc.Count = sampleCnt;
47     resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
48     resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;  // use driver-selected swizzle
49     resourceDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
50 
51     D3D12_CLEAR_VALUE clearValue = {};
52     clearValue.Format = format;
53     clearValue.DepthStencil.Depth = 0;
54     clearValue.DepthStencil.Stencil = 0;
55 
56     GrD3DTextureResourceInfo info;
57     if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, resourceDesc,
58                                                        D3D12_RESOURCE_STATE_DEPTH_WRITE,
59                                                        GrProtected::kNo, &clearValue, &info)) {
60         return nullptr;
61     }
62 
63     GrD3DDescriptorHeap::CPUHandle view =
64             gpu->resourceProvider().createDepthStencilView(info.fResource.get());
65 
66     sk_sp<GrD3DResourceState> state(new GrD3DResourceState(info.fResourceState));
67     return sk_sp<GrD3DAttachment>(new GrD3DAttachment(gpu, dimensions,
68                                                       UsageFlags::kStencilAttachment,
69                                                       format, resourceDesc, info,
70                                                       std::move(state),
71                                                       view,
72                                                       /*label=*/"D3DAttachment_MakeStencil"));
73 }
74 
onRelease()75 void GrD3DAttachment::onRelease() {
76     GrD3DGpu* gpu = this->getD3DGpu();
77     this->releaseResource(gpu);
78 
79     GrAttachment::onRelease();
80 }
81 
onAbandon()82 void GrD3DAttachment::onAbandon() {
83     GrD3DGpu* gpu = this->getD3DGpu();
84     this->releaseResource(gpu);
85 
86     GrAttachment::onAbandon();
87 }
88 
getD3DGpu() const89 GrD3DGpu* GrD3DAttachment::getD3DGpu() const {
90     SkASSERT(!this->wasDestroyed());
91     return static_cast<GrD3DGpu*>(this->getGpu());
92 }
93 
onSetLabel()94 void GrD3DAttachment::onSetLabel() {
95     SkASSERT(this->d3dResource());
96     if (!this->getLabel().empty()) {
97         const std::wstring label = L"_Skia_" + GrD3DMultiByteToWide(this->getLabel());
98         this->d3dResource()->SetName(label.c_str());
99     }
100 }
101