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 "SkTypes.h"
9 
10 #include "GrContextFactory.h"
11 #include "GrContextPriv.h"
12 #include "GrCaps.h"
13 #include "SkExecutor.h"
14 #include "Test.h"
15 
16 using namespace sk_gpu_test;
17 
DEF_GPUTEST(GrContextFactory_NVPRContextOptionHasPathRenderingSupport,reporter,options)18 DEF_GPUTEST(GrContextFactory_NVPRContextOptionHasPathRenderingSupport, reporter, options) {
19     // Test that if NVPR is requested, the context always has path rendering
20     // or the context creation fails.
21     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
22         GrContextFactory testFactory(options);
23         // Test that if NVPR is possible, caps are in sync.
24         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
25         GrContext* context = testFactory.get(ctxType,
26                                            GrContextFactory::ContextOverrides::kRequireNVPRSupport);
27         if (!context) {
28             continue;
29         }
30         REPORTER_ASSERT(reporter,
31                         context->contextPriv().caps()->shaderCaps()->pathRenderingSupport());
32     }
33 }
34 
DEF_GPUTEST(GrContextFactory_NoPathRenderingIfNVPRDisabled,reporter,options)35 DEF_GPUTEST(GrContextFactory_NoPathRenderingIfNVPRDisabled, reporter, options) {
36     // Test that if NVPR is explicitly disabled, the context has no path rendering support.
37 
38     for (int i = 0; i <= GrContextFactory::kLastContextType; ++i) {
39         GrContextFactory testFactory(options);
40         GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType)i;
41         GrContext* context =
42             testFactory.get(ctxType, GrContextFactory::ContextOverrides::kDisableNVPR);
43         if (context) {
44             REPORTER_ASSERT(reporter,
45                             !context->contextPriv().caps()->shaderCaps()->pathRenderingSupport());
46         }
47     }
48 }
49 
DEF_GPUTEST(GrContextFactory_abandon,reporter,options)50 DEF_GPUTEST(GrContextFactory_abandon, reporter, options) {
51     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
52         GrContextFactory testFactory(options);
53         GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
54         ContextInfo info1 = testFactory.getContextInfo(ctxType);
55         if (!info1.grContext()) {
56             continue;
57         }
58         REPORTER_ASSERT(reporter, info1.testContext());
59          // Ref for comparison. The API does not explicitly say that this stays alive.
60         info1.grContext()->ref();
61         testFactory.abandonContexts();
62 
63         // Test that we get different context after abandon.
64         ContextInfo info2 = testFactory.getContextInfo(ctxType);
65         REPORTER_ASSERT(reporter, info2.grContext());
66         REPORTER_ASSERT(reporter, info2.testContext());
67 
68         REPORTER_ASSERT(reporter, info1.grContext() != info2.grContext());
69         // The GL context should also change, but it also could get the same address.
70 
71         info1.grContext()->unref();
72     }
73 }
74 
DEF_GPUTEST(GrContextFactory_sharedContexts,reporter,options)75 DEF_GPUTEST(GrContextFactory_sharedContexts, reporter, options) {
76     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
77         GrContextFactory testFactory(options);
78         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
79         ContextInfo info1 = testFactory.getContextInfo(ctxType);
80         if (!info1.grContext()) {
81             continue;
82         }
83 
84         // Ref for passing in. The API does not explicitly say that this stays alive.
85         info1.grContext()->ref();
86         testFactory.abandonContexts();
87 
88         // Test that creating a context in a share group with an abandoned context fails.
89         ContextInfo info2 = testFactory.getSharedContextInfo(info1.grContext());
90         REPORTER_ASSERT(reporter, !info2.grContext());
91         info1.grContext()->unref();
92 
93         // Create a new base context
94         ContextInfo info3 = testFactory.getContextInfo(ctxType);
95         if (!info3.grContext()) {
96             // Vulkan NexusPlayer bot fails here. Sigh.
97             continue;
98         }
99 
100         // Creating a context in a share group may fail, but should never crash.
101         ContextInfo info4 = testFactory.getSharedContextInfo(info3.grContext());
102         if (!info4.grContext()) {
103             continue;
104         }
105         REPORTER_ASSERT(reporter, info3.grContext() != info4.grContext());
106         REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext());
107 
108         // Passing a different index should create a new (unique) context.
109         ContextInfo info5 = testFactory.getSharedContextInfo(info3.grContext(), 1);
110         REPORTER_ASSERT(reporter, info5.grContext());
111         REPORTER_ASSERT(reporter, info5.testContext());
112         REPORTER_ASSERT(reporter, info5.grContext() != info4.grContext());
113         REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext());
114     }
115 }
116 
DEF_GPUTEST(GrContextFactory_executorAndTaskGroup,reporter,options)117 DEF_GPUTEST(GrContextFactory_executorAndTaskGroup, reporter, options) {
118     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
119         // Verify that contexts have a task group iff we supply an executor with context options
120         GrContextOptions contextOptions = options;
121         contextOptions.fExecutor = nullptr;
122         GrContextFactory serialFactory(contextOptions);
123 
124         std::unique_ptr<SkExecutor> threadPool = SkExecutor::MakeFIFOThreadPool(1);
125         contextOptions.fExecutor = threadPool.get();
126         GrContextFactory threadedFactory(contextOptions);
127 
128         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
129         ContextInfo serialInfo = serialFactory.getContextInfo(ctxType);
130         if (GrContext* serialContext = serialInfo.grContext()) {
131             REPORTER_ASSERT(reporter, nullptr == serialContext->contextPriv().getTaskGroup());
132         }
133 
134         ContextInfo threadedInfo = threadedFactory.getContextInfo(ctxType);
135         if (GrContext* threadedContext = threadedInfo.grContext()) {
136             REPORTER_ASSERT(reporter, nullptr != threadedContext->contextPriv().getTaskGroup());
137         }
138     }
139 }
140 
141 #ifdef SK_ENABLE_DUMP_GPU
DEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump,reporter,ctxInfo)142 DEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump, reporter, ctxInfo) {
143     // Ensure that GrContext::dump doesn't assert (which is possible, if the JSON code is wrong)
144     SkString result = ctxInfo.grContext()->contextPriv().dump();
145     REPORTER_ASSERT(reporter, !result.isEmpty());
146 }
147 #endif
148