blob: c8e89a4c8ca43c6f21489d5776a96ca2799f8aee [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.
Kevin Lubickcd654232022-09-21 11:41:10 -04006 *
7 * This file implements many functions defined in Test.h that are required to be implemented by
8 * test runners (such as DM) to support GPU backends.
Hal Canaryb6c5e5b2017-10-09 16:13:02 -04009 */
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "tests/Test.h"
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040012
Robert Phillipsb87b39b2020-07-01 14:45:24 -040013#include "include/gpu/GrDirectContext.h"
14
Kevin Lubick0f7b44e2023-02-28 09:13:11 -050015#if defined(SK_GRAPHITE)
Jim Van Verth42f710f2022-04-11 11:48:46 -040016#include "include/gpu/graphite/Context.h"
Robert Phillips30627592021-10-11 11:28:21 -040017#include "tools/graphite/ContextFactory.h"
Robert Phillipsb23372d2021-10-11 16:37:42 -040018#endif
Robert Phillips30627592021-10-11 11:28:21 -040019
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040020using sk_gpu_test::GrContextFactory;
Leon Scroggins1351a592022-02-02 22:03:33 +000021using sk_gpu_test::ContextInfo;
Kevin Lubick805acda2022-02-02 09:55:42 -050022
Kevin Lubick49df61f2022-02-03 10:54:07 -050023#ifdef SK_GL
24using sk_gpu_test::GLTestContext;
25#endif
26
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040027namespace skiatest {
28
John Stilesba7c5252023-08-25 10:50:10 -040029bool IsGLContextType(skgpu::ContextType type) {
John Stilesac39b122023-08-25 16:23:37 -040030#if defined(SK_GANESH)
John Stiles365cde72023-08-25 15:34:46 -040031 return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kOpenGL;
John Stilesac39b122023-08-25 16:23:37 -040032#else
33 return false;
34#endif
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040035}
John Stilesac39b122023-08-25 16:23:37 -040036
John Stilesba7c5252023-08-25 10:50:10 -040037bool IsVulkanContextType(skgpu::ContextType type) {
John Stilesac39b122023-08-25 16:23:37 -040038#if defined(SK_GANESH)
John Stiles365cde72023-08-25 15:34:46 -040039 return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kVulkan;
John Stilesac39b122023-08-25 16:23:37 -040040#elif defined(SK_GRAPHITE)
41 return skgpu::graphite::ContextTypeBackend(type) == BackendApi::kVulkan;
42#else
43 return false;
44#endif
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040045}
John Stilesac39b122023-08-25 16:23:37 -040046
John Stilesba7c5252023-08-25 10:50:10 -040047bool IsMetalContextType(skgpu::ContextType type) {
John Stilesac39b122023-08-25 16:23:37 -040048#if defined(SK_GANESH)
John Stiles365cde72023-08-25 15:34:46 -040049 return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kMetal;
John Stilesac39b122023-08-25 16:23:37 -040050#elif defined(SK_GRAPHITE)
51 return skgpu::graphite::ContextTypeBackend(type) == BackendApi::kMetal;
52#else
53 return false;
54#endif
Timothy Liang760dbc42018-07-17 13:28:20 -040055}
John Stilesac39b122023-08-25 16:23:37 -040056
John Stilesba7c5252023-08-25 10:50:10 -040057bool IsDirect3DContextType(skgpu::ContextType type) {
John Stilesac39b122023-08-25 16:23:37 -040058#if defined(SK_GANESH)
John Stiles365cde72023-08-25 15:34:46 -040059 return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kDirect3D;
John Stilesac39b122023-08-25 16:23:37 -040060#else
61 return false;
62#endif
Greg Danield4928d02020-06-19 11:13:26 -040063}
John Stilesac39b122023-08-25 16:23:37 -040064
John Stilesba7c5252023-08-25 10:50:10 -040065bool IsDawnContextType(skgpu::ContextType type) {
John Stilesac39b122023-08-25 16:23:37 -040066#if defined(SK_GRAPHITE)
67 return skgpu::graphite::ContextTypeBackend(type) == skgpu::BackendApi::kDawn;
68#else
69 return false;
70#endif
Greg Daniela58db7f2020-07-15 09:17:59 -040071}
John Stilesac39b122023-08-25 16:23:37 -040072
John Stilesba7c5252023-08-25 10:50:10 -040073bool IsMockContextType(skgpu::ContextType type) {
74 return type == skgpu::ContextType::kMock;
Brian Osman7c597742019-03-26 11:10:11 -040075}
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040076
John Stiles365cde72023-08-25 15:34:46 -040077void RunWithGaneshTestContexts(GrContextTestFn* testFn, ContextTypeFilterFn* filter,
Kevin Lubickcd654232022-09-21 11:41:10 -040078 Reporter* reporter, const GrContextOptions& options) {
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040079#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
John Stilesba7c5252023-08-25 10:50:10 -040080 static constexpr auto kNativeGLType = skgpu::ContextType::kGL;
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040081#else
John Stilesba7c5252023-08-25 10:50:10 -040082 static constexpr auto kNativeGLType = skgpu::ContextType::kGLES;
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040083#endif
84
John Stilesba7c5252023-08-25 10:50:10 -040085 for (int typeInt = 0; typeInt < skgpu::kContextTypeCount; ++typeInt) {
86 skgpu::ContextType contextType = static_cast<skgpu::ContextType>(typeInt);
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040087 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
88 // desktop since tests do not account for not fixing http://skbug.com/2809
John Stilesba7c5252023-08-25 10:50:10 -040089 if (contextType == skgpu::ContextType::kGL ||
90 contextType == skgpu::ContextType::kGLES) {
Hal Canaryb6c5e5b2017-10-09 16:13:02 -040091 if (contextType != kNativeGLType) {
92 continue;
93 }
94 }
Brian Salomondcfca432017-11-15 15:48:03 -050095 // We destroy the factory and its associated contexts after each test. This is due to the
96 // fact that the command buffer sits on top of the native GL windowing (cgl, wgl, ...) but
97 // also tracks which of its contexts is current above that API and gets tripped up if the
98 // native windowing API is used directly outside of the command buffer code.
99 GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -0600100 ContextInfo ctxInfo = factory.getContextInfo(contextType);
Kevin Lubickcd654232022-09-21 11:41:10 -0400101 if (filter && !(*filter)(contextType)) {
Hal Canaryb6c5e5b2017-10-09 16:13:02 -0400102 continue;
103 }
104
John Stilesba7c5252023-08-25 10:50:10 -0400105 ReporterContext ctx(reporter, SkString(skgpu::ContextTypeName(contextType)));
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400106 if (ctxInfo.directContext()) {
Kevin Lubickcd654232022-09-21 11:41:10 -0400107 (*testFn)(reporter, ctxInfo);
Greg Daniel58b5a542020-04-09 14:55:00 -0400108 // In case the test changed the current context make sure we move it back before
109 // calling flush.
110 ctxInfo.testContext()->makeCurrent();
Brian Salomon72050802020-10-12 20:45:06 -0400111 // Sync so any release/finished procs get called.
Kevin Lubick99bcee22023-09-06 10:09:08 -0400112 ctxInfo.directContext()->flushAndSubmit(GrSyncCpu::kYes);
Hal Canaryb6c5e5b2017-10-09 16:13:02 -0400113 }
Hal Canaryb6c5e5b2017-10-09 16:13:02 -0400114 }
Hal Canaryb6c5e5b2017-10-09 16:13:02 -0400115}
Robert Phillips30627592021-10-11 11:28:21 -0400116
Kevin Lubick0f7b44e2023-02-28 09:13:11 -0500117#if defined(SK_GRAPHITE)
Robert Phillips30627592021-10-11 11:28:21 -0400118
119namespace graphite {
120
John Stilesb5e8e342023-08-22 14:59:00 -0400121void RunWithGraphiteTestContexts(GraphiteTestFn* test,
John Stiles365cde72023-08-25 15:34:46 -0400122 ContextTypeFilterFn* filter,
John Stilesb5e8e342023-08-22 14:59:00 -0400123 Reporter* reporter,
Brian Salomon23e1cb22023-12-05 19:46:43 -0500124 const skiatest::graphite::TestOptions& options) {
125 ContextFactory factory(options);
John Stilesba7c5252023-08-25 10:50:10 -0400126 for (int typeInt = 0; typeInt < skgpu::kContextTypeCount; ++typeInt) {
127 skgpu::ContextType contextType = static_cast<skgpu::ContextType>(typeInt);
Greg Daniel22d6c0f2022-10-12 15:26:09 -0400128 if (filter && !(*filter)(contextType)) {
129 continue;
130 }
131
John Stiles66270742023-09-19 11:37:36 -0400132 skiatest::graphite::ContextInfo ctxInfo = factory.getContextInfo(contextType);
John Stiles8aafaa32023-09-19 11:37:37 -0400133 if (!ctxInfo.fContext) {
Greg Danielcb0e5d92022-09-16 14:45:17 -0400134 continue;
135 }
Robert Phillips30627592021-10-11 11:28:21 -0400136
John Stilesba7c5252023-08-25 10:50:10 -0400137 ReporterContext ctx(reporter, SkString(skgpu::ContextTypeName(contextType)));
John Stiles8aafaa32023-09-19 11:37:37 -0400138 (*test)(reporter, ctxInfo.fContext, ctxInfo.fTestContext);
Robert Phillips30627592021-10-11 11:28:21 -0400139 }
Robert Phillips30627592021-10-11 11:28:21 -0400140}
141
142} // namespace graphite
143
Kevin Lubick0f7b44e2023-02-28 09:13:11 -0500144#endif // SK_GRAPHITE
Robert Phillips30627592021-10-11 11:28:21 -0400145
Hal Canaryb6c5e5b2017-10-09 16:13:02 -0400146} // namespace skiatest