1 /*
2  * Copyright (C) 2011 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 "base/logging.h"
18 
19 #include "core/gl_env.h"
20 #include "core/vertex_frame.h"
21 
22 #include <GLES2/gl2ext.h>
23 #include <EGL/egl.h>
24 
25 namespace android {
26 namespace filterfw {
27 
VertexFrame(int size)28 VertexFrame::VertexFrame(int size)
29   : vbo_(0),
30     size_(size) {
31 }
32 
~VertexFrame()33 VertexFrame::~VertexFrame() {
34   glDeleteBuffers(1, &vbo_);
35 }
36 
CreateBuffer()37 bool VertexFrame::CreateBuffer() {
38   glGenBuffers(1, &vbo_);
39   return !GLEnv::CheckGLError("Generating VBO");
40 }
41 
WriteData(const uint8_t * data,int size)42 bool VertexFrame::WriteData(const uint8_t* data, int size) {
43   // Create buffer if not created already
44   const bool first_upload = !HasVBO();
45   if (first_upload && !CreateBuffer()) {
46     ALOGE("VertexFrame: Could not create vertex buffer!");
47     return false;
48   }
49 
50   // Upload the data
51   glBindBuffer(GL_ARRAY_BUFFER, vbo_);
52   if (GLEnv::CheckGLError("VBO Bind Buffer"))
53     return false;
54 
55   if (first_upload && size == size_)
56     glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
57   else if (!first_upload && size <= size_)
58     glBufferSubData(GL_ARRAY_BUFFER, 0, size, data);
59   else {
60     ALOGE("VertexFrame: Attempting to upload more data (%d bytes) than fits "
61          "inside the vertex frame (%d bytes)!", size, size_);
62     return false;
63   }
64 
65   // Make sure it worked
66   if (GLEnv::CheckGLError("VBO Data Upload"))
67     return false;
68 
69   // Subsequent uploads are now bound to the size given here
70   size_ = size;
71 
72   return true;
73 }
74 
Size() const75 int VertexFrame::Size() const {
76   return size_;
77 }
78 
79 } // namespace filterfw
80 } // namespace android
81