alloc_controller.cpp revision 073830dc96c3d434bdc24a9ca23a7479fa9de19f
1/*
2 * Copyright (c) 2011-2012, 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
40#ifdef VENUS_COLOR_FORMAT
41#include <media/msm_media_info.h>
42#else
43#define VENUS_Y_STRIDE(args...) 0
44#define VENUS_Y_SCANLINES(args...) 0
45#define VENUS_BUFFER_SIZE(args...) 0
46#endif
47
48using namespace gralloc;
49using namespace qdutils;
50
51ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
52
53//Common functions
54static bool canFallback(int usage, bool triedSystem)
55{
56    // Fallback to system heap when alloc fails unless
57    // 1. Composition type is MDP
58    // 2. Alloc from system heap was already tried
59    // 3. The heap type is requsted explicitly
60    // 4. The heap type is protected
61    // 5. The buffer is meant for external display only
62
63    if(QCCompositionType::getInstance().getCompositionType() &
64       COMPOSITION_TYPE_MDP)
65        return false;
66    if(triedSystem)
67        return false;
68    if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
69        return false;
70    if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
71        return false;
72    //Return true by default
73    return true;
74}
75
76static bool useUncached(int usage)
77{
78    if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
79        return true;
80    if(((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
81       ||((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
82        return true;
83    return false;
84}
85
86//-------------- AdrenoMemInfo-----------------------//
87AdrenoMemInfo::AdrenoMemInfo()
88{
89    libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
90    if (libadreno_utils) {
91        *(void **)&LINK_adreno_compute_padding = ::dlsym(libadreno_utils,
92                                           "compute_surface_padding");
93    }
94}
95
96AdrenoMemInfo::~AdrenoMemInfo()
97{
98    if (libadreno_utils) {
99        ::dlclose(libadreno_utils);
100    }
101}
102
103int AdrenoMemInfo::getStride(int width, int format)
104{
105    int stride = ALIGN(width, 32);
106    // Currently surface padding is only computed for RGB* surfaces.
107    if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
108        // Don't add any additional padding if debug.gralloc.map_fb_memory
109        // is enabled
110        char property[PROPERTY_VALUE_MAX];
111        if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
112           (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
113           (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
114              return stride;
115        }
116
117        int bpp = 4;
118        switch(format)
119        {
120            case HAL_PIXEL_FORMAT_RGB_888:
121                bpp = 3;
122                break;
123            case HAL_PIXEL_FORMAT_RGB_565:
124                bpp = 2;
125                break;
126            default: break;
127        }
128        if ((libadreno_utils) && (LINK_adreno_compute_padding)) {
129            int surface_tile_height = 1;   // Linear surface
130            int raster_mode         = 0;   // Adreno unknown raster mode.
131            int padding_threshold   = 512; // Threshold for padding surfaces.
132            // the function below expects the width to be a multiple of
133            // 32 pixels, hence we pass stride instead of width.
134            stride = LINK_adreno_compute_padding(stride, bpp,
135                                      surface_tile_height, raster_mode,
136                                      padding_threshold);
137        }
138    } else {
139        switch (format)
140        {
141            case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
142            case HAL_PIXEL_FORMAT_RAW_SENSOR:
143                stride = ALIGN(width, 32);
144                break;
145            case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
146                stride = ALIGN(width, 128);
147                break;
148            case HAL_PIXEL_FORMAT_YCbCr_420_SP:
149            case HAL_PIXEL_FORMAT_YCrCb_420_SP:
150            case HAL_PIXEL_FORMAT_YV12:
151            case HAL_PIXEL_FORMAT_YCbCr_422_SP:
152            case HAL_PIXEL_FORMAT_YCrCb_422_SP:
153                stride = ALIGN(width, 16);
154                break;
155            case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
156            case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
157                stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
158                break;
159            case HAL_PIXEL_FORMAT_BLOB:
160                stride = width;
161                break;
162            case HAL_PIXEL_FORMAT_NV21_ZSL:
163                stride = ALIGN(width, 64);
164                break;
165            default: break;
166        }
167    }
168    return stride;
169}
170
171//-------------- IAllocController-----------------------//
172IAllocController* IAllocController::sController = NULL;
173IAllocController* IAllocController::getInstance(void)
174{
175    if(sController == NULL) {
176        sController = new IonController();
177    }
178    return sController;
179}
180
181
182//-------------- IonController-----------------------//
183IonController::IonController()
184{
185    mIonAlloc = new IonAlloc();
186    mUseTZProtection = false;
187    char property[PROPERTY_VALUE_MAX];
188    if ((property_get("persist.gralloc.cp.level3", property, NULL) <= 0) ||
189                            (atoi(property) != 1)) {
190        mUseTZProtection = true;
191    }
192}
193
194int IonController::allocate(alloc_data& data, int usage)
195{
196    int ionFlags = 0;
197    int ret;
198
199    data.uncached = useUncached(usage);
200    data.allocType = 0;
201
202    if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
203        ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
204
205    if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
206        ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
207
208    if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
209        ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
210
211    if(usage & GRALLOC_USAGE_PROTECTED) {
212        if ((mUseTZProtection) && (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)) {
213            ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
214            ionFlags |= ION_SECURE;
215        } else {
216            // for targets/OEMs which do not need HW level protection
217            // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
218            ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
219        }
220    } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
221        //MM Heap is exclusively a secure heap.
222        //If it is used for non secure cases, fallback to IOMMU heap
223        ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
224                                cannot be used as an insecure heap!\
225                                trying to use IOMMU instead !!");
226        ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
227    }
228
229    if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
230        ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
231
232    if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
233        ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
234
235    if(ionFlags & ION_SECURE)
236         data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
237
238    // if no flags are set, default to
239    // SF + IOMMU heaps, so that bypass can work
240    // we can fall back to system heap if
241    // we run out.
242    if(!ionFlags)
243        ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
244
245    data.flags = ionFlags;
246    ret = mIonAlloc->alloc_buffer(data);
247
248    // Fallback
249    if(ret < 0 && canFallback(usage,
250                              (ionFlags & ION_SYSTEM_HEAP_ID)))
251    {
252        ALOGW("Falling back to system heap");
253        data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
254        ret = mIonAlloc->alloc_buffer(data);
255    }
256
257    if(ret >= 0 ) {
258        data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
259    }
260
261    return ret;
262}
263
264IMemAlloc* IonController::getAllocator(int flags)
265{
266    IMemAlloc* memalloc = NULL;
267    if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
268        memalloc = mIonAlloc;
269    } else {
270        ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
271    }
272
273    return memalloc;
274}
275
276size_t getBufferSizeAndDimensions(int width, int height, int format,
277                                  int& alignedw, int &alignedh)
278{
279    size_t size;
280
281    alignedw = AdrenoMemInfo::getInstance().getStride(width, format);
282    alignedh = ALIGN(height, 32);
283    switch (format) {
284        case HAL_PIXEL_FORMAT_RGBA_8888:
285        case HAL_PIXEL_FORMAT_RGBX_8888:
286        case HAL_PIXEL_FORMAT_BGRA_8888:
287        case HAL_PIXEL_FORMAT_sRGB_A_8888:
288        case HAL_PIXEL_FORMAT_sRGB_X_8888:
289            size = alignedw * alignedh * 4;
290            break;
291        case HAL_PIXEL_FORMAT_RGB_888:
292            size = alignedw * alignedh * 3;
293            break;
294        case HAL_PIXEL_FORMAT_RGB_565:
295        case HAL_PIXEL_FORMAT_RAW_SENSOR:
296            size = alignedw * alignedh * 2;
297            break;
298
299            // adreno formats
300        case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:  // NV21
301            size  = ALIGN(alignedw*alignedh, 4096);
302            size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
303            break;
304        case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:   // NV12
305            // The chroma plane is subsampled,
306            // but the pitch in bytes is unchanged
307            // The GPU needs 4K alignment, but the video decoder needs 8K
308            size  = ALIGN( alignedw * alignedh, 8192);
309            size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
310            break;
311        case HAL_PIXEL_FORMAT_YV12:
312            if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
313                ALOGE("w or h is odd for the YV12 format");
314                return -EINVAL;
315            }
316            alignedh = height;
317            size = alignedw*alignedh +
318                    (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
319            size = ALIGN(size, 4096);
320            break;
321        case HAL_PIXEL_FORMAT_YCbCr_420_SP:
322        case HAL_PIXEL_FORMAT_YCrCb_420_SP:
323            alignedh = height;
324            size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
325            break;
326        case HAL_PIXEL_FORMAT_YCbCr_422_SP:
327        case HAL_PIXEL_FORMAT_YCrCb_422_SP:
328            if(width & 1) {
329                ALOGE("width is odd for the YUV422_SP format");
330                return -EINVAL;
331            }
332            alignedh = height;
333            size = ALIGN(alignedw * alignedh * 2, 4096);
334            break;
335        case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
336        case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
337            alignedh = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
338            size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
339            break;
340        case HAL_PIXEL_FORMAT_BLOB:
341            if(height != 1) {
342                ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
343                      must have height==1 ", __FUNCTION__);
344                return -EINVAL;
345            }
346            alignedh = height;
347            alignedw = width;
348            size = width;
349            break;
350        case HAL_PIXEL_FORMAT_NV21_ZSL:
351            alignedh = ALIGN(height, 64);
352            size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
353            break;
354        default:
355            ALOGE("unrecognized pixel format: 0x%x", format);
356            return -EINVAL;
357    }
358
359    return size;
360}
361
362// Allocate buffer from width, height and format into a
363// private_handle_t. It is the responsibility of the caller
364// to free the buffer using the free_buffer function
365int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
366{
367    alloc_data data;
368    int alignedw, alignedh;
369    gralloc::IAllocController* sAlloc =
370        gralloc::IAllocController::getInstance();
371    data.base = 0;
372    data.fd = -1;
373    data.offset = 0;
374    data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh);
375    data.align = getpagesize();
376    data.uncached = useUncached(usage);
377    int allocFlags = usage;
378
379    int err = sAlloc->allocate(data, allocFlags);
380    if (0 != err) {
381        ALOGE("%s: allocate failed", __FUNCTION__);
382        return -ENOMEM;
383    }
384
385    private_handle_t* hnd = new private_handle_t(data.fd, data.size,
386                                                 data.allocType, 0, format,
387                                                 alignedw, alignedh);
388    hnd->base = (int) data.base;
389    hnd->offset = data.offset;
390    hnd->gpuaddr = 0;
391    *pHnd = hnd;
392    return 0;
393}
394
395void free_buffer(private_handle_t *hnd)
396{
397    gralloc::IAllocController* sAlloc =
398        gralloc::IAllocController::getInstance();
399    if (hnd && hnd->fd > 0) {
400        IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
401        memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
402    }
403    if(hnd)
404        delete hnd;
405
406}
407