blob: 3b34d63eea5e77a387afd2a04bd521f27358d87e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrGLIndexBuffer.h"
12#include "GrGpuGL.h"
13
bsalomon@google.com8fe72472011-03-30 21:26:44 +000014#define GPUGL static_cast<GrGpuGL*>(getGpu())
15
16GrGLIndexBuffer::GrGLIndexBuffer(GrGpuGL* gpu,
17 GrGLuint id,
18 size_t sizeInBytes,
19 bool dynamic)
20 : INHERITED(gpu, sizeInBytes, dynamic)
21 , fBufferID(id)
22 , fLockPtr(NULL) {
23
reed@google.comac10a2d2010-12-22 21:39:39 +000024}
25
bsalomon@google.com8fe72472011-03-30 21:26:44 +000026void GrGLIndexBuffer::onRelease() {
reed@google.comac10a2d2010-12-22 21:39:39 +000027 // make sure we've not been abandoned
28 if (fBufferID) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000029 GPUGL->notifyIndexBufferDelete(this);
reed@google.comac10a2d2010-12-22 21:39:39 +000030 GR_GL(DeleteBuffers(1, &fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000031 fBufferID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000032 }
33}
34
bsalomon@google.com8fe72472011-03-30 21:26:44 +000035void GrGLIndexBuffer::onAbandon() {
36 fBufferID = 0;
37 fLockPtr = NULL;
38}
39
bsalomon@google.com1c13c962011-02-14 16:51:21 +000040void GrGLIndexBuffer::bind() const {
twiz@google.com0f31ca72011-03-18 17:38:11 +000041 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000042 GPUGL->notifyIndexBufferBind(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000043}
44
twiz@google.com0f31ca72011-03-18 17:38:11 +000045GrGLuint GrGLIndexBuffer::bufferID() const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000046 return fBufferID;
47}
48
reed@google.comac10a2d2010-12-22 21:39:39 +000049void* GrGLIndexBuffer::lock() {
50 GrAssert(fBufferID);
51 GrAssert(!isLocked());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000052 if (GPUGL->supportsBufferLocking()) {
53 this->bind();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000054 // Let driver know it can discard the old data
bsalomon@google.comcee661a2011-07-26 12:32:36 +000055 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, this->sizeInBytes(), NULL,
56 this->dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.comc312bf92011-03-21 21:10:33 +000057 fLockPtr = GR_GL(MapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, GR_GL_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000058
59 return fLockPtr;
60 }
61 return NULL;
62}
63
bsalomon@google.com1c13c962011-02-14 16:51:21 +000064void* GrGLIndexBuffer::lockPtr() const {
65 return fLockPtr;
66}
67
reed@google.comac10a2d2010-12-22 21:39:39 +000068void GrGLIndexBuffer::unlock() {
69 GrAssert(fBufferID);
70 GrAssert(isLocked());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000071 GrAssert(GPUGL->supportsBufferLocking());
reed@google.comac10a2d2010-12-22 21:39:39 +000072
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000074 GR_GL(UnmapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000075 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000076}
77
78bool GrGLIndexBuffer::isLocked() const {
reed@google.comac10a2d2010-12-22 21:39:39 +000079#if GR_DEBUG
bsalomon@google.com8fe72472011-03-30 21:26:44 +000080 if (this->isValid() && GPUGL->supportsBufferLocking()) {
81 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000082 GrGLint mapped;
83 GR_GL(GetBufferParameteriv(GR_GL_ELEMENT_ARRAY_BUFFER,
bsalomon@google.comc312bf92011-03-21 21:10:33 +000084 GR_GL_BUFFER_MAPPED, &mapped));
reed@google.comac10a2d2010-12-22 21:39:39 +000085 GrAssert(!!mapped == !!fLockPtr);
86 }
87#endif
88 return NULL != fLockPtr;
89}
90
bsalomon@google.com1c13c962011-02-14 16:51:21 +000091bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000092 GrAssert(fBufferID);
93 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +000094 if (srcSizeInBytes > this->sizeInBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000095 return false;
96 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000097 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000098 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
bsalomon@google.comcee661a2011-07-26 12:32:36 +000099 if (this->sizeInBytes() == srcSizeInBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000100 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, srcSizeInBytes, src, usage));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000101 } else {
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000102#if GR_GL_USE_BUFFER_DATA_NULL_HINT
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000103 GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, this->sizeInBytes(), NULL, usage));
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000104#endif
twiz@google.com0f31ca72011-03-18 17:38:11 +0000105 GR_GL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, 0, srcSizeInBytes, src));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000106 }
107 return true;
108}
109
110bool GrGLIndexBuffer::updateSubData(const void* src,
111 size_t srcSizeInBytes,
112 size_t offset) {
113 GrAssert(fBufferID);
114 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000115 if (srcSizeInBytes + offset > this->sizeInBytes()) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116 return false;
117 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000118 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000119 GR_GL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, offset, srcSizeInBytes, src));
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 return true;
121}
122