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 "src/gpu/GrCaps.h"
12 #include "src/gpu/GrContextPriv.h"
13 #include "tests/Test.h"
14 #include "tools/gpu/GrContextFactory.h"
15
16 using namespace sk_gpu_test;
17
DEF_GPUTEST(GrContextFactory_abandon,reporter,options)18 DEF_GPUTEST(GrContextFactory_abandon, reporter, options) {
19 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
20 GrContextFactory testFactory(options);
21 GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
22 ContextInfo info1 = testFactory.getContextInfo(ctxType);
23 if (!info1.grContext()) {
24 continue;
25 }
26 REPORTER_ASSERT(reporter, info1.testContext());
27 // Ref for comparison. The API does not explicitly say that this stays alive.
28 info1.grContext()->ref();
29 testFactory.abandonContexts();
30
31 // Test that we get different context after abandon.
32 ContextInfo info2 = testFactory.getContextInfo(ctxType);
33 REPORTER_ASSERT(reporter, info2.grContext());
34 REPORTER_ASSERT(reporter, info2.testContext());
35
36 REPORTER_ASSERT(reporter, info1.grContext() != info2.grContext());
37 // The GL context should also change, but it also could get the same address.
38
39 info1.grContext()->unref();
40 }
41 }
42
DEF_GPUTEST(GrContextFactory_sharedContexts,reporter,options)43 DEF_GPUTEST(GrContextFactory_sharedContexts, reporter, options) {
44 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
45 GrContextFactory testFactory(options);
46 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.
57 ContextInfo info2 = testFactory.getSharedContextInfo(info1.grContext());
58 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.
65 ContextInfo info4 = testFactory.getSharedContextInfo(info3.grContext());
66 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.
73 ContextInfo info5 = testFactory.getSharedContextInfo(info3.grContext(), 1);
74 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());
78 }
79 }
80
DEF_GPUTEST(GrContextFactory_executorAndTaskGroup,reporter,options)81 DEF_GPUTEST(GrContextFactory_executorAndTaskGroup, reporter, options) {
82 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
83 // 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
92 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
93 ContextInfo serialInfo = serialFactory.getContextInfo(ctxType);
94 if (GrContext* serialContext = serialInfo.grContext()) {
95 REPORTER_ASSERT(reporter, nullptr == serialContext->priv().getTaskGroup());
96 }
97
98 ContextInfo threadedInfo = threadedFactory.getContextInfo(ctxType);
99 if (GrContext* threadedContext = threadedInfo.grContext()) {
100 REPORTER_ASSERT(reporter, nullptr != threadedContext->priv().getTaskGroup());
101 }
102 }
103 }
104
105 #ifdef SK_ENABLE_DUMP_GPU
DEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump,reporter,ctxInfo)106 DEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump, reporter, ctxInfo) {
107 // Ensure that GrContext::dump doesn't assert (which is possible, if the JSON code is wrong)
108 SkString result = ctxInfo.grContext()->dump();
109 REPORTER_ASSERT(reporter, !result.isEmpty());
110 }
111 #endif
112