blob: 6c3eb4cfab924c9c90e78d1d43235ec734748174 [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}
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040030bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
31 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
32}
Jim Van Verth5a8f3e42019-10-24 10:23:26 -040033bool IsRenderingGLOrMetalContextType(sk_gpu_test::GrContextFactory::ContextType type) {
34 return (IsGLContextType(type) || IsMetalContextType(type)) &&
35 GrContextFactory::IsRenderingContext(type);
36}
Brian Osman7c597742019-03-26 11:10:11 -040037bool IsMockContextType(sk_gpu_test::GrContextFactory::ContextType type) {
38 return type == GrContextFactory::kMock_ContextType;
39}
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040040
41void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
Brian Salomondcfca432017-11-15 15:48:03 -050042 Reporter* reporter, const GrContextOptions& options) {
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040043#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
44 static constexpr auto kNativeGLType = GrContextFactory::kGL_ContextType;
45#else
46 static constexpr auto kNativeGLType = GrContextFactory::kGLES_ContextType;
47#endif
48
49 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
50 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
51 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
52 // desktop since tests do not account for not fixing http://skbug.com/2809
53 if (contextType == GrContextFactory::kGL_ContextType ||
54 contextType == GrContextFactory::kGLES_ContextType) {
55 if (contextType != kNativeGLType) {
56 continue;
57 }
58 }
Brian Salomondcfca432017-11-15 15:48:03 -050059 // We destroy the factory and its associated contexts after each test. This is due to the
60 // fact that the command buffer sits on top of the native GL windowing (cgl, wgl, ...) but
61 // also tracks which of its contexts is current above that API and gets tripped up if the
62 // native windowing API is used directly outside of the command buffer code.
63 GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -060064 ContextInfo ctxInfo = factory.getContextInfo(contextType);
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040065 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
66 continue;
67 }
68
69 ReporterContext ctx(reporter, SkString(GrContextFactory::ContextTypeName(contextType)));
Robert Phillipsb87b39b2020-07-01 14:45:24 -040070 if (ctxInfo.directContext()) {
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040071 (*test)(reporter, ctxInfo);
Greg Daniel58b5a542020-04-09 14:55:00 -040072 // In case the test changed the current context make sure we move it back before
73 // calling flush.
74 ctxInfo.testContext()->makeCurrent();
Robert Phillipsb87b39b2020-07-01 14:45:24 -040075 ctxInfo.directContext()->flushAndSubmit();
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040076 }
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040077 }
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040078}
79} // namespace skiatest