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