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