2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "GLSharedGroup.h"
19 /**** KeyedVector utilities ****/
22 static void clearObjectMap(android::DefaultKeyedVector<GLuint, T>& v) {
23 for (size_t i = 0; i < v.size(); i++)
28 /**** BufferData ****/
30 BufferData::BufferData() : m_size(0) {};
31 BufferData::BufferData(GLsizeiptr size, void * data) : m_size(size)
34 if (size>0) buffer = m_fixedBuffer.alloc(size);
35 if (data) memcpy(buffer, data, size);
38 /**** ProgramData ****/
39 ProgramData::ProgramData() : m_numIndexes(0),
46 void ProgramData::initProgramData(GLuint numIndexes)
49 m_numIndexes = numIndexes;
51 m_Indexes = new IndexInfo[numIndexes];
52 m_locShiftWAR = false;
55 bool ProgramData::isInitialized()
60 ProgramData::~ProgramData()
66 void ProgramData::setIndexInfo(GLuint index, GLint base, GLint size, GLenum type)
68 if (index>=m_numIndexes)
70 m_Indexes[index].base = base;
71 m_Indexes[index].size = size;
72 m_Indexes[index].type = type;
74 m_Indexes[index].appBase = m_Indexes[index-1].appBase +
75 m_Indexes[index-1].size;
78 m_Indexes[index].appBase = 0;
80 m_Indexes[index].hostLocsPerElement = 1;
81 m_Indexes[index].flags = 0;
82 m_Indexes[index].samplerValue = 0;
85 void ProgramData::setIndexFlags(GLuint index, GLuint flags)
87 if (index >= m_numIndexes)
89 m_Indexes[index].flags |= flags;
92 GLuint ProgramData::getIndexForLocation(GLint location)
94 GLuint index = m_numIndexes;
96 for (GLuint i=0;i<m_numIndexes;++i)
98 GLint dist = location - m_Indexes[i].base;
100 (minDist < 0 || dist < minDist)) {
108 GLenum ProgramData::getTypeForLocation(GLint location)
110 GLuint index = getIndexForLocation(location);
111 if (index<m_numIndexes) {
112 return m_Indexes[index].type;
117 void ProgramData::setupLocationShiftWAR()
119 m_locShiftWAR = false;
120 for (GLuint i=0; i<m_numIndexes; i++) {
121 if (0 != (m_Indexes[i].base & 0xffff)) {
125 // if we have one uniform at location 0, we do not need the WAR.
126 if (m_numIndexes > 1) {
127 m_locShiftWAR = true;
131 GLint ProgramData::locationWARHostToApp(GLint hostLoc, GLint arrIndex)
133 if (!m_locShiftWAR) return hostLoc;
135 GLuint index = getIndexForLocation(hostLoc);
136 if (index<m_numIndexes) {
138 m_Indexes[index].hostLocsPerElement =
139 (hostLoc - m_Indexes[index].base) / arrIndex;
141 return m_Indexes[index].appBase + arrIndex;
146 GLint ProgramData::locationWARAppToHost(GLint appLoc)
148 if (!m_locShiftWAR) return appLoc;
150 for(GLuint i=0; i<m_numIndexes; i++) {
151 GLint elemIndex = appLoc - m_Indexes[i].appBase;
152 if (elemIndex >= 0 && elemIndex < m_Indexes[i].size) {
153 return m_Indexes[i].base +
154 elemIndex * m_Indexes[i].hostLocsPerElement;
160 GLint ProgramData::getNextSamplerUniform(GLint index, GLint* val, GLenum* target)
162 for (GLint i = index + 1; i >= 0 && i < (GLint)m_numIndexes; i++) {
163 if (m_Indexes[i].type == GL_SAMPLER_2D) {
164 if (val) *val = m_Indexes[i].samplerValue;
166 if (m_Indexes[i].flags & INDEX_FLAG_SAMPLER_EXTERNAL) {
167 *target = GL_TEXTURE_EXTERNAL_OES;
169 *target = GL_TEXTURE_2D;
178 bool ProgramData::setSamplerUniform(GLint appLoc, GLint val, GLenum* target)
180 for (GLuint i = 0; i < m_numIndexes; i++) {
181 GLint elemIndex = appLoc - m_Indexes[i].appBase;
182 if (elemIndex >= 0 && elemIndex < m_Indexes[i].size) {
183 if (m_Indexes[i].type == GL_TEXTURE_2D) {
184 m_Indexes[i].samplerValue = val;
186 if (m_Indexes[i].flags & INDEX_FLAG_SAMPLER_EXTERNAL) {
187 *target = GL_TEXTURE_EXTERNAL_OES;
189 *target = GL_TEXTURE_2D;
199 bool ProgramData::attachShader(GLuint shader)
201 size_t n = m_shaders.size();
202 for (size_t i = 0; i < n; i++) {
203 if (m_shaders[i] == shader) {
207 // AKA m_shaders.push_back(), but that has an ambiguous call to insertAt()
208 // due to the default parameters. This is the desired insertAt() overload.
209 m_shaders.insertAt(shader, m_shaders.size(), 1);
213 bool ProgramData::detachShader(GLuint shader)
215 size_t n = m_shaders.size();
216 for (size_t i = 0; i < n; i++) {
217 if (m_shaders[i] == shader) {
218 m_shaders.removeAt(i);
225 /***** GLSharedGroup ****/
227 GLSharedGroup::GLSharedGroup() :
228 m_buffers(android::DefaultKeyedVector<GLuint, BufferData*>(NULL)),
229 m_programs(android::DefaultKeyedVector<GLuint, ProgramData*>(NULL)),
230 m_shaders(android::DefaultKeyedVector<GLuint, ShaderData*>(NULL))
234 GLSharedGroup::~GLSharedGroup()
238 clearObjectMap(m_buffers);
239 clearObjectMap(m_programs);
240 clearObjectMap(m_shaders);
243 BufferData * GLSharedGroup::getBufferData(GLuint bufferId)
245 android::AutoMutex _lock(m_lock);
246 return m_buffers.valueFor(bufferId);
249 void GLSharedGroup::addBufferData(GLuint bufferId, GLsizeiptr size, void * data)
251 android::AutoMutex _lock(m_lock);
252 m_buffers.add(bufferId, new BufferData(size, data));
255 void GLSharedGroup::updateBufferData(GLuint bufferId, GLsizeiptr size, void * data)
257 android::AutoMutex _lock(m_lock);
258 ssize_t idx = m_buffers.indexOfKey(bufferId);
260 delete m_buffers.valueAt(idx);
261 m_buffers.editValueAt(idx) = new BufferData(size, data);
263 m_buffers.add(bufferId, new BufferData(size, data));
267 GLenum GLSharedGroup::subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, void * data)
269 android::AutoMutex _lock(m_lock);
270 BufferData * buf = m_buffers.valueFor(bufferId);
271 if ((!buf) || (buf->m_size < offset+size) || (offset < 0) || (size<0)) return GL_INVALID_VALUE;
273 //it's safe to update now
274 memcpy((char*)buf->m_fixedBuffer.ptr() + offset, data, size);
278 void GLSharedGroup::deleteBufferData(GLuint bufferId)
280 android::AutoMutex _lock(m_lock);
281 ssize_t idx = m_buffers.indexOfKey(bufferId);
283 delete m_buffers.valueAt(idx);
284 m_buffers.removeItemsAt(idx);
288 void GLSharedGroup::addProgramData(GLuint program)
290 android::AutoMutex _lock(m_lock);
291 ProgramData *pData = m_programs.valueFor(program);
294 m_programs.removeItem(program);
298 m_programs.add(program,new ProgramData());
301 void GLSharedGroup::initProgramData(GLuint program, GLuint numIndexes)
303 android::AutoMutex _lock(m_lock);
304 ProgramData *pData = m_programs.valueFor(program);
307 pData->initProgramData(numIndexes);
311 bool GLSharedGroup::isProgramInitialized(GLuint program)
313 android::AutoMutex _lock(m_lock);
314 ProgramData* pData = m_programs.valueFor(program);
317 return pData->isInitialized();
322 void GLSharedGroup::deleteProgramData(GLuint program)
324 android::AutoMutex _lock(m_lock);
325 ProgramData *pData = m_programs.valueFor(program);
328 m_programs.removeItem(program);
331 void GLSharedGroup::attachShader(GLuint program, GLuint shader)
333 android::AutoMutex _lock(m_lock);
334 ProgramData* programData = m_programs.valueFor(program);
335 ssize_t idx = m_shaders.indexOfKey(shader);
336 if (programData && idx >= 0) {
337 if (programData->attachShader(shader)) {
338 refShaderDataLocked(idx);
343 void GLSharedGroup::detachShader(GLuint program, GLuint shader)
345 android::AutoMutex _lock(m_lock);
346 ProgramData* programData = m_programs.valueFor(program);
347 ssize_t idx = m_shaders.indexOfKey(shader);
348 if (programData && idx >= 0) {
349 if (programData->detachShader(shader)) {
350 unrefShaderDataLocked(idx);
355 void GLSharedGroup::setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name)
357 android::AutoMutex _lock(m_lock);
358 ProgramData* pData = m_programs.valueFor(program);
361 pData->setIndexInfo(index,base,size,type);
363 if (type == GL_SAMPLER_2D) {
364 size_t n = pData->getNumShaders();
365 for (size_t i = 0; i < n; i++) {
366 GLuint shaderId = pData->getShader(i);
367 ShaderData* shader = m_shaders.valueFor(shaderId);
368 if (!shader) continue;
369 ShaderData::StringList::iterator nameIter = shader->samplerExternalNames.begin();
370 ShaderData::StringList::iterator nameEnd = shader->samplerExternalNames.end();
371 while (nameIter != nameEnd) {
372 if (*nameIter == name) {
373 pData->setIndexFlags(index, ProgramData::INDEX_FLAG_SAMPLER_EXTERNAL);
383 GLenum GLSharedGroup::getProgramUniformType(GLuint program, GLint location)
385 android::AutoMutex _lock(m_lock);
386 ProgramData* pData = m_programs.valueFor(program);
390 type = pData->getTypeForLocation(location);
395 bool GLSharedGroup::isProgram(GLuint program)
397 android::AutoMutex _lock(m_lock);
398 ProgramData* pData = m_programs.valueFor(program);
399 return (pData!=NULL);
402 void GLSharedGroup::setupLocationShiftWAR(GLuint program)
404 android::AutoMutex _lock(m_lock);
405 ProgramData* pData = m_programs.valueFor(program);
406 if (pData) pData->setupLocationShiftWAR();
409 GLint GLSharedGroup::locationWARHostToApp(GLuint program, GLint hostLoc, GLint arrIndex)
411 android::AutoMutex _lock(m_lock);
412 ProgramData* pData = m_programs.valueFor(program);
413 if (pData) return pData->locationWARHostToApp(hostLoc, arrIndex);
417 GLint GLSharedGroup::locationWARAppToHost(GLuint program, GLint appLoc)
419 android::AutoMutex _lock(m_lock);
420 ProgramData* pData = m_programs.valueFor(program);
421 if (pData) return pData->locationWARAppToHost(appLoc);
425 bool GLSharedGroup::needUniformLocationWAR(GLuint program)
427 android::AutoMutex _lock(m_lock);
428 ProgramData* pData = m_programs.valueFor(program);
429 if (pData) return pData->needUniformLocationWAR();
433 GLint GLSharedGroup::getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target) const
435 android::AutoMutex _lock(m_lock);
436 ProgramData* pData = m_programs.valueFor(program);
437 return pData ? pData->getNextSamplerUniform(index, val, target) : -1;
440 bool GLSharedGroup::setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target)
442 android::AutoMutex _lock(m_lock);
443 ProgramData* pData = m_programs.valueFor(program);
444 return pData ? pData->setSamplerUniform(appLoc, val, target) : false;
447 bool GLSharedGroup::addShaderData(GLuint shader)
449 android::AutoMutex _lock(m_lock);
450 ShaderData* data = new ShaderData;
452 if (m_shaders.add(shader, data) < 0) {
461 ShaderData* GLSharedGroup::getShaderData(GLuint shader)
463 android::AutoMutex _lock(m_lock);
464 return m_shaders.valueFor(shader);
467 void GLSharedGroup::unrefShaderData(GLuint shader)
469 android::AutoMutex _lock(m_lock);
470 ssize_t idx = m_shaders.indexOfKey(shader);
472 unrefShaderDataLocked(idx);
476 void GLSharedGroup::refShaderDataLocked(ssize_t shaderIdx)
478 assert(shaderIdx >= 0 && shaderIdx <= m_shaders.size());
479 ShaderData* data = m_shaders.valueAt(shaderIdx);
483 void GLSharedGroup::unrefShaderDataLocked(ssize_t shaderIdx)
485 assert(shaderIdx >= 0 && shaderIdx <= m_shaders.size());
486 ShaderData* data = m_shaders.valueAt(shaderIdx);
487 if (--data->refcount == 0) {
489 m_shaders.removeItemsAt(shaderIdx);