1 /*
2 * Copyright 2017 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 "tests/Test.h"
9
10 #include "include/gpu/GrDirectContext.h"
11
12 #ifdef SK_GRAPHITE_ENABLED
13 #include "experimental/graphite/include/Context.h"
14 #include "tools/graphite/ContextFactory.h"
15 #endif
16
17 using sk_gpu_test::GrContextFactory;
18 using sk_gpu_test::ContextInfo;
19
20 #ifdef SK_GL
21 using sk_gpu_test::GLTestContext;
22 #endif
23
24 namespace skiatest {
25
IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type)26 bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
27 return GrBackendApi::kOpenGL == GrContextFactory::ContextTypeBackend(type);
28 }
IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type)29 bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
30 return GrBackendApi::kVulkan == GrContextFactory::ContextTypeBackend(type);
31 }
IsMetalContextType(sk_gpu_test::GrContextFactory::ContextType type)32 bool IsMetalContextType(sk_gpu_test::GrContextFactory::ContextType type) {
33 return GrBackendApi::kMetal == GrContextFactory::ContextTypeBackend(type);
34 }
IsDirect3DContextType(sk_gpu_test::GrContextFactory::ContextType type)35 bool IsDirect3DContextType(sk_gpu_test::GrContextFactory::ContextType type) {
36 return GrBackendApi::kDirect3D == GrContextFactory::ContextTypeBackend(type);
37 }
IsDawnContextType(sk_gpu_test::GrContextFactory::ContextType type)38 bool IsDawnContextType(sk_gpu_test::GrContextFactory::ContextType type) {
39 return GrBackendApi::kDawn == GrContextFactory::ContextTypeBackend(type);
40 }
IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type)41 bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
42 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
43 }
IsMockContextType(sk_gpu_test::GrContextFactory::ContextType type)44 bool IsMockContextType(sk_gpu_test::GrContextFactory::ContextType type) {
45 return type == GrContextFactory::kMock_ContextType;
46 }
47
RunWithGPUTestContexts(GrContextTestFn * test,GrContextTypeFilterFn * contextTypeFilter,Reporter * reporter,const GrContextOptions & options)48 void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
49 Reporter* reporter, const GrContextOptions& options) {
50 #if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
51 static constexpr auto kNativeGLType = GrContextFactory::kGL_ContextType;
52 #else
53 static constexpr auto kNativeGLType = GrContextFactory::kGLES_ContextType;
54 #endif
55
56 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
57 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
58 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
59 // desktop since tests do not account for not fixing http://skbug.com/2809
60 if (contextType == GrContextFactory::kGL_ContextType ||
61 contextType == GrContextFactory::kGLES_ContextType) {
62 if (contextType != kNativeGLType) {
63 continue;
64 }
65 }
66 // We destroy the factory and its associated contexts after each test. This is due to the
67 // fact that the command buffer sits on top of the native GL windowing (cgl, wgl, ...) but
68 // also tracks which of its contexts is current above that API and gets tripped up if the
69 // native windowing API is used directly outside of the command buffer code.
70 GrContextFactory factory(options);
71 ContextInfo ctxInfo = factory.getContextInfo(contextType);
72 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
73 continue;
74 }
75
76 ReporterContext ctx(reporter, SkString(GrContextFactory::ContextTypeName(contextType)));
77 if (ctxInfo.directContext()) {
78 (*test)(reporter, ctxInfo);
79 // In case the test changed the current context make sure we move it back before
80 // calling flush.
81 ctxInfo.testContext()->makeCurrent();
82 // Sync so any release/finished procs get called.
83 ctxInfo.directContext()->flushAndSubmit(/*sync*/true);
84 }
85 }
86 }
87
88 #ifdef SK_GRAPHITE_ENABLED
89
90 namespace graphite {
91
RunWithGraphiteTestContexts(GraphiteTestFn * test,Reporter * reporter)92 void RunWithGraphiteTestContexts(GraphiteTestFn* test, Reporter* reporter) {
93 ContextFactory factory;
94
95 auto [_, context] = factory.getContextInfo(ContextFactory::ContextType::kMetal);
96 if (!context) {
97 return;
98 }
99
100 (*test)(reporter, context);
101 }
102
103 } // namespace graphite
104
105 #endif // SK_GRAPHITE_ENABLED
106
107 } // namespace skiatest
108