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