• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011-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 
30 #include <log/log.h>
31 #include <cutils/properties.h>
32 #include <algorithm>
33 #include <vector>
34 
35 #ifndef QMAA
36 #include <linux/msm_ion.h>
37 #endif
38 
39 #include "gr_allocator.h"
40 #include "gr_utils.h"
41 #include "gralloc_priv.h"
42 
43 #include "qd_utils.h"
44 
45 #ifndef ION_FLAG_CP_PIXEL
46 #define ION_FLAG_CP_PIXEL 0
47 #endif
48 
49 #ifndef ION_FLAG_ALLOW_NON_CONTIG
50 #define ION_FLAG_ALLOW_NON_CONTIG 0
51 #endif
52 
53 #ifndef ION_FLAG_CP_CAMERA_PREVIEW
54 #define ION_FLAG_CP_CAMERA_PREVIEW 0
55 #endif
56 
57 #ifndef ION_SECURE
58 #define ION_SECURE ION_FLAG_SECURE
59 #endif
60 
61 #ifndef ION_FLAG_CP_CDSP
62 #define ION_FLAG_CP_CDSP 0
63 #endif
64 
65 #ifdef SLAVE_SIDE_CP
66 #define CP_HEAP_ID ION_CP_MM_HEAP_ID
67 #define SD_HEAP_ID CP_HEAP_ID
68 #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
69 #define ION_SD_FLAGS ION_SECURE
70 #define ION_SC_FLAGS ION_SECURE
71 #define ION_SC_PREVIEW_FLAGS ION_SECURE
72 #else  // MASTER_SIDE_CP
73 #define CP_HEAP_ID ION_SECURE_HEAP_ID
74 #define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
75 #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
76 #define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
77 #define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
78 #define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
79 #endif
80 
81 using std::shared_ptr;
82 using std::vector;
83 
84 namespace gralloc {
85 
GetBufferInfo(const BufferDescriptor & descriptor)86 static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
87   return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
88                     descriptor.GetUsage());
89 }
90 
Allocator()91 Allocator::Allocator() : ion_allocator_(nullptr) {}
92 
Init()93 bool Allocator::Init() {
94   ion_allocator_ = new IonAlloc();
95 
96   if (!ion_allocator_->Init()) {
97     return false;
98   }
99 
100   return true;
101 }
102 
~Allocator()103 Allocator::~Allocator() {
104   if (ion_allocator_) {
105     delete ion_allocator_;
106   }
107 }
108 
SetProperties(gralloc::GrallocProperties props)109 void Allocator::SetProperties(gralloc::GrallocProperties props) {
110   use_system_heap_for_sensors_ = props.use_system_heap_for_sensors;
111 }
112 
AllocateMem(AllocData * alloc_data,uint64_t usage,int format)113 int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
114   int ret;
115   alloc_data->uncached = UseUncached(format, usage);
116 
117   // After this point we should have the right heap set, there is no fallback
118   GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
119 
120   ret = ion_allocator_->AllocBuffer(alloc_data);
121   if (ret >= 0) {
122     alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
123   } else {
124     ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
125           alloc_data->heap_id, alloc_data->flags);
126   }
127 
128   return ret;
129 }
130 
MapBuffer(void ** base,unsigned int size,unsigned int offset,int fd)131 int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
132   if (ion_allocator_) {
133     return ion_allocator_->MapBuffer(base, size, offset, fd);
134   }
135 
136   return -EINVAL;
137 }
138 
ImportBuffer(int fd)139 int Allocator::ImportBuffer(int fd) {
140   if (ion_allocator_) {
141     return ion_allocator_->ImportBuffer(fd);
142   }
143   return -EINVAL;
144 }
145 
FreeBuffer(void * base,unsigned int size,unsigned int offset,int fd,int handle)146 int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) {
147   if (ion_allocator_) {
148     return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
149   }
150 
151   return -EINVAL;
152 }
153 
CleanBuffer(void * base,unsigned int size,unsigned int offset,int handle,int op,int fd)154 int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
155                            int fd) {
156   if (ion_allocator_) {
157     return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd);
158   }
159 
160   return -EINVAL;
161 }
162 
CheckForBufferSharing(uint32_t num_descriptors,const vector<shared_ptr<BufferDescriptor>> & descriptors,ssize_t * max_index)163 bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
164                                       const vector<shared_ptr<BufferDescriptor>> &descriptors,
165                                       ssize_t *max_index) {
166   unsigned int cur_heap_id = 0, prev_heap_id = 0;
167   unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
168   unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
169   bool cur_uncached = false, prev_uncached = false;
170   unsigned int alignedw, alignedh;
171   unsigned int max_size = 0;
172 
173   *max_index = -1;
174   for (uint32_t i = 0; i < num_descriptors; i++) {
175     // Check Cached vs non-cached and all the ION flags
176     cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage());
177     GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
178 
179     if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
180                   cur_ion_flags != prev_ion_flags)) {
181       return false;
182     }
183 
184     // For same format type, find the descriptor with bigger size
185     GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
186     unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
187     if (max_size < size) {
188       *max_index = INT(i);
189       max_size = size;
190     }
191 
192     prev_heap_id = cur_heap_id;
193     prev_uncached = cur_uncached;
194     prev_ion_flags = cur_ion_flags;
195     prev_alloc_type = cur_alloc_type;
196   }
197 
198   return true;
199 }
200 
GetIonHeapInfo(uint64_t usage,unsigned int * ion_heap_id,unsigned int * alloc_type,unsigned int * ion_flags)201 void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
202                                unsigned int *ion_flags) {
203   unsigned int heap_id = 0;
204   unsigned int type = 0;
205   uint32_t flags = 0;
206 #ifndef QMAA
207   if (usage & GRALLOC_USAGE_PROTECTED) {
208     if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
209       heap_id = ION_HEAP(SD_HEAP_ID);
210       /*
211        * There is currently no flag in ION for Secure Display
212        * VM. Please add it to the define once available.
213        */
214       flags |= UINT(ION_SD_FLAGS);
215     } else if (usage & BufferUsage::CAMERA_OUTPUT) {
216       heap_id = ION_HEAP(SD_HEAP_ID);
217       if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
218         flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
219       }
220       if (usage & BufferUsage::COMPOSER_OVERLAY) {
221         flags |= UINT(ION_SC_PREVIEW_FLAGS);
222       } else {
223         flags |= UINT(ION_SC_FLAGS);
224       }
225     } else if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
226       heap_id = ION_HEAP(ION_SECURE_CARVEOUT_HEAP_ID);
227       flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
228     } else {
229       heap_id = ION_HEAP(CP_HEAP_ID);
230       flags |= UINT(ION_CP_FLAGS);
231     }
232   }
233 
234   if (usage & BufferUsage::SENSOR_DIRECT_DATA) {
235       if (use_system_heap_for_sensors_) {
236         ALOGI("gralloc::sns_direct_data with system_heap");
237         heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
238       } else {
239         ALOGI("gralloc::sns_direct_data with adsp_heap");
240         heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
241       }
242   }
243 
244   if (flags & UINT(ION_SECURE)) {
245     type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
246   }
247 
248   // if no ion heap flags are set, default to system heap
249   if (!heap_id) {
250     heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
251   }
252 #endif
253 
254   *alloc_type = type;
255   *ion_flags = flags;
256   *ion_heap_id = heap_id;
257 
258   return;
259 }
260 }  // namespace gralloc
261