blob: 9539c8a2a180bb0d3f1a8f038635bbd38cef1915 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
twiz@google.com59a190b2011-03-14 21:23:01 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
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));
twiz@google.com59a190b2011-03-14 21:23:01 +000060 fLockPtr = GR_GL(MapBuffer(GL_ELEMENT_ARRAY_BUFFER, GR_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000061
62 return fLockPtr;
63 }
64 return NULL;
65}
66
bsalomon@google.com1c13c962011-02-14 16:51:21 +000067void* GrGLIndexBuffer::lockPtr() const {
68 return fLockPtr;
69}
70
reed@google.comac10a2d2010-12-22 21:39:39 +000071void GrGLIndexBuffer::unlock() {
72 GrAssert(fBufferID);
73 GrAssert(isLocked());
bsalomon@google.com1c13c962011-02-14 16:51:21 +000074 GrAssert(fGL->supportsBufferLocking());
reed@google.comac10a2d2010-12-22 21:39:39 +000075
bsalomon@google.com1c13c962011-02-14 16:51:21 +000076 bind();
twiz@google.com59a190b2011-03-14 21:23:01 +000077 GR_GL(UnmapBuffer(GL_ELEMENT_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000078 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000079}
80
81bool GrGLIndexBuffer::isLocked() const {
82 GrAssert(fBufferID);
83#if GR_DEBUG
84 if (fGL->supportsBufferLocking()) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000085 bind();
reed@google.comac10a2d2010-12-22 21:39:39 +000086 GLint mapped;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000087 GR_GL(GetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +000088 GR_BUFFER_MAPPED, &mapped));
89 GrAssert(!!mapped == !!fLockPtr);
90 }
91#endif
92 return NULL != fLockPtr;
93}
94
bsalomon@google.com1c13c962011-02-14 16:51:21 +000095bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000096 GrAssert(fBufferID);
97 GrAssert(!isLocked());
98 if (srcSizeInBytes > size()) {
99 return false;
100 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000101 bind();
102 GLenum usage = dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
103 if (size() == srcSizeInBytes) {
104 GR_GL(BufferData(GL_ELEMENT_ARRAY_BUFFER, srcSizeInBytes, src, usage));
105 } else {
106 GR_GL(BufferData(GL_ELEMENT_ARRAY_BUFFER, size(), NULL, usage));
107 GR_GL(BufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, srcSizeInBytes, src));
108 }
109 return true;
110}
111
112bool GrGLIndexBuffer::updateSubData(const void* src,
113 size_t srcSizeInBytes,
114 size_t offset) {
115 GrAssert(fBufferID);
116 GrAssert(!isLocked());
117 if (srcSizeInBytes + offset > size()) {
118 return false;
119 }
120 bind();
121 GR_GL(BufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, srcSizeInBytes, src));
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 return true;
123}
124