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
25namespace android {
26namespace filterfw {
27
28// GL Extensions that are dynamically looked up at runtime
29static PFNGLMAPBUFFEROESPROC    GLMapBufferOES    = NULL;
30static PFNGLUNMAPBUFFEROESPROC  GLUnmapBufferOES  = NULL;
31
32VertexFrame::VertexFrame(int size)
33  : vbo_(0),
34    size_(size) {
35}
36
37VertexFrame::~VertexFrame() {
38  glDeleteBuffers(1, &vbo_);
39}
40
41bool VertexFrame::CreateBuffer() {
42  glGenBuffers(1, &vbo_);
43  return !GLEnv::CheckGLError("Generating VBO");
44}
45
46bool VertexFrame::WriteData(const uint8_t* data, int size) {
47  // Create buffer if not created already
48  const bool first_upload = !HasVBO();
49  if (first_upload && !CreateBuffer()) {
50    ALOGE("VertexFrame: Could not create vertex buffer!");
51    return false;
52  }
53
54  // Upload the data
55  glBindBuffer(GL_ARRAY_BUFFER, vbo_);
56  if (GLEnv::CheckGLError("VBO Bind Buffer"))
57    return false;
58
59  if (first_upload && size == size_)
60    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
61  else if (!first_upload && size <= size_)
62    glBufferSubData(GL_ARRAY_BUFFER, 0, size, data);
63  else {
64    ALOGE("VertexFrame: Attempting to upload more data (%d bytes) than fits "
65         "inside the vertex frame (%d bytes)!", size, size_);
66    return false;
67  }
68
69  // Make sure it worked
70  if (GLEnv::CheckGLError("VBO Data Upload"))
71    return false;
72
73  // Subsequent uploads are now bound to the size given here
74  size_ = size;
75
76  return true;
77}
78
79int VertexFrame::Size() const {
80  return size_;
81}
82
83} // namespace filterfw
84} // namespace android
85