blob: 54ee78bc272752bd279c35b1c5c99da60c03f369 [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
bsalomon@google.com0b77d682011-08-19 13:28:54 +000016#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
17
bsalomon@google.com8fe72472011-03-30 21:26:44 +000018GrGLIndexBuffer::GrGLIndexBuffer(GrGpuGL* gpu,
19 GrGLuint id,
20 size_t sizeInBytes,
21 bool dynamic)
22 : INHERITED(gpu, sizeInBytes, dynamic)
23 , fBufferID(id)
24 , fLockPtr(NULL) {
25
reed@google.comac10a2d2010-12-22 21:39:39 +000026}
27
bsalomon@google.com8fe72472011-03-30 21:26:44 +000028void GrGLIndexBuffer::onRelease() {
reed@google.comac10a2d2010-12-22 21:39:39 +000029 // make sure we've not been abandoned
30 if (fBufferID) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000031 GPUGL->notifyIndexBufferDelete(this);
bsalomon@google.com0b77d682011-08-19 13:28:54 +000032 GL_CALL(DeleteBuffers(1, &fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000033 fBufferID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000034 }
35}
36
bsalomon@google.com8fe72472011-03-30 21:26:44 +000037void GrGLIndexBuffer::onAbandon() {
38 fBufferID = 0;
39 fLockPtr = NULL;
40}
41
bsalomon@google.com1c13c962011-02-14 16:51:21 +000042void GrGLIndexBuffer::bind() const {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000043 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044 GPUGL->notifyIndexBufferBind(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000045}
46
twiz@google.com0f31ca72011-03-18 17:38:11 +000047GrGLuint GrGLIndexBuffer::bufferID() const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000048 return fBufferID;
49}
50
reed@google.comac10a2d2010-12-22 21:39:39 +000051void* GrGLIndexBuffer::lock() {
52 GrAssert(fBufferID);
53 GrAssert(!isLocked());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000054 if (GPUGL->supportsBufferLocking()) {
55 this->bind();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000056 // Let driver know it can discard the old data
bsalomon@google.com0b77d682011-08-19 13:28:54 +000057 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
58 this->sizeInBytes(),
59 NULL,
60 this->dynamic() ? GR_GL_DYNAMIC_DRAW :
61 GR_GL_STATIC_DRAW));
62 fLockPtr = GL_CALL(MapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER,
63 GR_GL_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000064
65 return fLockPtr;
66 }
67 return NULL;
68}
69
bsalomon@google.com1c13c962011-02-14 16:51:21 +000070void* GrGLIndexBuffer::lockPtr() const {
71 return fLockPtr;
72}
73
reed@google.comac10a2d2010-12-22 21:39:39 +000074void GrGLIndexBuffer::unlock() {
75 GrAssert(fBufferID);
76 GrAssert(isLocked());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000077 GrAssert(GPUGL->supportsBufferLocking());
reed@google.comac10a2d2010-12-22 21:39:39 +000078
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079 this->bind();
bsalomon@google.com0b77d682011-08-19 13:28:54 +000080 GL_CALL(UnmapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000081 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000082}
83
84bool GrGLIndexBuffer::isLocked() const {
reed@google.comac10a2d2010-12-22 21:39:39 +000085#if GR_DEBUG
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086 if (this->isValid() && GPUGL->supportsBufferLocking()) {
87 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000088 GrGLint mapped;
bsalomon@google.com0b77d682011-08-19 13:28:54 +000089 GL_CALL(GetBufferParameteriv(GR_GL_ELEMENT_ARRAY_BUFFER,
90 GR_GL_BUFFER_MAPPED, &mapped));
reed@google.comac10a2d2010-12-22 21:39:39 +000091 GrAssert(!!mapped == !!fLockPtr);
92 }
93#endif
94 return NULL != fLockPtr;
95}
96
bsalomon@google.com1c13c962011-02-14 16:51:21 +000097bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000098 GrAssert(fBufferID);
99 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000100 if (srcSizeInBytes > this->sizeInBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 return false;
102 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000103 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000104 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000105 if (this->sizeInBytes() == srcSizeInBytes) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000106 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
107 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
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000110 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
111 this->sizeInBytes(), NULL, usage));
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000112#endif
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000113 GL_CALL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER,
114 0, srcSizeInBytes, src));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115 }
116 return true;
117}
118
119bool GrGLIndexBuffer::updateSubData(const void* src,
120 size_t srcSizeInBytes,
121 size_t offset) {
122 GrAssert(fBufferID);
123 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000124 if (srcSizeInBytes + offset > this->sizeInBytes()) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000125 return false;
126 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000127 this->bind();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000128 GL_CALL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER,
129 offset, srcSizeInBytes, src));
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 return true;
131}
132