1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci// This is a GPU-backend specific test.
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "tests/Test.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
13cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h"
14cb93a386Sopenharmony_ci#include "src/gpu/GrProxyProvider.h"
15cb93a386Sopenharmony_ci#include "src/gpu/GrRecordingContextPriv.h"
16cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTargetProxy.h"
17cb93a386Sopenharmony_ci#include "src/gpu/GrResourceProvider.h"
18cb93a386Sopenharmony_ci#include "src/gpu/GrSurfaceProxy.h"
19cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h"
20cb93a386Sopenharmony_ci#include "src/gpu/GrTextureProxy.h"
21cb93a386Sopenharmony_ci#include "tests/TestUtils.h"
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_cistatic const int kWidthHeight = 128;
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_cistatic sk_sp<GrTextureProxy> make_deferred(GrRecordingContext* rContext) {
26cb93a386Sopenharmony_ci    GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
27cb93a386Sopenharmony_ci    const GrCaps* caps = rContext->priv().caps();
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_ci    const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
30cb93a386Sopenharmony_ci                                                                 GrRenderable::kYes);
31cb93a386Sopenharmony_ci    return proxyProvider->createProxy(format, {kWidthHeight, kWidthHeight}, GrRenderable::kYes, 1,
32cb93a386Sopenharmony_ci                                      GrMipmapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
33cb93a386Sopenharmony_ci                                      GrProtected::kNo);
34cb93a386Sopenharmony_ci}
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_cistatic sk_sp<GrTextureProxy> make_wrapped(GrRecordingContext* rContext) {
37cb93a386Sopenharmony_ci    GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci    return proxyProvider->testingOnly_createInstantiatedProxy(
40cb93a386Sopenharmony_ci            {kWidthHeight, kWidthHeight}, GrColorType::kRGBA_8888, GrRenderable::kYes, 1,
41cb93a386Sopenharmony_ci            SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
42cb93a386Sopenharmony_ci}
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
45cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
46cb93a386Sopenharmony_ci    GrResourceProvider* resourceProvider = dContext->priv().resourceProvider();
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    for (auto make : { make_deferred, make_wrapped }) {
49cb93a386Sopenharmony_ci        // An extra ref
50cb93a386Sopenharmony_ci        {
51cb93a386Sopenharmony_ci            sk_sp<GrTextureProxy> proxy((*make)(dContext));
52cb93a386Sopenharmony_ci            if (proxy) {
53cb93a386Sopenharmony_ci                sk_sp<GrTextureProxy> extraRef(proxy);  // NOLINT(performance-unnecessary-copy-initialization)
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci                int backingRefs = proxy->isInstantiated() ? 1 : -1;
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci                CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, backingRefs);
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_ci                proxy->instantiate(resourceProvider);
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ci                CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, 1);
62cb93a386Sopenharmony_ci            }
63cb93a386Sopenharmony_ci            CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
64cb93a386Sopenharmony_ci        }
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ci        // Multiple normal refs
67cb93a386Sopenharmony_ci        {
68cb93a386Sopenharmony_ci            sk_sp<GrTextureProxy> proxy((*make)(dContext));
69cb93a386Sopenharmony_ci            if (proxy) {
70cb93a386Sopenharmony_ci                proxy->ref();
71cb93a386Sopenharmony_ci                proxy->ref();
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci                int backingRefs = proxy->isInstantiated() ? 1 : -1;
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci                CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, backingRefs);
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci                proxy->instantiate(resourceProvider);
78cb93a386Sopenharmony_ci
79cb93a386Sopenharmony_ci                CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, 1);
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci                proxy->unref();
82cb93a386Sopenharmony_ci                proxy->unref();
83cb93a386Sopenharmony_ci            }
84cb93a386Sopenharmony_ci            CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
85cb93a386Sopenharmony_ci        }
86cb93a386Sopenharmony_ci
87cb93a386Sopenharmony_ci        // Continue using (reffing) proxy after instantiation
88cb93a386Sopenharmony_ci        {
89cb93a386Sopenharmony_ci            sk_sp<GrTextureProxy> proxy((*make)(dContext));
90cb93a386Sopenharmony_ci            if (proxy) {
91cb93a386Sopenharmony_ci                sk_sp<GrTextureProxy> firstExtraRef(proxy);  // NOLINT(performance-unnecessary-copy-initialization)
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci                int backingRefs = proxy->isInstantiated() ? 1 : -1;
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_ci                CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, backingRefs);
96cb93a386Sopenharmony_ci
97cb93a386Sopenharmony_ci                proxy->instantiate(resourceProvider);
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci                CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, 1);
100cb93a386Sopenharmony_ci
101cb93a386Sopenharmony_ci                sk_sp<GrTextureProxy> secondExtraRef(proxy);  // NOLINT(performance-unnecessary-copy-initialization)
102cb93a386Sopenharmony_ci                CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, 1);
103cb93a386Sopenharmony_ci            }
104cb93a386Sopenharmony_ci            CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
105cb93a386Sopenharmony_ci        }
106cb93a386Sopenharmony_ci    }
107cb93a386Sopenharmony_ci}
108