blob: 2a5ecb41624386083a06542cbb7c8223f636cdfe [file] [log] [blame]
robertphillips1125a032016-11-16 11:17:17 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
robertphillips1125a032016-11-16 11:17:17 -080011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrPendingIOResource.h"
15#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSurfaceProxy.h"
19#include "src/gpu/GrTextureProxy.h"
robertphillips1125a032016-11-16 11:17:17 -080020
robertphillips1125a032016-11-16 11:17:17 -080021int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const {
22 if (fTarget) {
23 return fTarget->fRefCnt;
24 }
25
Robert Phillips715d08c2018-07-18 13:56:48 -040026 return -1; // no backing GrSurface
robertphillips1125a032016-11-16 11:17:17 -080027}
28
Robert Phillips7928e762017-02-28 16:30:28 -050029static const int kWidthHeight = 128;
30
robertphillips1125a032016-11-16 11:17:17 -080031static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040032 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080033 int32_t expectedProxyRefs,
Robert Phillips3d4cac52019-06-11 08:08:08 -040034 int32_t expectedBackingRefs) {
35 int32_t actualProxyRefs = proxy->priv().getProxyRefCnt();
36 int32_t actualBackingRefs = proxy->getBackingRefCnt_TestOnly();
robertphillips1125a032016-11-16 11:17:17 -080037
Robert Phillips3d4cac52019-06-11 08:08:08 -040038 SkASSERT(actualProxyRefs == expectedProxyRefs);
39 SkASSERT(actualBackingRefs == expectedBackingRefs);
40
41 REPORTER_ASSERT(reporter, actualProxyRefs == expectedProxyRefs);
42 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
robertphillips1125a032016-11-16 11:17:17 -080043}
44
Greg Daniel4065d452018-11-16 15:43:41 -050045static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080046 GrSurfaceDesc desc;
47 desc.fFlags = kRenderTarget_GrSurfaceFlag;
48 desc.fWidth = kWidthHeight;
49 desc.fHeight = kWidthHeight;
50 desc.fConfig = kRGBA_8888_GrPixelConfig;
51
Greg Daniel4065d452018-11-16 15:43:41 -050052 const GrBackendFormat format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
53 return proxyProvider->createProxy(format, desc, kBottomLeft_GrSurfaceOrigin,
Robert Phillips10d17212019-04-24 14:09:10 -040054 SkBackingFit::kApprox, SkBudgeted::kYes);
robertphillips1125a032016-11-16 11:17:17 -080055}
56
Greg Daniel4065d452018-11-16 15:43:41 -050057static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080058 GrSurfaceDesc desc;
59 desc.fFlags = kRenderTarget_GrSurfaceFlag;
60 desc.fWidth = kWidthHeight;
61 desc.fHeight = kWidthHeight;
62 desc.fConfig = kRGBA_8888_GrPixelConfig;
63
Chris Daltond004e0b2018-09-27 09:28:03 -060064 return proxyProvider->testingOnly_createInstantiatedProxy(
65 desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo);
robertphillips1125a032016-11-16 11:17:17 -080066}
67
68DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050069 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
70 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
71 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
robertphillips1125a032016-11-16 11:17:17 -080072
73 for (auto make : { make_deferred, make_wrapped }) {
Robert Phillips3d4cac52019-06-11 08:08:08 -040074 // Pending IO ref
robertphillips1125a032016-11-16 11:17:17 -080075 {
Greg Daniel4065d452018-11-16 15:43:41 -050076 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040077 if (proxy.get()) {
Robert Phillips3d4cac52019-06-11 08:08:08 -040078 GrProxyPendingIO pendingIO(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -080079
Robert Phillips3d4cac52019-06-11 08:08:08 -040080 int backingRefs = proxy->isWrapped_ForTesting() ? 2 : -1;
robertphillips1125a032016-11-16 11:17:17 -080081
Robert Phillips3d4cac52019-06-11 08:08:08 -040082 check_refs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080083
Robert Phillips0bd24dc2018-01-16 08:06:32 -050084 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080085
Jim Van Verth311cc6b2017-09-20 17:51:59 -040086 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips3d4cac52019-06-11 08:08:08 -040087 check_refs(reporter, proxy.get(), 2, 2);
Jim Van Verth311cc6b2017-09-20 17:51:59 -040088 }
Robert Phillips3d4cac52019-06-11 08:08:08 -040089 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -080090 }
91
92 // Multiple normal refs
93 {
Greg Daniel4065d452018-11-16 15:43:41 -050094 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040095 if (proxy.get()) {
96 proxy->ref();
97 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -080098
Robert Phillips715d08c2018-07-18 13:56:48 -040099 int backingRefs = proxy->isWrapped_ForTesting() ? 3 : -1;
100
Robert Phillips3d4cac52019-06-11 08:08:08 -0400101 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -0800102
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500103 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800104
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400105 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips3d4cac52019-06-11 08:08:08 -0400106 check_refs(reporter, proxy.get(), 3, 3);
robertphillips1125a032016-11-16 11:17:17 -0800107
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400108 proxy->unref();
109 proxy->unref();
110 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400111 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800112 }
113
114 // Continue using (reffing) proxy after instantiation
115 {
Greg Daniel4065d452018-11-16 15:43:41 -0500116 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400117 if (proxy.get()) {
118 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800119
Robert Phillips3d4cac52019-06-11 08:08:08 -0400120 GrProxyPendingIO pendingIO(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800121
Robert Phillips3d4cac52019-06-11 08:08:08 -0400122 int backingRefs = proxy->isWrapped_ForTesting() ? 3 : -1;
robertphillips1125a032016-11-16 11:17:17 -0800123
Robert Phillips3d4cac52019-06-11 08:08:08 -0400124 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -0800125
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500126 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800127
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400128 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips3d4cac52019-06-11 08:08:08 -0400129 check_refs(reporter, proxy.get(), 3, 3);
robertphillips1125a032016-11-16 11:17:17 -0800130
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400131 proxy->unref();
Robert Phillips3d4cac52019-06-11 08:08:08 -0400132 check_refs(reporter, proxy.get(), 2, 2);
robertphillips1125a032016-11-16 11:17:17 -0800133
Robert Phillips3d4cac52019-06-11 08:08:08 -0400134 GrProxyPendingIO secondPendingIO(proxy.get());
135 check_refs(reporter, proxy.get(), 3, 3);
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400136 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400137 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800138 }
139 }
140}