blob: b81d56a1b6a5f6727ae239480763c00d1956ed6e [file] [log] [blame]
Hal Canaryb6c5e5b2017-10-09 16:13:02 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Hal Canaryb6c5e5b2017-10-09 16:13:02 -04009
Robert Phillipsb87b39b2020-07-01 14:45:24 -040010#include "include/gpu/GrDirectContext.h"
11
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040012using sk_gpu_test::GrContextFactory;
13using sk_gpu_test::GLTestContext;
14using sk_gpu_test::ContextInfo;
15
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040016namespace skiatest {
17
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040018bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
Greg Danielbdf12ad2018-10-12 09:31:11 -040019 return GrBackendApi::kOpenGL == GrContextFactory::ContextTypeBackend(type);
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040020}
21bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
Greg Danielbdf12ad2018-10-12 09:31:11 -040022 return GrBackendApi::kVulkan == GrContextFactory::ContextTypeBackend(type);
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040023}
Timothy Liang760dbc42018-07-17 13:28:20 -040024bool IsMetalContextType(sk_gpu_test::GrContextFactory::ContextType type) {
Greg Danielbdf12ad2018-10-12 09:31:11 -040025 return GrBackendApi::kMetal == GrContextFactory::ContextTypeBackend(type);
Timothy Liang760dbc42018-07-17 13:28:20 -040026}
Greg Danield4928d02020-06-19 11:13:26 -040027bool IsDirect3DContextType(sk_gpu_test::GrContextFactory::ContextType type) {
28 return GrBackendApi::kDirect3D == GrContextFactory::ContextTypeBackend(type);
29}
Greg Daniela58db7f2020-07-15 09:17:59 -040030bool IsDawnContextType(sk_gpu_test::GrContextFactory::ContextType type) {
31 return GrBackendApi::kDawn == GrContextFactory::ContextTypeBackend(type);
32}
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040033bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
34 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
35}
Jim Van Verth5a8f3e42019-10-24 10:23:26 -040036bool IsRenderingGLOrMetalContextType(sk_gpu_test::GrContextFactory::ContextType type) {
37 return (IsGLContextType(type) || IsMetalContextType(type)) &&
38 GrContextFactory::IsRenderingContext(type);
39}
Brian Osman7c597742019-03-26 11:10:11 -040040bool IsMockContextType(sk_gpu_test::GrContextFactory::ContextType type) {
41 return type == GrContextFactory::kMock_ContextType;
42}
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040043
44void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
Brian Salomondcfca432017-11-15 15:48:03 -050045 Reporter* reporter, const GrContextOptions& options) {
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040046#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
47 static constexpr auto kNativeGLType = GrContextFactory::kGL_ContextType;
48#else
49 static constexpr auto kNativeGLType = GrContextFactory::kGLES_ContextType;
50#endif
51
52 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
53 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
54 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
55 // desktop since tests do not account for not fixing http://skbug.com/2809
56 if (contextType == GrContextFactory::kGL_ContextType ||
57 contextType == GrContextFactory::kGLES_ContextType) {
58 if (contextType != kNativeGLType) {
59 continue;
60 }
61 }
Brian Salomondcfca432017-11-15 15:48:03 -050062 // We destroy the factory and its associated contexts after each test. This is due to the
63 // fact that the command buffer sits on top of the native GL windowing (cgl, wgl, ...) but
64 // also tracks which of its contexts is current above that API and gets tripped up if the
65 // native windowing API is used directly outside of the command buffer code.
66 GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -060067 ContextInfo ctxInfo = factory.getContextInfo(contextType);
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040068 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
69 continue;
70 }
71
72 ReporterContext ctx(reporter, SkString(GrContextFactory::ContextTypeName(contextType)));
Robert Phillipsb87b39b2020-07-01 14:45:24 -040073 if (ctxInfo.directContext()) {
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040074 (*test)(reporter, ctxInfo);
Greg Daniel58b5a542020-04-09 14:55:00 -040075 // In case the test changed the current context make sure we move it back before
76 // calling flush.
77 ctxInfo.testContext()->makeCurrent();
Brian Salomon72050802020-10-12 20:45:06 -040078 // Sync so any release/finished procs get called.
79 ctxInfo.directContext()->flushAndSubmit(/*sync*/true);
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040080 }
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040081 }
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040082}
83} // namespace skiatest