keyar@chromium.org | 5bdef29 | 2012-08-14 22:02:48 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkExecutor.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrCaps.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tests/Test.h" |
| 15 | #include "tools/gpu/GrContextFactory.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 16 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 17 | using namespace sk_gpu_test; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 18 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 19 | DEF_GPUTEST(GrContextFactory_abandon, reporter, options) { |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 20 | for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) { |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 21 | GrContextFactory testFactory(options); |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 22 | GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i; |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 23 | ContextInfo info1 = testFactory.getContextInfo(ctxType); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 24 | if (!info1.directContext()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 25 | continue; |
| 26 | } |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 27 | REPORTER_ASSERT(reporter, info1.testContext()); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 28 | // Ref for comparison. The API does not explicitly say that this stays alive. |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 29 | info1.directContext()->ref(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 30 | testFactory.abandonContexts(); |
| 31 | |
| 32 | // Test that we get different context after abandon. |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 33 | ContextInfo info2 = testFactory.getContextInfo(ctxType); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 34 | REPORTER_ASSERT(reporter, info2.directContext()); |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, info2.testContext()); |
| 36 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, info1.directContext() != info2.directContext()); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 38 | // The GL context should also change, but it also could get the same address. |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 39 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 40 | info1.directContext()->unref(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 44 | DEF_GPUTEST(GrContextFactory_sharedContexts, reporter, options) { |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 45 | for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) { |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 46 | GrContextFactory testFactory(options); |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 47 | GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i); |
| 48 | ContextInfo info1 = testFactory.getContextInfo(ctxType); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 49 | if (!info1.directContext()) { |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 50 | continue; |
| 51 | } |
| 52 | |
| 53 | // Ref for passing in. The API does not explicitly say that this stays alive. |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 54 | info1.directContext()->ref(); |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 55 | testFactory.abandonContexts(); |
| 56 | |
| 57 | // Test that creating a context in a share group with an abandoned context fails. |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 58 | ContextInfo info2 = testFactory.getSharedContextInfo(info1.directContext()); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, !info2.directContext()); |
| 60 | info1.directContext()->unref(); |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 61 | |
| 62 | // Create a new base context |
| 63 | ContextInfo info3 = testFactory.getContextInfo(ctxType); |
| 64 | |
| 65 | // Creating a context in a share group may fail, but should never crash. |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 66 | ContextInfo info4 = testFactory.getSharedContextInfo(info3.directContext()); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 67 | if (!info4.directContext()) { |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 68 | continue; |
| 69 | } |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, info3.directContext() != info4.directContext()); |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext()); |
| 72 | |
| 73 | // Passing a different index should create a new (unique) context. |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 74 | ContextInfo info5 = testFactory.getSharedContextInfo(info3.directContext(), 1); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, info5.directContext()); |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, info5.testContext()); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, info5.directContext() != info4.directContext()); |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext()); |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 82 | DEF_GPUTEST(GrContextFactory_executorAndTaskGroup, reporter, options) { |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 83 | for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) { |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 84 | // Verify that contexts have a task group iff we supply an executor with context options |
| 85 | GrContextOptions contextOptions = options; |
| 86 | contextOptions.fExecutor = nullptr; |
| 87 | GrContextFactory serialFactory(contextOptions); |
| 88 | |
| 89 | std::unique_ptr<SkExecutor> threadPool = SkExecutor::MakeFIFOThreadPool(1); |
| 90 | contextOptions.fExecutor = threadPool.get(); |
| 91 | GrContextFactory threadedFactory(contextOptions); |
| 92 | |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 93 | GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i); |
| 94 | ContextInfo serialInfo = serialFactory.getContextInfo(ctxType); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 95 | if (auto serialContext = serialInfo.directContext()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 96 | REPORTER_ASSERT(reporter, nullptr == serialContext->priv().getTaskGroup()); |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | ContextInfo threadedInfo = threadedFactory.getContextInfo(ctxType); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 100 | if (auto threadedContext = threadedInfo.directContext()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, nullptr != threadedContext->priv().getTaskGroup()); |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 106 | #ifdef SK_ENABLE_DUMP_GPU |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 107 | DEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump, reporter, ctxInfo) { |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 108 | // Ensure that GrDirectContext::dump doesn't assert (which is possible, if the JSON code |
| 109 | // is wrong) |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 110 | SkString result = ctxInfo.directContext()->dump(); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, !result.isEmpty()); |
| 112 | } |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 113 | #endif |