blob: 0cca7729d5fb783bdbe58f1bca646968e9f18134 [file] [log] [blame]
keyar@chromium.org5bdef292012-08-14 22:02:48 +00001/*
2 * Copyright 2011 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
halcanary96fcdcc2015-08-27 07:41:13 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkExecutor.h"
11#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrContextPriv.h"
13#include "tests/Test.h"
14#include "tools/gpu/GrContextFactory.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000015
bsalomonf2f1c172016-04-05 12:59:06 -070016using namespace sk_gpu_test;
bsalomon3724e572016-03-30 18:56:19 -070017
Brian Salomondcfca432017-11-15 15:48:03 -050018DEF_GPUTEST(GrContextFactory_abandon, reporter, options) {
bsalomon85b4b532016-04-05 11:06:27 -070019 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -050020 GrContextFactory testFactory(options);
bsalomon85b4b532016-04-05 11:06:27 -070021 GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
bsalomonf2f1c172016-04-05 12:59:06 -070022 ContextInfo info1 = testFactory.getContextInfo(ctxType);
bsalomon8b7451a2016-05-11 06:33:06 -070023 if (!info1.grContext()) {
kkinnunen34058002016-01-06 23:49:30 -080024 continue;
25 }
bsalomon0fd3c812016-05-11 10:38:05 -070026 REPORTER_ASSERT(reporter, info1.testContext());
kkinnunen34058002016-01-06 23:49:30 -080027 // Ref for comparison. The API does not explicitly say that this stays alive.
bsalomon8b7451a2016-05-11 06:33:06 -070028 info1.grContext()->ref();
kkinnunen34058002016-01-06 23:49:30 -080029 testFactory.abandonContexts();
30
31 // Test that we get different context after abandon.
bsalomonf2f1c172016-04-05 12:59:06 -070032 ContextInfo info2 = testFactory.getContextInfo(ctxType);
bsalomon8b7451a2016-05-11 06:33:06 -070033 REPORTER_ASSERT(reporter, info2.grContext());
bsalomon0fd3c812016-05-11 10:38:05 -070034 REPORTER_ASSERT(reporter, info2.testContext());
35
bsalomon8b7451a2016-05-11 06:33:06 -070036 REPORTER_ASSERT(reporter, info1.grContext() != info2.grContext());
37 // The GL context should also change, but it also could get the same address.
kkinnunen34058002016-01-06 23:49:30 -080038
bsalomon8b7451a2016-05-11 06:33:06 -070039 info1.grContext()->unref();
kkinnunen34058002016-01-06 23:49:30 -080040 }
41}
42
Brian Salomondcfca432017-11-15 15:48:03 -050043DEF_GPUTEST(GrContextFactory_sharedContexts, reporter, options) {
Brian Osman60c774d2017-02-21 16:58:08 -050044 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -050045 GrContextFactory testFactory(options);
Brian Osman60c774d2017-02-21 16:58:08 -050046 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
47 ContextInfo info1 = testFactory.getContextInfo(ctxType);
48 if (!info1.grContext()) {
49 continue;
50 }
51
52 // Ref for passing in. The API does not explicitly say that this stays alive.
53 info1.grContext()->ref();
54 testFactory.abandonContexts();
55
56 // Test that creating a context in a share group with an abandoned context fails.
Brian Osman9eac2ea2017-02-24 14:51:44 -050057 ContextInfo info2 = testFactory.getSharedContextInfo(info1.grContext());
Brian Osman60c774d2017-02-21 16:58:08 -050058 REPORTER_ASSERT(reporter, !info2.grContext());
59 info1.grContext()->unref();
60
61 // Create a new base context
62 ContextInfo info3 = testFactory.getContextInfo(ctxType);
63
64 // Creating a context in a share group may fail, but should never crash.
Brian Osman9eac2ea2017-02-24 14:51:44 -050065 ContextInfo info4 = testFactory.getSharedContextInfo(info3.grContext());
Brian Osman60c774d2017-02-21 16:58:08 -050066 if (!info4.grContext()) {
67 continue;
68 }
69 REPORTER_ASSERT(reporter, info3.grContext() != info4.grContext());
70 REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext());
71
72 // Passing a different index should create a new (unique) context.
Brian Osman9eac2ea2017-02-24 14:51:44 -050073 ContextInfo info5 = testFactory.getSharedContextInfo(info3.grContext(), 1);
Brian Osman60c774d2017-02-21 16:58:08 -050074 REPORTER_ASSERT(reporter, info5.grContext());
75 REPORTER_ASSERT(reporter, info5.testContext());
76 REPORTER_ASSERT(reporter, info5.grContext() != info4.grContext());
77 REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext());
Brian Osman60c774d2017-02-21 16:58:08 -050078 }
79}
80
Brian Salomondcfca432017-11-15 15:48:03 -050081DEF_GPUTEST(GrContextFactory_executorAndTaskGroup, reporter, options) {
Brian Osman51279982017-08-23 10:12:00 -040082 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -050083 // Verify that contexts have a task group iff we supply an executor with context options
84 GrContextOptions contextOptions = options;
85 contextOptions.fExecutor = nullptr;
86 GrContextFactory serialFactory(contextOptions);
87
88 std::unique_ptr<SkExecutor> threadPool = SkExecutor::MakeFIFOThreadPool(1);
89 contextOptions.fExecutor = threadPool.get();
90 GrContextFactory threadedFactory(contextOptions);
91
Brian Osman51279982017-08-23 10:12:00 -040092 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
93 ContextInfo serialInfo = serialFactory.getContextInfo(ctxType);
94 if (GrContext* serialContext = serialInfo.grContext()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050095 REPORTER_ASSERT(reporter, nullptr == serialContext->priv().getTaskGroup());
Brian Osman51279982017-08-23 10:12:00 -040096 }
97
98 ContextInfo threadedInfo = threadedFactory.getContextInfo(ctxType);
99 if (GrContext* threadedContext = threadedInfo.grContext()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500100 REPORTER_ASSERT(reporter, nullptr != threadedContext->priv().getTaskGroup());
Brian Osman51279982017-08-23 10:12:00 -0400101 }
102 }
103}
104
Kevin Lubickf4def342018-10-04 12:52:50 -0400105#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400106DEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump, reporter, ctxInfo) {
107 // Ensure that GrContext::dump doesn't assert (which is possible, if the JSON code is wrong)
Brian Salomonec22b1a2019-08-09 09:41:48 -0400108 SkString result = ctxInfo.grContext()->dump();
Brian Osman71a18892017-08-10 10:23:25 -0400109 REPORTER_ASSERT(reporter, !result.isEmpty());
110}
Kevin Lubickf4def342018-10-04 12:52:50 -0400111#endif