Christopher Cameron | 7e987473 | 2019-10-25 00:40:36 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_METAL_UTIL_TEST_SHADER_H_ |
| 6 | #define COMPONENTS_METAL_UTIL_TEST_SHADER_H_ |
| 7 | |
| 8 | #include "base/callback.h" |
| 9 | #include "base/task_runner.h" |
| 10 | #include "base/time/time.h" |
| 11 | #include "components/metal_util/metal_util_export.h" |
| 12 | |
| 13 | namespace metal { |
| 14 | |
Christopher Cameron | 83dc2c4 | 2020-01-29 00:09:21 | [diff] [blame] | 15 | enum class METAL_UTIL_EXPORT TestShaderComponent { |
| 16 | // Test a shader compile from source. |
| 17 | kCompile, |
| 18 | // Test linking a precompiled shader. |
| 19 | kLink, |
| 20 | }; |
| 21 | |
Christopher Cameron | 7e987473 | 2019-10-25 00:40:36 | [diff] [blame] | 22 | enum class METAL_UTIL_EXPORT TestShaderResult { |
| 23 | // Not attempted (e.g, because macOS version does not support Metal). |
| 24 | kNotAttempted, |
| 25 | // Shader compile succeeded. |
| 26 | kSucceeded, |
| 27 | // Shader compile failed. |
| 28 | kFailed, |
| 29 | // Shader compile timed out. |
| 30 | kTimedOut, |
| 31 | }; |
| 32 | |
Christopher Cameron | d362a66 | 2019-11-27 17:45:18 | [diff] [blame] | 33 | using TestShaderCallback = |
| 34 | base::OnceCallback<void(TestShaderResult result, |
| 35 | const base::TimeDelta& method_time, |
| 36 | const base::TimeDelta& compile_time)>; |
Christopher Cameron | 7e987473 | 2019-10-25 00:40:36 | [diff] [blame] | 37 | |
Christopher Cameron | d362a66 | 2019-11-27 17:45:18 | [diff] [blame] | 38 | // A default timeout value for compiling the test shader. |
| 39 | constexpr base::TimeDelta kTestShaderTimeout = base::TimeDelta::FromMinutes(1); |
| 40 | |
| 41 | // Return the value kTestShaderTimeoutTime for |method_time| and |compile_time| |
| 42 | // if they time out. |
| 43 | constexpr base::TimeDelta kTestShaderTimeForever = |
| 44 | base::TimeDelta::FromMinutes(3); |
| 45 | |
| 46 | // A default delay before attempting to compile the test shader. |
Christopher Cameron | 83dc2c4 | 2020-01-29 00:09:21 | [diff] [blame] | 47 | constexpr base::TimeDelta kTestShaderDelay = base::TimeDelta::FromMinutes(3); |
Christopher Cameron | d362a66 | 2019-11-27 17:45:18 | [diff] [blame] | 48 | |
| 49 | // Attempt to asynchronously compile a trivial Metal shader. If |delay| is zero, |
| 50 | // then compile synchronously, otherwise, post a delayed task to do the compile. |
| 51 | // |callback| with the result when the shader succeeds or after |timeout| has |
| 52 | // elapsed. |
| 53 | // |
Christopher Cameron | 7e987473 | 2019-10-25 00:40:36 | [diff] [blame] | 54 | // This is used to determine of the Metal shader compiler is resposive. Note |
| 55 | // that |callback| will be called either on another thread or inside the |
Christopher Cameron | d362a66 | 2019-11-27 17:45:18 | [diff] [blame] | 56 | // TestShader function call. |
Christopher Cameron | 7e987473 | 2019-10-25 00:40:36 | [diff] [blame] | 57 | // https://crbug.com/974219 |
Christopher Cameron | d362a66 | 2019-11-27 17:45:18 | [diff] [blame] | 58 | void METAL_UTIL_EXPORT |
| 59 | TestShader(TestShaderCallback callback, |
| 60 | const base::TimeDelta& delay = kTestShaderDelay, |
Christopher Cameron | 83dc2c4 | 2020-01-29 00:09:21 | [diff] [blame] | 61 | const base::TimeDelta& timeout = kTestShaderTimeout, |
| 62 | TestShaderComponent component = TestShaderComponent::kLink); |
Christopher Cameron | 21c4abc | 2019-11-15 04:06:15 | [diff] [blame] | 63 | |
Christopher Cameron | 7e987473 | 2019-10-25 00:40:36 | [diff] [blame] | 64 | } // namespace metal |
| 65 | |
| 66 | #endif // COMPONENTS_METAL_UTIL_TEST_SHADER_H_ |