blob: 708c7b3034b3a93ddfef233f42c4572f563fa201 [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 Phillips123b7b82017-04-11 09:09:24 -040050#ifndef SK_DISABLE_DEFERRED_PROXIES
51
Robert Phillips7928e762017-02-28 16:30:28 -050052static const int kWidthHeight = 128;
53
robertphillips1125a032016-11-16 11:17:17 -080054static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040055 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080056 int32_t expectedProxyRefs,
57 int32_t expectedBackingRefs,
58 int32_t expectedNumReads,
59 int32_t expectedNumWrites) {
60 REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
61 REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
62 REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
63 REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
64
65 SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
66 SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
67 SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
68 SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
69}
70
Robert Phillips7ee385e2017-03-30 08:02:11 -040071static sk_sp<GrTextureProxy> make_deferred(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080072 GrSurfaceDesc desc;
73 desc.fFlags = kRenderTarget_GrSurfaceFlag;
74 desc.fWidth = kWidthHeight;
75 desc.fHeight = kWidthHeight;
76 desc.fConfig = kRGBA_8888_GrPixelConfig;
77
Robert Phillips1ec1faa2017-03-28 16:21:27 -040078 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
Robert Phillips7928e762017-02-28 16:30:28 -050079 SkBackingFit::kApprox, SkBudgeted::kYes);
robertphillips1125a032016-11-16 11:17:17 -080080}
81
Robert Phillips7ee385e2017-03-30 08:02:11 -040082static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080083 GrSurfaceDesc desc;
84 desc.fFlags = kRenderTarget_GrSurfaceFlag;
85 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 Phillips1119dc32017-04-11 12:54:57 -040091 return GrSurfaceProxy::MakeWrapped(std::move(tex));
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()));
robertphillips1125a032016-11-16 11:17:17 -0800101
Robert Phillips7ee385e2017-03-30 08:02:11 -0400102 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800103
Robert Phillips1119dc32017-04-11 12:54:57 -0400104 static const int kExpectedReads = 0;
105 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800106
Robert Phillips1119dc32017-04-11 12:54:57 -0400107 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800108
Robert Phillips7ee385e2017-03-30 08:02:11 -0400109 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800110
111 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400112 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800113 }
114
115 // A single read
116 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400117 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800118
Robert Phillips7ee385e2017-03-30 08:02:11 -0400119 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800120
Robert Phillips1119dc32017-04-11 12:54:57 -0400121 static const int kExpectedReads = 1;
122 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800123
Robert Phillips1119dc32017-04-11 12:54:57 -0400124 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800125
Robert Phillips7ee385e2017-03-30 08:02:11 -0400126 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800127
128 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400129 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800130 }
131
132 // A single read/write pair
133 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400134 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800135
Robert Phillips7ee385e2017-03-30 08:02:11 -0400136 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800137
Robert Phillips1119dc32017-04-11 12:54:57 -0400138 static const int kExpectedReads = 1;
139 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800140
Robert Phillips1119dc32017-04-11 12:54:57 -0400141 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800142
Robert Phillips7ee385e2017-03-30 08:02:11 -0400143 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800144
Brian Salomon09d994e2016-12-21 11:14:46 -0500145 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400146 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800147 }
148
149 // Multiple normal refs
150 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400151 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
152 proxy->ref();
153 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800154
Robert Phillips1119dc32017-04-11 12:54:57 -0400155 static const int kExpectedReads = 0;
156 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800157
Robert Phillips1119dc32017-04-11 12:54:57 -0400158 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800159
Robert Phillips7ee385e2017-03-30 08:02:11 -0400160 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800161
Brian Salomon09d994e2016-12-21 11:14:46 -0500162 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400163 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800164
Robert Phillips7ee385e2017-03-30 08:02:11 -0400165 proxy->unref();
166 proxy->unref();
robertphillips1125a032016-11-16 11:17:17 -0800167 }
168
169 // Continue using (reffing) proxy after instantiation
170 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400171 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
172 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800173
Robert Phillips7ee385e2017-03-30 08:02:11 -0400174 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800175
Robert Phillips1119dc32017-04-11 12:54:57 -0400176 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800177
Robert Phillips1119dc32017-04-11 12:54:57 -0400178 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800179
Robert Phillips7ee385e2017-03-30 08:02:11 -0400180 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800181
182 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400183 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800184
Robert Phillips7ee385e2017-03-30 08:02:11 -0400185 proxy->unref();
Robert Phillips1119dc32017-04-11 12:54:57 -0400186 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800187
Robert Phillips7ee385e2017-03-30 08:02:11 -0400188 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
Robert Phillips1119dc32017-04-11 12:54:57 -0400189 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800190 }
191 }
192}
Robert Phillips7928e762017-02-28 16:30:28 -0500193#endif
Robert Phillips123b7b82017-04-11 09:09:24 -0400194
195#endif