native_window.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2020 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 "gpu/vulkan/tests/native_window.h"
  5. #include "base/containers/flat_map.h"
  6. #include "ui/platform_window/platform_window_delegate.h"
  7. #include "ui/platform_window/platform_window_init_properties.h"
  8. #if defined(USE_OZONE)
  9. #include "ui/ozone/public/ozone_platform.h"
  10. #endif
  11. namespace gpu {
  12. namespace {
  13. class Window : public ui::PlatformWindowDelegate {
  14. public:
  15. Window() = default;
  16. ~Window() override = default;
  17. void Initialize(const gfx::Rect& bounds) {
  18. DCHECK(!platform_window_);
  19. #if defined(USE_OZONE)
  20. ui::PlatformWindowInitProperties props(bounds);
  21. platform_window_ = ui::OzonePlatform::GetInstance()->CreatePlatformWindow(
  22. this, std::move(props));
  23. #else
  24. NOTIMPLEMENTED();
  25. return;
  26. #endif
  27. platform_window_->Show();
  28. }
  29. gfx::AcceleratedWidget widget() const { return widget_; }
  30. private:
  31. // ui::PlatformWindowDelegate:
  32. void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override {
  33. widget_ = widget;
  34. }
  35. void OnBoundsChanged(const BoundsChange& change) override {}
  36. void OnDamageRect(const gfx::Rect& damaged_region) override {}
  37. void DispatchEvent(ui::Event* event) override {}
  38. void OnCloseRequest() override {}
  39. void OnClosed() override {}
  40. void OnWindowStateChanged(ui::PlatformWindowState old_state,
  41. ui::PlatformWindowState new_state) override {}
  42. void OnLostCapture() override {}
  43. void OnWillDestroyAcceleratedWidget() override {}
  44. void OnAcceleratedWidgetDestroyed() override {}
  45. void OnActivationChanged(bool active) override {}
  46. void OnMouseEnter() override {}
  47. std::unique_ptr<ui::PlatformWindow> platform_window_;
  48. gfx::AcceleratedWidget widget_ = gfx::kNullAcceleratedWidget;
  49. };
  50. base::flat_map<gfx::AcceleratedWidget, std::unique_ptr<Window>> g_windows_;
  51. } // namespace
  52. gfx::AcceleratedWidget CreateNativeWindow(const gfx::Rect& bounds) {
  53. auto window = std::make_unique<Window>();
  54. window->Initialize(bounds);
  55. gfx::AcceleratedWidget widget = window->widget();
  56. if (widget != gfx::kNullAcceleratedWidget)
  57. g_windows_[widget] = std::move(window);
  58. return widget;
  59. }
  60. void DestroyNativeWindow(gfx::AcceleratedWidget window) {
  61. auto it = g_windows_.find(window);
  62. DCHECK(it != g_windows_.end());
  63. g_windows_.erase(it);
  64. }
  65. } // namespace gpu