blob: 61e57209ce2be7119c46bf0731353e84d4ebf8e2 [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 "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050015#include "GrResourceProvider.h"
16#include "GrSurfaceProxy.h"
Robert Phillips646e4292017-06-13 12:44:56 -040017#include "GrTexture.h"
Brian Osman32342f02017-03-04 08:12:46 -050018#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) {
robertphillips1125a032016-11-16 11:17:17 -080034 return fTarget->fPendingReads;
35 }
36
37 return fPendingReads;
38}
39
40int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const {
41 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080042 return fTarget->fPendingWrites;
43 }
44
45 return fPendingWrites;
46}
47
Robert Phillips7928e762017-02-28 16:30:28 -050048static const int kWidthHeight = 128;
49
robertphillips1125a032016-11-16 11:17:17 -080050static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040051 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080052 int32_t expectedProxyRefs,
53 int32_t expectedBackingRefs,
54 int32_t expectedNumReads,
55 int32_t expectedNumWrites) {
56 REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
57 REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
58 REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
59 REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
60
61 SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
62 SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
63 SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
64 SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
65}
66
Robert Phillips7ee385e2017-03-30 08:02:11 -040067static sk_sp<GrTextureProxy> make_deferred(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080068 GrSurfaceDesc desc;
69 desc.fFlags = kRenderTarget_GrSurfaceFlag;
70 desc.fWidth = kWidthHeight;
71 desc.fHeight = kWidthHeight;
72 desc.fConfig = kRGBA_8888_GrPixelConfig;
73
Robert Phillips1ec1faa2017-03-28 16:21:27 -040074 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
Robert Phillipsf5442bb2017-04-17 14:18:34 -040075 SkBackingFit::kApprox, SkBudgeted::kYes,
76 GrResourceProvider::kNoPendingIO_Flag);
robertphillips1125a032016-11-16 11:17:17 -080077}
78
Robert Phillips7ee385e2017-03-30 08:02:11 -040079static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080080 GrSurfaceDesc desc;
81 desc.fFlags = kRenderTarget_GrSurfaceFlag;
82 desc.fWidth = kWidthHeight;
83 desc.fHeight = kWidthHeight;
84 desc.fConfig = kRGBA_8888_GrPixelConfig;
85
Robert Phillips1ec1faa2017-03-28 16:21:27 -040086 sk_sp<GrTexture> tex(context->resourceProvider()->createTexture(desc, SkBudgeted::kNo));
robertphillips1125a032016-11-16 11:17:17 -080087
Robert Phillips1119dc32017-04-11 12:54:57 -040088 return GrSurfaceProxy::MakeWrapped(std::move(tex));
robertphillips1125a032016-11-16 11:17:17 -080089}
90
91DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -050092 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080093
94 for (auto make : { make_deferred, make_wrapped }) {
95 // A single write
96 {
Robert Phillips7ee385e2017-03-30 08:02:11 -040097 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -080098
Robert Phillips7ee385e2017-03-30 08:02:11 -040099 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800100
Robert Phillips1119dc32017-04-11 12:54:57 -0400101 static const int kExpectedReads = 0;
102 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800103
Robert Phillips1119dc32017-04-11 12:54:57 -0400104 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800105
Robert Phillips7ee385e2017-03-30 08:02:11 -0400106 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800107
108 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400109 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800110 }
111
112 // A single read
113 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400114 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800115
Robert Phillips7ee385e2017-03-30 08:02:11 -0400116 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800117
Robert Phillips1119dc32017-04-11 12:54:57 -0400118 static const int kExpectedReads = 1;
119 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800120
Robert Phillips1119dc32017-04-11 12:54:57 -0400121 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800122
Robert Phillips7ee385e2017-03-30 08:02:11 -0400123 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800124
125 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400126 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800127 }
128
129 // A single read/write pair
130 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400131 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800132
Robert Phillips7ee385e2017-03-30 08:02:11 -0400133 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800134
Robert Phillips1119dc32017-04-11 12:54:57 -0400135 static const int kExpectedReads = 1;
136 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800137
Robert Phillips1119dc32017-04-11 12:54:57 -0400138 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800139
Robert Phillips7ee385e2017-03-30 08:02:11 -0400140 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800141
Brian Salomon09d994e2016-12-21 11:14:46 -0500142 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400143 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800144 }
145
146 // Multiple normal refs
147 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400148 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
149 proxy->ref();
150 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800151
Robert Phillips1119dc32017-04-11 12:54:57 -0400152 static const int kExpectedReads = 0;
153 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800154
Robert Phillips1119dc32017-04-11 12:54:57 -0400155 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800156
Robert Phillips7ee385e2017-03-30 08:02:11 -0400157 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800158
Brian Salomon09d994e2016-12-21 11:14:46 -0500159 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400160 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800161
Robert Phillips7ee385e2017-03-30 08:02:11 -0400162 proxy->unref();
163 proxy->unref();
robertphillips1125a032016-11-16 11:17:17 -0800164 }
165
166 // Continue using (reffing) proxy after instantiation
167 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400168 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
169 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800170
Robert Phillips7ee385e2017-03-30 08:02:11 -0400171 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800172
Robert Phillips1119dc32017-04-11 12:54:57 -0400173 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800174
Robert Phillips1119dc32017-04-11 12:54:57 -0400175 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800176
Robert Phillips7ee385e2017-03-30 08:02:11 -0400177 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800178
179 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400180 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800181
Robert Phillips7ee385e2017-03-30 08:02:11 -0400182 proxy->unref();
Robert Phillips1119dc32017-04-11 12:54:57 -0400183 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800184
Robert Phillips7ee385e2017-03-30 08:02:11 -0400185 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
Robert Phillips1119dc32017-04-11 12:54:57 -0400186 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800187 }
188 }
189}
Robert Phillips123b7b82017-04-11 09:09:24 -0400190
191#endif