1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/gl/GrGLVertexArray.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#include "GrGLVertexArray.h" 1.12 +#include "GrGpuGL.h" 1.13 + 1.14 +#define GPUGL static_cast<GrGpuGL*>(this->getGpu()) 1.15 +#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X); 1.16 + 1.17 +void GrGLAttribArrayState::set(const GrGpuGL* gpu, 1.18 + int index, 1.19 + GrGLVertexBuffer* buffer, 1.20 + GrGLint size, 1.21 + GrGLenum type, 1.22 + GrGLboolean normalized, 1.23 + GrGLsizei stride, 1.24 + GrGLvoid* offset) { 1.25 + SkASSERT(index >= 0 && index < fAttribArrayStates.count()); 1.26 + AttribArrayState* array = &fAttribArrayStates[index]; 1.27 + if (!array->fEnableIsValid || !array->fEnabled) { 1.28 + GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(index)); 1.29 + array->fEnableIsValid = true; 1.30 + array->fEnabled = true; 1.31 + } 1.32 + if (!array->fAttribPointerIsValid || 1.33 + array->fVertexBufferID != buffer->bufferID() || 1.34 + array->fSize != size || 1.35 + array->fNormalized != normalized || 1.36 + array->fStride != stride || 1.37 + array->fOffset != offset) { 1.38 + 1.39 + buffer->bind(); 1.40 + GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index, 1.41 + size, 1.42 + type, 1.43 + normalized, 1.44 + stride, 1.45 + offset)); 1.46 + array->fAttribPointerIsValid = true; 1.47 + array->fVertexBufferID = buffer->bufferID(); 1.48 + array->fSize = size; 1.49 + array->fNormalized = normalized; 1.50 + array->fStride = stride; 1.51 + array->fOffset = offset; 1.52 + } 1.53 +} 1.54 + 1.55 +void GrGLAttribArrayState::disableUnusedArrays(const GrGpuGL* gpu, uint64_t usedMask) { 1.56 + int count = fAttribArrayStates.count(); 1.57 + for (int i = 0; i < count; ++i) { 1.58 + if (!(usedMask & 0x1)) { 1.59 + if (!fAttribArrayStates[i].fEnableIsValid || fAttribArrayStates[i].fEnabled) { 1.60 + GR_GL_CALL(gpu->glInterface(), DisableVertexAttribArray(i)); 1.61 + fAttribArrayStates[i].fEnableIsValid = true; 1.62 + fAttribArrayStates[i].fEnabled = false; 1.63 + } 1.64 + } else { 1.65 + SkASSERT(fAttribArrayStates[i].fEnableIsValid && fAttribArrayStates[i].fEnabled); 1.66 + } 1.67 + // if the count is greater than 64 then this will become 0 and we will disable arrays 64+. 1.68 + usedMask >>= 1; 1.69 + } 1.70 +} 1.71 + 1.72 +/////////////////////////////////////////////////////////////////////////////////////////////////// 1.73 + 1.74 +GrGLVertexArray::GrGLVertexArray(GrGpuGL* gpu, GrGLint id, int attribCount) 1.75 + : GrResource(gpu, false) 1.76 + , fID(id) 1.77 + , fAttribArrays(attribCount) 1.78 + , fIndexBufferIDIsValid(false) { 1.79 +} 1.80 + 1.81 +void GrGLVertexArray::onAbandon() { 1.82 + fID = 0; 1.83 + INHERITED::onAbandon(); 1.84 +} 1.85 + 1.86 +void GrGLVertexArray::onRelease() { 1.87 + if (0 != fID) { 1.88 + GL_CALL(DeleteVertexArrays(1, &fID)); 1.89 + GPUGL->notifyVertexArrayDelete(fID); 1.90 + fID = 0; 1.91 + } 1.92 + INHERITED::onRelease(); 1.93 +} 1.94 + 1.95 +GrGLAttribArrayState* GrGLVertexArray::bind() { 1.96 + if (0 == fID) { 1.97 + return NULL; 1.98 + } 1.99 + GPUGL->bindVertexArray(fID); 1.100 + return &fAttribArrays; 1.101 +} 1.102 + 1.103 +GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(const GrGLIndexBuffer* buffer) { 1.104 + GrGLAttribArrayState* state = this->bind(); 1.105 + if (NULL != state && NULL != buffer) { 1.106 + GrGLuint bufferID = buffer->bufferID(); 1.107 + if (!fIndexBufferIDIsValid || bufferID != fIndexBufferID) { 1.108 + GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, bufferID)); 1.109 + fIndexBufferIDIsValid = true; 1.110 + fIndexBufferID = bufferID; 1.111 + } 1.112 + } 1.113 + return state; 1.114 +} 1.115 + 1.116 +void GrGLVertexArray::notifyIndexBufferDelete(GrGLuint bufferID) { 1.117 + if (fIndexBufferIDIsValid && bufferID == fIndexBufferID) { 1.118 + fIndexBufferID = 0; 1.119 + } 1.120 + } 1.121 + 1.122 +void GrGLVertexArray::invalidateCachedState() { 1.123 + fAttribArrays.invalidate(); 1.124 + fIndexBufferIDIsValid = false; 1.125 +}