1/*
2 ** Copyright 2008, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17#include <stdint.h>
18#include <stddef.h>
19#include <sys/types.h>
20
21#include <utils/Atomic.h>
22#include <utils/RefBase.h>
23#include <utils/KeyedVector.h>
24#include <utils/Errors.h>
25
26#include <GLES/gl.h>
27
28#include "BufferObjectManager.h"
29
30
31namespace android {
32
33using namespace gl;
34
35// ----------------------------------------------------------------------------
36
37EGLBufferObjectManager::EGLBufferObjectManager()
38: TokenManager(), mCount(0)
39{
40}
41
42EGLBufferObjectManager::~EGLBufferObjectManager()
43{
44    // destroy all the buffer objects and their storage
45    GLsizei n = mBuffers.size();
46    for (GLsizei i=0 ; i<n ; i++) {
47        buffer_t* bo = mBuffers.valueAt(i);
48        free(bo->data);
49        delete bo;
50    }
51}
52
53buffer_t const* EGLBufferObjectManager::bind(GLuint buffer)
54{
55    Mutex::Autolock _l(mLock);
56    int32_t i = mBuffers.indexOfKey(buffer);
57    if (i >= 0) {
58        return mBuffers.valueAt(i);
59    }
60    buffer_t* bo = new buffer_t;
61    bo->data = 0;
62    bo->usage = GL_STATIC_DRAW;
63    bo->size = 0;
64    bo->name = buffer;
65    mBuffers.add(buffer, bo);
66    return bo;
67}
68
69int EGLBufferObjectManager::allocateStore(buffer_t* bo,
70        GLsizeiptr size, GLenum usage)
71{
72    Mutex::Autolock _l(mLock);
73    if (size != bo->size) {
74       uint8_t* data = (uint8_t*)malloc(size);
75        if (data == 0)
76            return -1;
77        free(bo->data);
78        bo->data = data;
79        bo->size = size;
80    }
81    bo->usage = usage;
82    return 0;
83}
84
85void EGLBufferObjectManager::deleteBuffers(GLsizei n, const GLuint* buffers)
86{
87    Mutex::Autolock _l(mLock);
88    while (n--) {
89        const GLuint t = *buffers++;
90        if (t) {
91            int32_t index = mBuffers.indexOfKey(t);
92            if (index >= 0) {
93                buffer_t* bo = mBuffers.valueAt(index);
94                free(bo->data);
95                mBuffers.removeItemsAt(index);
96                delete bo;
97            }
98        }
99    }
100}
101
102// ----------------------------------------------------------------------------
103}; // namespace android
104