Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / ozone / platform / drm / host / drm_overlay_candidates_host.cc
blob2fcbe8c90a8f4309bb0adb8bb0fbb069cf25c386
1 // Copyright 2015 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.
5 #include "ui/ozone/platform/drm/host/drm_overlay_candidates_host.h"
7 #include <algorithm>
9 #include "ui/gfx/geometry/rect_conversions.h"
10 #include "ui/ozone/common/gpu/ozone_gpu_messages.h"
11 #include "ui/ozone/platform/drm/host/drm_gpu_platform_support_host.h"
13 namespace ui {
15 namespace {
16 const size_t kMaxCacheSize = 100;
17 } // namespace
19 bool DrmOverlayCandidatesHost::OverlayCompare::operator()(
20 const OverlayCheck_Params& l,
21 const OverlayCheck_Params& r) {
22 if (l.plane_z_order < r.plane_z_order)
23 return true;
24 if (l.plane_z_order > r.plane_z_order)
25 return false;
26 if (l.display_rect < r.display_rect)
27 return true;
28 if (l.display_rect != r.display_rect)
29 return false;
30 if (l.format < r.format)
31 return true;
32 if (l.format > r.format)
33 return false;
34 if (l.transform < r.transform)
35 return true;
36 if (l.transform > r.transform)
37 return false;
38 if (l.buffer_size.width() < r.buffer_size.width())
39 return true;
40 if (l.buffer_size.width() > r.buffer_size.width())
41 return false;
42 return l.buffer_size.height() < r.buffer_size.height();
45 DrmOverlayCandidatesHost::DrmOverlayCandidatesHost(
46 gfx::AcceleratedWidget widget,
47 DrmGpuPlatformSupportHost* platform_support)
48 : widget_(widget),
49 platform_support_(platform_support),
50 cache_(kMaxCacheSize) {
51 platform_support_->RegisterHandler(this);
54 DrmOverlayCandidatesHost::~DrmOverlayCandidatesHost() {
55 platform_support_->UnregisterHandler(this);
58 void DrmOverlayCandidatesHost::CheckOverlaySupport(
59 OverlaySurfaceCandidateList* candidates) {
60 if (candidates->size() == 2)
61 CheckSingleOverlay(candidates);
64 void DrmOverlayCandidatesHost::CheckSingleOverlay(
65 OverlaySurfaceCandidateList* candidates) {
66 OverlayCandidatesOzone::OverlaySurfaceCandidate* first = &(*candidates)[0];
67 OverlayCandidatesOzone::OverlaySurfaceCandidate* second = &(*candidates)[1];
68 OverlayCandidatesOzone::OverlaySurfaceCandidate* overlay;
69 if (first->plane_z_order == 0) {
70 overlay = second;
71 } else if (second->plane_z_order == 0) {
72 overlay = first;
73 } else {
74 return;
76 // 0.01 constant chosen to match DCHECKs in gfx::ToNearestRect and avoid
77 // that code asserting on quads that we accept.
78 if (!gfx::IsNearestRectWithinDistance(overlay->display_rect, 0.01f))
79 return;
80 if (overlay->transform == gfx::OVERLAY_TRANSFORM_INVALID)
81 return;
82 if (overlay->is_clipped &&
83 !overlay->clip_rect.Contains(overlay->quad_rect_in_target_space))
84 return;
86 OverlayCheck_Params lookup(*overlay);
87 auto iter = cache_.Get(lookup);
88 if (iter == cache_.end()) {
89 cache_.Put(lookup, false);
90 SendRequest(*candidates, lookup);
91 } else {
92 overlay->overlay_handled = iter->second;
96 void DrmOverlayCandidatesHost::OnChannelEstablished(
97 int host_id,
98 scoped_refptr<base::SingleThreadTaskRunner> send_runner,
99 const base::Callback<void(IPC::Message*)>& sender) {
102 void DrmOverlayCandidatesHost::OnChannelDestroyed(int host_id) {
105 bool DrmOverlayCandidatesHost::OnMessageReceived(const IPC::Message& message) {
106 bool handled = false;
107 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(DrmOverlayCandidatesHost, message, &handled)
108 IPC_MESSAGE_FORWARD(OzoneHostMsg_OverlayCapabilitiesReceived, this,
109 DrmOverlayCandidatesHost::OnOverlayResult)
110 IPC_END_MESSAGE_MAP()
111 return handled;
114 void DrmOverlayCandidatesHost::SendRequest(
115 const OverlaySurfaceCandidateList& candidates,
116 const OverlayCheck_Params& check) {
117 if (!platform_support_->IsConnected())
118 return;
119 pending_checks_.push_back(check);
120 std::vector<OverlayCheck_Params> list;
121 for (const auto& candidate : candidates)
122 list.push_back(OverlayCheck_Params(candidate));
123 platform_support_->Send(
124 new OzoneGpuMsg_CheckOverlayCapabilities(widget_, list));
127 void DrmOverlayCandidatesHost::OnOverlayResult(bool* handled,
128 gfx::AcceleratedWidget widget,
129 bool result) {
130 if (widget != widget_)
131 return;
132 *handled = true;
133 DCHECK(!pending_checks_.empty());
134 ProcessResult(pending_checks_.front(), result);
135 pending_checks_.pop_front();
138 void DrmOverlayCandidatesHost::ProcessResult(const OverlayCheck_Params& check,
139 bool result) {
140 if (result)
141 cache_.Put(check, true);
144 } // namespace ui