window_occlusion_change_builder.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2018 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/aura/window_occlusion_change_builder.h"
  5. #include "base/check_op.h"
  6. #include "base/containers/flat_map.h"
  7. #include "components/viz/client/frame_eviction_manager.h"
  8. #include "third_party/skia/include/core/SkRegion.h"
  9. #include "ui/aura/window_tracker.h"
  10. namespace aura {
  11. // Provide updating of occlusion info on aura::Window.
  12. class DefaultWindowOcclusionChangeBuilder
  13. : public WindowOcclusionChangeBuilder {
  14. public:
  15. DefaultWindowOcclusionChangeBuilder() = default;
  16. DefaultWindowOcclusionChangeBuilder(
  17. const DefaultWindowOcclusionChangeBuilder&) = delete;
  18. DefaultWindowOcclusionChangeBuilder& operator=(
  19. const DefaultWindowOcclusionChangeBuilder&) = delete;
  20. ~DefaultWindowOcclusionChangeBuilder() override {
  21. // No frame eviction until all occlusion state changes are applied.
  22. viz::FrameEvictionManager::ScopedPause scoped_frame_eviction_pause;
  23. while (!windows_.windows().empty()) {
  24. Window* window = windows_.Pop();
  25. auto it = changes_.find(window);
  26. if (it == changes_.end())
  27. continue;
  28. window->SetOcclusionInfo(it->second.occlusion_state,
  29. it->second.occluded_region);
  30. }
  31. changes_.clear();
  32. }
  33. private:
  34. struct OcclusionData {
  35. Window::OcclusionState occlusion_state;
  36. SkRegion occluded_region;
  37. };
  38. // WindowOcclusionChangeBuilder:
  39. void Add(Window* window,
  40. Window::OcclusionState occlusion_state,
  41. SkRegion occluded_region) override {
  42. // Change back to UNKNOWN is not allowed.
  43. DCHECK_NE(occlusion_state, Window::OcclusionState::UNKNOWN);
  44. windows_.Add(window);
  45. changes_[window] = {occlusion_state, occluded_region};
  46. }
  47. // Tracks live windows that has a change. This is needed in addition to the
  48. // keys in |changes_| because the window tree may change while changes are
  49. // accumulated or being applied.
  50. WindowTracker windows_;
  51. // Stores the accumulated occlusion changes.
  52. base::flat_map<Window*, OcclusionData> changes_;
  53. };
  54. // static
  55. std::unique_ptr<WindowOcclusionChangeBuilder>
  56. WindowOcclusionChangeBuilder::Create() {
  57. return std::make_unique<DefaultWindowOcclusionChangeBuilder>();
  58. }
  59. } // namespace aura