test_compositor_host_ozone.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2013 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/compositor/test/test_compositor_host_ozone.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/check_op.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. #include "build/build_config.h"
  12. #include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
  13. #include "ui/compositor/compositor.h"
  14. #include "ui/gfx/geometry/rect.h"
  15. #include "ui/gfx/native_widget_types.h"
  16. #include "ui/ozone/public/ozone_platform.h"
  17. #include "ui/platform_window/platform_window.h"
  18. #include "ui/platform_window/platform_window_delegate.h"
  19. #include "ui/platform_window/platform_window_init_properties.h"
  20. namespace ui {
  21. // Stub implementation of PlatformWindowDelegate that stores the
  22. // AcceleratedWidget.
  23. class TestCompositorHostOzone::StubPlatformWindowDelegate
  24. : public PlatformWindowDelegate {
  25. public:
  26. StubPlatformWindowDelegate() {}
  27. StubPlatformWindowDelegate(const StubPlatformWindowDelegate&) = delete;
  28. StubPlatformWindowDelegate& operator=(const StubPlatformWindowDelegate&) =
  29. delete;
  30. ~StubPlatformWindowDelegate() override {}
  31. gfx::AcceleratedWidget widget() const { return widget_; }
  32. // PlatformWindowDelegate:
  33. void OnBoundsChanged(const BoundsChange& change) override {}
  34. void OnDamageRect(const gfx::Rect& damaged_region) override {}
  35. void DispatchEvent(Event* event) override {}
  36. void OnCloseRequest() override {}
  37. void OnClosed() override {}
  38. void OnWindowStateChanged(PlatformWindowState old_state,
  39. PlatformWindowState new_state) override {}
  40. void OnLostCapture() override {}
  41. void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override {
  42. widget_ = widget;
  43. }
  44. void OnWillDestroyAcceleratedWidget() override {}
  45. void OnAcceleratedWidgetDestroyed() override {
  46. widget_ = gfx::kNullAcceleratedWidget;
  47. }
  48. void OnActivationChanged(bool active) override {}
  49. void OnMouseEnter() override {}
  50. private:
  51. gfx::AcceleratedWidget widget_ = gfx::kNullAcceleratedWidget;
  52. };
  53. TestCompositorHostOzone::TestCompositorHostOzone(
  54. const gfx::Rect& bounds,
  55. ui::ContextFactory* context_factory)
  56. : bounds_(bounds),
  57. compositor_(context_factory->AllocateFrameSinkId(),
  58. context_factory,
  59. base::ThreadTaskRunnerHandle::Get(),
  60. false /* enable_pixel_canvas */),
  61. window_delegate_(std::make_unique<StubPlatformWindowDelegate>()) {}
  62. TestCompositorHostOzone::~TestCompositorHostOzone() {
  63. // |window_| should be destroyed earlier than |window_delegate_| as it refers
  64. // to its delegate on destroying.
  65. window_.reset();
  66. }
  67. void TestCompositorHostOzone::Show() {
  68. ui::PlatformWindowInitProperties properties;
  69. properties.bounds = bounds_;
  70. // Create a PlatformWindow to get the AcceleratedWidget backing it.
  71. window_ = ui::OzonePlatform::GetInstance()->CreatePlatformWindow(
  72. window_delegate_.get(), std::move(properties));
  73. window_->Show();
  74. DCHECK_NE(window_delegate_->widget(), gfx::kNullAcceleratedWidget);
  75. allocator_.GenerateId();
  76. compositor_.SetAcceleratedWidget(window_delegate_->widget());
  77. compositor_.SetScaleAndSize(1.0f, bounds_.size(),
  78. allocator_.GetCurrentLocalSurfaceId());
  79. compositor_.SetVisible(true);
  80. }
  81. ui::Compositor* TestCompositorHostOzone::GetCompositor() {
  82. return &compositor_;
  83. }
  84. // static
  85. TestCompositorHost* TestCompositorHost::Create(
  86. const gfx::Rect& bounds,
  87. ui::ContextFactory* context_factory) {
  88. return new TestCompositorHostOzone(bounds, context_factory);
  89. }
  90. } // namespace ui