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