Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Xiyuan Xia | 40cd41d | 2018-12-18 16:22:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ui/aura/window_occlusion_change_builder.h" |
| 6 | |
Hans Wennborg | 8586102b | 2020-05-05 13:43:29 | [diff] [blame] | 7 | #include "base/check_op.h" |
Xiyuan Xia | 40cd41d | 2018-12-18 16:22:43 | [diff] [blame] | 8 | #include "base/containers/flat_map.h" |
Xiyuan Xia | d7e4d94 | 2019-06-12 22:32:52 | [diff] [blame] | 9 | #include "components/viz/client/frame_eviction_manager.h" |
Xiyuan Xia | 40cd41d | 2018-12-18 16:22:43 | [diff] [blame] | 10 | #include "third_party/skia/include/core/SkRegion.h" |
| 11 | #include "ui/aura/window_tracker.h" |
| 12 | |
| 13 | namespace aura { |
| 14 | |
| 15 | // Provide updating of occlusion info on aura::Window. |
| 16 | class DefaultWindowOcclusionChangeBuilder |
| 17 | : public WindowOcclusionChangeBuilder { |
| 18 | public: |
| 19 | DefaultWindowOcclusionChangeBuilder() = default; |
Peter Boström | c8c1235 | 2021-09-21 23:37:15 | [diff] [blame] | 20 | |
| 21 | DefaultWindowOcclusionChangeBuilder( |
| 22 | const DefaultWindowOcclusionChangeBuilder&) = delete; |
| 23 | DefaultWindowOcclusionChangeBuilder& operator=( |
| 24 | const DefaultWindowOcclusionChangeBuilder&) = delete; |
| 25 | |
Xiyuan Xia | 40cd41d | 2018-12-18 16:22:43 | [diff] [blame] | 26 | ~DefaultWindowOcclusionChangeBuilder() override { |
Xiyuan Xia | d7e4d94 | 2019-06-12 22:32:52 | [diff] [blame] | 27 | // No frame eviction until all occlusion state changes are applied. |
| 28 | viz::FrameEvictionManager::ScopedPause scoped_frame_eviction_pause; |
| 29 | |
Xiyuan Xia | 40cd41d | 2018-12-18 16:22:43 | [diff] [blame] | 30 | while (!windows_.windows().empty()) { |
| 31 | Window* window = windows_.Pop(); |
| 32 | auto it = changes_.find(window); |
| 33 | if (it == changes_.end()) |
| 34 | continue; |
Xiyuan Xia | 40cd41d | 2018-12-18 16:22:43 | [diff] [blame] | 35 | window->SetOcclusionInfo(it->second.occlusion_state, |
| 36 | it->second.occluded_region); |
| 37 | } |
| 38 | changes_.clear(); |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | struct OcclusionData { |
| 43 | Window::OcclusionState occlusion_state; |
| 44 | SkRegion occluded_region; |
| 45 | }; |
| 46 | |
| 47 | // WindowOcclusionChangeBuilder: |
| 48 | void Add(Window* window, |
| 49 | Window::OcclusionState occlusion_state, |
| 50 | SkRegion occluded_region) override { |
| 51 | // Change back to UNKNOWN is not allowed. |
| 52 | DCHECK_NE(occlusion_state, Window::OcclusionState::UNKNOWN); |
| 53 | |
| 54 | windows_.Add(window); |
| 55 | changes_[window] = {occlusion_state, occluded_region}; |
| 56 | } |
| 57 | |
| 58 | // Tracks live windows that has a change. This is needed in addition to the |
| 59 | // keys in |changes_| because the window tree may change while changes are |
| 60 | // accumulated or being applied. |
| 61 | WindowTracker windows_; |
| 62 | |
| 63 | // Stores the accumulated occlusion changes. |
| 64 | base::flat_map<Window*, OcclusionData> changes_; |
Xiyuan Xia | 40cd41d | 2018-12-18 16:22:43 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | // static |
| 68 | std::unique_ptr<WindowOcclusionChangeBuilder> |
| 69 | WindowOcclusionChangeBuilder::Create() { |
| 70 | return std::make_unique<DefaultWindowOcclusionChangeBuilder>(); |
| 71 | } |
| 72 | |
| 73 | } // namespace aura |