1 /*
2  * Copyright (c) 2016-2017, 2019-2020 The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *    * Redistributions of source code must retain the above copyright
8  *      notice, this list of conditions and the following disclaimer.
9  *    * Redistributions in binary form must reproduce the above
10  *      copyright notice, this list of conditions and the following
11  *      disclaimer in the documentation and/or other materials provided
12  *      with the distribution.
13  *    * Neither the name of The Linux Foundation. nor the names of its
14  *      contributors may be used to endorse or promote products derived
15  *      from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include <utils/constants.h>
30 #include <utils/debug.h>
31 #include <utils/locker.h>
32 #include "hwc_callbacks.h"
33 
34 #define __CLASS__ "HWCCallbacks"
35 
36 namespace sdm {
37 
Hotplug(hwc2_display_t display,HWC2::Connection state)38 HWC2::Error HWCCallbacks::Hotplug(hwc2_display_t display, HWC2::Connection state) {
39   SCOPE_LOCK(hotplug_lock_);
40   DTRACE_SCOPED();
41 
42   // If client has not registered hotplug, wait for it finitely:
43   //   1. spurious wake up (!hotplug_), wait again
44   //   2. error = ETIMEDOUT, return with warning 1
45   //   3. error != ETIMEDOUT, return with warning 2
46   //   4. error == NONE and no spurious wake up, then !hotplug_ is false, exit loop
47   while (!hotplug_) {
48     DLOGW("Attempting to send client a hotplug too early Display = %" PRIu64 " state = %d", display,
49           state);
50     int ret = hotplug_lock_.WaitFinite(5000);
51     if (ret == ETIMEDOUT) {
52       DLOGW("Client didn't connect on time, dropping hotplug!");
53       return HWC2::Error::None;
54     } else if (ret != 0) {
55       DLOGW("Failed client connection wait. Error %s, dropping hotplug!", strerror(ret));
56       return HWC2::Error::None;
57     }
58   }
59   hotplug_(hotplug_data_, display, INT32(state));
60   return HWC2::Error::None;
61 }
62 
Refresh(hwc2_display_t display)63 HWC2::Error HWCCallbacks::Refresh(hwc2_display_t display) {
64   SCOPE_LOCK(refresh_lock_);
65   // Do not lock, will cause hotplug deadlock
66   DTRACE_SCOPED();
67   // If client has not registered refresh, drop it
68   if (!refresh_) {
69     return HWC2::Error::NoResources;
70   }
71   refresh_(refresh_data_, display);
72   pending_refresh_.set(UINT32(display));
73   return HWC2::Error::None;
74 }
75 
Vsync(hwc2_display_t display,int64_t timestamp)76 HWC2::Error HWCCallbacks::Vsync(hwc2_display_t display, int64_t timestamp) {
77   SCOPE_LOCK(vsync_lock_);
78   // Do not lock, may cause hotplug deadlock
79   DTRACE_SCOPED();
80   // If client has not registered vsync, drop it
81   if (!vsync_) {
82     return HWC2::Error::NoResources;
83   }
84   vsync_(vsync_data_, display, timestamp);
85   return HWC2::Error::None;
86 }
87 
Vsync_2_4(hwc2_display_t display,int64_t timestamp,uint32_t period)88 HWC2::Error HWCCallbacks::Vsync_2_4(hwc2_display_t display, int64_t timestamp, uint32_t period) {
89   SCOPE_LOCK(vsync_2_4_lock_);
90   DTRACE_SCOPED();
91   if (!vsync_2_4_) {
92     return HWC2::Error::NoResources;
93   }
94 
95   vsync_2_4_(vsync_2_4_data_, display, timestamp, period);
96   return HWC2::Error::None;
97 }
98 
VsyncPeriodTimingChanged(hwc2_display_t display,hwc_vsync_period_change_timeline_t * updated_timeline)99 HWC2::Error HWCCallbacks::VsyncPeriodTimingChanged(
100     hwc2_display_t display, hwc_vsync_period_change_timeline_t *updated_timeline) {
101   SCOPE_LOCK(vsync_period_timing_changed_lock_);
102   DTRACE_SCOPED();
103   if (!vsync_period_timing_changed_) {
104     return HWC2::Error::NoResources;
105   }
106 
107   vsync_period_timing_changed_(vsync_period_timing_changed_data_, display, updated_timeline);
108   return HWC2::Error::None;
109 }
110 
SeamlessPossible(hwc2_display_t display)111 HWC2::Error HWCCallbacks::SeamlessPossible(hwc2_display_t display) {
112   SCOPE_LOCK(seamless_possible_lock_);
113   DTRACE_SCOPED();
114   if (!seamless_possible_) {
115     return HWC2::Error::NoResources;
116   }
117 
118   seamless_possible_(seamless_possible_data_, display);
119   return HWC2::Error::None;
120 }
121 
Register(HWC2::Callback descriptor,hwc2_callback_data_t callback_data,hwc2_function_pointer_t pointer)122 HWC2::Error HWCCallbacks::Register(HWC2::Callback descriptor, hwc2_callback_data_t callback_data,
123                                    hwc2_function_pointer_t pointer) {
124   switch (descriptor) {
125     case HWC2::Callback::Hotplug: {
126       SCOPE_LOCK(hotplug_lock_);
127       hotplug_data_ = callback_data;
128       hotplug_ = reinterpret_cast<HWC2_PFN_HOTPLUG>(pointer);
129       client_connected_ = true;
130       hotplug_lock_.Broadcast();
131       } break;
132     case HWC2::Callback::Refresh: {
133       SCOPE_LOCK(refresh_lock_);
134       refresh_data_ = callback_data;
135       refresh_ = reinterpret_cast<HWC2_PFN_REFRESH>(pointer);
136       } break;
137     case HWC2::Callback::Vsync: {
138       SCOPE_LOCK(vsync_lock_);
139       vsync_data_ = callback_data;
140       vsync_ = reinterpret_cast<HWC2_PFN_VSYNC>(pointer);
141       } break;
142     case HWC2::Callback::Vsync_2_4: {
143       SCOPE_LOCK(vsync_2_4_lock_);
144       vsync_2_4_data_ = callback_data;
145       vsync_2_4_ = reinterpret_cast<HWC2_PFN_VSYNC_2_4>(pointer);
146       } break;
147     case HWC2::Callback::VsyncPeriodTimingChanged: {
148       SCOPE_LOCK(vsync_period_timing_changed_lock_);
149       vsync_period_timing_changed_data_ = callback_data;
150       vsync_period_timing_changed_ =
151           reinterpret_cast<HWC2_PFN_VSYNC_PERIOD_TIMING_CHANGED>(pointer);
152       } break;
153     case HWC2::Callback::SeamlessPossible: {
154       SCOPE_LOCK(seamless_possible_lock_);
155       seamless_possible_data_ = callback_data;
156       seamless_possible_ = reinterpret_cast<HWC2_PFN_SEAMLESS_POSSIBLE>(pointer);
157       } break;
158     default:
159       return HWC2::Error::BadParameter;
160   }
161 
162   return HWC2::Error::None;
163 }
164 
165 }  // namespace sdm
166