blob: 1d3f89319df58714283abfdb785c0f1bbe8ca897 [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.com8fe72472011-03-30 21:26:44 +000021#define GPUGL static_cast<GrGpuGL*>(getGpu())
22
23GrGLIndexBuffer::GrGLIndexBuffer(GrGpuGL* gpu,
24 GrGLuint id,
25 size_t sizeInBytes,
26 bool dynamic)
27 : INHERITED(gpu, sizeInBytes, dynamic)
28 , fBufferID(id)
29 , fLockPtr(NULL) {
30
reed@google.comac10a2d2010-12-22 21:39:39 +000031}
32
bsalomon@google.com8fe72472011-03-30 21:26:44 +000033void GrGLIndexBuffer::onRelease() {
reed@google.comac10a2d2010-12-22 21:39:39 +000034 // make sure we've not been abandoned
35 if (fBufferID) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000036 GPUGL->notifyIndexBufferDelete(this);
reed@google.comac10a2d2010-12-22 21:39:39 +000037 GR_GL(DeleteBuffers(1, &fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000038 fBufferID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000039 }
40}
41
bsalomon@google.com8fe72472011-03-30 21:26:44 +000042void GrGLIndexBuffer::onAbandon() {
43 fBufferID = 0;
44 fLockPtr = NULL;
45}
46
bsalomon@google.com1c13c962011-02-14 16:51:21 +000047void GrGLIndexBuffer::bind() const {
twiz@google.com0f31ca72011-03-18 17:38:11 +000048 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000049 GPUGL->notifyIndexBufferBind(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000050}
51
twiz@google.com0f31ca72011-03-18 17:38:11 +000052GrGLuint GrGLIndexBuffer::bufferID() const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000053 return fBufferID;
54}
55
reed@google.comac10a2d2010-12-22 21:39:39 +000056void* GrGLIndexBuffer::lock() {
57 GrAssert(fBufferID);
58 GrAssert(!isLocked());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000059 if (GPUGL->supportsBufferLocking()) {
60 this->bind();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000061 // Let driver know it can discard the old data
vandebo@chromium.orgec364712011-07-26 03:44:05 +000062 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size(), NULL,
63 dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.comc312bf92011-03-21 21:10:33 +000064 fLockPtr = GR_GL(MapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, GR_GL_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000065
66 return fLockPtr;
67 }
68 return NULL;
69}
70
bsalomon@google.com1c13c962011-02-14 16:51:21 +000071void* GrGLIndexBuffer::lockPtr() const {
72 return fLockPtr;
73}
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075void GrGLIndexBuffer::unlock() {
76 GrAssert(fBufferID);
77 GrAssert(isLocked());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000078 GrAssert(GPUGL->supportsBufferLocking());
reed@google.comac10a2d2010-12-22 21:39:39 +000079
bsalomon@google.com8fe72472011-03-30 21:26:44 +000080 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000081 GR_GL(UnmapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000082 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000083}
84
85bool GrGLIndexBuffer::isLocked() const {
reed@google.comac10a2d2010-12-22 21:39:39 +000086#if GR_DEBUG
bsalomon@google.com8fe72472011-03-30 21:26:44 +000087 if (this->isValid() && GPUGL->supportsBufferLocking()) {
88 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000089 GrGLint mapped;
90 GR_GL(GetBufferParameteriv(GR_GL_ELEMENT_ARRAY_BUFFER,
bsalomon@google.comc312bf92011-03-21 21:10:33 +000091 GR_GL_BUFFER_MAPPED, &mapped));
reed@google.comac10a2d2010-12-22 21:39:39 +000092 GrAssert(!!mapped == !!fLockPtr);
93 }
94#endif
95 return NULL != fLockPtr;
96}
97
bsalomon@google.com1c13c962011-02-14 16:51:21 +000098bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000099 GrAssert(fBufferID);
100 GrAssert(!isLocked());
vandebo@chromium.orgec364712011-07-26 03:44:05 +0000101 if (srcSizeInBytes > size()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 return false;
103 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000105 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
vandebo@chromium.orgec364712011-07-26 03:44:05 +0000106 if (size() == srcSizeInBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, srcSizeInBytes, src, usage));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000108 } else {
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000109#if GR_GL_USE_BUFFER_DATA_NULL_HINT
vandebo@chromium.orgec364712011-07-26 03:44:05 +0000110 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size(), NULL, usage));
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000111#endif
twiz@google.com0f31ca72011-03-18 17:38:11 +0000112 GR_GL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, 0, srcSizeInBytes, src));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000113 }
114 return true;
115}
116
117bool GrGLIndexBuffer::updateSubData(const void* src,
118 size_t srcSizeInBytes,
119 size_t offset) {
120 GrAssert(fBufferID);
121 GrAssert(!isLocked());
vandebo@chromium.orgec364712011-07-26 03:44:05 +0000122 if (srcSizeInBytes + offset > size()) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000123 return false;
124 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000125 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000126 GR_GL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, offset, srcSizeInBytes, src));
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 return true;
128}
129