xref: /aosp_15_r20/external/skia/tests/ProxyRefTest.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/SkTypes.h"
12 #include "include/gpu/GpuTypes.h"
13 #include "include/gpu/ganesh/GrBackendSurface.h"
14 #include "include/gpu/ganesh/GrDirectContext.h"
15 #include "include/gpu/ganesh/GrRecordingContext.h"
16 #include "include/gpu/ganesh/GrTypes.h"
17 #include "include/private/gpu/ganesh/GrTypesPriv.h"
18 #include "src/gpu/SkBackingFit.h"
19 #include "src/gpu/ganesh/GrCaps.h"
20 #include "src/gpu/ganesh/GrDirectContextPriv.h"
21 #include "src/gpu/ganesh/GrProxyProvider.h"
22 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
23 #include "src/gpu/ganesh/GrTextureProxy.h"
24 #include "tests/CtsEnforcement.h"
25 #include "tests/Test.h"
26 #include "tests/TestUtils.h"
27 
28 #include <initializer_list>
29 
30 class GrResourceProvider;
31 struct GrContextOptions;
32 
33 static const int kWidthHeight = 128;
34 
make_deferred(GrRecordingContext * rContext)35 static sk_sp<GrTextureProxy> make_deferred(GrRecordingContext* rContext) {
36     GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
37     const GrCaps* caps = rContext->priv().caps();
38 
39     const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
40                                                                  GrRenderable::kYes);
41     return proxyProvider->createProxy(format,
42                                       {kWidthHeight, kWidthHeight},
43                                       GrRenderable::kYes,
44                                       1,
45                                       skgpu::Mipmapped::kNo,
46                                       SkBackingFit::kApprox,
47                                       skgpu::Budgeted::kYes,
48                                       GrProtected::kNo,
49                                       /*label=*/"ProxyRefTest");
50 }
51 
make_wrapped(GrRecordingContext * rContext)52 static sk_sp<GrTextureProxy> make_wrapped(GrRecordingContext* rContext) {
53     GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
54 
55     return proxyProvider->testingOnly_createInstantiatedProxy({kWidthHeight, kWidthHeight},
56                                                               GrColorType::kRGBA_8888,
57                                                               GrRenderable::kYes,
58                                                               1,
59                                                               SkBackingFit::kExact,
60                                                               skgpu::Budgeted::kNo,
61                                                               GrProtected::kNo);
62 }
63 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ProxyRefTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)64 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ProxyRefTest,
65                                        reporter,
66                                        ctxInfo,
67                                        CtsEnforcement::kApiLevel_T) {
68     auto dContext = ctxInfo.directContext();
69     GrResourceProvider* resourceProvider = dContext->priv().resourceProvider();
70 
71     for (auto make : { make_deferred, make_wrapped }) {
72         // An extra ref
73         {
74             sk_sp<GrTextureProxy> proxy((*make)(dContext));
75             if (proxy) {
76                 sk_sp<GrTextureProxy> extraRef(proxy);  // NOLINT(performance-unnecessary-copy-initialization)
77 
78                 int backingRefs = proxy->isInstantiated() ? 1 : -1;
79 
80                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, backingRefs);
81 
82                 proxy->instantiate(resourceProvider);
83 
84                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, 1);
85             }
86             CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
87         }
88 
89         // Multiple normal refs
90         {
91             sk_sp<GrTextureProxy> proxy((*make)(dContext));
92             if (proxy) {
93                 proxy->ref();
94                 proxy->ref();
95 
96                 int backingRefs = proxy->isInstantiated() ? 1 : -1;
97 
98                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, backingRefs);
99 
100                 proxy->instantiate(resourceProvider);
101 
102                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, 1);
103 
104                 proxy->unref();
105                 proxy->unref();
106             }
107             CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
108         }
109 
110         // Continue using (reffing) proxy after instantiation
111         {
112             sk_sp<GrTextureProxy> proxy((*make)(dContext));
113             if (proxy) {
114                 sk_sp<GrTextureProxy> firstExtraRef(proxy);  // NOLINT(performance-unnecessary-copy-initialization)
115 
116                 int backingRefs = proxy->isInstantiated() ? 1 : -1;
117 
118                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, backingRefs);
119 
120                 proxy->instantiate(resourceProvider);
121 
122                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, 1);
123 
124                 sk_sp<GrTextureProxy> secondExtraRef(proxy);  // NOLINT(performance-unnecessary-copy-initialization)
125                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, 1);
126             }
127             CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
128         }
129     }
130 }
131