1 // Copyright 2019 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 
5 #include "ui/ozone/platform/drm/gpu/drm_overlay_manager_gpu.h"
6 
7 #include <utility>
8 
9 #include "base/bind.h"
10 #include "base/trace_event/trace_event.h"
11 #include "ui/ozone/platform/drm/gpu/drm_thread_proxy.h"
12 #include "ui/ozone/public/overlay_surface_candidate.h"
13 
14 namespace ui {
15 
DrmOverlayManagerGpu(DrmThreadProxy * drm_thread_proxy)16 DrmOverlayManagerGpu::DrmOverlayManagerGpu(DrmThreadProxy* drm_thread_proxy)
17     : drm_thread_proxy_(drm_thread_proxy) {}
18 
19 DrmOverlayManagerGpu::~DrmOverlayManagerGpu() = default;
20 
SendOverlayValidationRequest(const std::vector<OverlaySurfaceCandidate> & candidates,gfx::AcceleratedWidget widget)21 void DrmOverlayManagerGpu::SendOverlayValidationRequest(
22     const std::vector<OverlaySurfaceCandidate>& candidates,
23     gfx::AcceleratedWidget widget) {
24   TRACE_EVENT_ASYNC_BEGIN0(
25       "hwoverlays", "DrmOverlayManagerGpu::SendOverlayValidationRequest", this);
26   SetClearCacheCallbackIfNecessary();
27   drm_thread_proxy_->CheckOverlayCapabilities(
28       widget, candidates,
29       base::BindOnce(&DrmOverlayManagerGpu::ReceiveOverlayValidationResponse,
30                      weak_ptr_factory_.GetWeakPtr()));
31 }
32 
33 std::vector<OverlayStatus>
SendOverlayValidationRequestSync(const std::vector<OverlaySurfaceCandidate> & candidates,gfx::AcceleratedWidget widget)34 DrmOverlayManagerGpu::SendOverlayValidationRequestSync(
35     const std::vector<OverlaySurfaceCandidate>& candidates,
36     gfx::AcceleratedWidget widget) {
37   TRACE_EVENT_ASYNC_BEGIN0(
38       "hwoverlays", "DrmOverlayManagerGpu::SendOverlayValidationRequestSync",
39       this);
40   SetClearCacheCallbackIfNecessary();
41   return drm_thread_proxy_->CheckOverlayCapabilitiesSync(widget, candidates);
42 }
43 
SetClearCacheCallbackIfNecessary()44 void DrmOverlayManagerGpu::SetClearCacheCallbackIfNecessary() {
45   // Adds a callback for the DRM thread to let us know when display
46   // configuration has changed and to reset cache of valid overlay
47   // configurations. This happens in SendOverlayValidationRequest() because the
48   // DrmThread has been started by this point *and* we are on the thread the
49   // callback should run on. Those two conditions are not necessarily true in
50   // the constructor.
51   if (!has_set_clear_cache_callback_) {
52     has_set_clear_cache_callback_ = true;
53     drm_thread_proxy_->SetClearOverlayCacheCallback(base::BindRepeating(
54         &DrmOverlayManagerGpu::ResetCache, weak_ptr_factory_.GetWeakPtr()));
55   }
56 }
57 
ReceiveOverlayValidationResponse(gfx::AcceleratedWidget widget,const std::vector<OverlaySurfaceCandidate> & candidates,const std::vector<OverlayStatus> & status)58 void DrmOverlayManagerGpu::ReceiveOverlayValidationResponse(
59     gfx::AcceleratedWidget widget,
60     const std::vector<OverlaySurfaceCandidate>& candidates,
61     const std::vector<OverlayStatus>& status) {
62   TRACE_EVENT_ASYNC_END0(
63       "hwoverlays", "DrmOverlayManagerGpu::SendOverlayValidationRequest", this);
64 
65   UpdateCacheForOverlayCandidates(candidates, widget, status);
66 }
67 
68 }  // namespace ui
69