blob: 16f4f42632cd6486b1f3115cd6ede8b58f8b16f7 [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
10#include "Test.h"
11
12#if SK_SUPPORT_GPU
Robert Phillips7ee385e2017-03-30 08:02:11 -040013#include "GrContextPriv.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040014#include "GrGpuResourceRef.h"
robertphillips1125a032016-11-16 11:17:17 -080015#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050016#include "GrResourceProvider.h"
17#include "GrSurfaceProxy.h"
Robert Phillips646e4292017-06-13 12:44:56 -040018#include "GrTexture.h"
Brian Osman32342f02017-03-04 08:12:46 -050019#include "GrTextureProxy.h"
robertphillips1125a032016-11-16 11:17:17 -080020
robertphillips1125a032016-11-16 11:17:17 -080021int32_t GrIORefProxy::getProxyRefCnt_TestOnly() const {
22 return fRefCnt;
23}
24
25int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const {
26 if (fTarget) {
27 return fTarget->fRefCnt;
28 }
29
30 return fRefCnt;
31}
32
33int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const {
34 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080035 return fTarget->fPendingReads;
36 }
37
38 return fPendingReads;
39}
40
41int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const {
42 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080043 return fTarget->fPendingWrites;
44 }
45
46 return fPendingWrites;
47}
48
Robert Phillips7928e762017-02-28 16:30:28 -050049static const int kWidthHeight = 128;
50
robertphillips1125a032016-11-16 11:17:17 -080051static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040052 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080053 int32_t expectedProxyRefs,
54 int32_t expectedBackingRefs,
55 int32_t expectedNumReads,
56 int32_t expectedNumWrites) {
57 REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
58 REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
59 REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
60 REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
61
62 SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
63 SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
64 SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
65 SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
66}
67
Robert Phillips7ee385e2017-03-30 08:02:11 -040068static sk_sp<GrTextureProxy> make_deferred(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080069 GrSurfaceDesc desc;
70 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -040071 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
robertphillips1125a032016-11-16 11:17:17 -080072 desc.fWidth = kWidthHeight;
73 desc.fHeight = kWidthHeight;
74 desc.fConfig = kRGBA_8888_GrPixelConfig;
75
Robert Phillips1ec1faa2017-03-28 16:21:27 -040076 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
Robert Phillipsf5442bb2017-04-17 14:18:34 -040077 SkBackingFit::kApprox, SkBudgeted::kYes,
78 GrResourceProvider::kNoPendingIO_Flag);
robertphillips1125a032016-11-16 11:17:17 -080079}
80
Robert Phillips7ee385e2017-03-30 08:02:11 -040081static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080082 GrSurfaceDesc desc;
83 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipse44ef102017-07-21 15:37:19 -040084 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
robertphillips1125a032016-11-16 11:17:17 -080085 desc.fWidth = kWidthHeight;
86 desc.fHeight = kWidthHeight;
87 desc.fConfig = kRGBA_8888_GrPixelConfig;
88
Robert Phillips1ec1faa2017-03-28 16:21:27 -040089 sk_sp<GrTexture> tex(context->resourceProvider()->createTexture(desc, SkBudgeted::kNo));
robertphillips1125a032016-11-16 11:17:17 -080090
Robert Phillips066f0202017-07-25 10:16:35 -040091 return GrSurfaceProxy::MakeWrapped(std::move(tex), desc.fOrigin);
robertphillips1125a032016-11-16 11:17:17 -080092}
93
94DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -050095 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080096
97 for (auto make : { make_deferred, make_wrapped }) {
98 // A single write
99 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400100 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400101 if (proxy.get()) {
102 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800103
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400104 static const int kExpectedReads = 0;
105 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800106
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400107 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800108
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400109 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800110
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400111 // In the deferred case, this checks that the refs transfered to the GrSurface
112 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
113 }
robertphillips1125a032016-11-16 11:17:17 -0800114 }
115
116 // A single read
117 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400118 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400119 if (proxy.get()) {
120 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800121
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400122 static const int kExpectedReads = 1;
123 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800124
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400125 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800126
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400127 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800128
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400129 // In the deferred case, this checks that the refs transfered to the GrSurface
130 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
131 }
robertphillips1125a032016-11-16 11:17:17 -0800132 }
133
134 // A single read/write pair
135 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400136 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400137 if (proxy.get()) {
138 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800139
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400140 static const int kExpectedReads = 1;
141 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800142
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400143 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800144
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400145 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800146
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400147 // In the deferred case, this checks that the refs transferred to the GrSurface
148 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
149 }
robertphillips1125a032016-11-16 11:17:17 -0800150 }
151
152 // Multiple normal refs
153 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400154 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400155 if (proxy.get()) {
156 proxy->ref();
157 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800158
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400159 static const int kExpectedReads = 0;
160 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800161
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400162 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800163
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400164 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800165
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400166 // In the deferred case, this checks that the refs transferred to the GrSurface
167 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800168
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400169 proxy->unref();
170 proxy->unref();
171 }
robertphillips1125a032016-11-16 11:17:17 -0800172 }
173
174 // Continue using (reffing) proxy after instantiation
175 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400176 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400177 if (proxy.get()) {
178 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800179
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400180 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800181
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400182 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800183
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400184 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800185
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400186 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800187
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400188 // In the deferred case, this checks that the refs transfered to the GrSurface
189 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800190
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400191 proxy->unref();
192 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800193
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400194 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
195 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
196 }
robertphillips1125a032016-11-16 11:17:17 -0800197 }
198 }
199}
Robert Phillips123b7b82017-04-11 09:09:24 -0400200
201#endif