xref: /aosp_15_r20/external/skia/tools/gpu/BackendSurfaceFactory.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 "tools/gpu/BackendSurfaceFactory.h"
9 
10 #include "include/core/SkSurface.h"
11 #include "include/gpu/ganesh/GrDirectContext.h"
12 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
13 #include "src/gpu/ganesh/GrDirectContextPriv.h"
14 #include "src/gpu/ganesh/GrGpu.h"
15 #include "tools/gpu/ManagedBackendTexture.h"
16 
17 #if defined(SK_GRAPHITE)
18 #include "include/gpu/graphite/Surface.h"
19 #if defined(SK_DAWN)
20 #include "include/gpu/graphite/dawn/DawnTypes.h"
21 #include "src/gpu/graphite/dawn/DawnGraphiteTypesPriv.h"
22 
23 #include "webgpu/webgpu_cpp.h"  // NO_G3_REWRITE
24 #endif
25 #endif
26 
27 namespace sk_gpu_test {
28 
MakeBackendTextureSurface(GrDirectContext * dContext,const SkImageInfo & ii,GrSurfaceOrigin origin,int sampleCnt,skgpu::Mipmapped mipmapped,GrProtected isProtected,const SkSurfaceProps * props)29 sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext,
30                                            const SkImageInfo& ii,
31                                            GrSurfaceOrigin origin,
32                                            int sampleCnt,
33                                            skgpu::Mipmapped mipmapped,
34                                            GrProtected isProtected,
35                                            const SkSurfaceProps* props) {
36     if (ii.alphaType() == kUnpremul_SkAlphaType) {
37         return nullptr;
38     }
39     auto mbet = ManagedBackendTexture::MakeWithoutData(dContext,
40                                                        ii.width(),
41                                                        ii.height(),
42                                                        ii.colorType(),
43                                                        mipmapped,
44                                                        GrRenderable::kYes,
45                                                        isProtected);
46     if (!mbet) {
47         return nullptr;
48     }
49     return SkSurfaces::WrapBackendTexture(dContext,
50                                           mbet->texture(),
51                                           origin,
52                                           sampleCnt,
53                                           ii.colorType(),
54                                           ii.refColorSpace(),
55                                           props,
56                                           ManagedBackendTexture::ReleaseProc,
57                                           mbet->releaseContext());
58 }
59 
MakeBackendTextureSurface(GrDirectContext * dContext,SkISize dimensions,GrSurfaceOrigin origin,int sampleCnt,SkColorType colorType,sk_sp<SkColorSpace> colorSpace,skgpu::Mipmapped mipmapped,GrProtected isProtected,const SkSurfaceProps * props)60 sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext,
61                                            SkISize dimensions,
62                                            GrSurfaceOrigin origin,
63                                            int sampleCnt,
64                                            SkColorType colorType,
65                                            sk_sp<SkColorSpace> colorSpace,
66                                            skgpu::Mipmapped mipmapped,
67                                            GrProtected isProtected,
68                                            const SkSurfaceProps* props) {
69     auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace));
70     return MakeBackendTextureSurface(
71             dContext, ii, origin, sampleCnt, mipmapped, isProtected, props);
72 }
MakeBackendRenderTargetSurface(GrDirectContext * dContext,const SkImageInfo & ii,GrSurfaceOrigin origin,int sampleCnt,GrProtected isProtected,const SkSurfaceProps * props)73 sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext,
74                                                 const SkImageInfo& ii,
75                                                 GrSurfaceOrigin origin,
76                                                 int sampleCnt,
77                                                 GrProtected isProtected,
78                                                 const SkSurfaceProps* props) {
79     if (ii.alphaType() == kUnpremul_SkAlphaType || ii.alphaType() == kUnknown_SkAlphaType) {
80         return nullptr;
81     }
82     auto ct = SkColorTypeToGrColorType(ii.colorType());
83 
84     struct ReleaseContext {
85         sk_sp<GrDirectContext> fContext;
86         GrBackendRenderTarget fRenderTarget;
87     };
88 
89     auto bert = dContext->priv().getGpu()->createTestingOnlyBackendRenderTarget(
90             ii.dimensions(), ct, sampleCnt, isProtected);
91     auto rc = new ReleaseContext{sk_ref_sp(dContext), bert};
92     SkASSERT(!bert.isValid() || bert.sampleCnt() >= sampleCnt);
93 
94     auto proc = [](void* c) {
95         const auto* rc = static_cast<ReleaseContext*>(c);
96         if (auto gpu = rc->fContext->priv().getGpu(); gpu && rc->fRenderTarget.isValid()) {
97             gpu->deleteTestingOnlyBackendRenderTarget(rc->fRenderTarget);
98         }
99         delete rc;
100     };
101 
102     return SkSurfaces::WrapBackendRenderTarget(
103             dContext, bert, origin, ii.colorType(), ii.refColorSpace(), props, proc, rc);
104 }
105 
MakeBackendRenderTargetSurface(GrDirectContext * dContext,SkISize dimensions,GrSurfaceOrigin origin,int sampleCnt,SkColorType colorType,sk_sp<SkColorSpace> colorSpace,GrProtected isProtected,const SkSurfaceProps * props)106 sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext,
107                                                 SkISize dimensions,
108                                                 GrSurfaceOrigin origin,
109                                                 int sampleCnt,
110                                                 SkColorType colorType,
111                                                 sk_sp<SkColorSpace> colorSpace,
112                                                 GrProtected isProtected,
113                                                 const SkSurfaceProps* props) {
114     auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace));
115     return MakeBackendRenderTargetSurface(dContext, ii, origin, sampleCnt, isProtected, props);
116 }
117 
118 #ifdef SK_GRAPHITE
MakeBackendTextureSurface(skgpu::graphite::Recorder * recorder,const SkImageInfo & ii,skgpu::Mipmapped mipmapped,skgpu::Protected isProtected,const SkSurfaceProps * props)119 sk_sp<SkSurface> MakeBackendTextureSurface(skgpu::graphite::Recorder* recorder,
120                                            const SkImageInfo& ii,
121                                            skgpu::Mipmapped mipmapped,
122                                            skgpu::Protected isProtected,
123                                            const SkSurfaceProps* props) {
124     if (ii.alphaType() == kUnpremul_SkAlphaType) {
125         return nullptr;
126     }
127     sk_sp<ManagedGraphiteTexture> mbet = ManagedGraphiteTexture::MakeUnInit(recorder,
128                                                                             ii,
129                                                                             mipmapped,
130                                                                             skgpu::Renderable::kYes,
131                                                                             isProtected);
132     if (!mbet) {
133         return nullptr;
134     }
135     return SkSurfaces::WrapBackendTexture(recorder,
136                                           mbet->texture(),
137                                           ii.colorType(),
138                                           ii.refColorSpace(),
139                                           props,
140                                           ManagedGraphiteTexture::ReleaseProc,
141                                           mbet->releaseContext());
142 }
143 
144 #if defined(SK_DAWN)
MakeBackendTextureViewSurface(skgpu::graphite::Recorder * recorder,const SkImageInfo & ii,skgpu::Mipmapped mipmapped,skgpu::Protected isProtected,const SkSurfaceProps * props)145 sk_sp<SkSurface> MakeBackendTextureViewSurface(skgpu::graphite::Recorder* recorder,
146                                                const SkImageInfo& ii,
147                                                skgpu::Mipmapped mipmapped,
148                                                skgpu::Protected isProtected,
149                                                const SkSurfaceProps* props) {
150     if (recorder->backend() != skgpu::BackendApi::kDawn) {
151         return nullptr;
152     }
153 
154     if (ii.alphaType() == kUnpremul_SkAlphaType) {
155         return nullptr;
156     }
157 
158     auto mbet = ManagedGraphiteTexture::MakeUnInit(recorder,
159                                                    ii,
160                                                    mipmapped,
161                                                    skgpu::Renderable::kYes,
162                                                    isProtected);
163     if (!mbet) {
164         return nullptr;
165     }
166 
167     wgpu::Texture texture(skgpu::graphite::BackendTextures::GetDawnTexturePtr(mbet->texture()));
168     SkASSERT(texture);
169 
170     wgpu::TextureView view = texture.CreateView();
171     SkASSERT(view);
172 
173     skgpu::graphite::DawnTextureInfo textureInfo;
174     textureInfo.fAspect      = wgpu::TextureAspect::All;
175     textureInfo.fFormat      = texture.GetFormat();
176     textureInfo.fMipmapped   = mipmapped;
177     textureInfo.fSampleCount = texture.GetSampleCount();
178     textureInfo.fUsage       = texture.GetUsage();
179 
180     skgpu::graphite::BackendTexture betFromView =
181             skgpu::graphite::BackendTextures::MakeDawn(ii.dimensions(), textureInfo, view.Get());
182 
183     auto release = [](void* ctx) { static_cast<ManagedGraphiteTexture*>(ctx)->unref(); };
184 
185     return SkSurfaces::WrapBackendTexture(recorder,
186                                           betFromView,
187                                           ii.colorType(),
188                                           ii.refColorSpace(),
189                                           props,
190                                           release,
191                                           mbet.release());
192     return nullptr;
193 }
194 #endif // SK_DAWN
195 
196 #endif  // SK_GRAPHITE
197 
198 }  // namespace sk_gpu_test
199