1/*
2* Copyright (c) 2015-2017, 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
30#include <gralloc_priv.h>
31
32#include <core/buffer_allocator.h>
33#include <utils/constants.h>
34#include <utils/debug.h>
35
36#include "hwc_buffer_allocator.h"
37#include "hwc_debugger.h"
38
39#define __CLASS__ "HWCBufferAllocator"
40namespace sdm {
41
42HWCBufferAllocator::HWCBufferAllocator() {
43  int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module_);
44  if (err != 0) {
45    DLOGE("FATAL: can not open GRALLOC module");
46  } else {
47    gralloc1_open(module_, &gralloc_device_);
48  }
49  ReleaseBuffer_ = reinterpret_cast<GRALLOC1_PFN_RELEASE>(
50      gralloc_device_->getFunction(gralloc_device_, GRALLOC1_FUNCTION_RELEASE));
51  Perform_ = reinterpret_cast<GRALLOC1_PFN_PERFORM>(
52      gralloc_device_->getFunction(gralloc_device_, GRALLOC1_FUNCTION_PERFORM));
53}
54
55HWCBufferAllocator::~HWCBufferAllocator() {
56  if (gralloc_device_ != nullptr) {
57    gralloc1_close(gralloc_device_);
58  }
59}
60
61DisplayError HWCBufferAllocator::AllocateBuffer(BufferInfo *buffer_info) {
62  const BufferConfig &buffer_config = buffer_info->buffer_config;
63  AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
64  uint32_t width = buffer_config.width;
65  uint32_t height = buffer_config.height;
66  int format;
67  int alloc_flags = 0;
68  int error = SetBufferInfo(buffer_config.format, &format, &alloc_flags);
69  if (error != 0) {
70    return kErrorParameters;
71  }
72
73  if (buffer_config.secure) {
74    alloc_flags |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
75  }
76
77  if (!buffer_config.cache) {
78    // Allocate uncached buffers
79    alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
80  }
81  uint64_t producer_usage = UINT64(alloc_flags);
82  uint64_t consumer_usage = UINT64(alloc_flags);
83  // CreateBuffer
84  private_handle_t *hnd = nullptr;
85  Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER, width, height, format,
86           producer_usage, consumer_usage, &hnd);
87
88  if (hnd) {
89    alloc_buffer_info->fd = hnd->fd;
90    alloc_buffer_info->stride = UINT32(hnd->width);
91    alloc_buffer_info->size = hnd->size;
92  } else {
93    DLOGE("Failed to allocate memory");
94    return kErrorMemory;
95  }
96
97  buffer_info->private_data = reinterpret_cast<void *>(hnd);
98  return kErrorNone;
99}
100
101DisplayError HWCBufferAllocator::FreeBuffer(BufferInfo *buffer_info) {
102  DisplayError err = kErrorNone;
103  buffer_handle_t hnd = static_cast<private_handle_t *>(buffer_info->private_data);
104  ReleaseBuffer_(gralloc_device_, hnd);
105  AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
106  alloc_buffer_info->fd = -1;
107  alloc_buffer_info->stride = 0;
108  alloc_buffer_info->size = 0;
109  // Works around b/36355756
110  if (hnd != nullptr) {
111    delete hnd;
112  }
113  buffer_info->private_data = NULL;
114  return err;
115}
116
117void HWCBufferAllocator::GetCustomWidthAndHeight(const private_handle_t *handle, int *width,
118                                                 int *height) {
119  Perform_(gralloc_device_, GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE, handle,
120           width, height);
121}
122
123void HWCBufferAllocator::GetAlignedWidthAndHeight(int width, int height, int format,
124                                                  uint32_t alloc_type, int *aligned_width,
125                                                  int *aligned_height) {
126  int tile_enabled;
127  gralloc1_producer_usage_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
128  gralloc1_consumer_usage_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
129  if (alloc_type & GRALLOC_USAGE_HW_FB) {
130    consumer_usage = GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET;
131  }
132
133  Perform_(gralloc_device_, GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES, width, height, format,
134           producer_usage, consumer_usage, aligned_width, aligned_height, &tile_enabled);
135}
136
137uint32_t HWCBufferAllocator::GetBufferSize(BufferInfo *buffer_info) {
138  const BufferConfig &buffer_config = buffer_info->buffer_config;
139  int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
140
141  int width = INT(buffer_config.width);
142  int height = INT(buffer_config.height);
143  int format;
144
145  if (buffer_config.secure) {
146    alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
147  }
148
149  if (!buffer_config.cache) {
150    // Allocate uncached buffers
151    alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
152  }
153
154  if (SetBufferInfo(buffer_config.format, &format, &alloc_flags) < 0) {
155    return 0;
156  }
157
158  uint32_t aligned_width = 0, aligned_height = 0, buffer_size = 0;
159  uint64_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
160  uint64_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
161  // TODO(user): Currently both flags are treated similarly in gralloc
162  producer_usage = UINT64(alloc_flags);
163  consumer_usage = producer_usage;
164  Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS, width, height,
165           format, producer_usage, consumer_usage, &aligned_width, &aligned_height, &buffer_size);
166  return buffer_size;
167}
168
169int HWCBufferAllocator::SetBufferInfo(LayerBufferFormat format, int *target, int *flags) {
170  switch (format) {
171    case kFormatRGBA8888:
172      *target = HAL_PIXEL_FORMAT_RGBA_8888;
173      break;
174    case kFormatRGBX8888:
175      *target = HAL_PIXEL_FORMAT_RGBX_8888;
176      break;
177    case kFormatRGB888:
178      *target = HAL_PIXEL_FORMAT_RGB_888;
179      break;
180    case kFormatRGB565:
181      *target = HAL_PIXEL_FORMAT_RGB_565;
182      break;
183    case kFormatBGR565:
184      *target = HAL_PIXEL_FORMAT_BGR_565;
185      break;
186    case kFormatBGRA8888:
187      *target = HAL_PIXEL_FORMAT_BGRA_8888;
188      break;
189    case kFormatYCrCb420PlanarStride16:
190      *target = HAL_PIXEL_FORMAT_YV12;
191      break;
192    case kFormatYCrCb420SemiPlanar:
193      *target = HAL_PIXEL_FORMAT_YCrCb_420_SP;
194      break;
195    case kFormatYCbCr420SemiPlanar:
196      *target = HAL_PIXEL_FORMAT_YCbCr_420_SP;
197      break;
198    case kFormatYCbCr422H2V1Packed:
199      *target = HAL_PIXEL_FORMAT_YCbCr_422_I;
200      break;
201    case kFormatYCbCr422H2V1SemiPlanar:
202      *target = HAL_PIXEL_FORMAT_YCbCr_422_SP;
203      break;
204    case kFormatYCbCr420SemiPlanarVenus:
205      *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
206      break;
207    case kFormatYCrCb420SemiPlanarVenus:
208      *target = HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS;
209      break;
210    case kFormatYCbCr420SPVenusUbwc:
211      *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
212      break;
213    case kFormatRGBA5551:
214      *target = HAL_PIXEL_FORMAT_RGBA_5551;
215      break;
216    case kFormatRGBA4444:
217      *target = HAL_PIXEL_FORMAT_RGBA_4444;
218      break;
219    case kFormatRGBA1010102:
220      *target = HAL_PIXEL_FORMAT_RGBA_1010102;
221      break;
222    case kFormatARGB2101010:
223      *target = HAL_PIXEL_FORMAT_ARGB_2101010;
224      break;
225    case kFormatRGBX1010102:
226      *target = HAL_PIXEL_FORMAT_RGBX_1010102;
227      break;
228    case kFormatXRGB2101010:
229      *target = HAL_PIXEL_FORMAT_XRGB_2101010;
230      break;
231    case kFormatBGRA1010102:
232      *target = HAL_PIXEL_FORMAT_BGRA_1010102;
233      break;
234    case kFormatABGR2101010:
235      *target = HAL_PIXEL_FORMAT_ABGR_2101010;
236      break;
237    case kFormatBGRX1010102:
238      *target = HAL_PIXEL_FORMAT_BGRX_1010102;
239      break;
240    case kFormatXBGR2101010:
241      *target = HAL_PIXEL_FORMAT_XBGR_2101010;
242      break;
243    case kFormatYCbCr420P010:
244      *target = HAL_PIXEL_FORMAT_YCbCr_420_P010;
245      break;
246    case kFormatYCbCr420TP10Ubwc:
247      *target = HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC;
248      break;
249    case kFormatRGBA8888Ubwc:
250      *target = HAL_PIXEL_FORMAT_RGBA_8888;
251      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
252      break;
253    case kFormatRGBX8888Ubwc:
254      *target = HAL_PIXEL_FORMAT_RGBX_8888;
255      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
256      break;
257    case kFormatBGR565Ubwc:
258      *target = HAL_PIXEL_FORMAT_BGR_565;
259      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
260      break;
261    case kFormatRGBA1010102Ubwc:
262      *target = HAL_PIXEL_FORMAT_RGBA_1010102;
263      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
264      break;
265    case kFormatRGBX1010102Ubwc:
266      *target = HAL_PIXEL_FORMAT_RGBX_1010102;
267      *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
268      break;
269    default:
270      DLOGE("Unsupported format = 0x%x", format);
271      return -EINVAL;
272  }
273  return 0;
274}
275
276DisplayError HWCBufferAllocator::GetAllocatedBufferInfo(
277    const BufferConfig &buffer_config, AllocatedBufferInfo *allocated_buffer_info) {
278  // TODO(user): This API should pass the buffer_info of the already allocated buffer
279  // The private_data can then be typecast to the private_handle and used directly.
280  int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
281
282  int width = INT(buffer_config.width);
283  int height = INT(buffer_config.height);
284  int format;
285
286  if (buffer_config.secure) {
287    alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
288  }
289
290  if (!buffer_config.cache) {
291    // Allocate uncached buffers
292    alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
293  }
294
295  if (SetBufferInfo(buffer_config.format, &format, &alloc_flags) < 0) {
296    return kErrorParameters;
297  }
298
299  uint32_t aligned_width = 0, aligned_height = 0, buffer_size = 0;
300  uint64_t producer_usage = GRALLOC1_PRODUCER_USAGE_NONE;
301  uint64_t consumer_usage = GRALLOC1_CONSUMER_USAGE_NONE;
302  // TODO(user): Currently both flags are treated similarly in gralloc
303  producer_usage = UINT64(alloc_flags);
304  consumer_usage = producer_usage;
305  Perform_(gralloc_device_, GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS, width, height,
306           format, producer_usage, consumer_usage, &aligned_width, &aligned_height, &buffer_size);
307  allocated_buffer_info->stride = UINT32(aligned_width);
308  allocated_buffer_info->aligned_width = UINT32(aligned_width);
309  allocated_buffer_info->aligned_height = UINT32(aligned_height);
310  allocated_buffer_info->size = UINT32(buffer_size);
311
312  return kErrorNone;
313}
314
315}  // namespace sdm
316