• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2015 - 2016, 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 #include <memalloc.h>
32 #include <gr.h>
33 #include <alloc_controller.h>
34 #include <utils/constants.h>
35 #include <utils/debug.h>
36 #include <core/buffer_allocator.h>
37 
38 #include "hwc_debugger.h"
39 #include "hwc_buffer_allocator.h"
40 
41 #define __CLASS__ "HWCBufferAllocator"
42 
43 namespace sdm {
44 
HWCBufferAllocator()45 HWCBufferAllocator::HWCBufferAllocator() {
46   alloc_controller_ = gralloc::IAllocController::getInstance();
47 }
48 
AllocateBuffer(BufferInfo * buffer_info)49 DisplayError HWCBufferAllocator::AllocateBuffer(BufferInfo *buffer_info) {
50   gralloc::alloc_data data;
51 
52   const BufferConfig &buffer_config = buffer_info->buffer_config;
53   AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
54   MetaBufferInfo *meta_buffer_info = new MetaBufferInfo();
55 
56   if (!meta_buffer_info) {
57     return kErrorMemory;
58   }
59 
60   int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
61   int error = 0;
62 
63   int width = INT(buffer_config.width);
64   int height = INT(buffer_config.height);
65   int format;
66 
67   if (buffer_config.secure) {
68     alloc_flags = INT(GRALLOC_USAGE_PRIVATE_MM_HEAP);
69     alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
70     data.align = SECURE_ALIGN;
71   } else {
72     data.align = UINT32(getpagesize());
73   }
74 
75   if (buffer_config.cache == false) {
76     // Allocate uncached buffers
77     alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
78   }
79 
80   error = SetBufferInfo(buffer_config.format, &format, &alloc_flags);
81   if (error != 0) {
82     delete meta_buffer_info;
83     return kErrorParameters;
84   }
85 
86   int aligned_width = 0, aligned_height = 0;
87   uint32_t buffer_size = getBufferSizeAndDimensions(width, height, format, alloc_flags,
88                                                     aligned_width, aligned_height);
89 
90   buffer_size = ROUND_UP(buffer_size, data.align) * buffer_config.buffer_count;
91 
92   data.base = 0;
93   data.fd = -1;
94   data.offset = 0;
95   data.size = buffer_size;
96   data.uncached = !buffer_config.cache;
97 
98   error = alloc_controller_->allocate(data, alloc_flags);
99   if (error != 0) {
100     DLOGE("Error allocating memory size %d uncached %d", data.size, data.uncached);
101     delete meta_buffer_info;
102     return kErrorMemory;
103   }
104 
105   alloc_buffer_info->fd = data.fd;
106   alloc_buffer_info->stride = UINT32(aligned_width);
107   alloc_buffer_info->size = buffer_size;
108 
109   meta_buffer_info->base_addr = data.base;
110   meta_buffer_info->alloc_type = data.allocType;
111 
112   buffer_info->private_data = meta_buffer_info;
113 
114   return kErrorNone;
115 }
116 
FreeBuffer(BufferInfo * buffer_info)117 DisplayError HWCBufferAllocator::FreeBuffer(BufferInfo *buffer_info) {
118   int ret = 0;
119 
120   AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
121 
122   // Deallocate the buffer, only if the buffer fd is valid.
123   if (alloc_buffer_info->fd > 0) {
124     MetaBufferInfo *meta_buffer_info = static_cast<MetaBufferInfo *> (buffer_info->private_data);
125     gralloc::IMemAlloc *memalloc = alloc_controller_->getAllocator(meta_buffer_info->alloc_type);
126     if (memalloc == NULL) {
127       DLOGE("Memalloc handle is NULL, alloc type %d", meta_buffer_info->alloc_type);
128       return kErrorResources;
129     }
130 
131     ret = memalloc->free_buffer(meta_buffer_info->base_addr, alloc_buffer_info->size, 0,
132                                 alloc_buffer_info->fd);
133     if (ret != 0) {
134       DLOGE("Error freeing buffer base_addr %p size %d fd %d", meta_buffer_info->base_addr,
135             alloc_buffer_info->size, alloc_buffer_info->fd);
136       return kErrorMemory;
137     }
138 
139     alloc_buffer_info->fd = -1;
140     alloc_buffer_info->stride = 0;
141     alloc_buffer_info->size = 0;
142 
143     meta_buffer_info->base_addr = NULL;
144     meta_buffer_info->alloc_type = 0;
145 
146     delete meta_buffer_info;
147     meta_buffer_info = NULL;
148   }
149 
150   return kErrorNone;
151 }
152 
GetBufferSize(BufferInfo * buffer_info)153 uint32_t HWCBufferAllocator::GetBufferSize(BufferInfo *buffer_info) {
154   uint32_t align = UINT32(getpagesize());
155 
156   const BufferConfig &buffer_config = buffer_info->buffer_config;
157 
158   int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
159 
160   int width = INT(buffer_config.width);
161   int height = INT(buffer_config.height);
162   int format;
163 
164   if (buffer_config.secure) {
165     alloc_flags = INT(GRALLOC_USAGE_PRIVATE_MM_HEAP);
166     alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
167     align = SECURE_ALIGN;
168   }
169 
170   if (buffer_config.cache == false) {
171     // Allocate uncached buffers
172     alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
173   }
174 
175   if (SetBufferInfo(buffer_config.format, &format, &alloc_flags) < 0) {
176     return 0;
177   }
178 
179   int aligned_width = 0;
180   int aligned_height = 0;
181   uint32_t buffer_size = getBufferSizeAndDimensions(width, height, format, alloc_flags,
182                                                     aligned_width, aligned_height);
183 
184   buffer_size = ROUND_UP(buffer_size, align) * buffer_config.buffer_count;
185 
186   return buffer_size;
187 }
188 
SetBufferInfo(LayerBufferFormat format,int * target,int * flags)189 int HWCBufferAllocator::SetBufferInfo(LayerBufferFormat format, int *target, int *flags) {
190   switch (format) {
191   case kFormatRGBA8888:                 *target = HAL_PIXEL_FORMAT_RGBA_8888;             break;
192   case kFormatRGBX8888:                 *target = HAL_PIXEL_FORMAT_RGBX_8888;             break;
193   case kFormatRGB888:                   *target = HAL_PIXEL_FORMAT_RGB_888;               break;
194   case kFormatRGB565:                   *target = HAL_PIXEL_FORMAT_RGB_565;               break;
195   case kFormatBGR565:                   *target = HAL_PIXEL_FORMAT_BGR_565;               break;
196   case kFormatBGRA8888:                 *target = HAL_PIXEL_FORMAT_BGRA_8888;             break;
197   case kFormatYCrCb420PlanarStride16:   *target = HAL_PIXEL_FORMAT_YV12;                  break;
198   case kFormatYCrCb420SemiPlanar:       *target = HAL_PIXEL_FORMAT_YCrCb_420_SP;          break;
199   case kFormatYCbCr420SemiPlanar:       *target = HAL_PIXEL_FORMAT_YCbCr_420_SP;          break;
200   case kFormatYCbCr422H2V1Packed:       *target = HAL_PIXEL_FORMAT_YCbCr_422_I;           break;
201   case kFormatYCbCr422H2V1SemiPlanar:   *target = HAL_PIXEL_FORMAT_YCbCr_422_SP;          break;
202   case kFormatYCbCr420SemiPlanarVenus:  *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;    break;
203   case kFormatYCrCb420SemiPlanarVenus:  *target = HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS;    break;
204   case kFormatYCbCr420SPVenusUbwc:    *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC; break;
205   case kFormatRGBA5551:                 *target = HAL_PIXEL_FORMAT_RGBA_5551;             break;
206   case kFormatRGBA4444:                 *target = HAL_PIXEL_FORMAT_RGBA_4444;             break;
207   case kFormatRGBA1010102:              *target = HAL_PIXEL_FORMAT_RGBA_1010102;          break;
208   case kFormatARGB2101010:              *target = HAL_PIXEL_FORMAT_ARGB_2101010;          break;
209   case kFormatRGBX1010102:              *target = HAL_PIXEL_FORMAT_RGBX_1010102;          break;
210   case kFormatXRGB2101010:              *target = HAL_PIXEL_FORMAT_XRGB_2101010;          break;
211   case kFormatBGRA1010102:              *target = HAL_PIXEL_FORMAT_BGRA_1010102;          break;
212   case kFormatABGR2101010:              *target = HAL_PIXEL_FORMAT_ABGR_2101010;          break;
213   case kFormatBGRX1010102:              *target = HAL_PIXEL_FORMAT_BGRX_1010102;          break;
214   case kFormatXBGR2101010:              *target = HAL_PIXEL_FORMAT_XBGR_2101010;          break;
215   case kFormatYCbCr420P010:             *target = HAL_PIXEL_FORMAT_YCbCr_420_P010;        break;
216   case kFormatYCbCr420TP10Ubwc:         *target = HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC;   break;
217   case kFormatRGBA8888Ubwc:
218     *target = HAL_PIXEL_FORMAT_RGBA_8888;
219     *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
220     break;
221   case kFormatRGBX8888Ubwc:
222     *target = HAL_PIXEL_FORMAT_RGBX_8888;
223     *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
224     break;
225   case kFormatBGR565Ubwc:
226     *target = HAL_PIXEL_FORMAT_BGR_565;
227     *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
228     break;
229   case kFormatRGBA1010102Ubwc:
230     *target = HAL_PIXEL_FORMAT_RGBA_1010102;
231     *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
232     break;
233   case kFormatRGBX1010102Ubwc:
234     *target = HAL_PIXEL_FORMAT_RGBX_1010102;
235     *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
236     break;
237   default:
238     DLOGE("Unsupported format = 0x%x", format);
239     return -EINVAL;
240   }
241 
242   return 0;
243 }
244 
245 }  // namespace sdm
246