• 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 "SkTypes.h"
9 
10 #if SK_SUPPORT_GPU
11 
12 #include "GrContextFactory.h"
13 #include "GrCaps.h"
14 #include "Test.h"
15 
16 using namespace sk_gpu_test;
17 
18 DEF_GPUTEST(GrContextFactory_NVPRContextOptionHasPathRenderingSupport, reporter, /*factory*/) {
19     // Test that if NVPR is requested, the context always has path rendering
20     // or the context creation fails.
21     GrContextFactory testFactory;
22     // Test that if NVPR is possible, caps are in sync.
23     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
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(
31             reporter,
32             context->caps()->shaderCaps()->pathRenderingSupport());
33     }
34 }
35 
36 DEF_GPUTEST(GrContextFactory_NoPathRenderingIfNVPRDisabled, reporter, /*factory*/) {
37     // Test that if NVPR is explicitly disabled, the context has no path rendering support.
38 
39     GrContextFactory testFactory;
40     for (int i = 0; i <= GrContextFactory::kLastContextType; ++i) {
41         GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType)i;
42         GrContext* context =
43             testFactory.get(ctxType, GrContextFactory::ContextOverrides::kDisableNVPR);
44         if (context) {
45             REPORTER_ASSERT(
46                 reporter,
47                 !context->caps()->shaderCaps()->pathRenderingSupport());
48         }
49     }
50 }
51 
52 DEF_GPUTEST(GrContextFactory_RequiredSRGBSupport, reporter, /*factory*/) {
53     // Test that if sRGB support is requested, the context always has that capability
54     // or the context creation fails. Also test that if the creation fails, a context
55     // created without that flag would not have had sRGB support.
56     GrContextFactory testFactory;
57     // Test that if sRGB is requested, caps are in sync.
58     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
59         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
60         GrContext* context =
61             testFactory.get(ctxType, GrContextFactory::ContextOverrides::kRequireSRGBSupport);
62 
63         if (context) {
64             REPORTER_ASSERT(reporter, context->caps()->srgbSupport());
65         } else {
66             context = testFactory.get(ctxType);
67             if (context) {
68                 REPORTER_ASSERT(reporter, !context->caps()->srgbSupport());
69             }
70         }
71     }
72 }
73 
74 DEF_GPUTEST(GrContextFactory_abandon, reporter, /*factory*/) {
75     GrContextFactory testFactory;
76     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
77         GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
78         ContextInfo info1 = testFactory.getContextInfo(ctxType);
79         if (!info1.grContext()) {
80             continue;
81         }
82         REPORTER_ASSERT(reporter, info1.testContext());
83          // Ref for comparison. The API does not explicitly say that this stays alive.
84         info1.grContext()->ref();
85         testFactory.abandonContexts();
86 
87         // Test that we get different context after abandon.
88         ContextInfo info2 = testFactory.getContextInfo(ctxType);
89         REPORTER_ASSERT(reporter, info2.grContext());
90         REPORTER_ASSERT(reporter, info2.testContext());
91 
92         REPORTER_ASSERT(reporter, info1.grContext() != info2.grContext());
93         // The GL context should also change, but it also could get the same address.
94 
95         info1.grContext()->unref();
96     }
97 }
98 
99 DEF_GPUTEST(GrContextFactory_sharedContexts, reporter, /*factory*/) {
100     GrContextFactory testFactory;
101 
102     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
103         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
104         ContextInfo info1 = testFactory.getContextInfo(ctxType);
105         if (!info1.grContext()) {
106             continue;
107         }
108 
109         // Ref for passing in. The API does not explicitly say that this stays alive.
110         info1.grContext()->ref();
111         testFactory.abandonContexts();
112 
113         // Test that creating a context in a share group with an abandoned context fails.
114         ContextInfo info2 = testFactory.getSharedContextInfo(info1.grContext());
115         REPORTER_ASSERT(reporter, !info2.grContext());
116         info1.grContext()->unref();
117 
118         // Create a new base context
119         ContextInfo info3 = testFactory.getContextInfo(ctxType);
120         if (!info3.grContext()) {
121             // Vulkan NexusPlayer bot fails here. Sigh.
122             continue;
123         }
124 
125         // Creating a context in a share group may fail, but should never crash.
126         ContextInfo info4 = testFactory.getSharedContextInfo(info3.grContext());
127         if (!info4.grContext()) {
128             continue;
129         }
130         REPORTER_ASSERT(reporter, info3.grContext() != info4.grContext());
131         REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext());
132 
133         // Passing a different index should create a new (unique) context.
134         ContextInfo info5 = testFactory.getSharedContextInfo(info3.grContext(), 1);
135         REPORTER_ASSERT(reporter, info5.grContext());
136         REPORTER_ASSERT(reporter, info5.testContext());
137         REPORTER_ASSERT(reporter, info5.grContext() != info4.grContext());
138         REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext());
139     }
140 }
141 
142 #endif
143