blob: 3648122652ae02cef582ec397080217154820a02 [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;
Robert Phillipse44ef102017-07-21 15:37:19 -040083 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
robertphillips1125a032016-11-16 11:17:17 -080084 desc.fWidth = kWidthHeight;
85 desc.fHeight = kWidthHeight;
86 desc.fConfig = kRGBA_8888_GrPixelConfig;
87
Robert Phillips1ec1faa2017-03-28 16:21:27 -040088 sk_sp<GrTexture> tex(context->resourceProvider()->createTexture(desc, SkBudgeted::kNo));
robertphillips1125a032016-11-16 11:17:17 -080089
Robert Phillips066f0202017-07-25 10:16:35 -040090 return GrSurfaceProxy::MakeWrapped(std::move(tex), desc.fOrigin);
robertphillips1125a032016-11-16 11:17:17 -080091}
92
93DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -050094 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080095
96 for (auto make : { make_deferred, make_wrapped }) {
97 // A single write
98 {
Robert Phillips7ee385e2017-03-30 08:02:11 -040099 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800100
Robert Phillips7ee385e2017-03-30 08:02:11 -0400101 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800102
Robert Phillips1119dc32017-04-11 12:54:57 -0400103 static const int kExpectedReads = 0;
104 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800105
Robert Phillips1119dc32017-04-11 12:54:57 -0400106 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800107
Robert Phillips7ee385e2017-03-30 08:02:11 -0400108 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800109
110 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400111 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800112 }
113
114 // A single read
115 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400116 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800117
Robert Phillips7ee385e2017-03-30 08:02:11 -0400118 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800119
Robert Phillips1119dc32017-04-11 12:54:57 -0400120 static const int kExpectedReads = 1;
121 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800122
Robert Phillips1119dc32017-04-11 12:54:57 -0400123 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800124
Robert Phillips7ee385e2017-03-30 08:02:11 -0400125 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800126
127 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400128 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800129 }
130
131 // A single read/write pair
132 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400133 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800134
Robert Phillips7ee385e2017-03-30 08:02:11 -0400135 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800136
Robert Phillips1119dc32017-04-11 12:54:57 -0400137 static const int kExpectedReads = 1;
138 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800139
Robert Phillips1119dc32017-04-11 12:54:57 -0400140 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800141
Robert Phillips7ee385e2017-03-30 08:02:11 -0400142 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800143
Brian Salomon09d994e2016-12-21 11:14:46 -0500144 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400145 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800146 }
147
148 // Multiple normal refs
149 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400150 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
151 proxy->ref();
152 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800153
Robert Phillips1119dc32017-04-11 12:54:57 -0400154 static const int kExpectedReads = 0;
155 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800156
Robert Phillips1119dc32017-04-11 12:54:57 -0400157 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800158
Robert Phillips7ee385e2017-03-30 08:02:11 -0400159 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800160
Brian Salomon09d994e2016-12-21 11:14:46 -0500161 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400162 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800163
Robert Phillips7ee385e2017-03-30 08:02:11 -0400164 proxy->unref();
165 proxy->unref();
robertphillips1125a032016-11-16 11:17:17 -0800166 }
167
168 // Continue using (reffing) proxy after instantiation
169 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400170 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
171 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800172
Robert Phillips7ee385e2017-03-30 08:02:11 -0400173 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800174
Robert Phillips1119dc32017-04-11 12:54:57 -0400175 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800176
Robert Phillips1119dc32017-04-11 12:54:57 -0400177 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800178
Robert Phillips7ee385e2017-03-30 08:02:11 -0400179 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800180
181 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400182 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800183
Robert Phillips7ee385e2017-03-30 08:02:11 -0400184 proxy->unref();
Robert Phillips1119dc32017-04-11 12:54:57 -0400185 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800186
Robert Phillips7ee385e2017-03-30 08:02:11 -0400187 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
Robert Phillips1119dc32017-04-11 12:54:57 -0400188 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800189 }
190 }
191}
Robert Phillips123b7b82017-04-11 09:09:24 -0400192
193#endif