• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkExecutor.h"
9 #include "include/core/SkTypes.h"
10 #include "include/gpu/GrContextOptions.h"
11 #include "include/gpu/GrDirectContext.h"
12 #include "src/gpu/ganesh/GrDirectContextPriv.h"
13 #include "tests/CtsEnforcement.h"
14 #include "tests/Test.h"
15 #include "tools/gpu/FenceSync.h"
16 
17 #include <memory>
18 
19 using namespace sk_gpu_test;
20 
DEF_GANESH_TEST(GrContextFactory_abandon,reporter,options,CtsEnforcement::kNever)21 DEF_GANESH_TEST(GrContextFactory_abandon, reporter, options, CtsEnforcement::kNever) {
22     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
23         GrContextFactory testFactory(options);
24         GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
25         ContextInfo info1 = testFactory.getContextInfo(ctxType);
26         if (!info1.directContext()) {
27             continue;
28         }
29         REPORTER_ASSERT(reporter, info1.testContext());
30          // Ref for comparison. The API does not explicitly say that this stays alive.
31         info1.directContext()->ref();
32         testFactory.abandonContexts();
33 
34         // Test that we get different context after abandon.
35         ContextInfo info2 = testFactory.getContextInfo(ctxType);
36         REPORTER_ASSERT(reporter, info2.directContext());
37         REPORTER_ASSERT(reporter, info2.testContext());
38 
39         REPORTER_ASSERT(reporter, info1.directContext() != info2.directContext());
40         // The GL context should also change, but it also could get the same address.
41 
42         info1.directContext()->unref();
43     }
44 }
45 
DEF_GANESH_TEST(GrContextFactory_sharedContexts,reporter,options,CtsEnforcement::kApiLevel_T)46 DEF_GANESH_TEST(GrContextFactory_sharedContexts, reporter, options, CtsEnforcement::kApiLevel_T) {
47     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
48         GrContextFactory testFactory(options);
49         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
50         ContextInfo info1 = testFactory.getContextInfo(ctxType);
51         if (!info1.directContext()) {
52             continue;
53         }
54 
55         // Ref for passing in. The API does not explicitly say that this stays alive.
56         info1.directContext()->ref();
57         testFactory.abandonContexts();
58 
59         // Test that creating a context in a share group with an abandoned context fails.
60         ContextInfo info2 = testFactory.getSharedContextInfo(info1.directContext());
61         REPORTER_ASSERT(reporter, !info2.directContext());
62         info1.directContext()->unref();
63 
64         // Create a new base context
65         ContextInfo info3 = testFactory.getContextInfo(ctxType);
66 
67         // Creating a context in a share group may fail, but should never crash.
68         ContextInfo info4 = testFactory.getSharedContextInfo(info3.directContext());
69         if (!info4.directContext()) {
70             continue;
71         }
72         REPORTER_ASSERT(reporter, info3.directContext() != info4.directContext());
73         REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext());
74 
75         // Passing a different index should create a new (unique) context.
76         ContextInfo info5 = testFactory.getSharedContextInfo(info3.directContext(), 1);
77         REPORTER_ASSERT(reporter, info5.directContext());
78         REPORTER_ASSERT(reporter, info5.testContext());
79         REPORTER_ASSERT(reporter, info5.directContext() != info4.directContext());
80         REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext());
81     }
82 }
83 
DEF_GANESH_TEST(GrContextFactory_executorAndTaskGroup,reporter,options,CtsEnforcement::kNever)84 DEF_GANESH_TEST(GrContextFactory_executorAndTaskGroup, reporter, options, CtsEnforcement::kNever) {
85     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
86         // Verify that contexts have a task group iff we supply an executor with context options
87         GrContextOptions contextOptions = options;
88         contextOptions.fExecutor = nullptr;
89         GrContextFactory serialFactory(contextOptions);
90 
91         std::unique_ptr<SkExecutor> threadPool = SkExecutor::MakeFIFOThreadPool(1);
92         contextOptions.fExecutor = threadPool.get();
93         GrContextFactory threadedFactory(contextOptions);
94 
95         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
96         ContextInfo serialInfo = serialFactory.getContextInfo(ctxType);
97         if (auto serialContext = serialInfo.directContext()) {
98             REPORTER_ASSERT(reporter, nullptr == serialContext->priv().getTaskGroup());
99         }
100 
101         ContextInfo threadedInfo = threadedFactory.getContextInfo(ctxType);
102         if (auto threadedContext = threadedInfo.directContext()) {
103             REPORTER_ASSERT(reporter, nullptr != threadedContext->priv().getTaskGroup());
104         }
105     }
106 }
107 
108 #ifdef SK_ENABLE_DUMP_GPU
DEF_GANESH_TEST_FOR_ALL_CONTEXTS(GrContextDump,reporter,ctxInfo,CtsEnforcement::kNever)109 DEF_GANESH_TEST_FOR_ALL_CONTEXTS(GrContextDump, reporter, ctxInfo, CtsEnforcement::kNever) {
110     // Ensure that GrDirectContext::dump doesn't assert (which is possible, if the JSON code
111     // is wrong)
112     SkString result = ctxInfo.directContext()->dump();
113     REPORTER_ASSERT(reporter, !result.isEmpty());
114 }
115 #endif
116