blob: 4dd831d942a90c346e46dd173d9a16237f7a13bc [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
robertphillips1125a032016-11-16 11:17:17 -080056 GrSurfaceDesc desc;
robertphillips1125a032016-11-16 11:17:17 -080057 desc.fWidth = kWidthHeight;
58 desc.fHeight = kWidthHeight;
59 desc.fConfig = kRGBA_8888_GrPixelConfig;
60
Chris Daltond004e0b2018-09-27 09:28:03 -060061 return proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -040062 desc, GrRenderable::kYes, 1, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact,
Brian Salomone8a766b2019-07-19 14:24:36 -040063 SkBudgeted::kNo, GrProtected::kNo);
robertphillips1125a032016-11-16 11:17:17 -080064}
65
66DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050067 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080068
69 for (auto make : { make_deferred, make_wrapped }) {
Robert Phillips3d4cac52019-06-11 08:08:08 -040070 // Pending IO ref
robertphillips1125a032016-11-16 11:17:17 -080071 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040072 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040073 if (proxy.get()) {
Robert Phillips3d4cac52019-06-11 08:08:08 -040074 GrProxyPendingIO pendingIO(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -080075
Robert Phillipsb5204762019-06-19 14:12:13 -040076 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -080077
Robert Phillips3d4cac52019-06-11 08:08:08 -040078 check_refs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080079
Robert Phillips0bd24dc2018-01-16 08:06:32 -050080 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080081
Robert Phillipse5f73282019-06-18 17:15:04 -040082 check_refs(reporter, proxy.get(), 2, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -040083 }
Robert Phillips3d4cac52019-06-11 08:08:08 -040084 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -080085 }
86
87 // Multiple normal refs
88 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040089 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040090 if (proxy.get()) {
91 proxy->ref();
92 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -080093
Robert Phillipsb5204762019-06-19 14:12:13 -040094 int backingRefs = proxy->isInstantiated() ? 1 : -1;
Robert Phillips715d08c2018-07-18 13:56:48 -040095
Robert Phillips3d4cac52019-06-11 08:08:08 -040096 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080097
Robert Phillips0bd24dc2018-01-16 08:06:32 -050098 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080099
Robert Phillipse5f73282019-06-18 17:15:04 -0400100 check_refs(reporter, proxy.get(), 3, 1);
robertphillips1125a032016-11-16 11:17:17 -0800101
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400102 proxy->unref();
103 proxy->unref();
104 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400105 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800106 }
107
108 // Continue using (reffing) proxy after instantiation
109 {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400110 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400111 if (proxy.get()) {
112 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800113
Robert Phillips3d4cac52019-06-11 08:08:08 -0400114 GrProxyPendingIO pendingIO(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800115
Robert Phillipsb5204762019-06-19 14:12:13 -0400116 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -0800117
Robert Phillips3d4cac52019-06-11 08:08:08 -0400118 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -0800119
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500120 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800121
Robert Phillipse5f73282019-06-18 17:15:04 -0400122 check_refs(reporter, proxy.get(), 3, 1);
robertphillips1125a032016-11-16 11:17:17 -0800123
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400124 proxy->unref();
Robert Phillipse5f73282019-06-18 17:15:04 -0400125 check_refs(reporter, proxy.get(), 2, 1);
robertphillips1125a032016-11-16 11:17:17 -0800126
Robert Phillips3d4cac52019-06-11 08:08:08 -0400127 GrProxyPendingIO secondPendingIO(proxy.get());
Robert Phillipse5f73282019-06-18 17:15:04 -0400128 check_refs(reporter, proxy.get(), 3, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400129 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400130 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800131 }
132 }
133}