blob: a18dd61f1c4c7ae12e0f81de14377f10d60e9a54 [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 Phillipsf5442bb2017-04-17 14:18:34 -040079 SkBackingFit::kApprox, SkBudgeted::kYes,
80 GrResourceProvider::kNoPendingIO_Flag);
robertphillips1125a032016-11-16 11:17:17 -080081}
82
Robert Phillips7ee385e2017-03-30 08:02:11 -040083static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
robertphillips1125a032016-11-16 11:17:17 -080084 GrSurfaceDesc desc;
85 desc.fFlags = kRenderTarget_GrSurfaceFlag;
86 desc.fWidth = kWidthHeight;
87 desc.fHeight = kWidthHeight;
88 desc.fConfig = kRGBA_8888_GrPixelConfig;
89
Robert Phillips1ec1faa2017-03-28 16:21:27 -040090 sk_sp<GrTexture> tex(context->resourceProvider()->createTexture(desc, SkBudgeted::kNo));
robertphillips1125a032016-11-16 11:17:17 -080091
Robert Phillips1119dc32017-04-11 12:54:57 -040092 return GrSurfaceProxy::MakeWrapped(std::move(tex));
robertphillips1125a032016-11-16 11:17:17 -080093}
94
95DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Brian Osman32342f02017-03-04 08:12:46 -050096 GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080097
98 for (auto make : { make_deferred, make_wrapped }) {
99 // A single write
100 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400101 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800102
Robert Phillips7ee385e2017-03-30 08:02:11 -0400103 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800104
Robert Phillips1119dc32017-04-11 12:54:57 -0400105 static const int kExpectedReads = 0;
106 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800107
Robert Phillips1119dc32017-04-11 12:54:57 -0400108 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800109
Robert Phillips7ee385e2017-03-30 08:02:11 -0400110 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800111
112 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400113 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800114 }
115
116 // A single read
117 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400118 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800119
Robert Phillips7ee385e2017-03-30 08:02:11 -0400120 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800121
Robert Phillips1119dc32017-04-11 12:54:57 -0400122 static const int kExpectedReads = 1;
123 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800124
Robert Phillips1119dc32017-04-11 12:54:57 -0400125 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800126
Robert Phillips7ee385e2017-03-30 08:02:11 -0400127 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800128
129 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400130 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800131 }
132
133 // A single read/write pair
134 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400135 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
robertphillips1125a032016-11-16 11:17:17 -0800136
Robert Phillips7ee385e2017-03-30 08:02:11 -0400137 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800138
Robert Phillips1119dc32017-04-11 12:54:57 -0400139 static const int kExpectedReads = 1;
140 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800141
Robert Phillips1119dc32017-04-11 12:54:57 -0400142 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800143
Robert Phillips7ee385e2017-03-30 08:02:11 -0400144 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800145
Brian Salomon09d994e2016-12-21 11:14:46 -0500146 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400147 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800148 }
149
150 // Multiple normal refs
151 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400152 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
153 proxy->ref();
154 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800155
Robert Phillips1119dc32017-04-11 12:54:57 -0400156 static const int kExpectedReads = 0;
157 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800158
Robert Phillips1119dc32017-04-11 12:54:57 -0400159 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800160
Robert Phillips7ee385e2017-03-30 08:02:11 -0400161 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800162
Brian Salomon09d994e2016-12-21 11:14:46 -0500163 // In the deferred case, this checks that the refs transferred to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400164 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800165
Robert Phillips7ee385e2017-03-30 08:02:11 -0400166 proxy->unref();
167 proxy->unref();
robertphillips1125a032016-11-16 11:17:17 -0800168 }
169
170 // Continue using (reffing) proxy after instantiation
171 {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400172 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
173 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800174
Robert Phillips7ee385e2017-03-30 08:02:11 -0400175 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800176
Robert Phillips1119dc32017-04-11 12:54:57 -0400177 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800178
Robert Phillips1119dc32017-04-11 12:54:57 -0400179 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800180
Robert Phillips7ee385e2017-03-30 08:02:11 -0400181 proxy->instantiate(provider);
robertphillips1125a032016-11-16 11:17:17 -0800182
183 // In the deferred case, this checks that the refs transfered to the GrSurface
Robert Phillips1119dc32017-04-11 12:54:57 -0400184 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800185
Robert Phillips7ee385e2017-03-30 08:02:11 -0400186 proxy->unref();
Robert Phillips1119dc32017-04-11 12:54:57 -0400187 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800188
Robert Phillips7ee385e2017-03-30 08:02:11 -0400189 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
Robert Phillips1119dc32017-04-11 12:54:57 -0400190 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800191 }
192 }
193}
Robert Phillips7928e762017-02-28 16:30:28 -0500194#endif
Robert Phillips123b7b82017-04-11 09:09:24 -0400195
196#endif