1 /*
2  * Copyright (c) 2011-2014, 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 <cutils/log.h>
31 #include <fcntl.h>
32 #include <dlfcn.h>
33 #include "gralloc_priv.h"
34 #include "alloc_controller.h"
35 #include "memalloc.h"
36 #include "ionalloc.h"
37 #include "gr.h"
38 #include "comptype.h"
39 #include "mdp_version.h"
40 
41 #ifdef VENUS_COLOR_FORMAT
42 #include <media/msm_media_info.h>
43 #else
44 #define VENUS_Y_STRIDE(args...) 0
45 #define VENUS_Y_SCANLINES(args...) 0
46 #define VENUS_BUFFER_SIZE(args...) 0
47 #endif
48 
49 #define ASTC_BLOCK_SIZE 16
50 
51 using namespace gralloc;
52 using namespace qdutils;
53 
54 ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
55 
56 //Common functions
canFallback(int usage,bool triedSystem)57 static bool canFallback(int usage, bool triedSystem)
58 {
59     // Fallback to system heap when alloc fails unless
60     // 1. Composition type is MDP
61     // 2. Alloc from system heap was already tried
62     // 3. The heap type is requsted explicitly
63     // 4. The heap type is protected
64     // 5. The buffer is meant for external display only
65 
66     if(QCCompositionType::getInstance().getCompositionType() &
67        COMPOSITION_TYPE_MDP)
68         return false;
69     if(triedSystem)
70         return false;
71     if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
72         return false;
73     if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
74         return false;
75     //Return true by default
76     return true;
77 }
78 
useUncached(int usage)79 static bool useUncached(int usage)
80 {
81     if (usage & GRALLOC_USAGE_PROTECTED)
82         return true;
83     if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
84         return true;
85     if(((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
86        ||((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
87         return true;
88     return false;
89 }
90 
91 //-------------- AdrenoMemInfo-----------------------//
AdrenoMemInfo()92 AdrenoMemInfo::AdrenoMemInfo()
93 {
94     LINK_adreno_compute_aligned_width_and_height = NULL;
95     LINK_adreno_compute_padding = NULL;
96     LINK_adreno_isMacroTilingSupportedByGpu = NULL;
97     LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
98 
99     libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
100     if (libadreno_utils) {
101         *(void **)&LINK_adreno_compute_aligned_width_and_height =
102                 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
103         *(void **)&LINK_adreno_compute_padding =
104                 ::dlsym(libadreno_utils, "compute_surface_padding");
105         *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
106                 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
107         *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
108                 ::dlsym(libadreno_utils,
109                         "compute_compressedfmt_aligned_width_and_height");
110     }
111 }
112 
~AdrenoMemInfo()113 AdrenoMemInfo::~AdrenoMemInfo()
114 {
115     if (libadreno_utils) {
116         ::dlclose(libadreno_utils);
117     }
118 }
119 
isMacroTilingSupportedByGPU()120 int AdrenoMemInfo::isMacroTilingSupportedByGPU()
121 {
122     if ((libadreno_utils)) {
123         if(LINK_adreno_isMacroTilingSupportedByGpu) {
124             return LINK_adreno_isMacroTilingSupportedByGpu();
125         }
126     }
127     return 0;
128 }
129 
130 
getAlignedWidthAndHeight(int width,int height,int format,int tile_enabled,int & aligned_w,int & aligned_h)131 void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
132                             int tile_enabled, int& aligned_w, int& aligned_h)
133 {
134     aligned_w = width;
135     aligned_h = height;
136     // Currently surface padding is only computed for RGB* surfaces.
137     if (format <= HAL_PIXEL_FORMAT_BGRA_8888) {
138         aligned_w = ALIGN(width, 32);
139         aligned_h = ALIGN(height, 32);
140         // Don't add any additional padding if debug.gralloc.map_fb_memory
141         // is enabled
142         char property[PROPERTY_VALUE_MAX];
143         if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
144            (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
145            (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
146               return;
147         }
148 
149         int bpp = 4;
150         switch(format)
151         {
152             case HAL_PIXEL_FORMAT_RGB_888:
153                 bpp = 3;
154                 break;
155             case HAL_PIXEL_FORMAT_RGB_565:
156                 bpp = 2;
157                 break;
158             default: break;
159         }
160         if (libadreno_utils) {
161             int raster_mode         = 0;   // Adreno unknown raster mode.
162             int padding_threshold   = 512; // Threshold for padding surfaces.
163             // the function below computes aligned width and aligned height
164             // based on linear or macro tile mode selected.
165             if(LINK_adreno_compute_aligned_width_and_height) {
166                 LINK_adreno_compute_aligned_width_and_height(width,
167                                      height, bpp, tile_enabled,
168                                      raster_mode, padding_threshold,
169                                      &aligned_w, &aligned_h);
170 
171             } else if(LINK_adreno_compute_padding) {
172                 int surface_tile_height = 1;   // Linear surface
173                 aligned_w = LINK_adreno_compute_padding(width, bpp,
174                                      surface_tile_height, raster_mode,
175                                      padding_threshold);
176                 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
177                                                             __FUNCTION__);
178             } else {
179                 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
180                     "compute_aligned_width_and_height not found", __FUNCTION__);
181             }
182         }
183     } else {
184         switch (format)
185         {
186             case HAL_PIXEL_FORMAT_YCrCb_420_SP:
187             case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
188                 aligned_w = ALIGN(width, 32);
189                 break;
190             case HAL_PIXEL_FORMAT_RAW16:
191                 aligned_w = ALIGN(width, 16);
192                 break;
193             case HAL_PIXEL_FORMAT_RAW10:
194                 aligned_w = ALIGN(width * 10 /8, 16);
195                 break;
196             case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
197                 aligned_w = ALIGN(width, 128);
198                 break;
199             case HAL_PIXEL_FORMAT_YCbCr_420_SP:
200             case HAL_PIXEL_FORMAT_YV12:
201             case HAL_PIXEL_FORMAT_YCbCr_422_SP:
202             case HAL_PIXEL_FORMAT_YCrCb_422_SP:
203             case HAL_PIXEL_FORMAT_YCbCr_422_I:
204             case HAL_PIXEL_FORMAT_YCrCb_422_I:
205                 aligned_w = ALIGN(width, 16);
206                 break;
207             case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
208             case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
209                 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
210                 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
211                 break;
212             case HAL_PIXEL_FORMAT_BLOB:
213             case HAL_PIXEL_FORMAT_RAW_OPAQUE:
214                 break;
215             case HAL_PIXEL_FORMAT_NV21_ZSL:
216                 aligned_w = ALIGN(width, 64);
217                 aligned_h = ALIGN(height, 64);
218                 break;
219             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
220             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
221             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
222             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
223             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
224             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
225             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
226             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
227             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
228             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
229             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
230             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
231             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
232             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
233             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
234             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
235             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
236             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
237             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
238             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
239             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
240             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
241             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
242             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
243             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
244             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
245             case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
246             case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
247                 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
248                     int bytesPerPixel = 0;
249                     int raster_mode         = 0;   //Adreno unknown raster mode.
250                     int padding_threshold   = 512; //Threshold for padding
251                     //surfaces.
252 
253                     LINK_adreno_compute_compressedfmt_aligned_width_and_height(
254                         width, height, format, 0,raster_mode, padding_threshold,
255                         &aligned_w, &aligned_h, &bytesPerPixel);
256 
257                 } else {
258                     ALOGW("%s: Warning!! Symbols" \
259                           " compute_compressedfmt_aligned_width_and_height" \
260                           " not found", __FUNCTION__);
261                 }
262                 break;
263             default: break;
264         }
265     }
266 }
267 
268 //-------------- IAllocController-----------------------//
269 IAllocController* IAllocController::sController = NULL;
getInstance(void)270 IAllocController* IAllocController::getInstance(void)
271 {
272     if(sController == NULL) {
273         sController = new IonController();
274     }
275     return sController;
276 }
277 
278 
279 //-------------- IonController-----------------------//
IonController()280 IonController::IonController()
281 {
282     mIonAlloc = new IonAlloc();
283 }
284 
allocate(alloc_data & data,int usage)285 int IonController::allocate(alloc_data& data, int usage)
286 {
287     int ionFlags = 0;
288     int ret;
289 
290     data.uncached = useUncached(usage);
291     data.allocType = 0;
292 
293     if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
294         ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
295 
296     if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
297         ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
298 
299     if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
300         ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
301 
302     if(usage & GRALLOC_USAGE_PROTECTED) {
303         ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
304         ionFlags |= ION_SECURE;
305     } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
306         //MM Heap is exclusively a secure heap.
307         //If it is used for non secure cases, fallback to IOMMU heap
308         ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
309                                 cannot be used as an insecure heap!\
310                                 trying to use IOMMU instead !!");
311         ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
312     }
313 
314     if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
315         ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
316 
317     if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
318         ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
319 
320     if(ionFlags & ION_SECURE)
321          data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
322 
323     // if no flags are set, default to
324     // SF + IOMMU heaps, so that bypass can work
325     // we can fall back to system heap if
326     // we run out.
327     if(!ionFlags)
328         ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
329 
330     data.flags = ionFlags;
331     ret = mIonAlloc->alloc_buffer(data);
332 
333     // Fallback
334     if(ret < 0 && canFallback(usage,
335                               (ionFlags & ION_SYSTEM_HEAP_ID)))
336     {
337         ALOGW("Falling back to system heap");
338         data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
339         ret = mIonAlloc->alloc_buffer(data);
340     }
341 
342     if(ret >= 0 ) {
343         data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
344     }
345 
346     return ret;
347 }
348 
getAllocator(int flags)349 IMemAlloc* IonController::getAllocator(int flags)
350 {
351     IMemAlloc* memalloc = NULL;
352     if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
353         memalloc = mIonAlloc;
354     } else {
355         ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
356     }
357 
358     return memalloc;
359 }
360 
isMacroTileEnabled(int format,int usage)361 bool isMacroTileEnabled(int format, int usage)
362 {
363     bool tileEnabled = false;
364 
365     // Check whether GPU & MDSS supports MacroTiling feature
366     if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
367             qdutils::MDPVersion::getInstance().supportsMacroTile())
368     {
369         // check the format
370         switch(format)
371         {
372             case  HAL_PIXEL_FORMAT_RGBA_8888:
373             case  HAL_PIXEL_FORMAT_RGBX_8888:
374             case  HAL_PIXEL_FORMAT_BGRA_8888:
375             case  HAL_PIXEL_FORMAT_RGB_565:
376                 {
377                     tileEnabled = true;
378                     // check the usage flags
379                     if (usage & (GRALLOC_USAGE_SW_READ_MASK |
380                                 GRALLOC_USAGE_SW_WRITE_MASK)) {
381                         // Application intends to use CPU for rendering
382                         tileEnabled = false;
383                     }
384                     break;
385                 }
386             default:
387                 break;
388         }
389     }
390     return tileEnabled;
391 }
392 
393 // helper function
getSize(int format,int width,int height,const int alignedw,const int alignedh)394 size_t getSize(int format, int width, int height, const int alignedw,
395         const int alignedh) {
396     size_t size = 0;
397 
398     switch (format) {
399         case HAL_PIXEL_FORMAT_RGBA_8888:
400         case HAL_PIXEL_FORMAT_RGBX_8888:
401         case HAL_PIXEL_FORMAT_BGRA_8888:
402             size = alignedw * alignedh * 4;
403             break;
404         case HAL_PIXEL_FORMAT_RGB_888:
405             size = alignedw * alignedh * 3;
406             break;
407         case HAL_PIXEL_FORMAT_RGB_565:
408         case HAL_PIXEL_FORMAT_RAW16:
409             size = alignedw * alignedh * 2;
410             break;
411         case HAL_PIXEL_FORMAT_RAW10:
412             size = ALIGN(alignedw * alignedh, 4096);
413             break;
414 
415             // adreno formats
416         case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:  // NV21
417             size  = ALIGN(alignedw*alignedh, 4096);
418             size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
419             break;
420         case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:   // NV12
421             // The chroma plane is subsampled,
422             // but the pitch in bytes is unchanged
423             // The GPU needs 4K alignment, but the video decoder needs 8K
424             size  = ALIGN( alignedw * alignedh, 8192);
425             size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
426             break;
427         case HAL_PIXEL_FORMAT_YV12:
428             if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
429                 ALOGE("w or h is odd for the YV12 format");
430                 return 0;
431             }
432             size = alignedw*alignedh +
433                     (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
434             size = ALIGN(size, (size_t)4096);
435             break;
436         case HAL_PIXEL_FORMAT_YCbCr_420_SP:
437         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
438             size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
439             break;
440         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
441         case HAL_PIXEL_FORMAT_YCrCb_422_SP:
442         case HAL_PIXEL_FORMAT_YCbCr_422_I:
443         case HAL_PIXEL_FORMAT_YCrCb_422_I:
444             if(width & 1) {
445                 ALOGE("width is odd for the YUV422_SP format");
446                 return 0;
447             }
448             size = ALIGN(alignedw * alignedh * 2, 4096);
449             break;
450         case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
451         case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
452             size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
453             break;
454         case HAL_PIXEL_FORMAT_BLOB:
455         case HAL_PIXEL_FORMAT_RAW_OPAQUE:
456             if(height != 1) {
457                 ALOGE("%s: Buffers with RAW_OPAQUE/BLOB formats \
458                       must have height==1 ", __FUNCTION__);
459                 return 0;
460             }
461             size = width;
462             break;
463         case HAL_PIXEL_FORMAT_NV21_ZSL:
464             size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
465             break;
466         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
467         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
468         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
469         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
470         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
471         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
472         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
473         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
474         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
475         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
476         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
477         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
478         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
479         case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
480         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
481         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
482         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
483         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
484         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
485         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
486         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
487         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
488         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
489         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
490         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
491         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
492         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
493         case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
494             size = alignedw * alignedh * ASTC_BLOCK_SIZE;
495             break;
496         default:
497             ALOGE("%s: unrecognized pixel format: 0x%x", __FUNCTION__, format);
498             return 0;
499     }
500     return size;
501 }
502 
getBufferSizeAndDimensions(int width,int height,int format,int & alignedw,int & alignedh)503 size_t getBufferSizeAndDimensions(int width, int height, int format,
504         int& alignedw, int &alignedh)
505 {
506     size_t size;
507 
508     AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
509             height,
510             format,
511             false,
512             alignedw,
513             alignedh);
514 
515     size = getSize(format, width, height, alignedw, alignedh);
516 
517     return size;
518 }
519 
520 
getBufferSizeAndDimensions(int width,int height,int format,int usage,int & alignedw,int & alignedh)521 size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,
522         int& alignedw, int &alignedh)
523 {
524     size_t size;
525     int tileEnabled = isMacroTileEnabled(format, usage);
526 
527     AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
528             height,
529             format,
530             tileEnabled,
531             alignedw,
532             alignedh);
533 
534     size = getSize(format, width, height, alignedw, alignedh);
535 
536     return size;
537 }
538 
539 
getBufferAttributes(int width,int height,int format,int usage,int & alignedw,int & alignedh,int & tileEnabled,size_t & size)540 void getBufferAttributes(int width, int height, int format, int usage,
541         int& alignedw, int &alignedh, int& tileEnabled, size_t& size)
542 {
543     tileEnabled = isMacroTileEnabled(format, usage);
544 
545     AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
546             height,
547             format,
548             tileEnabled,
549             alignedw,
550             alignedh);
551     size = getSize(format, width, height, alignedw, alignedh);
552 }
553 
getYUVPlaneInfo(private_handle_t * hnd,struct android_ycbcr * ycbcr)554 int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
555 {
556     int err = 0;
557     size_t ystride, cstride;
558     memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
559 
560     // Get the chroma offsets from the handle width/height. We take advantage
561     // of the fact the width _is_ the stride
562     switch (hnd->format) {
563         //Semiplanar
564         case HAL_PIXEL_FORMAT_YCbCr_420_SP:
565         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
566         case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
567         case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
568             ystride = cstride = hnd->width;
569             ycbcr->y  = (void*)hnd->base;
570             ycbcr->cb = (void*)(hnd->base + ystride * hnd->height);
571             ycbcr->cr = (void*)(hnd->base + ystride * hnd->height + 1);
572             ycbcr->ystride = ystride;
573             ycbcr->cstride = cstride;
574             ycbcr->chroma_step = 2;
575         break;
576 
577         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
578         case HAL_PIXEL_FORMAT_YCrCb_422_SP:
579         case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
580         case HAL_PIXEL_FORMAT_NV21_ZSL:
581         case HAL_PIXEL_FORMAT_RAW16:
582         case HAL_PIXEL_FORMAT_RAW10:
583             ystride = cstride = hnd->width;
584             ycbcr->y  = (void*)hnd->base;
585             ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
586             ycbcr->cb = (void*)(hnd->base + ystride * hnd->height + 1);
587             ycbcr->ystride = ystride;
588             ycbcr->cstride = cstride;
589             ycbcr->chroma_step = 2;
590         break;
591 
592         //Planar
593         case HAL_PIXEL_FORMAT_YV12:
594             ystride = hnd->width;
595             cstride = ALIGN(hnd->width/2, 16);
596             ycbcr->y  = (void*)hnd->base;
597             ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
598             ycbcr->cb = (void*)(hnd->base + ystride * hnd->height +
599                     cstride * hnd->height/2);
600             ycbcr->ystride = ystride;
601             ycbcr->cstride = cstride;
602             ycbcr->chroma_step = 1;
603 
604         break;
605         //Unsupported formats
606         case HAL_PIXEL_FORMAT_YCbCr_422_I:
607         case HAL_PIXEL_FORMAT_YCrCb_422_I:
608         case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
609         default:
610         ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
611                 hnd->format);
612         err = -EINVAL;
613     }
614     return err;
615 
616 }
617 
618 
619 
620 // Allocate buffer from width, height and format into a
621 // private_handle_t. It is the responsibility of the caller
622 // to free the buffer using the free_buffer function
alloc_buffer(private_handle_t ** pHnd,int w,int h,int format,int usage)623 int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
624 {
625     alloc_data data;
626     int alignedw, alignedh;
627     gralloc::IAllocController* sAlloc =
628         gralloc::IAllocController::getInstance();
629     data.base = 0;
630     data.fd = -1;
631     data.offset = 0;
632     data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
633                                             alignedh);
634 
635     data.align = getpagesize();
636     data.uncached = useUncached(usage);
637     int allocFlags = usage;
638 
639     int err = sAlloc->allocate(data, allocFlags);
640     if (0 != err) {
641         ALOGE("%s: allocate failed", __FUNCTION__);
642         return -ENOMEM;
643     }
644 
645     private_handle_t* hnd = new private_handle_t(data.fd, data.size,
646                                                  data.allocType, 0, format,
647                                                  alignedw, alignedh);
648     hnd->base = (uintptr_t) data.base;
649     hnd->offset = data.offset;
650     hnd->gpuaddr = 0;
651     *pHnd = hnd;
652     return 0;
653 }
654 
free_buffer(private_handle_t * hnd)655 void free_buffer(private_handle_t *hnd)
656 {
657     gralloc::IAllocController* sAlloc =
658         gralloc::IAllocController::getInstance();
659     if (hnd && hnd->fd > 0) {
660         IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
661         memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
662     }
663     if(hnd)
664         delete hnd;
665 
666 }
667