blob: b82ab91d82716e23fec56aa6a1b1196c071de9df [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
Robert Phillips0a15cc62019-07-30 12:49:10 -040037static sk_sp<GrTextureProxy> make_deferred(GrContext* context) {
38 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
39 const GrCaps* caps = context->priv().caps();
40
robertphillips1125a032016-11-16 11:17:17 -080041 GrSurfaceDesc desc;
robertphillips1125a032016-11-16 11:17:17 -080042 desc.fWidth = kWidthHeight;
43 desc.fHeight = kWidthHeight;
44 desc.fConfig = kRGBA_8888_GrPixelConfig;
45
Robert Phillips0a15cc62019-07-30 12:49:10 -040046 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
47 GrRenderable::kYes);
Brian Salomon27b4d8d2019-07-22 14:23:45 -040048 return proxyProvider->createProxy(format, desc, GrRenderable::kYes, 1,
49 kBottomLeft_GrSurfaceOrigin, SkBackingFit::kApprox,
50 SkBudgeted::kYes, GrProtected::kNo);
robertphillips1125a032016-11-16 11:17:17 -080051}
52
Robert Phillips0a15cc62019-07-30 12:49:10 -040053static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
54 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
55
Chris Daltond004e0b2018-09-27 09:28:03 -060056 return proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -040057 {kWidthHeight, kWidthHeight}, GrColorType::kRGBA_8888, GrRenderable::kYes, 1,
58 kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
robertphillips1125a032016-11-16 11:17:17 -080059}
60
61DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050062 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080063
64 for (auto make : { make_deferred, make_wrapped }) {
Robert Phillips80bff5b2019-08-20 16:56:18 -040065 // An extra ref
robertphillips1125a032016-11-16 11:17:17 -080066 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040067 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Robert Phillips80bff5b2019-08-20 16:56:18 -040068 if (proxy) {
69 sk_sp<GrTextureProxy> extraRef(proxy);
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 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040084 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
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 {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400105 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Robert Phillips80bff5b2019-08-20 16:56:18 -0400106 if (proxy) {
107 sk_sp<GrTextureProxy> firstExtraRef(proxy);
robertphillips1125a032016-11-16 11:17:17 -0800108
Robert Phillipsb5204762019-06-19 14:12:13 -0400109 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -0800110
Robert Phillips80bff5b2019-08-20 16:56:18 -0400111 check_refs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -0800112
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500113 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800114
Robert Phillipse5f73282019-06-18 17:15:04 -0400115 check_refs(reporter, proxy.get(), 2, 1);
robertphillips1125a032016-11-16 11:17:17 -0800116
Robert Phillips80bff5b2019-08-20 16:56:18 -0400117 sk_sp<GrTextureProxy> secondExtraRef(proxy);
Robert Phillipse5f73282019-06-18 17:15:04 -0400118 check_refs(reporter, proxy.get(), 3, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400119 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400120 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800121 }
122 }
123}