summaryrefslogtreecommitdiffstats
path: root/camera/MemoryManager.cpp
blob: b684203d7e6d38fb88ed0c16b636aaf2ba8c2aea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "CameraHal.h"
#include "TICameraParameters.h"

extern "C" {

//#include <timm_osal_interfaces.h>
//#include <timm_osal_trace.h>


};

namespace Ti {
namespace Camera {

///@todo Move these constants to a common header file, preferably in tiler.h
#define STRIDE_8BIT (4 * 1024)
#define STRIDE_16BIT (4 * 1024)

#define ALLOCATION_2D 2

///Utility Macro Declarations

/*--------------------MemoryManager Class STARTS here-----------------------------*/
MemoryManager::MemoryManager() {
    mIonFd = -1;
}

MemoryManager::~MemoryManager() {
    if ( mIonFd >= 0 ) {
        ion_close(mIonFd);
        mIonFd = -1;
    }
}

status_t MemoryManager::initialize() {
    if ( mIonFd == -1 ) {
        mIonFd = ion_open();
        if ( mIonFd < 0 ) {
            CAMHAL_LOGE("ion_open() failed, error: %d", mIonFd);
            mIonFd = -1;
            return NO_INIT;
        }
    }

    return OK;
}

CameraBuffer* MemoryManager::allocateBufferList(int width, int height, const char* format, int &size, int numBufs)
{
    LOG_FUNCTION_NAME;

    CAMHAL_ASSERT(mIonFd != -1);

    ///We allocate numBufs+1 because the last entry will be marked NULL to indicate end of array, which is used when freeing
    ///the buffers
    const uint numArrayEntriesC = (uint)(numBufs+1);

    ///Allocate a buffer array
    CameraBuffer *buffers = new CameraBuffer [numArrayEntriesC];
    if(!buffers) {
        CAMHAL_LOGEB("Allocation failed when creating buffers array of %d CameraBuffer elements", numArrayEntriesC);
        goto error;
    }

    ///Initialize the array with zeros - this will help us while freeing the array in case of error
    ///If a value of an array element is NULL, it means we didnt allocate it
    memset(buffers, 0, sizeof(CameraBuffer) * numArrayEntriesC);

    //2D Allocations are not supported currently
    if(size != 0) {
        struct ion_handle *handle;
        int mmap_fd;
        size_t stride;

        ///1D buffers
        for (int i = 0; i < numBufs; i++) {
            unsigned char *data;
#ifdef USE_LIBION_TI
            int ret = ion_alloc(mIonFd, size, 0, 1 << ION_HEAP_TYPE_CARVEOUT,
                    &handle);
#else
            int ret = ion_alloc(mIonFd, size, 0, 1 << ION_HEAP_TYPE_CARVEOUT, 0,
                    &handle);
#endif
            if((ret < 0) || ((int)handle == -ENOMEM)) {
                ret = ion_alloc_tiler(mIonFd, (size_t)size, 1, TILER_PIXEL_FMT_PAGE,
                OMAP_ION_HEAP_TILER_MASK, &handle, &stride);
            }

            if((ret < 0) || ((int)handle == -ENOMEM)) {
                CAMHAL_LOGEB("FAILED to allocate ion buffer of size=%d. ret=%d(0x%x)", size, ret, ret);
                goto error;
            }

            CAMHAL_LOGDB("Before mapping, handle = %p, nSize = %d", handle, size);
            if ((ret = ion_map(mIonFd, handle, size, PROT_READ | PROT_WRITE, MAP_SHARED, 0,
                          &data, &mmap_fd)) < 0) {
                CAMHAL_LOGEB("Userspace mapping of ION buffers returned error %d", ret);
                ion_free(mIonFd, handle);
                goto error;
            }

            buffers[i].type = CAMERA_BUFFER_ION;
            buffers[i].opaque = data;
            buffers[i].mapped = data;
            buffers[i].ion_handle = handle;
            buffers[i].ion_fd = mIonFd;
            buffers[i].fd = mmap_fd;
            buffers[i].size = size;
            buffers[i].format = CameraHal::getPixelFormatConstant(format);

        }
    }

    LOG_FUNCTION_NAME_EXIT;

    return buffers;

error:

    CAMHAL_LOGE("Freeing buffers already allocated after error occurred");
    if(buffers)
        freeBufferList(buffers);

    if ( NULL != mErrorNotifier.get() )
        mErrorNotifier->errorNotify(-ENOMEM);
    LOG_FUNCTION_NAME_EXIT;

    return NULL;
}

CameraBuffer* MemoryManager::getBufferList(int *numBufs) {
    LOG_FUNCTION_NAME;
    if (numBufs) *numBufs = -1;

    return NULL;
}

//TODO: Get needed data to map tiler buffers
//Return dummy data for now
uint32_t * MemoryManager::getOffsets()
{
    LOG_FUNCTION_NAME;

    LOG_FUNCTION_NAME_EXIT;

    return NULL;
}

int MemoryManager::getFd()
{
    LOG_FUNCTION_NAME;

    LOG_FUNCTION_NAME_EXIT;

    return -1;
}

int MemoryManager::freeBufferList(CameraBuffer *buffers)
{
    status_t ret = NO_ERROR;
    LOG_FUNCTION_NAME;

    int i;

    if(!buffers)
        {
        CAMHAL_LOGEA("NULL pointer passed to freebuffer");
        LOG_FUNCTION_NAME_EXIT;
        return BAD_VALUE;
        }

    i = 0;
    while(buffers[i].type == CAMERA_BUFFER_ION)
        {
        if(buffers[i].size)
            {
            munmap(buffers[i].opaque, buffers[i].size);
            close(buffers[i].fd);
            ion_free(mIonFd, buffers[i].ion_handle);
            }
        else
            {
            CAMHAL_LOGEA("Not a valid Memory Manager buffer");
            }
        i++;
        }

    delete [] buffers;

    LOG_FUNCTION_NAME_EXIT;
    return ret;
}

status_t MemoryManager::setErrorHandler(ErrorNotifier *errorNotifier)
{
    status_t ret = NO_ERROR;

    LOG_FUNCTION_NAME;

    if ( NULL == errorNotifier )
        {
        CAMHAL_LOGEA("Invalid Error Notifier reference");
        ret = -EINVAL;
        }

    if ( NO_ERROR == ret )
        {
        mErrorNotifier = errorNotifier;
        }

    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

} // namespace Camera
} // namespace Ti


/*--------------------MemoryManager Class ENDS here-----------------------------*/