dcomp_surface_registry.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2021 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. #include "ui/gl/dcomp_surface_registry.h"
  5. #include "base/logging.h"
  6. #include "base/no_destructor.h"
  7. namespace gl {
  8. DCOMPSurfaceRegistry* DCOMPSurfaceRegistry::GetInstance() {
  9. static base::NoDestructor<DCOMPSurfaceRegistry> instance;
  10. return instance.get();
  11. }
  12. DCOMPSurfaceRegistry::DCOMPSurfaceRegistry() = default;
  13. DCOMPSurfaceRegistry::~DCOMPSurfaceRegistry() = default;
  14. base::UnguessableToken DCOMPSurfaceRegistry::RegisterDCOMPSurfaceHandle(
  15. base::win::ScopedHandle surface) {
  16. DVLOG(1) << __func__;
  17. base::UnguessableToken token = base::UnguessableToken::Create();
  18. DCHECK(surface_handle_map_.find(token) == surface_handle_map_.end());
  19. surface_handle_map_[token] = std::move(surface);
  20. DVLOG(1) << __func__ << ": Surface handle registered with token " << token;
  21. return token;
  22. }
  23. void DCOMPSurfaceRegistry::UnregisterDCOMPSurfaceHandle(
  24. const base::UnguessableToken& token) {
  25. DVLOG(1) << __func__;
  26. surface_handle_map_.erase(token);
  27. }
  28. base::win::ScopedHandle DCOMPSurfaceRegistry::TakeDCOMPSurfaceHandle(
  29. const base::UnguessableToken& token) {
  30. DVLOG(1) << __func__;
  31. auto surface_iter = surface_handle_map_.find(token);
  32. if (surface_iter != surface_handle_map_.end()) {
  33. // Take ownership.
  34. auto surface_handle = std::move(surface_iter->second);
  35. surface_handle_map_.erase(surface_iter);
  36. return surface_handle;
  37. }
  38. DLOG(ERROR) << __func__ << ": No surface handle found for token " << token;
  39. return base::win::ScopedHandle();
  40. }
  41. } // namespace gl