gl_implementation_wrapper.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef UI_GL_GL_IMPLEMENTATION_WRAPPER_H_
  5. #define UI_GL_GL_IMPLEMENTATION_WRAPPER_H_
  6. #include <memory>
  7. #include "base/command_line.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "ui/gl/gl_switches.h"
  10. #define GL_IMPL_WRAPPER_TYPE(name) \
  11. GLImplementationWrapper<name##Api, Trace##name##Api, Log##name##Api>
  12. namespace gl {
  13. // Wraps a GLApi with its tracing and logging variants when the corresponding
  14. // command line flags are passed.
  15. template <class GLImplApi, class GLTraceImplApi, class GLLogImplApi>
  16. class GLImplementationWrapper {
  17. public:
  18. GLImplementationWrapper(std::unique_ptr<GLImplApi> real_gl)
  19. : real_gl_(std::move(real_gl)) {
  20. gl_api_ = real_gl_.get();
  21. static bool enable_tracing =
  22. base::CommandLine::ForCurrentProcess()->HasSwitch(
  23. switches::kEnableGPUServiceTracing);
  24. if (enable_tracing) {
  25. trace_gl_ = std::make_unique<GLTraceImplApi>(gl_api_);
  26. gl_api_ = trace_gl_.get();
  27. }
  28. static bool enable_logging =
  29. base::CommandLine::ForCurrentProcess()->HasSwitch(
  30. switches::kEnableGPUServiceLogging);
  31. if (enable_logging) {
  32. log_gl_ = std::make_unique<GLLogImplApi>(gl_api_);
  33. gl_api_ = log_gl_.get();
  34. }
  35. }
  36. GLImplementationWrapper(const GLImplementationWrapper&) = delete;
  37. GLImplementationWrapper& operator=(const GLImplementationWrapper&) = delete;
  38. ~GLImplementationWrapper() = default;
  39. GLImplApi* api() { return gl_api_; }
  40. private:
  41. std::unique_ptr<GLImplApi> real_gl_;
  42. std::unique_ptr<GLTraceImplApi> trace_gl_;
  43. std::unique_ptr<GLLogImplApi> log_gl_;
  44. raw_ptr<GLImplApi> gl_api_ = nullptr;
  45. };
  46. } // namespace gl
  47. #endif // UI_GL_GL_IMPLEMENTATION_WRAPPER_H_