1 #include "include/dvr/dvr_display_manager.h"
2 
3 #include <dvr/dvr_buffer.h>
4 #include <pdx/rpc/variant.h>
5 #include <private/android/AHardwareBufferHelpers.h>
6 #include <private/dvr/buffer_hub_client.h>
7 #include <private/dvr/buffer_hub_queue_client.h>
8 #include <private/dvr/display_manager_client.h>
9 
10 #include "dvr_internal.h"
11 
12 using android::AHardwareBuffer_convertToGrallocUsageBits;
13 using android::dvr::BufferConsumer;
14 using android::dvr::display::DisplayManagerClient;
15 using android::dvr::display::SurfaceAttributes;
16 using android::dvr::display::SurfaceAttribute;
17 using android::dvr::display::SurfaceState;
18 using android::dvr::CreateDvrReadBufferQueueFromConsumerQueue;
19 using android::pdx::rpc::EmptyVariant;
20 
21 namespace {
22 
23 // Extracts type and value from the attribute Variant and writes them into the
24 // respective fields of DvrSurfaceAttribute.
25 struct AttributeVisitor {
26   DvrSurfaceAttribute* attribute;
27 
operator ()__anon5c1839f30111::AttributeVisitor28   void operator()(int32_t value) {
29     attribute->value.int32_value = value;
30     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_INT32;
31   }
operator ()__anon5c1839f30111::AttributeVisitor32   void operator()(int64_t value) {
33     attribute->value.int64_value = value;
34     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_INT64;
35   }
operator ()__anon5c1839f30111::AttributeVisitor36   void operator()(bool value) {
37     attribute->value.bool_value = value;
38     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_BOOL;
39   }
operator ()__anon5c1839f30111::AttributeVisitor40   void operator()(float value) {
41     attribute->value.float_value = value;
42     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT;
43   }
operator ()__anon5c1839f30111::AttributeVisitor44   void operator()(const std::array<float, 2>& value) {
45     std::copy(value.cbegin(), value.cend(), attribute->value.float2_value);
46     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT2;
47   }
operator ()__anon5c1839f30111::AttributeVisitor48   void operator()(const std::array<float, 3>& value) {
49     std::copy(value.cbegin(), value.cend(), attribute->value.float3_value);
50     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT3;
51   }
operator ()__anon5c1839f30111::AttributeVisitor52   void operator()(const std::array<float, 4>& value) {
53     std::copy(value.cbegin(), value.cend(), attribute->value.float4_value);
54     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT4;
55   }
operator ()__anon5c1839f30111::AttributeVisitor56   void operator()(const std::array<float, 8>& value) {
57     std::copy(value.cbegin(), value.cend(), attribute->value.float8_value);
58     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT8;
59   }
operator ()__anon5c1839f30111::AttributeVisitor60   void operator()(const std::array<float, 16>& value) {
61     std::copy(value.cbegin(), value.cend(), attribute->value.float16_value);
62     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT16;
63   }
operator ()__anon5c1839f30111::AttributeVisitor64   void operator()(EmptyVariant) {
65     attribute->value.type = DVR_SURFACE_ATTRIBUTE_TYPE_NONE;
66   }
67 };
68 
ConvertSurfaceAttributes(const SurfaceAttributes & surface_attributes,DvrSurfaceAttribute * attributes,size_t max_count)69 size_t ConvertSurfaceAttributes(const SurfaceAttributes& surface_attributes,
70                                 DvrSurfaceAttribute* attributes,
71                                 size_t max_count) {
72   size_t count = 0;
73   for (const auto& attribute : surface_attributes) {
74     if (count >= max_count)
75       break;
76 
77     // Copy the key and extract the Variant value using a visitor.
78     attributes[count].key = attribute.first;
79     attribute.second.Visit(AttributeVisitor{&attributes[count]});
80     count++;
81   }
82 
83   return count;
84 }
85 
86 }  // anonymous namespace
87 
88 extern "C" {
89 
90 struct DvrDisplayManager {
91   std::unique_ptr<DisplayManagerClient> client;
92 };
93 
94 struct DvrSurfaceState {
95   std::vector<SurfaceState> state;
96 };
97 
dvrDisplayManagerCreate(DvrDisplayManager ** client_out)98 int dvrDisplayManagerCreate(DvrDisplayManager** client_out) {
99   if (!client_out)
100     return -EINVAL;
101 
102   auto client = DisplayManagerClient::Create();
103   if (!client) {
104     ALOGE("dvrDisplayManagerCreate: Failed to create display manager client!");
105     return -EIO;
106   }
107 
108   *client_out = new DvrDisplayManager{std::move(client)};
109   return 0;
110 }
111 
dvrDisplayManagerDestroy(DvrDisplayManager * client)112 void dvrDisplayManagerDestroy(DvrDisplayManager* client) { delete client; }
113 
dvrDisplayManagerSetupNamedBuffer(DvrDisplayManager * client,const char * name,size_t size,uint64_t usage,DvrBuffer ** buffer_out)114 int dvrDisplayManagerSetupNamedBuffer(DvrDisplayManager* client,
115                                       const char* name, size_t size,
116                                       uint64_t usage, DvrBuffer** buffer_out) {
117   if (!client || !name || !buffer_out)
118     return -EINVAL;
119 
120   uint64_t gralloc_usage = AHardwareBuffer_convertToGrallocUsageBits(usage);
121 
122   auto buffer_status =
123       client->client->SetupNamedBuffer(name, size, gralloc_usage);
124   if (!buffer_status) {
125     ALOGE("dvrDisplayManagerSetupPoseBuffer: Failed to setup named buffer: %s",
126           buffer_status.GetErrorMessage().c_str());
127     return -buffer_status.error();
128   }
129 
130   *buffer_out = CreateDvrBufferFromIonBuffer(buffer_status.take());
131   return 0;
132 }
133 
dvrDisplayManagerGetEventFd(DvrDisplayManager * client)134 int dvrDisplayManagerGetEventFd(DvrDisplayManager* client) {
135   if (!client)
136     return -EINVAL;
137 
138   return client->client->event_fd();
139 }
140 
dvrDisplayManagerTranslateEpollEventMask(DvrDisplayManager * client,int in_events,int * out_events)141 int dvrDisplayManagerTranslateEpollEventMask(DvrDisplayManager* client,
142                                              int in_events, int* out_events) {
143   if (!client || !out_events)
144     return -EINVAL;
145 
146   auto status = client->client->GetEventMask(in_events);
147   if (!status)
148     return -status.error();
149 
150   *out_events = status.get();
151   return 0;
152 }
153 
dvrDisplayManagerGetSurfaceState(DvrDisplayManager * client,DvrSurfaceState * state)154 int dvrDisplayManagerGetSurfaceState(DvrDisplayManager* client,
155                                      DvrSurfaceState* state) {
156   if (!client || !state)
157     return -EINVAL;
158 
159   auto status = client->client->GetSurfaceState();
160   if (!status)
161     return -status.error();
162 
163   state->state = status.take();
164   return 0;
165 }
166 
dvrDisplayManagerGetReadBufferQueue(DvrDisplayManager * client,int surface_id,int queue_id,DvrReadBufferQueue ** queue_out)167 int dvrDisplayManagerGetReadBufferQueue(DvrDisplayManager* client,
168                                         int surface_id, int queue_id,
169                                         DvrReadBufferQueue** queue_out) {
170   if (!client || !queue_out)
171     return -EINVAL;
172 
173   auto status = client->client->GetSurfaceQueue(surface_id, queue_id);
174   if (!status) {
175     ALOGE("dvrDisplayManagerGetReadBufferQueue: Failed to get queue: %s",
176           status.GetErrorMessage().c_str());
177     return -status.error();
178   }
179 
180   *queue_out = CreateDvrReadBufferQueueFromConsumerQueue(status.take());
181   return 0;
182 }
183 
dvrSurfaceStateCreate(DvrSurfaceState ** surface_state_out)184 int dvrSurfaceStateCreate(DvrSurfaceState** surface_state_out) {
185   if (!surface_state_out)
186     return -EINVAL;
187 
188   *surface_state_out = new DvrSurfaceState{};
189   return 0;
190 }
191 
dvrSurfaceStateDestroy(DvrSurfaceState * surface_state)192 void dvrSurfaceStateDestroy(DvrSurfaceState* surface_state) {
193   delete surface_state;
194 }
195 
dvrSurfaceStateGetSurfaceCount(DvrSurfaceState * surface_state,size_t * count_out)196 int dvrSurfaceStateGetSurfaceCount(DvrSurfaceState* surface_state,
197                                    size_t* count_out) {
198   if (!surface_state)
199     return -EINVAL;
200 
201   *count_out = surface_state->state.size();
202   return 0;
203 }
204 
dvrSurfaceStateGetUpdateFlags(DvrSurfaceState * surface_state,size_t surface_index,DvrSurfaceUpdateFlags * flags_out)205 int dvrSurfaceStateGetUpdateFlags(DvrSurfaceState* surface_state,
206                                   size_t surface_index,
207                                   DvrSurfaceUpdateFlags* flags_out) {
208   if (!surface_state || surface_index >= surface_state->state.size())
209     return -EINVAL;
210 
211   *flags_out = surface_state->state[surface_index].update_flags;
212   return 0;
213 }
214 
dvrSurfaceStateGetSurfaceId(DvrSurfaceState * surface_state,size_t surface_index,int * surface_id_out)215 int dvrSurfaceStateGetSurfaceId(DvrSurfaceState* surface_state,
216                                 size_t surface_index, int* surface_id_out) {
217   if (!surface_state || surface_index >= surface_state->state.size())
218     return -EINVAL;
219 
220   *surface_id_out = surface_state->state[surface_index].surface_id;
221   return 0;
222 }
223 
dvrSurfaceStateGetProcessId(DvrSurfaceState * surface_state,size_t surface_index,int * process_id_out)224 int dvrSurfaceStateGetProcessId(DvrSurfaceState* surface_state,
225                                 size_t surface_index, int* process_id_out) {
226   if (!surface_state || surface_index >= surface_state->state.size())
227     return -EINVAL;
228 
229   *process_id_out = surface_state->state[surface_index].process_id;
230   return 0;
231 }
232 
dvrSurfaceStateGetQueueCount(DvrSurfaceState * surface_state,size_t surface_index,size_t * count_out)233 int dvrSurfaceStateGetQueueCount(DvrSurfaceState* surface_state,
234                                  size_t surface_index, size_t* count_out) {
235   if (!surface_state || surface_index >= surface_state->state.size())
236     return -EINVAL;
237 
238   *count_out = surface_state->state[surface_index].queue_ids.size();
239   return 0;
240 }
241 
dvrSurfaceStateGetQueueIds(DvrSurfaceState * surface_state,size_t surface_index,int * queue_ids,size_t max_count)242 ssize_t dvrSurfaceStateGetQueueIds(DvrSurfaceState* surface_state,
243                                    size_t surface_index, int* queue_ids,
244                                    size_t max_count) {
245   if (!surface_state || surface_index >= surface_state->state.size())
246     return -EINVAL;
247 
248   size_t i;
249   const auto& state = surface_state->state[surface_index];
250   for (i = 0; i < std::min(max_count, state.queue_ids.size()); i++) {
251     queue_ids[i] = state.queue_ids[i];
252   }
253 
254   return i;
255 }
256 
dvrSurfaceStateGetZOrder(DvrSurfaceState * surface_state,size_t surface_index,int * z_order_out)257 int dvrSurfaceStateGetZOrder(DvrSurfaceState* surface_state,
258                              size_t surface_index, int* z_order_out) {
259   if (!surface_state || surface_index >= surface_state->state.size() ||
260       !z_order_out) {
261     return -EINVAL;
262   }
263 
264   *z_order_out = surface_state->state[surface_index].GetZOrder();
265   return 0;
266 }
267 
dvrSurfaceStateGetVisible(DvrSurfaceState * surface_state,size_t surface_index,bool * visible_out)268 int dvrSurfaceStateGetVisible(DvrSurfaceState* surface_state,
269                               size_t surface_index, bool* visible_out) {
270   if (!surface_state || surface_index >= surface_state->state.size() ||
271       !visible_out) {
272     return -EINVAL;
273   }
274 
275   *visible_out = surface_state->state[surface_index].GetVisible();
276   return 0;
277 }
278 
dvrSurfaceStateGetAttributeCount(DvrSurfaceState * surface_state,size_t surface_index,size_t * count_out)279 int dvrSurfaceStateGetAttributeCount(DvrSurfaceState* surface_state,
280                                      size_t surface_index, size_t* count_out) {
281   if (!surface_state || surface_index >= surface_state->state.size() ||
282       !count_out) {
283     return -EINVAL;
284   }
285 
286   *count_out = surface_state->state[surface_index].surface_attributes.size();
287   return 0;
288 }
289 
dvrSurfaceStateGetAttributes(DvrSurfaceState * surface_state,size_t surface_index,DvrSurfaceAttribute * attributes,size_t max_count)290 ssize_t dvrSurfaceStateGetAttributes(DvrSurfaceState* surface_state,
291                                      size_t surface_index,
292                                      DvrSurfaceAttribute* attributes,
293                                      size_t max_count) {
294   if (!surface_state || surface_index >= surface_state->state.size() ||
295       !attributes) {
296     return -EINVAL;
297   }
298 
299   return ConvertSurfaceAttributes(
300       surface_state->state[surface_index].surface_attributes, attributes,
301       max_count);
302 }
303 
304 }  // extern "C"
305