blob: a272d271df577b4c0ab451d3e43c3e6bf830a8d4 [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
twiz@google.com0f31ca72011-03-18 17:38:11 +000021GrGLIndexBuffer::GrGLIndexBuffer(GrGLuint id, GrGpuGL* gl, size_t sizeInBytes,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000022 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 {
twiz@google.com0f31ca72011-03-18 17:38:11 +000038 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000039 fGL->notifyIndexBufferBind(this);
40}
41
twiz@google.com0f31ca72011-03-18 17:38:11 +000042GrGLuint GrGLIndexBuffer::bufferID() const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000043 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
twiz@google.com0f31ca72011-03-18 17:38:11 +000058 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size(), NULL,
59 dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
60 fLockPtr = GR_GL(MapBuffer(GR_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.com0f31ca72011-03-18 17:38:11 +000077 GR_GL(UnmapBuffer(GR_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();
twiz@google.com0f31ca72011-03-18 17:38:11 +000086 GrGLint mapped;
87 GR_GL(GetBufferParameteriv(GR_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();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000102 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000103 if (size() == srcSizeInBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000104 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, srcSizeInBytes, src, usage));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000105 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000106 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size(), NULL, usage));
107 GR_GL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, 0, srcSizeInBytes, src));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000108 }
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();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000121 GR_GL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, offset, srcSizeInBytes, src));
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 return true;
123}
124