• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011-2018 The Linux Foundation. All rights reserved.
3  * Not a Contribution
4  *
5  * Copyright (C) 2010 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #define DEBUG 0
21 #include <iomanip>
22 #include <utility>
23 #include <vector>
24 #include <sstream>
25 
26 #include "qd_utils.h"
27 #include "gr_priv_handle.h"
28 #include "gr_buf_descriptor.h"
29 #include "gr_utils.h"
30 #include "gr_buf_mgr.h"
31 #include "qdMetaData.h"
32 
33 namespace gralloc1 {
34 std::atomic<gralloc1_buffer_descriptor_t> BufferDescriptor::next_id_(1);
35 
BufferManager()36 BufferManager::BufferManager() : next_id_(0) {
37   char property[PROPERTY_VALUE_MAX];
38 
39   // Map framebuffer memory
40   if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
41       (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
42        (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
43     map_fb_mem_ = true;
44   }
45 
46   // Enable UBWC for framebuffer
47   if ((property_get("debug.gralloc.enable_fb_ubwc", property, NULL) > 0) &&
48       (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
49        (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
50     ubwc_for_fb_ = true;
51   }
52 
53   handles_map_.clear();
54   allocator_ = new Allocator();
55   allocator_->Init();
56 }
57 
58 
CreateBufferDescriptor(gralloc1_buffer_descriptor_t * descriptor_id)59 gralloc1_error_t BufferManager::CreateBufferDescriptor(
60     gralloc1_buffer_descriptor_t *descriptor_id) {
61   std::lock_guard<std::mutex> lock(descriptor_lock_);
62   auto descriptor = std::make_shared<BufferDescriptor>();
63   descriptors_map_.emplace(descriptor->GetId(), descriptor);
64   *descriptor_id = descriptor->GetId();
65   return GRALLOC1_ERROR_NONE;
66 }
67 
DestroyBufferDescriptor(gralloc1_buffer_descriptor_t descriptor_id)68 gralloc1_error_t BufferManager::DestroyBufferDescriptor(
69     gralloc1_buffer_descriptor_t descriptor_id) {
70   std::lock_guard<std::mutex> lock(descriptor_lock_);
71   const auto descriptor = descriptors_map_.find(descriptor_id);
72   if (descriptor == descriptors_map_.end()) {
73     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
74   }
75   descriptors_map_.erase(descriptor);
76   return GRALLOC1_ERROR_NONE;
77 }
78 
~BufferManager()79 BufferManager::~BufferManager() {
80   if (allocator_) {
81     delete allocator_;
82   }
83 }
84 
AllocateBuffers(uint32_t num_descriptors,const gralloc1_buffer_descriptor_t * descriptor_ids,buffer_handle_t * out_buffers)85 gralloc1_error_t BufferManager::AllocateBuffers(uint32_t num_descriptors,
86                                                 const gralloc1_buffer_descriptor_t *descriptor_ids,
87                                                 buffer_handle_t *out_buffers) {
88   bool shared = true;
89   gralloc1_error_t status = GRALLOC1_ERROR_NONE;
90 
91   // since GRALLOC1_CAPABILITY_TEST_ALLOCATE capability is supported
92   // client can ask to test the allocation by passing NULL out_buffers
93   bool test_allocate = !out_buffers;
94 
95   // Validate descriptors
96   std::lock_guard<std::mutex> descriptor_lock(descriptor_lock_);
97   std::vector<std::shared_ptr<BufferDescriptor>> descriptors;
98   for (uint32_t i = 0; i < num_descriptors; i++) {
99     const auto map_descriptor = descriptors_map_.find(descriptor_ids[i]);
100     if (map_descriptor == descriptors_map_.end()) {
101       return GRALLOC1_ERROR_BAD_DESCRIPTOR;
102     } else {
103       descriptors.push_back(map_descriptor->second);
104     }
105   }
106 
107   //  Resolve implementation defined formats
108   for (auto &descriptor : descriptors) {
109     descriptor->SetColorFormat(allocator_->GetImplDefinedFormat(descriptor->GetProducerUsage(),
110                                                                 descriptor->GetConsumerUsage(),
111                                                                 descriptor->GetFormat()));
112   }
113 
114   // Check if input descriptors can be supported AND
115   // Find out if a single buffer can be shared for all the given input descriptors
116   uint32_t i = 0;
117   ssize_t max_buf_index = -1;
118   shared = allocator_->CheckForBufferSharing(num_descriptors, descriptors, &max_buf_index);
119 
120   if (test_allocate) {
121     status = shared ? GRALLOC1_ERROR_NOT_SHARED : status;
122     return status;
123   }
124 
125   std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
126   if (shared && (max_buf_index >= 0)) {
127     // Allocate one and duplicate/copy the handles for each descriptor
128     if (AllocateBuffer(*descriptors[UINT(max_buf_index)], &out_buffers[max_buf_index])) {
129       return GRALLOC1_ERROR_NO_RESOURCES;
130     }
131 
132     for (i = 0; i < num_descriptors; i++) {
133       // Create new handle for a given descriptor.
134       // Current assumption is even MetaData memory would be same
135       // Need to revisit if there is a need for own metadata memory
136       if (i != UINT(max_buf_index)) {
137         CreateSharedHandle(out_buffers[max_buf_index], *descriptors[i], &out_buffers[i]);
138       }
139     }
140   } else {
141     // Buffer sharing is not feasible.
142     // Allocate separate buffer for each descriptor
143     for (i = 0; i < num_descriptors; i++) {
144       if (AllocateBuffer(*descriptors[i], &out_buffers[i])) {
145         return GRALLOC1_ERROR_NO_RESOURCES;
146       }
147     }
148   }
149 
150   // Allocation is successful. If backstore is not shared inform the client.
151   if (!shared) {
152     return GRALLOC1_ERROR_NOT_SHARED;
153   }
154 
155   return status;
156 }
157 
CreateSharedHandle(buffer_handle_t inbuffer,const BufferDescriptor & descriptor,buffer_handle_t * outbuffer)158 void BufferManager::CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
159                                        buffer_handle_t *outbuffer) {
160   // TODO(user): This path is not verified
161   private_handle_t const *input = reinterpret_cast<private_handle_t const *>(inbuffer);
162 
163   // Get Buffer attributes or dimension
164   unsigned int alignedw = 0, alignedh = 0;
165   allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
166 
167   // create new handle from input reference handle and given descriptor
168   int flags = GetHandleFlags(descriptor.GetFormat(), descriptor.GetProducerUsage(),
169                              descriptor.GetConsumerUsage());
170   int buffer_type = GetBufferType(descriptor.GetFormat());
171 
172   // Duplicate the fds
173   private_handle_t *out_hnd = new private_handle_t(dup(input->fd),
174                                                    dup(input->fd_metadata),
175                                                    flags,
176                                                    INT(alignedw),
177                                                    INT(alignedh),
178                                                    descriptor.GetWidth(),
179                                                    descriptor.GetHeight(),
180                                                    descriptor.GetFormat(),
181                                                    buffer_type,
182                                                    input->size,
183                                                    descriptor.GetProducerUsage(),
184                                                    descriptor.GetConsumerUsage());
185   out_hnd->id = ++next_id_;
186   // TODO(user): Base address of shared handle and ion handles
187   RegisterHandleLocked(out_hnd, -1, -1);
188   *outbuffer = out_hnd;
189 }
190 
FreeBuffer(std::shared_ptr<Buffer> buf)191 gralloc1_error_t BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
192   auto hnd = buf->handle;
193   ALOGD_IF(DEBUG, "FreeBuffer handle:%p", hnd);
194 
195   if (private_handle_t::validate(hnd) != 0) {
196     ALOGE("FreeBuffer: Invalid handle: %p", hnd);
197     return GRALLOC1_ERROR_BAD_HANDLE;
198   }
199 
200   if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
201                              hnd->fd, buf->ion_handle_main) != 0) {
202     return GRALLOC1_ERROR_BAD_HANDLE;
203   }
204 
205   unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
206   if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
207                              hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) {
208     return GRALLOC1_ERROR_BAD_HANDLE;
209   }
210 
211   private_handle_t * handle = const_cast<private_handle_t *>(hnd);
212   handle->fd = -1;
213   handle->fd_metadata = -1;
214   if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) {
215       delete handle;
216   }
217   return GRALLOC1_ERROR_NONE;
218 }
219 
RegisterHandleLocked(const private_handle_t * hnd,int ion_handle,int ion_handle_meta)220 void BufferManager::RegisterHandleLocked(const private_handle_t *hnd,
221                                          int ion_handle,
222                                          int ion_handle_meta) {
223   auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
224   handles_map_.emplace(std::make_pair(hnd, buffer));
225 }
226 
ImportHandleLocked(private_handle_t * hnd)227 gralloc1_error_t BufferManager::ImportHandleLocked(private_handle_t *hnd) {
228    if (private_handle_t::validate(hnd) != 0) {
229      ALOGE("ImportHandleLocked: Invalid handle: %p", hnd);
230      return GRALLOC1_ERROR_BAD_HANDLE;
231    }
232   ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
233   int ion_handle = allocator_->ImportBuffer(hnd->fd);
234   if (ion_handle < 0) {
235     ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
236     return GRALLOC1_ERROR_BAD_HANDLE;
237   }
238   int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
239   if (ion_handle_meta < 0) {
240     ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd,
241           hnd->fd, hnd->id);
242     return GRALLOC1_ERROR_BAD_HANDLE;
243   }
244   // Set base pointers to NULL since the data here was received over binder
245   hnd->base = 0;
246   hnd->base_metadata = 0;
247   RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
248   return GRALLOC1_ERROR_NONE;
249 }
250 
251 std::shared_ptr<BufferManager::Buffer>
GetBufferFromHandleLocked(const private_handle_t * hnd)252 BufferManager::GetBufferFromHandleLocked(const private_handle_t *hnd) {
253   auto it = handles_map_.find(hnd);
254   if (it != handles_map_.end()) {
255     return it->second;
256   } else {
257     return nullptr;
258   }
259 }
260 
MapBuffer(private_handle_t const * handle)261 gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) {
262   private_handle_t *hnd = const_cast<private_handle_t *>(handle);
263   ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
264 
265   hnd->base = 0;
266   if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
267                             hnd->fd) != 0) {
268     return GRALLOC1_ERROR_BAD_HANDLE;
269   }
270   return GRALLOC1_ERROR_NONE;
271 }
272 
RetainBuffer(private_handle_t const * hnd)273 gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) {
274   ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
275   gralloc1_error_t err = GRALLOC1_ERROR_NONE;
276   std::lock_guard<std::mutex> lock(buffer_lock_);
277   auto buf = GetBufferFromHandleLocked(hnd);
278   if (buf != nullptr) {
279     buf->IncRef();
280   } else {
281     private_handle_t *handle = const_cast<private_handle_t *>(hnd);
282     err = ImportHandleLocked(handle);
283   }
284   return err;
285 }
286 
ReleaseBuffer(private_handle_t const * hnd)287 gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
288   ALOGD_IF(DEBUG, "Release buffer handle:%p", hnd);
289   std::lock_guard<std::mutex> lock(buffer_lock_);
290   auto buf = GetBufferFromHandleLocked(hnd);
291   if (buf == nullptr) {
292     ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
293     return GRALLOC1_ERROR_BAD_HANDLE;
294   } else {
295     if (buf->DecRef()) {
296       handles_map_.erase(hnd);
297       // Unmap, close ion handle and close fd
298       FreeBuffer(buf);
299     }
300   }
301   return GRALLOC1_ERROR_NONE;
302 }
303 
LockBuffer(const private_handle_t * hnd,gralloc1_producer_usage_t prod_usage,gralloc1_consumer_usage_t cons_usage)304 gralloc1_error_t BufferManager::LockBuffer(const private_handle_t *hnd,
305                                            gralloc1_producer_usage_t prod_usage,
306                                            gralloc1_consumer_usage_t cons_usage) {
307   std::lock_guard<std::mutex> lock(buffer_lock_);
308   gralloc1_error_t err = GRALLOC1_ERROR_NONE;
309   ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
310 
311   // If buffer is not meant for CPU return err
312   if (!CpuCanAccess(prod_usage, cons_usage)) {
313     return GRALLOC1_ERROR_BAD_VALUE;
314   }
315 
316   if (hnd->base == 0) {
317     // we need to map for real
318     err = MapBuffer(hnd);
319   }
320 
321   auto buf = GetBufferFromHandleLocked(hnd);
322   if (buf == nullptr) {
323     return GRALLOC1_ERROR_BAD_HANDLE;
324   }
325 
326   // Invalidate if CPU reads in software and there are non-CPU
327   // writers. No need to do this for the metadata buffer as it is
328   // only read/written in software.
329 
330   // todo use handle here
331   if (!err && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
332       (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
333     if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
334                                 buf->ion_handle_main, CACHE_INVALIDATE)) {
335       return GRALLOC1_ERROR_BAD_HANDLE;
336     }
337   }
338 
339   // Mark the buffer to be flushed after CPU write.
340   if (!err && CpuCanWrite(prod_usage)) {
341     private_handle_t *handle = const_cast<private_handle_t *>(hnd);
342     handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
343   }
344 
345   return err;
346 }
347 
UnlockBuffer(const private_handle_t * handle)348 gralloc1_error_t BufferManager::UnlockBuffer(const private_handle_t *handle) {
349   std::lock_guard<std::mutex> lock(buffer_lock_);
350   gralloc1_error_t status = GRALLOC1_ERROR_NONE;
351 
352   private_handle_t *hnd = const_cast<private_handle_t *>(handle);
353   auto buf = GetBufferFromHandleLocked(hnd);
354   if (buf == nullptr) {
355     return GRALLOC1_ERROR_BAD_HANDLE;
356   }
357 
358   if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
359     if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
360                                 buf->ion_handle_main, CACHE_CLEAN) != 0) {
361       status = GRALLOC1_ERROR_BAD_HANDLE;
362     }
363     hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
364   }
365 
366   return status;
367 }
368 
GetHandleFlags(int format,gralloc1_producer_usage_t prod_usage,gralloc1_consumer_usage_t cons_usage)369 int BufferManager::GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
370                                   gralloc1_consumer_usage_t cons_usage) {
371   int flags = 0;
372   if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY) {
373     flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
374   }
375 
376   if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY) {
377     flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
378   }
379 
380   if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
381     flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
382   }
383 
384   if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
385     flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
386   }
387 
388   if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
389     flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
390   }
391 
392   if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
393     flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
394   }
395 
396   if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) {
397     flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
398   }
399 
400   if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
401     flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
402   }
403 
404   if (allocator_->IsUBwcEnabled(format, prod_usage, cons_usage)) {
405     flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
406   }
407 
408   if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) {
409     flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
410   }
411 
412   // TODO(user): is this correct???
413   if ((cons_usage &
414        (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) ||
415       (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) {
416     flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
417   }
418 
419   if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
420     flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
421   }
422 
423   if (!allocator_->UseUncached(prod_usage, cons_usage)) {
424     flags |= private_handle_t::PRIV_FLAGS_CACHED;
425   }
426 
427   return flags;
428 }
429 
GetBufferType(int inputFormat)430 int BufferManager::GetBufferType(int inputFormat) {
431   int buffer_type = BUFFER_TYPE_VIDEO;
432   if (IsUncompressedRGBFormat(inputFormat)) {
433     // RGB formats
434     buffer_type = BUFFER_TYPE_UI;
435   }
436 
437   return buffer_type;
438 }
439 
AllocateBuffer(const BufferDescriptor & descriptor,buffer_handle_t * handle,unsigned int bufferSize)440 int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
441                                   unsigned int bufferSize) {
442   if (!handle)
443     return -EINVAL;
444 
445   int format = descriptor.GetFormat();
446   gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
447   gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
448   uint32_t layer_count = descriptor.GetLayerCount();
449 
450   // Get implementation defined format
451   int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
452 
453   unsigned int size;
454   unsigned int alignedw, alignedh;
455   int buffer_type = GetBufferType(gralloc_format);
456   allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
457   size = (bufferSize >= size) ? bufferSize : size;
458 
459   int err = 0;
460   int flags = 0;
461   auto page_size = UINT(getpagesize());
462   AllocData data;
463   data.align = allocator_->GetDataAlignment(format, prod_usage, cons_usage);
464   data.size = size;
465   data.handle = (uintptr_t) handle;
466   data.uncached = allocator_->UseUncached(prod_usage, cons_usage);
467 
468   // Allocate buffer memory
469   err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
470   if (err) {
471     ALOGE("gralloc failed to allocate err=%s", strerror(-err));
472     return err;
473   }
474 
475   // Allocate memory for MetaData
476   AllocData e_data;
477   e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
478   e_data.handle = data.handle;
479   e_data.align = page_size;
480 
481   err =
482       allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE);
483   if (err) {
484     ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
485     return err;
486   }
487 
488   flags = GetHandleFlags(format, prod_usage, cons_usage);
489   flags |= data.alloc_type;
490 
491   // Create handle
492   private_handle_t *hnd = new private_handle_t(data.fd,
493                                                e_data.fd,
494                                                flags,
495                                                INT(alignedw),
496                                                INT(alignedh),
497                                                descriptor.GetWidth(),
498                                                descriptor.GetHeight(),
499                                                format,
500                                                buffer_type,
501                                                size,
502                                                prod_usage,
503                                                cons_usage);
504 
505   hnd->id = ++next_id_;
506   hnd->base = 0;
507   hnd->base_metadata = 0;
508   hnd->layer_count = layer_count;
509 
510   ColorSpace_t colorSpace = ITU_R_601;
511   setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
512   *handle = hnd;
513   RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
514   ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
515   if (DEBUG) {
516     private_handle_t::Dump(hnd);
517   }
518   return err;
519 }
520 
Perform(int operation,va_list args)521 gralloc1_error_t BufferManager::Perform(int operation, va_list args) {
522   switch (operation) {
523     case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
524       int fd = va_arg(args, int);
525       unsigned int size = va_arg(args, unsigned int);
526       unsigned int offset = va_arg(args, unsigned int);
527       void *base = va_arg(args, void *);
528       int width = va_arg(args, int);
529       int height = va_arg(args, int);
530       int format = va_arg(args, int);
531 
532       native_handle_t **handle = va_arg(args, native_handle_t **);
533       private_handle_t *hnd = reinterpret_cast<private_handle_t *>(
534           native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts()));
535       if (hnd) {
536         unsigned int alignedw = 0, alignedh = 0;
537         hnd->magic = private_handle_t::kMagic;
538         hnd->fd = fd;
539         hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
540         hnd->size = size;
541         hnd->offset = offset;
542         hnd->base = uint64_t(base) + offset;
543         hnd->gpuaddr = 0;
544         BufferDescriptor descriptor(width, height, format);
545         allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
546         hnd->unaligned_width = width;
547         hnd->unaligned_height = height;
548         hnd->width = INT(alignedw);
549         hnd->height = INT(alignedh);
550         hnd->format = format;
551         *handle = reinterpret_cast<native_handle_t *>(hnd);
552       }
553     } break;
554 
555     case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
556       int width = va_arg(args, int);
557       int format = va_arg(args, int);
558       int *stride = va_arg(args, int *);
559       unsigned int alignedw = 0, alignedh = 0;
560       BufferDescriptor descriptor(width, width, format);
561       allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
562       *stride = INT(alignedw);
563     } break;
564 
565     case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
566       private_handle_t *hnd = va_arg(args, private_handle_t *);
567       int *stride = va_arg(args, int *);
568       if (private_handle_t::validate(hnd) != 0) {
569         return GRALLOC1_ERROR_BAD_HANDLE;
570       }
571 
572       BufferDim_t buffer_dim;
573       if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
574         *stride = buffer_dim.sliceWidth;
575       } else {
576         *stride = hnd->width;
577       }
578     } break;
579 
580     // TODO(user) : this alone should be sufficient, ask gfx to get rid of above
581     case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
582       private_handle_t *hnd = va_arg(args, private_handle_t *);
583       int *stride = va_arg(args, int *);
584       int *height = va_arg(args, int *);
585       if (private_handle_t::validate(hnd) != 0) {
586         return GRALLOC1_ERROR_BAD_HANDLE;
587       }
588 
589       BufferDim_t buffer_dim;
590       if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
591         *stride = buffer_dim.sliceWidth;
592         *height = buffer_dim.sliceHeight;
593       } else {
594         *stride = hnd->width;
595         *height = hnd->height;
596       }
597     } break;
598 
599     case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
600       // TODO(user): Usage is split now. take care of it from Gfx client.
601       // see if we can directly expect descriptor from gfx client.
602       int width = va_arg(args, int);
603       int height = va_arg(args, int);
604       int format = va_arg(args, int);
605       uint64_t producer_usage = va_arg(args, uint64_t);
606       uint64_t consumer_usage = va_arg(args, uint64_t);
607       gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t>(producer_usage);
608       gralloc1_consumer_usage_t cons_usage = static_cast<gralloc1_consumer_usage_t>(consumer_usage);
609 
610       int *aligned_width = va_arg(args, int *);
611       int *aligned_height = va_arg(args, int *);
612       int *tile_enabled = va_arg(args, int *);
613       unsigned int alignedw, alignedh;
614       BufferDescriptor descriptor(width, height, format, prod_usage, cons_usage);
615       *tile_enabled = allocator_->IsUBwcEnabled(format, prod_usage, cons_usage);
616 
617       allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
618       *aligned_width = INT(alignedw);
619       *aligned_height = INT(alignedh);
620     } break;
621 
622     case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
623       private_handle_t *hnd = va_arg(args, private_handle_t *);
624       int *color_space = va_arg(args, int *);
625       if (private_handle_t::validate(hnd) != 0) {
626         return GRALLOC1_ERROR_BAD_HANDLE;
627       }
628       *color_space = 0;
629 #ifdef USE_COLOR_METADATA
630       ColorMetaData color_metadata;
631       if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
632         switch (color_metadata.colorPrimaries) {
633           case ColorPrimaries_BT709_5:
634             *color_space = HAL_CSC_ITU_R_709;
635             break;
636           case ColorPrimaries_BT601_6_525:
637           case ColorPrimaries_BT601_6_625:
638             *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
639             break;
640           case ColorPrimaries_BT2020:
641             *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
642             break;
643           default:
644             ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
645             break;
646         }
647         break;
648       }
649       if (getMetaData(hnd, GET_COLOR_SPACE, &color_metadata) != 0) {
650           *color_space = 0;
651       }
652 #endif
653     } break;
654     case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
655       private_handle_t *hnd = va_arg(args, private_handle_t *);
656       android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
657       if (private_handle_t::validate(hnd) != 0) {
658         return GRALLOC1_ERROR_BAD_HANDLE;
659       }
660       if (allocator_->GetYUVPlaneInfo(hnd, ycbcr)) {
661         return GRALLOC1_ERROR_UNDEFINED;
662       }
663     } break;
664 
665     case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
666       private_handle_t *hnd = va_arg(args, private_handle_t *);
667       int *map_secure_buffer = va_arg(args, int *);
668       if (private_handle_t::validate(hnd) != 0) {
669         return GRALLOC1_ERROR_BAD_HANDLE;
670       }
671 
672       if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) == 0) {
673         *map_secure_buffer = 0;
674       }
675     } break;
676 
677     case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
678       private_handle_t *hnd = va_arg(args, private_handle_t *);
679       int *flag = va_arg(args, int *);
680       if (private_handle_t::validate(hnd) != 0) {
681         return GRALLOC1_ERROR_BAD_HANDLE;
682       }
683       *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
684     } break;
685 
686     case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
687       private_handle_t *hnd = va_arg(args, private_handle_t *);
688       void **rgb_data = va_arg(args, void **);
689       if (private_handle_t::validate(hnd) != 0) {
690         return GRALLOC1_ERROR_BAD_HANDLE;
691       }
692       if (allocator_->GetRgbDataAddress(hnd, rgb_data)) {
693         return GRALLOC1_ERROR_UNDEFINED;
694       }
695     } break;
696 
697     case GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS: {
698       int width = va_arg(args, int);
699       int height = va_arg(args, int);
700       int format = va_arg(args, int);
701       uint64_t p_usage = va_arg(args, uint64_t);
702       uint64_t c_usage = va_arg(args, uint64_t);
703       gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
704       gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
705       uint32_t *aligned_width = va_arg(args, uint32_t *);
706       uint32_t *aligned_height = va_arg(args, uint32_t *);
707       uint32_t *size = va_arg(args, uint32_t *);
708       auto descriptor = BufferDescriptor(width, height, format, producer_usage, consumer_usage);
709       allocator_->GetBufferSizeAndDimensions(descriptor, size, aligned_width, aligned_height);
710     } break;
711 
712       // TODO(user): Break out similar functionality, preferably moving to a common lib.
713 
714     case GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER: {
715       int width = va_arg(args, int);
716       int height = va_arg(args, int);
717       int format = va_arg(args, int);
718       uint64_t p_usage = va_arg(args, uint64_t);
719       uint64_t c_usage = va_arg(args, uint64_t);
720       buffer_handle_t *hnd = va_arg(args, buffer_handle_t*);
721       gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
722       gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
723       BufferDescriptor descriptor(width, height, format, producer_usage, consumer_usage);
724       unsigned int size;
725       unsigned int alignedw, alignedh;
726       allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
727       AllocateBuffer(descriptor, hnd, size);
728     } break;
729 
730     default:
731       break;
732   }
733   return GRALLOC1_ERROR_NONE;
734 }
735 
IsYuvFormat(const private_handle_t * hnd)736 static bool IsYuvFormat(const private_handle_t *hnd) {
737   switch (hnd->format) {
738     case HAL_PIXEL_FORMAT_YCbCr_420_SP:
739     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
740     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
741     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:   // Same as YCbCr_420_SP_VENUS
742     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
743     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
744     case HAL_PIXEL_FORMAT_YCrCb_422_SP:
745     case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
746     case HAL_PIXEL_FORMAT_NV21_ZSL:
747     case HAL_PIXEL_FORMAT_RAW16:
748     case HAL_PIXEL_FORMAT_RAW12:
749     case HAL_PIXEL_FORMAT_RAW10:
750     case HAL_PIXEL_FORMAT_YV12:
751       return true;
752     default:
753       return false;
754   }
755 }
756 
GetNumFlexPlanes(const private_handle_t * hnd,uint32_t * out_num_planes)757 gralloc1_error_t BufferManager::GetNumFlexPlanes(const private_handle_t *hnd,
758                                                  uint32_t *out_num_planes) {
759   if (!IsYuvFormat(hnd)) {
760     return GRALLOC1_ERROR_UNSUPPORTED;
761   } else {
762     *out_num_planes = 3;
763   }
764   return GRALLOC1_ERROR_NONE;
765 }
766 
GetFlexLayout(const private_handle_t * hnd,struct android_flex_layout * layout)767 gralloc1_error_t BufferManager::GetFlexLayout(const private_handle_t *hnd,
768                                               struct android_flex_layout *layout) {
769   if (!IsYuvFormat(hnd)) {
770     return GRALLOC1_ERROR_UNSUPPORTED;
771   }
772 
773   android_ycbcr ycbcr;
774   int err = allocator_->GetYUVPlaneInfo(hnd, &ycbcr);
775 
776   if (err != 0) {
777     return GRALLOC1_ERROR_BAD_HANDLE;
778   }
779 
780   layout->format = FLEX_FORMAT_YCbCr;
781   layout->num_planes = 3;
782 
783   for (uint32_t i = 0; i < layout->num_planes; i++) {
784     layout->planes[i].bits_per_component = 8;
785     layout->planes[i].bits_used = 8;
786     layout->planes[i].h_increment = 1;
787     layout->planes[i].v_increment = 1;
788     layout->planes[i].h_subsampling = 2;
789     layout->planes[i].v_subsampling = 2;
790   }
791 
792   layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
793   layout->planes[0].component = FLEX_COMPONENT_Y;
794   layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
795 
796   layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
797   layout->planes[1].component = FLEX_COMPONENT_Cb;
798   layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
799   layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
800 
801   layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
802   layout->planes[2].component = FLEX_COMPONENT_Cr;
803   layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
804   layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
805   return GRALLOC1_ERROR_NONE;
806 }
807 
Dump(std::ostringstream * os)808 gralloc1_error_t BufferManager::Dump(std::ostringstream *os) {
809   for (auto it : handles_map_) {
810     auto buf = it.second;
811     auto hnd = buf->handle;
812     *os << "handle id: " << std::setw(4) << hnd->id;
813     *os << " fd: "       << std::setw(3) << hnd->fd;
814     *os << " fd_meta: "  << std::setw(3) << hnd->fd_metadata;
815     *os << " wxh: "      << std::setw(4) << hnd->width <<" x " << std::setw(4) <<  hnd->height;
816     *os << " uwxuh: "    << std::setw(4) << hnd->unaligned_width << " x ";
817     *os << std::setw(4)  <<  hnd->unaligned_height;
818     *os << " size: "     << std::setw(9) << hnd->size;
819     *os << std::hex << std::setfill('0');
820     *os << " priv_flags: " << "0x" << std::setw(8) << hnd->flags;
821     *os << " prod_usage: " << "0x" << std::setw(8) << hnd->producer_usage;
822     *os << " cons_usage: " << "0x" << std::setw(8) << hnd->consumer_usage;
823     // TODO(user): get format string from qdutils
824     *os << " format: "     << "0x" << std::setw(8) << hnd->format;
825     *os << std::dec  << std::setfill(' ') << std::endl;
826   }
827   return GRALLOC1_ERROR_NONE;
828 }
829 
IsBufferImported(const private_handle_t * hnd)830 gralloc1_error_t BufferManager::IsBufferImported(const private_handle_t *hnd) {
831   std::lock_guard<std::mutex> lock(buffer_lock_);
832   auto buf = GetBufferFromHandleLocked(hnd);
833   if (buf != NULL) {
834     return GRALLOC1_ERROR_NONE;
835   }
836   return GRALLOC1_ERROR_BAD_HANDLE;
837 }
838 
ValidateBufferSize(private_handle_t const * hnd,BufferDescriptor descriptor)839 gralloc1_error_t BufferManager::ValidateBufferSize(private_handle_t const *hnd,
840                                                    BufferDescriptor descriptor) {
841   unsigned int size, alignedw, alignedh;
842   const int format = allocator_->GetImplDefinedFormat(descriptor.GetProducerUsage(),
843                                                       descriptor.GetConsumerUsage(),
844                                                       descriptor.GetFormat());
845   descriptor.SetColorFormat(format);
846   allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
847 
848   auto ion_fd_size = static_cast<unsigned int>(lseek(hnd->fd, 0, SEEK_END));
849   if (size != ion_fd_size) {
850     return GRALLOC1_ERROR_BAD_VALUE;
851   }
852   return GRALLOC1_ERROR_NONE;
853 }
854 
855 }  //  namespace gralloc1
856