blob: bacec67834965f0d62416c611b00482a739ae6ab [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
robertphillips1125a032016-11-16 11:17:17 -080011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrPendingIOResource.h"
15#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSurfaceProxy.h"
19#include "src/gpu/GrTextureProxy.h"
robertphillips1125a032016-11-16 11:17:17 -080020
Robert Phillips7928e762017-02-28 16:30:28 -050021static const int kWidthHeight = 128;
22
robertphillips1125a032016-11-16 11:17:17 -080023static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040024 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080025 int32_t expectedProxyRefs,
Robert Phillips3d4cac52019-06-11 08:08:08 -040026 int32_t expectedBackingRefs) {
27 int32_t actualProxyRefs = proxy->priv().getProxyRefCnt();
Robert Phillipsb5204762019-06-19 14:12:13 -040028 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
robertphillips1125a032016-11-16 11:17:17 -080029
Robert Phillips3d4cac52019-06-11 08:08:08 -040030 SkASSERT(actualProxyRefs == expectedProxyRefs);
31 SkASSERT(actualBackingRefs == expectedBackingRefs);
32
33 REPORTER_ASSERT(reporter, actualProxyRefs == expectedProxyRefs);
34 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
robertphillips1125a032016-11-16 11:17:17 -080035}
36
Greg Daniel4065d452018-11-16 15:43:41 -050037static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080038 GrSurfaceDesc desc;
robertphillips1125a032016-11-16 11:17:17 -080039 desc.fWidth = kWidthHeight;
40 desc.fHeight = kWidthHeight;
41 desc.fConfig = kRGBA_8888_GrPixelConfig;
42
Greg Daniel627d0532019-07-08 16:48:14 -040043 const GrBackendFormat format = caps->getBackendFormatFromColorType(GrColorType::kRGBA_8888);
Brian Salomonf2c2ba92019-07-17 09:59:59 -040044 return proxyProvider->createProxy(format, desc, GrRenderable::kYes, kBottomLeft_GrSurfaceOrigin,
Robert Phillips10d17212019-04-24 14:09:10 -040045 SkBackingFit::kApprox, SkBudgeted::kYes);
robertphillips1125a032016-11-16 11:17:17 -080046}
47
Greg Daniel4065d452018-11-16 15:43:41 -050048static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080049 GrSurfaceDesc desc;
robertphillips1125a032016-11-16 11:17:17 -080050 desc.fWidth = kWidthHeight;
51 desc.fHeight = kWidthHeight;
52 desc.fConfig = kRGBA_8888_GrPixelConfig;
53
Chris Daltond004e0b2018-09-27 09:28:03 -060054 return proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -040055 desc, GrRenderable::kYes, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact,
56 SkBudgeted::kNo);
robertphillips1125a032016-11-16 11:17:17 -080057}
58
59DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050060 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
61 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
62 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
robertphillips1125a032016-11-16 11:17:17 -080063
64 for (auto make : { make_deferred, make_wrapped }) {
Robert Phillips3d4cac52019-06-11 08:08:08 -040065 // Pending IO ref
robertphillips1125a032016-11-16 11:17:17 -080066 {
Greg Daniel4065d452018-11-16 15:43:41 -050067 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040068 if (proxy.get()) {
Robert Phillips3d4cac52019-06-11 08:08:08 -040069 GrProxyPendingIO pendingIO(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -080070
Robert Phillipsb5204762019-06-19 14:12:13 -040071 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -080072
Robert Phillips3d4cac52019-06-11 08:08:08 -040073 check_refs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080074
Robert Phillips0bd24dc2018-01-16 08:06:32 -050075 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080076
Robert Phillipse5f73282019-06-18 17:15:04 -040077 check_refs(reporter, proxy.get(), 2, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -040078 }
Robert Phillips3d4cac52019-06-11 08:08:08 -040079 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -080080 }
81
82 // Multiple normal refs
83 {
Greg Daniel4065d452018-11-16 15:43:41 -050084 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040085 if (proxy.get()) {
86 proxy->ref();
87 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -080088
Robert Phillipsb5204762019-06-19 14:12:13 -040089 int backingRefs = proxy->isInstantiated() ? 1 : -1;
Robert Phillips715d08c2018-07-18 13:56:48 -040090
Robert Phillips3d4cac52019-06-11 08:08:08 -040091 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080092
Robert Phillips0bd24dc2018-01-16 08:06:32 -050093 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080094
Robert Phillipse5f73282019-06-18 17:15:04 -040095 check_refs(reporter, proxy.get(), 3, 1);
robertphillips1125a032016-11-16 11:17:17 -080096
Jim Van Verth311cc6b2017-09-20 17:51:59 -040097 proxy->unref();
98 proxy->unref();
99 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400100 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800101 }
102
103 // Continue using (reffing) proxy after instantiation
104 {
Greg Daniel4065d452018-11-16 15:43:41 -0500105 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400106 if (proxy.get()) {
107 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800108
Robert Phillips3d4cac52019-06-11 08:08:08 -0400109 GrProxyPendingIO pendingIO(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800110
Robert Phillipsb5204762019-06-19 14:12:13 -0400111 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -0800112
Robert Phillips3d4cac52019-06-11 08:08:08 -0400113 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -0800114
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500115 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800116
Robert Phillipse5f73282019-06-18 17:15:04 -0400117 check_refs(reporter, proxy.get(), 3, 1);
robertphillips1125a032016-11-16 11:17:17 -0800118
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400119 proxy->unref();
Robert Phillipse5f73282019-06-18 17:15:04 -0400120 check_refs(reporter, proxy.get(), 2, 1);
robertphillips1125a032016-11-16 11:17:17 -0800121
Robert Phillips3d4cac52019-06-11 08:08:08 -0400122 GrProxyPendingIO secondPendingIO(proxy.get());
Robert Phillipse5f73282019-06-18 17:15:04 -0400123 check_refs(reporter, proxy.get(), 3, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400124 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400125 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800126 }
127 }
128}