xref: /aosp_15_r20/external/skia/tests/ProxyConversionTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 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 
8 // This is a GPU-backend specific test.
9 
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkSize.h"
12 #include "include/core/SkTypes.h"
13 #include "include/gpu/GpuTypes.h"
14 #include "include/gpu/ganesh/GrBackendSurface.h"
15 #include "include/gpu/ganesh/GrDirectContext.h"
16 #include "include/gpu/ganesh/GrTypes.h"
17 #include "include/private/gpu/ganesh/GrTypesPriv.h"
18 #include "src/gpu/RefCntedCallback.h"
19 #include "src/gpu/SkBackingFit.h"
20 #include "src/gpu/ganesh/GrCaps.h"
21 #include "src/gpu/ganesh/GrDirectContextPriv.h"
22 #include "src/gpu/ganesh/GrGpu.h"
23 #include "src/gpu/ganesh/GrProxyProvider.h"
24 #include "src/gpu/ganesh/GrRenderTarget.h"
25 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
26 #include "src/gpu/ganesh/GrSurfaceProxy.h"
27 #include "src/gpu/ganesh/GrTextureProxy.h"
28 #include "tests/CtsEnforcement.h"
29 #include "tests/Test.h"
30 
31 #include <utility>
32 
33 struct GrContextOptions;
34 
make_wrapped_rt(GrProxyProvider * provider,GrGpu * gpu,skiatest::Reporter * reporter,const SkISize & size,GrColorType colorType)35 static sk_sp<GrSurfaceProxy> make_wrapped_rt(GrProxyProvider* provider,
36                                              GrGpu* gpu,
37                                              skiatest::Reporter* reporter,
38                                              const SkISize& size,
39                                              GrColorType colorType) {
40     auto backendRT = gpu->createTestingOnlyBackendRenderTarget(size, colorType);
41     return provider->wrapBackendRenderTarget(backendRT, nullptr);
42 }
43 
clean_up_wrapped_rt(GrGpu * gpu,sk_sp<GrSurfaceProxy> proxy)44 void clean_up_wrapped_rt(GrGpu* gpu, sk_sp<GrSurfaceProxy> proxy) {
45     SkASSERT(proxy->unique());
46     SkASSERT(proxy->peekRenderTarget());
47     GrBackendRenderTarget rt = proxy->peekRenderTarget()->getBackendRenderTarget();
48     proxy.reset();
49     gpu->deleteTestingOnlyBackendRenderTarget(rt);
50 }
51 
make_offscreen_rt(GrProxyProvider * provider,SkISize dimensions,GrColorType colorType)52 static sk_sp<GrSurfaceProxy> make_offscreen_rt(GrProxyProvider* provider,
53                                                SkISize dimensions,
54                                                GrColorType colorType) {
55     return provider->testingOnly_createInstantiatedProxy(dimensions,
56                                                          colorType,
57                                                          GrRenderable::kYes,
58                                                          1,
59                                                          SkBackingFit::kExact,
60                                                          skgpu::Budgeted::kYes,
61                                                          GrProtected::kNo);
62 }
63 
make_texture(GrProxyProvider * provider,SkISize dimensions,GrColorType colorType,GrRenderable renderable)64 static sk_sp<GrSurfaceProxy> make_texture(GrProxyProvider* provider,
65                                           SkISize dimensions,
66                                           GrColorType colorType,
67                                           GrRenderable renderable) {
68     return provider->testingOnly_createInstantiatedProxy(dimensions,
69                                                          colorType,
70                                                          renderable,
71                                                          1,
72                                                          SkBackingFit::kExact,
73                                                          skgpu::Budgeted::kYes,
74                                                          GrProtected::kNo);
75 }
76 
77 // Test converting between RenderTargetProxies and TextureProxies for preinstantiated Proxies
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(PreinstantiatedProxyConversionTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)78 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(PreinstantiatedProxyConversionTest,
79                                        reporter,
80                                        ctxInfo,
81                                        CtsEnforcement::kApiLevel_T) {
82     auto context = ctxInfo.directContext();
83     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
84     GrGpu* gpu = context->priv().getGpu();
85 
86     static constexpr auto kSize = SkISize::Make(64, 64);
87     static constexpr auto kColorType = GrColorType::kRGBA_8888;
88 
89     {
90         // External on-screen render target.
91         sk_sp<GrSurfaceProxy> sProxy(
92                 make_wrapped_rt(proxyProvider, gpu, reporter, kSize, kColorType));
93         if (sProxy) {
94             // RenderTarget-only
95             GrRenderTargetProxy* rtProxy = sProxy->asRenderTargetProxy();
96             REPORTER_ASSERT(reporter, rtProxy);
97             REPORTER_ASSERT(reporter, !rtProxy->asTextureProxy());
98             REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy);
99             clean_up_wrapped_rt(gpu, std::move(sProxy));
100         }
101     }
102 
103     {
104         // Internal offscreen render target.
105         sk_sp<GrSurfaceProxy> sProxy(make_offscreen_rt(proxyProvider, kSize, kColorType));
106         if (sProxy) {
107             // Both RenderTarget and Texture
108             GrRenderTargetProxy* rtProxy = sProxy->asRenderTargetProxy();
109             REPORTER_ASSERT(reporter, rtProxy);
110             GrTextureProxy* tProxy = rtProxy->asTextureProxy();
111             REPORTER_ASSERT(reporter, tProxy);
112             REPORTER_ASSERT(reporter, tProxy->asRenderTargetProxy() == rtProxy);
113             REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy);
114         }
115     }
116 
117     {
118         // Internal offscreen render target - but through GrTextureProxy
119         sk_sp<GrSurfaceProxy> sProxy(
120                 make_texture(proxyProvider, kSize, kColorType, GrRenderable::kYes));
121         if (sProxy) {
122             // Both RenderTarget and Texture
123             GrTextureProxy* tProxy = sProxy->asTextureProxy();
124             REPORTER_ASSERT(reporter, tProxy);
125             GrRenderTargetProxy* rtProxy = tProxy->asRenderTargetProxy();
126             REPORTER_ASSERT(reporter, rtProxy);
127             REPORTER_ASSERT(reporter, rtProxy->asTextureProxy() == tProxy);
128             REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy);
129         }
130     }
131 
132     {
133         // force no-RT
134         sk_sp<GrSurfaceProxy> sProxy(
135                 make_texture(proxyProvider, kSize, kColorType, GrRenderable::kNo));
136         if (sProxy) {
137             // Texture-only
138             GrTextureProxy* tProxy = sProxy->asTextureProxy();
139             REPORTER_ASSERT(reporter, tProxy);
140             REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy);
141             REPORTER_ASSERT(reporter, !tProxy->asRenderTargetProxy());
142         }
143     }
144 }
145 
146 // Test converting between RenderTargetProxies and TextureProxies for deferred
147 // Proxies
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(DefferredProxyConversionTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)148 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(DefferredProxyConversionTest,
149                                        reporter,
150                                        ctxInfo,
151                                        CtsEnforcement::kApiLevel_T) {
152     auto context = ctxInfo.directContext();
153     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
154     const GrCaps* caps = context->priv().caps();
155 
156     static constexpr SkISize kDims = {64, 64};
157 
158     const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
159                                                                  GrRenderable::kYes);
160     {
161         sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format,
162                                                                  kDims,
163                                                                  GrRenderable::kYes,
164                                                                  1,
165                                                                  skgpu::Mipmapped::kNo,
166                                                                  SkBackingFit::kApprox,
167                                                                  skgpu::Budgeted::kYes,
168                                                                  GrProtected::kNo,
169                                                                  /*label=*/{});
170 
171         // Both RenderTarget and Texture
172         GrRenderTargetProxy* rtProxy = proxy->asRenderTargetProxy();
173         REPORTER_ASSERT(reporter, rtProxy);
174         GrTextureProxy* tProxy = rtProxy->asTextureProxy();
175         REPORTER_ASSERT(reporter, tProxy);
176         REPORTER_ASSERT(reporter, tProxy->asRenderTargetProxy() == rtProxy);
177         REPORTER_ASSERT(reporter, rtProxy->asRenderTargetProxy() == rtProxy);
178     }
179 
180     {
181         sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format,
182                                                                  kDims,
183                                                                  GrRenderable::kYes,
184                                                                  1,
185                                                                  skgpu::Mipmapped::kNo,
186                                                                  SkBackingFit::kApprox,
187                                                                  skgpu::Budgeted::kYes,
188                                                                  GrProtected::kNo,
189                                                                  /*label=*/{});
190 
191         // Both RenderTarget and Texture - but via GrTextureProxy
192         GrTextureProxy* tProxy = proxy->asTextureProxy();
193         REPORTER_ASSERT(reporter, tProxy);
194         GrRenderTargetProxy* rtProxy = tProxy->asRenderTargetProxy();
195         REPORTER_ASSERT(reporter, rtProxy);
196         REPORTER_ASSERT(reporter, rtProxy->asTextureProxy() == tProxy);
197         REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy);
198     }
199 
200     {
201         sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format,
202                                                                  kDims,
203                                                                  GrRenderable::kNo,
204                                                                  1,
205                                                                  skgpu::Mipmapped::kNo,
206                                                                  SkBackingFit::kApprox,
207                                                                  skgpu::Budgeted::kYes,
208                                                                  GrProtected::kNo,
209                                                                  /*label=*/{});
210         // Texture-only
211         GrTextureProxy* tProxy = proxy->asTextureProxy();
212         REPORTER_ASSERT(reporter, tProxy);
213         REPORTER_ASSERT(reporter, tProxy->asTextureProxy() == tProxy);
214         REPORTER_ASSERT(reporter, !tProxy->asRenderTargetProxy());
215     }
216 }
217