blob: 04510b3e7f25f5a7390502074e9ab70c15e0016a [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;
71 desc.fWidth = kWidthHeight;
72 desc.fHeight = kWidthHeight;
73 desc.fConfig = kRGBA_8888_GrPixelConfig;
74
Robert Phillips1ec1faa2017-03-28 16:21:27 -040075 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
Robert Phillipsf5442bb2017-04-17 14:18:34 -040076 SkBackingFit::kApprox, SkBudgeted::kYes,
77 GrResourceProvider::kNoPendingIO_Flag);
robertphillips1125a032016-11-16 11:17:17 -080078}
79
Robert Phillips7ee385e2017-03-30 08:02:11 -040080static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080081 GrSurfaceDesc desc;
82 desc.fFlags = kRenderTarget_GrSurfaceFlag;
83 desc.fWidth = kWidthHeight;
84 desc.fHeight = kWidthHeight;
85 desc.fConfig = kRGBA_8888_GrPixelConfig;
86
Robert Phillips1ec1faa2017-03-28 16:21:27 -040087 sk_sp<GrTexture> tex(context->resourceProvider()->createTexture(desc, SkBudgeted::kNo));
robertphillips1125a032016-11-16 11:17:17 -080088
Robert Phillips1119dc32017-04-11 12:54:57 -040089 return GrSurfaceProxy::MakeWrapped(std::move(tex));
robertphillips1125a032016-11-16 11:17:17 -080090}
91
92DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -050093 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080094
95 for (auto make : { make_deferred, make_wrapped }) {
96 // A single write
97 {
Robert Phillips7ee385e2017-03-30 08:02:11 -040098 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -080099
Robert Phillips7ee385e2017-03-30 08:02:11 -0400100 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800101
Robert Phillips1119dc32017-04-11 12:54:57 -0400102 static const int kExpectedReads = 0;
103 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800104
Robert Phillips1119dc32017-04-11 12:54:57 -0400105 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800106
Robert Phillips7ee385e2017-03-30 08:02:11 -0400107 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800108
109 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400110 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800111 }
112
113 // A single read
114 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400115 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800116
Robert Phillips7ee385e2017-03-30 08:02:11 -0400117 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800118
Robert Phillips1119dc32017-04-11 12:54:57 -0400119 static const int kExpectedReads = 1;
120 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800121
Robert Phillips1119dc32017-04-11 12:54:57 -0400122 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800123
Robert Phillips7ee385e2017-03-30 08:02:11 -0400124 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800125
126 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400127 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800128 }
129
130 // A single read/write pair
131 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400132 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800133
Robert Phillips7ee385e2017-03-30 08:02:11 -0400134 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800135
Robert Phillips1119dc32017-04-11 12:54:57 -0400136 static const int kExpectedReads = 1;
137 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800138
Robert Phillips1119dc32017-04-11 12:54:57 -0400139 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800140
Robert Phillips7ee385e2017-03-30 08:02:11 -0400141 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800142
Brian Salomon09d994e2016-12-21 11:14:46 -0500143 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400144 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800145 }
146
147 // Multiple normal refs
148 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400149 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
150 proxy->ref();
151 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800152
Robert Phillips1119dc32017-04-11 12:54:57 -0400153 static const int kExpectedReads = 0;
154 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800155
Robert Phillips1119dc32017-04-11 12:54:57 -0400156 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800157
Robert Phillips7ee385e2017-03-30 08:02:11 -0400158 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800159
Brian Salomon09d994e2016-12-21 11:14:46 -0500160 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400161 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800162
Robert Phillips7ee385e2017-03-30 08:02:11 -0400163 proxy->unref();
164 proxy->unref();
robertphillips1125a032016-11-16 11:17:17 -0800165 }
166
167 // Continue using (reffing) proxy after instantiation
168 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400169 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
170 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800171
Robert Phillips7ee385e2017-03-30 08:02:11 -0400172 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800173
Robert Phillips1119dc32017-04-11 12:54:57 -0400174 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800175
Robert Phillips1119dc32017-04-11 12:54:57 -0400176 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800177
Robert Phillips7ee385e2017-03-30 08:02:11 -0400178 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800179
180 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400181 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800182
Robert Phillips7ee385e2017-03-30 08:02:11 -0400183 proxy->unref();
Robert Phillips1119dc32017-04-11 12:54:57 -0400184 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800185
Robert Phillips7ee385e2017-03-30 08:02:11 -0400186 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
Robert Phillips1119dc32017-04-11 12:54:57 -0400187 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800188 }
189 }
190}
Robert Phillips123b7b82017-04-11 09:09:24 -0400191
192#endif