blob: f5fe6a40477fdb8c8c46804d2816cc7e8add103d [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"
robertphillips1125a032016-11-16 11:17:17 -080014#include "GrRenderTargetPriv.h"
15#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050016#include "GrResourceProvider.h"
17#include "GrSurfaceProxy.h"
18#include "GrTextureProxy.h"
robertphillips1125a032016-11-16 11:17:17 -080019
robertphillips1125a032016-11-16 11:17:17 -080020int32_t GrIORefProxy::getProxyRefCnt_TestOnly() const {
21 return fRefCnt;
22}
23
24int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const {
25 if (fTarget) {
26 return fTarget->fRefCnt;
27 }
28
29 return fRefCnt;
30}
31
32int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const {
33 if (fTarget) {
34 SkASSERT(!fPendingReads);
35 return fTarget->fPendingReads;
36 }
37
38 return fPendingReads;
39}
40
41int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const {
42 if (fTarget) {
43 SkASSERT(!fPendingWrites);
44 return fTarget->fPendingWrites;
45 }
46
47 return fPendingWrites;
48}
49
Robert Phillips7928e762017-02-28 16:30:28 -050050static const int kWidthHeight = 128;
51
robertphillips1125a032016-11-16 11:17:17 -080052static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040053 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080054 int32_t expectedProxyRefs,
55 int32_t expectedBackingRefs,
56 int32_t expectedNumReads,
57 int32_t expectedNumWrites) {
58 REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
59 REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
60 REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
61 REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
62
63 SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
64 SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
65 SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
66 SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
67}
68
Robert Phillips7ee385e2017-03-30 08:02:11 -040069static sk_sp<GrTextureProxy> make_deferred(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080070 GrSurfaceDesc desc;
71 desc.fFlags = kRenderTarget_GrSurfaceFlag;
72 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 Phillips7928e762017-02-28 16:30:28 -050077 SkBackingFit::kApprox, SkBudgeted::kYes);
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 Phillips7ee385e2017-03-30 08:02:11 -040089 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex));
robertphillips1125a032016-11-16 11:17:17 -080090
Robert Phillips7ee385e2017-03-30 08:02:11 -040091 // Flush the IOWrite from the initial discard or it will confuse the later ref count checks
92 context->contextPriv().flushSurfaceWrites(proxy.get());
93
94 return proxy;
robertphillips1125a032016-11-16 11:17:17 -080095}
96
97DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -050098 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080099 const GrCaps& caps = *ctxInfo.grContext()->caps();
100
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500101 // Currently the op itself takes a pending write and the render target op list does as well.
102 static const int kWritesForDiscard = 2;
robertphillips1125a032016-11-16 11:17:17 -0800103 for (auto make : { make_deferred, make_wrapped }) {
104 // A single write
105 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400106 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800107
Robert Phillips7ee385e2017-03-30 08:02:11 -0400108 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800109
Robert Phillips7ee385e2017-03-30 08:02:11 -0400110 check_refs(reporter, proxy.get(), 1, 1, 0, 1);
robertphillips1125a032016-11-16 11:17:17 -0800111
Brian Salomon09d994e2016-12-21 11:14:46 -0500112 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800113 // extra ref and write
Robert Phillips7ee385e2017-03-30 08:02:11 -0400114 bool proxyGetsDiscardRef = !proxy->isWrapped_ForTesting() &&
robertphillips1125a032016-11-16 11:17:17 -0800115 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500116 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
robertphillips1125a032016-11-16 11:17:17 -0800117
Robert Phillips7ee385e2017-03-30 08:02:11 -0400118 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800119
120 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips7ee385e2017-03-30 08:02:11 -0400121 check_refs(reporter, proxy.get(), 1, 1, 0, expectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800122 }
123
124 // A single read
125 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400126 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800127
Robert Phillips7ee385e2017-03-30 08:02:11 -0400128 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800129
Robert Phillips7ee385e2017-03-30 08:02:11 -0400130 check_refs(reporter, proxy.get(), 1, 1, 1, 0);
robertphillips1125a032016-11-16 11:17:17 -0800131
Brian Salomon09d994e2016-12-21 11:14:46 -0500132 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800133 // extra ref and write
Robert Phillips7ee385e2017-03-30 08:02:11 -0400134 bool proxyGetsDiscardRef = !proxy->isWrapped_ForTesting() &&
robertphillips1125a032016-11-16 11:17:17 -0800135 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500136 int expectedWrites = proxyGetsDiscardRef ? kWritesForDiscard : 0;
robertphillips1125a032016-11-16 11:17:17 -0800137
Robert Phillips7ee385e2017-03-30 08:02:11 -0400138 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800139
140 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips7ee385e2017-03-30 08:02:11 -0400141 check_refs(reporter, proxy.get(), 1, 1, 1, expectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800142 }
143
144 // A single read/write pair
145 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400146 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800147
Robert Phillips7ee385e2017-03-30 08:02:11 -0400148 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800149
Robert Phillips7ee385e2017-03-30 08:02:11 -0400150 check_refs(reporter, proxy.get(), 1, 1, 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800151
Brian Salomon09d994e2016-12-21 11:14:46 -0500152 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800153 // extra ref and write
Robert Phillips7ee385e2017-03-30 08:02:11 -0400154 bool proxyGetsDiscardRef = !proxy->isWrapped_ForTesting() &&
robertphillips1125a032016-11-16 11:17:17 -0800155 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500156 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
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 Phillips7ee385e2017-03-30 08:02:11 -0400161 check_refs(reporter, proxy.get(), 1, 1, 1, expectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800162 }
163
164 // Multiple normal refs
165 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400166 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
167 proxy->ref();
168 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800169
Robert Phillips7ee385e2017-03-30 08:02:11 -0400170 check_refs(reporter, proxy.get(), 3, 3, 0, 0);
robertphillips1125a032016-11-16 11:17:17 -0800171
Robert Phillips7ee385e2017-03-30 08:02:11 -0400172 bool proxyGetsDiscardRef = !proxy->isWrapped_ForTesting() &&
robertphillips1125a032016-11-16 11:17:17 -0800173 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500174 int expectedWrites = proxyGetsDiscardRef ? kWritesForDiscard : 0;
robertphillips1125a032016-11-16 11:17:17 -0800175
Robert Phillips7ee385e2017-03-30 08:02:11 -0400176 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800177
Brian Salomon09d994e2016-12-21 11:14:46 -0500178 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips7ee385e2017-03-30 08:02:11 -0400179 check_refs(reporter, proxy.get(), 3, 3, 0, expectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800180
Robert Phillips7ee385e2017-03-30 08:02:11 -0400181 proxy->unref();
182 proxy->unref();
robertphillips1125a032016-11-16 11:17:17 -0800183 }
184
185 // Continue using (reffing) proxy after instantiation
186 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400187 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
188 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800189
Robert Phillips7ee385e2017-03-30 08:02:11 -0400190 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800191
Robert Phillips7ee385e2017-03-30 08:02:11 -0400192 check_refs(reporter, proxy.get(), 2, 2, 0, 1);
robertphillips1125a032016-11-16 11:17:17 -0800193
Robert Phillips7ee385e2017-03-30 08:02:11 -0400194 bool proxyGetsDiscardRef = !proxy->isWrapped_ForTesting() &&
robertphillips1125a032016-11-16 11:17:17 -0800195 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500196 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
robertphillips1125a032016-11-16 11:17:17 -0800197
Robert Phillips7ee385e2017-03-30 08:02:11 -0400198 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800199
200 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips7ee385e2017-03-30 08:02:11 -0400201 check_refs(reporter, proxy.get(), 2, 2, 0, expectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800202
Robert Phillips7ee385e2017-03-30 08:02:11 -0400203 proxy->unref();
204 check_refs(reporter, proxy.get(), 1, 1, 0, expectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800205
Robert Phillips7ee385e2017-03-30 08:02:11 -0400206 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
207 check_refs(reporter, proxy.get(), 1, 1, 1, expectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800208 }
209 }
210}
Robert Phillips7928e762017-02-28 16:30:28 -0500211#endif