• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tests/Test.h"
11 
12 #include "include/gpu/GrTexture.h"
13 #include "src/gpu/GrContextPriv.h"
14 #include "src/gpu/GrPendingIOResource.h"
15 #include "src/gpu/GrProxyProvider.h"
16 #include "src/gpu/GrRenderTargetProxy.h"
17 #include "src/gpu/GrResourceProvider.h"
18 #include "src/gpu/GrSurfaceProxy.h"
19 #include "src/gpu/GrTextureProxy.h"
20 
21 static const int kWidthHeight = 128;
22 
check_refs(skiatest::Reporter * reporter,GrTextureProxy * proxy,int32_t expectedProxyRefs,int32_t expectedBackingRefs)23 static void check_refs(skiatest::Reporter* reporter,
24                        GrTextureProxy* proxy,
25                        int32_t expectedProxyRefs,
26                        int32_t expectedBackingRefs) {
27     int32_t actualProxyRefs = proxy->priv().getProxyRefCnt();
28     int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
29 
30     SkASSERT(actualProxyRefs == expectedProxyRefs);
31     SkASSERT(actualBackingRefs == expectedBackingRefs);
32 
33     REPORTER_ASSERT(reporter, actualProxyRefs == expectedProxyRefs);
34     REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
35 }
36 
make_deferred(GrContext * context)37 static sk_sp<GrTextureProxy> make_deferred(GrContext* context) {
38     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
39     const GrCaps* caps = context->priv().caps();
40 
41     GrSurfaceDesc desc;
42     desc.fWidth = kWidthHeight;
43     desc.fHeight = kWidthHeight;
44     desc.fConfig = kRGBA_8888_GrPixelConfig;
45 
46     const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
47                                                                  GrRenderable::kYes);
48     return proxyProvider->createProxy(format, desc, GrRenderable::kYes, 1,
49                                       kBottomLeft_GrSurfaceOrigin, SkBackingFit::kApprox,
50                                       SkBudgeted::kYes, GrProtected::kNo);
51 }
52 
make_wrapped(GrContext * context)53 static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
54     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
55 
56     return proxyProvider->testingOnly_createInstantiatedProxy(
57             {kWidthHeight, kWidthHeight}, GrColorType::kRGBA_8888, GrRenderable::kYes, 1,
58             kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
59 }
60 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest,reporter,ctxInfo)61 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
62     GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
63 
64     for (auto make : { make_deferred, make_wrapped }) {
65         // Pending IO ref
66         {
67             sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
68             if (proxy.get()) {
69                 GrProxyPendingIO pendingIO(proxy.get());
70 
71                 int backingRefs = proxy->isInstantiated() ? 1 : -1;
72 
73                 check_refs(reporter, proxy.get(), 2, backingRefs);
74 
75                 proxy->instantiate(resourceProvider);
76 
77                 check_refs(reporter, proxy.get(), 2, 1);
78             }
79             check_refs(reporter, proxy.get(), 1, 1);
80         }
81 
82         // Multiple normal refs
83         {
84             sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
85             if (proxy.get()) {
86                 proxy->ref();
87                 proxy->ref();
88 
89                 int backingRefs = proxy->isInstantiated() ? 1 : -1;
90 
91                 check_refs(reporter, proxy.get(), 3, backingRefs);
92 
93                 proxy->instantiate(resourceProvider);
94 
95                 check_refs(reporter, proxy.get(), 3, 1);
96 
97                 proxy->unref();
98                 proxy->unref();
99             }
100             check_refs(reporter, proxy.get(), 1, 1);
101         }
102 
103         // Continue using (reffing) proxy after instantiation
104         {
105             sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
106             if (proxy.get()) {
107                 proxy->ref();
108 
109                 GrProxyPendingIO pendingIO(proxy.get());
110 
111                 int backingRefs = proxy->isInstantiated() ? 1 : -1;
112 
113                 check_refs(reporter, proxy.get(), 3, backingRefs);
114 
115                 proxy->instantiate(resourceProvider);
116 
117                 check_refs(reporter, proxy.get(), 3, 1);
118 
119                 proxy->unref();
120                 check_refs(reporter, proxy.get(), 2, 1);
121 
122                 GrProxyPendingIO secondPendingIO(proxy.get());
123                 check_refs(reporter, proxy.get(), 3, 1);
124             }
125             check_refs(reporter, proxy.get(), 1, 1);
126         }
127     }
128 }
129