blob: ec489367ed32e16843c725377905cc1b918c6985 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
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
18#include "GrGLVertexBuffer.h"
19#include "GrGpuGL.h"
20
bsalomon@google.com1c13c962011-02-14 16:51:21 +000021GrGLVertexBuffer::GrGLVertexBuffer(GLuint id, GrGpuGL* gl, size_t sizeInBytes,
22 bool dynamic) :
reed@google.comac10a2d2010-12-22 21:39:39 +000023 INHERITED(sizeInBytes, dynamic),
24 fGL(gl),
25 fBufferID(id),
26 fLockPtr(NULL) {
27}
28
29GrGLVertexBuffer::~GrGLVertexBuffer() {
30 // make sure we've not been abandoned
31 if (fBufferID) {
32 fGL->notifyVertexBufferDelete(this);
33 GR_GL(DeleteBuffers(1, &fBufferID));
34 }
35}
36
bsalomon@google.com1c13c962011-02-14 16:51:21 +000037void GrGLVertexBuffer::bind() const {
38 GR_GL(BindBuffer(GL_ARRAY_BUFFER, fBufferID));
39 fGL->notifyVertexBufferBind(this);
40}
41
reed@google.comac10a2d2010-12-22 21:39:39 +000042GLuint GrGLVertexBuffer::bufferID() const {
43 return fBufferID;
44}
45
bsalomon@google.com1c13c962011-02-14 16:51:21 +000046void GrGLVertexBuffer::abandon() {
reed@google.comac10a2d2010-12-22 21:39:39 +000047 fBufferID = 0;
48 fGL = NULL;
49 fLockPtr = NULL;
50}
51
52void* GrGLVertexBuffer::lock() {
53 GrAssert(fBufferID);
54 GrAssert(!isLocked());
55 if (fGL->supportsBufferLocking()) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000056 bind();
57 // Let driver know it can discard the old data
58 GR_GL(BufferData(GL_ARRAY_BUFFER, size(), NULL,
reed@google.comac10a2d2010-12-22 21:39:39 +000059 dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
60 fLockPtr = GR_GLEXT(fGL->extensions(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +000061 MapBuffer(GL_ARRAY_BUFFER, GR_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000062 return fLockPtr;
63 }
64 return NULL;
65}
66
bsalomon@google.com1c13c962011-02-14 16:51:21 +000067void* GrGLVertexBuffer::lockPtr() const {
68 return fLockPtr;
69}
70
reed@google.comac10a2d2010-12-22 21:39:39 +000071void GrGLVertexBuffer::unlock() {
72 GrAssert(fBufferID);
73 GrAssert(isLocked());
bsalomon@google.com1c13c962011-02-14 16:51:21 +000074 GrAssert(fGL->supportsBufferLocking());
75
76 bind();
77 GR_GLEXT(fGL->extensions(), UnmapBuffer(GL_ARRAY_BUFFER));
78 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000079}
80
81bool GrGLVertexBuffer::isLocked() const {
82 GrAssert(fBufferID);
83#if GR_DEBUG
84 if (fGL->supportsBufferLocking()) {
85 GLint mapped;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000086 bind();
reed@google.comac10a2d2010-12-22 21:39:39 +000087 GR_GL(GetBufferParameteriv(GL_ARRAY_BUFFER, GR_BUFFER_MAPPED, &mapped));
88 GrAssert(!!mapped == !!fLockPtr);
89 }
90#endif
91 return NULL != fLockPtr;
92}
93
bsalomon@google.com1c13c962011-02-14 16:51:21 +000094bool GrGLVertexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000095 GrAssert(fBufferID);
96 GrAssert(!isLocked());
97 if (srcSizeInBytes > size()) {
98 return false;
99 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000100 bind();
101 GLenum usage = dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
102 if (size() == srcSizeInBytes) {
103 GR_GL(BufferData(GL_ARRAY_BUFFER, srcSizeInBytes, src, usage));
104 } else {
105 GR_GL(BufferData(GL_ARRAY_BUFFER, size(), NULL, usage));
106 GR_GL(BufferSubData(GL_ARRAY_BUFFER, 0, srcSizeInBytes, src));
107 }
108 return true;
109}
110
111bool GrGLVertexBuffer::updateSubData(const void* src,
112 size_t srcSizeInBytes,
113 size_t offset) {
114 GrAssert(fBufferID);
115 GrAssert(!isLocked());
116 if (srcSizeInBytes + offset > size()) {
117 return false;
118 }
119 bind();
120 GR_GL(BufferSubData(GL_ARRAY_BUFFER, offset, srcSizeInBytes, src));
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 return true;
122}
123