blob: 2dc415420906b2d41d89c05b0a7ea360d53f7de0 [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 "GrGLIndexBuffer.h"
19#include "GrGpuGL.h"
20
bsalomon@google.com1c13c962011-02-14 16:51:21 +000021GrGLIndexBuffer::GrGLIndexBuffer(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
reed@google.comac10a2d2010-12-22 21:39:39 +000029GrGLIndexBuffer::~GrGLIndexBuffer() {
30 // make sure we've not been abandoned
31 if (fBufferID) {
32 fGL->notifyIndexBufferDelete(this);
33 GR_GL(DeleteBuffers(1, &fBufferID));
34 }
35}
36
bsalomon@google.com1c13c962011-02-14 16:51:21 +000037void GrGLIndexBuffer::bind() const {
38 GR_GL(BindBuffer(GL_ELEMENT_ARRAY_BUFFER, fBufferID));
39 fGL->notifyIndexBufferBind(this);
40}
41
42GLuint GrGLIndexBuffer::bufferID() const {
43 return fBufferID;
44}
45
reed@google.comac10a2d2010-12-22 21:39:39 +000046void GrGLIndexBuffer::abandon() {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000047 fBufferID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000048 fGL = NULL;
49 fLockPtr = NULL;
50}
51
52void* GrGLIndexBuffer::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_ELEMENT_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(),
61 MapBuffer(GL_ELEMENT_ARRAY_BUFFER, GR_WRITE_ONLY));
62
63 return fLockPtr;
64 }
65 return NULL;
66}
67
bsalomon@google.com1c13c962011-02-14 16:51:21 +000068void* GrGLIndexBuffer::lockPtr() const {
69 return fLockPtr;
70}
71
reed@google.comac10a2d2010-12-22 21:39:39 +000072void GrGLIndexBuffer::unlock() {
73 GrAssert(fBufferID);
74 GrAssert(isLocked());
bsalomon@google.com1c13c962011-02-14 16:51:21 +000075 GrAssert(fGL->supportsBufferLocking());
reed@google.comac10a2d2010-12-22 21:39:39 +000076
bsalomon@google.com1c13c962011-02-14 16:51:21 +000077 bind();
78 GR_GLEXT(fGL->extensions(), UnmapBuffer(GL_ELEMENT_ARRAY_BUFFER));
79 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000080}
81
82bool GrGLIndexBuffer::isLocked() const {
83 GrAssert(fBufferID);
84#if GR_DEBUG
85 if (fGL->supportsBufferLocking()) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000086 bind();
reed@google.comac10a2d2010-12-22 21:39:39 +000087 GLint mapped;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000088 GR_GL(GetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +000089 GR_BUFFER_MAPPED, &mapped));
90 GrAssert(!!mapped == !!fLockPtr);
91 }
92#endif
93 return NULL != fLockPtr;
94}
95
bsalomon@google.com1c13c962011-02-14 16:51:21 +000096bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000097 GrAssert(fBufferID);
98 GrAssert(!isLocked());
99 if (srcSizeInBytes > size()) {
100 return false;
101 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000102 bind();
103 GLenum usage = dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
104 if (size() == srcSizeInBytes) {
105 GR_GL(BufferData(GL_ELEMENT_ARRAY_BUFFER, srcSizeInBytes, src, usage));
106 } else {
107 GR_GL(BufferData(GL_ELEMENT_ARRAY_BUFFER, size(), NULL, usage));
108 GR_GL(BufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, srcSizeInBytes, src));
109 }
110 return true;
111}
112
113bool GrGLIndexBuffer::updateSubData(const void* src,
114 size_t srcSizeInBytes,
115 size_t offset) {
116 GrAssert(fBufferID);
117 GrAssert(!isLocked());
118 if (srcSizeInBytes + offset > size()) {
119 return false;
120 }
121 bind();
122 GR_GL(BufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, srcSizeInBytes, src));
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 return true;
124}
125