summaryrefslogtreecommitdiff
path: root/chromium/components/metal_util/test_shader.h
blob: 87b3d63e902615fafa47d664794366baf61ab72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_METAL_UTIL_TEST_SHADER_H_
#define COMPONENTS_METAL_UTIL_TEST_SHADER_H_

#include <vector>
#include "base/callback.h"
#include "base/task_runner.h"
#include "base/time/time.h"
#include "components/metal_util/metal_util_export.h"

namespace metal {

enum class TestShaderComponent {
  // Test a shader compile from source.
  kCompile,
  // Test linking a precompiled shader.
  kLink,
};

enum class TestShaderResult {
  // Not attempted (e.g, because macOS version does not support Metal).
  kNotAttempted,
  // Shader compile succeeded.
  kSucceeded,
  // Shader compile failed.
  kFailed,
  // Shader compile timed out.
  kTimedOut,
};

using TestShaderCallback =
    base::OnceCallback<void(TestShaderComponent component,
                            TestShaderResult result,
                            const base::TimeDelta& compile_time)>;

// A default timeout value for compiling the test shader.
constexpr base::TimeDelta kTestShaderTimeout = base::TimeDelta::FromMinutes(1);

// Return the value kTestShaderTimeoutTime for |compile_time| if it times out.
constexpr base::TimeDelta kTestShaderTimeForever =
    base::TimeDelta::FromMinutes(3);

// A default delay before attempting to compile the test shader.
constexpr base::TimeDelta kTestShaderDelay = base::TimeDelta::FromMinutes(3);

// Attempt to asynchronously compile a trivial Metal shader. If |delay| is zero,
// then compile synchronously, otherwise, post a delayed task to do the compile.
// |callback| with the result when the shader succeeds or after |timeout| has
// elapsed. Whether compile or link was tested is communicated to |callback| in
// its |component| argument.
//
// This is used to determine of the Metal shader compiler is resposive. Note
// that |callback| will be called either on another thread or inside the
// TestShader function call.
// https://crbug.com/974219
METAL_UTIL_EXPORT void TestShader(
    TestShaderCallback callback,
    const base::TimeDelta& delay = kTestShaderDelay,
    const base::TimeDelta& timeout = kTestShaderTimeForever);

// Exposed for testing.
METAL_UTIL_EXPORT extern const size_t kTestLibSize;
METAL_UTIL_EXPORT extern const size_t kLiteralOffset;
METAL_UTIL_EXPORT extern const size_t kLiteralSize;
METAL_UTIL_EXPORT std::vector<uint8_t> GetAlteredLibraryData();

}  // namespace metal

#endif  // COMPONENTS_METAL_UTIL_TEST_SHADER_H_