1 /*
2  **
3  ** Copyright 2006, The Android Open Source Project
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  **     http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  */
17 
18 #ifndef ANDROID_OPENGLES_BUFFER_OBJECT_MANAGER_H
19 #define ANDROID_OPENGLES_BUFFER_OBJECT_MANAGER_H
20 
21 #include <atomic>
22 #include <stdint.h>
23 #include <stddef.h>
24 #include <sys/types.h>
25 
26 #include <utils/RefBase.h>
27 #include <utils/KeyedVector.h>
28 #include <utils/Errors.h>
29 
30 #include <GLES/gl.h>
31 
32 #include "Tokenizer.h"
33 #include "TokenManager.h"
34 
35 
36 namespace android {
37 
38 // ----------------------------------------------------------------------------
39 
40 namespace gl {
41 
42 struct buffer_t {
43     GLsizeiptr      size;
44     GLenum          usage;
45     uint8_t*        data;
46     uint32_t        name;
47 };
48 
49 };
50 
51 class EGLBufferObjectManager : public TokenManager
52 {
53 public:
54     EGLBufferObjectManager();
55     ~EGLBufferObjectManager();
56 
57     // protocol for sp<>
58     inline  void    incStrong(const void* id) const;
59     inline  void    decStrong(const void* id) const;
60     typedef void    weakref_type;
61 
62     gl::buffer_t const* bind(GLuint buffer);
63     int                 allocateStore(gl::buffer_t* bo, GLsizeiptr size, GLenum usage);
64     void                deleteBuffers(GLsizei n, const GLuint* buffers);
65 
66 private:
67     mutable std::atomic_size_t          mCount;
68     mutable Mutex                       mLock;
69     KeyedVector<GLuint, gl::buffer_t*>  mBuffers;
70 };
71 
incStrong(const void *)72 void EGLBufferObjectManager::incStrong(const void* /*id*/) const {
73     mCount.fetch_add(1, std::memory_order_relaxed);
74 }
decStrong(const void *)75 void EGLBufferObjectManager::decStrong(const void* /*id*/) const {
76     if (mCount.fetch_sub(1, std::memory_order_release) == 0) {
77         std::atomic_thread_fence(std::memory_order_acquire);
78         delete this;
79     }
80 }
81 
82 // ----------------------------------------------------------------------------
83 }; // namespace android
84 
85 #endif // ANDROID_OPENGLES_BUFFER_OBJECT_MANAGER_H
86 
87