blob: 0ddb69a3b01b8c71161e0ecd9cac45832275b589 [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"
Robert Phillips6d344c32020-07-06 10:56:46 -040011#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040013#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/Test.h"
15#include "tools/gpu/GrContextFactory.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000016
bsalomonf2f1c172016-04-05 12:59:06 -070017using namespace sk_gpu_test;
bsalomon3724e572016-03-30 18:56:19 -070018
Brian Salomondcfca432017-11-15 15:48:03 -050019DEF_GPUTEST(GrContextFactory_abandon, reporter, options) {
bsalomon85b4b532016-04-05 11:06:27 -070020 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -050021 GrContextFactory testFactory(options);
bsalomon85b4b532016-04-05 11:06:27 -070022 GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
bsalomonf2f1c172016-04-05 12:59:06 -070023 ContextInfo info1 = testFactory.getContextInfo(ctxType);
Robert Phillips6d344c32020-07-06 10:56:46 -040024 if (!info1.directContext()) {
kkinnunen34058002016-01-06 23:49:30 -080025 continue;
26 }
bsalomon0fd3c812016-05-11 10:38:05 -070027 REPORTER_ASSERT(reporter, info1.testContext());
kkinnunen34058002016-01-06 23:49:30 -080028 // Ref for comparison. The API does not explicitly say that this stays alive.
Robert Phillips6d344c32020-07-06 10:56:46 -040029 info1.directContext()->ref();
kkinnunen34058002016-01-06 23:49:30 -080030 testFactory.abandonContexts();
31
32 // Test that we get different context after abandon.
bsalomonf2f1c172016-04-05 12:59:06 -070033 ContextInfo info2 = testFactory.getContextInfo(ctxType);
Robert Phillips6d344c32020-07-06 10:56:46 -040034 REPORTER_ASSERT(reporter, info2.directContext());
bsalomon0fd3c812016-05-11 10:38:05 -070035 REPORTER_ASSERT(reporter, info2.testContext());
36
Robert Phillips6d344c32020-07-06 10:56:46 -040037 REPORTER_ASSERT(reporter, info1.directContext() != info2.directContext());
bsalomon8b7451a2016-05-11 06:33:06 -070038 // The GL context should also change, but it also could get the same address.
kkinnunen34058002016-01-06 23:49:30 -080039
Robert Phillips6d344c32020-07-06 10:56:46 -040040 info1.directContext()->unref();
kkinnunen34058002016-01-06 23:49:30 -080041 }
42}
43
Brian Salomondcfca432017-11-15 15:48:03 -050044DEF_GPUTEST(GrContextFactory_sharedContexts, reporter, options) {
Brian Osman60c774d2017-02-21 16:58:08 -050045 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -050046 GrContextFactory testFactory(options);
Brian Osman60c774d2017-02-21 16:58:08 -050047 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
48 ContextInfo info1 = testFactory.getContextInfo(ctxType);
Robert Phillips6d344c32020-07-06 10:56:46 -040049 if (!info1.directContext()) {
Brian Osman60c774d2017-02-21 16:58:08 -050050 continue;
51 }
52
53 // Ref for passing in. The API does not explicitly say that this stays alive.
Robert Phillips6d344c32020-07-06 10:56:46 -040054 info1.directContext()->ref();
Brian Osman60c774d2017-02-21 16:58:08 -050055 testFactory.abandonContexts();
56
57 // Test that creating a context in a share group with an abandoned context fails.
Robert Phillips00f78de2020-07-01 16:09:43 -040058 ContextInfo info2 = testFactory.getSharedContextInfo(info1.directContext());
Robert Phillips6d344c32020-07-06 10:56:46 -040059 REPORTER_ASSERT(reporter, !info2.directContext());
60 info1.directContext()->unref();
Brian Osman60c774d2017-02-21 16:58:08 -050061
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 Phillips00f78de2020-07-01 16:09:43 -040066 ContextInfo info4 = testFactory.getSharedContextInfo(info3.directContext());
Robert Phillips6d344c32020-07-06 10:56:46 -040067 if (!info4.directContext()) {
Brian Osman60c774d2017-02-21 16:58:08 -050068 continue;
69 }
Robert Phillips6d344c32020-07-06 10:56:46 -040070 REPORTER_ASSERT(reporter, info3.directContext() != info4.directContext());
Brian Osman60c774d2017-02-21 16:58:08 -050071 REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext());
72
73 // Passing a different index should create a new (unique) context.
Robert Phillips00f78de2020-07-01 16:09:43 -040074 ContextInfo info5 = testFactory.getSharedContextInfo(info3.directContext(), 1);
Robert Phillips6d344c32020-07-06 10:56:46 -040075 REPORTER_ASSERT(reporter, info5.directContext());
Brian Osman60c774d2017-02-21 16:58:08 -050076 REPORTER_ASSERT(reporter, info5.testContext());
Robert Phillips6d344c32020-07-06 10:56:46 -040077 REPORTER_ASSERT(reporter, info5.directContext() != info4.directContext());
Brian Osman60c774d2017-02-21 16:58:08 -050078 REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext());
Brian Osman60c774d2017-02-21 16:58:08 -050079 }
80}
81
Brian Salomondcfca432017-11-15 15:48:03 -050082DEF_GPUTEST(GrContextFactory_executorAndTaskGroup, reporter, options) {
Brian Osman51279982017-08-23 10:12:00 -040083 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -050084 // 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 Osman51279982017-08-23 10:12:00 -040093 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
94 ContextInfo serialInfo = serialFactory.getContextInfo(ctxType);
Robert Phillips6d344c32020-07-06 10:56:46 -040095 if (auto serialContext = serialInfo.directContext()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050096 REPORTER_ASSERT(reporter, nullptr == serialContext->priv().getTaskGroup());
Brian Osman51279982017-08-23 10:12:00 -040097 }
98
99 ContextInfo threadedInfo = threadedFactory.getContextInfo(ctxType);
Robert Phillips6d344c32020-07-06 10:56:46 -0400100 if (auto threadedContext = threadedInfo.directContext()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500101 REPORTER_ASSERT(reporter, nullptr != threadedContext->priv().getTaskGroup());
Brian Osman51279982017-08-23 10:12:00 -0400102 }
103 }
104}
105
Kevin Lubickf4def342018-10-04 12:52:50 -0400106#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400107DEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump, reporter, ctxInfo) {
Robert Phillipse94b4e12020-07-23 13:54:35 -0400108 // Ensure that GrDirectContext::dump doesn't assert (which is possible, if the JSON code
109 // is wrong)
Robert Phillips6d344c32020-07-06 10:56:46 -0400110 SkString result = ctxInfo.directContext()->dump();
Brian Osman71a18892017-08-10 10:23:25 -0400111 REPORTER_ASSERT(reporter, !result.isEmpty());
112}
Kevin Lubickf4def342018-10-04 12:52:50 -0400113#endif