gfx/angle/src/libGLESv2/renderer/IndexDataManager.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 #include "precompiled.h"
michael@0 2 //
michael@0 3 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
michael@0 4 // Use of this source code is governed by a BSD-style license that can be
michael@0 5 // found in the LICENSE file.
michael@0 6 //
michael@0 7
michael@0 8 // IndexDataManager.cpp: Defines the IndexDataManager, a class that
michael@0 9 // runs the Buffer translation process for index buffers.
michael@0 10
michael@0 11 #include "libGLESv2/renderer/IndexDataManager.h"
michael@0 12 #include "libGLESv2/renderer/BufferStorage.h"
michael@0 13
michael@0 14 #include "libGLESv2/Buffer.h"
michael@0 15 #include "libGLESv2/main.h"
michael@0 16 #include "libGLESv2/utilities.h"
michael@0 17 #include "libGLESv2/renderer/IndexBuffer.h"
michael@0 18
michael@0 19 namespace rx
michael@0 20 {
michael@0 21
michael@0 22 IndexDataManager::IndexDataManager(Renderer *renderer) : mRenderer(renderer)
michael@0 23 {
michael@0 24 mStreamingBufferShort = new StreamingIndexBufferInterface(mRenderer);
michael@0 25 if (!mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
michael@0 26 {
michael@0 27 delete mStreamingBufferShort;
michael@0 28 mStreamingBufferShort = NULL;
michael@0 29 }
michael@0 30
michael@0 31 mStreamingBufferInt = new StreamingIndexBufferInterface(mRenderer);
michael@0 32 if (!mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
michael@0 33 {
michael@0 34 delete mStreamingBufferInt;
michael@0 35 mStreamingBufferInt = NULL;
michael@0 36 }
michael@0 37
michael@0 38 if (!mStreamingBufferShort)
michael@0 39 {
michael@0 40 // Make sure both buffers are deleted.
michael@0 41 delete mStreamingBufferInt;
michael@0 42 mStreamingBufferInt = NULL;
michael@0 43
michael@0 44 ERR("Failed to allocate the streaming index buffer(s).");
michael@0 45 }
michael@0 46
michael@0 47 mCountingBuffer = NULL;
michael@0 48 }
michael@0 49
michael@0 50 IndexDataManager::~IndexDataManager()
michael@0 51 {
michael@0 52 delete mStreamingBufferShort;
michael@0 53 delete mStreamingBufferInt;
michael@0 54 delete mCountingBuffer;
michael@0 55 }
michael@0 56
michael@0 57 static void convertIndices(GLenum type, const void *input, GLsizei count, void *output)
michael@0 58 {
michael@0 59 if (type == GL_UNSIGNED_BYTE)
michael@0 60 {
michael@0 61 const GLubyte *in = static_cast<const GLubyte*>(input);
michael@0 62 GLushort *out = static_cast<GLushort*>(output);
michael@0 63
michael@0 64 for (GLsizei i = 0; i < count; i++)
michael@0 65 {
michael@0 66 out[i] = in[i];
michael@0 67 }
michael@0 68 }
michael@0 69 else if (type == GL_UNSIGNED_INT)
michael@0 70 {
michael@0 71 memcpy(output, input, count * sizeof(GLuint));
michael@0 72 }
michael@0 73 else if (type == GL_UNSIGNED_SHORT)
michael@0 74 {
michael@0 75 memcpy(output, input, count * sizeof(GLushort));
michael@0 76 }
michael@0 77 else UNREACHABLE();
michael@0 78 }
michael@0 79
michael@0 80 template <class IndexType>
michael@0 81 static void computeRange(const IndexType *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex)
michael@0 82 {
michael@0 83 *minIndex = indices[0];
michael@0 84 *maxIndex = indices[0];
michael@0 85
michael@0 86 for (GLsizei i = 0; i < count; i++)
michael@0 87 {
michael@0 88 if (*minIndex > indices[i]) *minIndex = indices[i];
michael@0 89 if (*maxIndex < indices[i]) *maxIndex = indices[i];
michael@0 90 }
michael@0 91 }
michael@0 92
michael@0 93 static void computeRange(GLenum type, const GLvoid *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex)
michael@0 94 {
michael@0 95 if (type == GL_UNSIGNED_BYTE)
michael@0 96 {
michael@0 97 computeRange(static_cast<const GLubyte*>(indices), count, minIndex, maxIndex);
michael@0 98 }
michael@0 99 else if (type == GL_UNSIGNED_INT)
michael@0 100 {
michael@0 101 computeRange(static_cast<const GLuint*>(indices), count, minIndex, maxIndex);
michael@0 102 }
michael@0 103 else if (type == GL_UNSIGNED_SHORT)
michael@0 104 {
michael@0 105 computeRange(static_cast<const GLushort*>(indices), count, minIndex, maxIndex);
michael@0 106 }
michael@0 107 else UNREACHABLE();
michael@0 108 }
michael@0 109
michael@0 110 GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated)
michael@0 111 {
michael@0 112 if (!mStreamingBufferShort)
michael@0 113 {
michael@0 114 return GL_OUT_OF_MEMORY;
michael@0 115 }
michael@0 116
michael@0 117 GLenum destinationIndexType = (type == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
michael@0 118 intptr_t offset = reinterpret_cast<intptr_t>(indices);
michael@0 119 bool alignedOffset = false;
michael@0 120
michael@0 121 BufferStorage *storage = NULL;
michael@0 122
michael@0 123 if (buffer != NULL)
michael@0 124 {
michael@0 125 storage = buffer->getStorage();
michael@0 126
michael@0 127 switch (type)
michael@0 128 {
michael@0 129 case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break;
michael@0 130 case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break;
michael@0 131 case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break;
michael@0 132 default: UNREACHABLE(); alignedOffset = false;
michael@0 133 }
michael@0 134
michael@0 135 unsigned int typeSize = gl::ComputeTypeSize(type);
michael@0 136
michael@0 137 // check for integer overflows and underflows
michael@0 138 if (static_cast<unsigned int>(offset) > (std::numeric_limits<unsigned int>::max() / typeSize) ||
michael@0 139 static_cast<unsigned int>(count) > ((std::numeric_limits<unsigned int>::max() / typeSize) - offset))
michael@0 140 {
michael@0 141 return GL_OUT_OF_MEMORY;
michael@0 142 }
michael@0 143
michael@0 144 if (typeSize * static_cast<unsigned int>(count) + offset > storage->getSize())
michael@0 145 {
michael@0 146 return GL_INVALID_OPERATION;
michael@0 147 }
michael@0 148
michael@0 149 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
michael@0 150 }
michael@0 151
michael@0 152 StreamingIndexBufferInterface *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort;
michael@0 153
michael@0 154 StaticIndexBufferInterface *staticBuffer = buffer ? buffer->getStaticIndexBuffer() : NULL;
michael@0 155 IndexBufferInterface *indexBuffer = streamingBuffer;
michael@0 156 bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() &&
michael@0 157 destinationIndexType == type;
michael@0 158 unsigned int streamOffset = 0;
michael@0 159
michael@0 160 if (directStorage)
michael@0 161 {
michael@0 162 indexBuffer = streamingBuffer;
michael@0 163 streamOffset = offset;
michael@0 164 storage->markBufferUsage();
michael@0 165
michael@0 166 if (!buffer->getIndexRangeCache()->findRange(type, offset, count, &translated->minIndex,
michael@0 167 &translated->maxIndex, NULL))
michael@0 168 {
michael@0 169 computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);
michael@0 170 buffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex,
michael@0 171 translated->maxIndex, offset);
michael@0 172 }
michael@0 173 }
michael@0 174 else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset)
michael@0 175 {
michael@0 176 indexBuffer = staticBuffer;
michael@0 177 if (!staticBuffer->getIndexRangeCache()->findRange(type, offset, count, &translated->minIndex,
michael@0 178 &translated->maxIndex, &streamOffset))
michael@0 179 {
michael@0 180 streamOffset = (offset / gl::ComputeTypeSize(type)) * gl::ComputeTypeSize(destinationIndexType);
michael@0 181 computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);
michael@0 182 staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex,
michael@0 183 translated->maxIndex, streamOffset);
michael@0 184 }
michael@0 185 }
michael@0 186 else
michael@0 187 {
michael@0 188 unsigned int convertCount = count;
michael@0 189
michael@0 190 if (staticBuffer)
michael@0 191 {
michael@0 192 if (staticBuffer->getBufferSize() == 0 && alignedOffset)
michael@0 193 {
michael@0 194 indexBuffer = staticBuffer;
michael@0 195 convertCount = storage->getSize() / gl::ComputeTypeSize(type);
michael@0 196 }
michael@0 197 else
michael@0 198 {
michael@0 199 buffer->invalidateStaticData();
michael@0 200 staticBuffer = NULL;
michael@0 201 }
michael@0 202 }
michael@0 203
michael@0 204 if (!indexBuffer)
michael@0 205 {
michael@0 206 ERR("No valid index buffer.");
michael@0 207 return GL_INVALID_OPERATION;
michael@0 208 }
michael@0 209
michael@0 210 unsigned int indexTypeSize = gl::ComputeTypeSize(destinationIndexType);
michael@0 211 if (convertCount > std::numeric_limits<unsigned int>::max() / indexTypeSize)
michael@0 212 {
michael@0 213 ERR("Reserving %u indicies of %u bytes each exceeds the maximum buffer size.", convertCount, indexTypeSize);
michael@0 214 return GL_OUT_OF_MEMORY;
michael@0 215 }
michael@0 216
michael@0 217 unsigned int bufferSizeRequired = convertCount * indexTypeSize;
michael@0 218 if (!indexBuffer->reserveBufferSpace(bufferSizeRequired, type))
michael@0 219 {
michael@0 220 ERR("Failed to reserve %u bytes in an index buffer.", bufferSizeRequired);
michael@0 221 return GL_OUT_OF_MEMORY;
michael@0 222 }
michael@0 223
michael@0 224 void* output = NULL;
michael@0 225 if (!indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset))
michael@0 226 {
michael@0 227 ERR("Failed to map index buffer.");
michael@0 228 return GL_OUT_OF_MEMORY;
michael@0 229 }
michael@0 230
michael@0 231 convertIndices(type, staticBuffer ? storage->getData() : indices, convertCount, output);
michael@0 232
michael@0 233 if (!indexBuffer->unmapBuffer())
michael@0 234 {
michael@0 235 ERR("Failed to unmap index buffer.");
michael@0 236 return GL_OUT_OF_MEMORY;
michael@0 237 }
michael@0 238
michael@0 239 computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);
michael@0 240
michael@0 241 if (staticBuffer)
michael@0 242 {
michael@0 243 streamOffset = (offset / gl::ComputeTypeSize(type)) * gl::ComputeTypeSize(destinationIndexType);
michael@0 244 staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->minIndex,
michael@0 245 translated->maxIndex, streamOffset);
michael@0 246 }
michael@0 247 }
michael@0 248
michael@0 249 translated->storage = directStorage ? storage : NULL;
michael@0 250 translated->indexBuffer = indexBuffer->getIndexBuffer();
michael@0 251 translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial();
michael@0 252 translated->startIndex = streamOffset / gl::ComputeTypeSize(destinationIndexType);
michael@0 253 translated->startOffset = streamOffset;
michael@0 254
michael@0 255 if (buffer)
michael@0 256 {
michael@0 257 buffer->promoteStaticUsage(count * gl::ComputeTypeSize(type));
michael@0 258 }
michael@0 259
michael@0 260 return GL_NO_ERROR;
michael@0 261 }
michael@0 262
michael@0 263 StaticIndexBufferInterface *IndexDataManager::getCountingIndices(GLsizei count)
michael@0 264 {
michael@0 265 if (count <= 65536) // 16-bit indices
michael@0 266 {
michael@0 267 const unsigned int spaceNeeded = count * sizeof(unsigned short);
michael@0 268
michael@0 269 if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded)
michael@0 270 {
michael@0 271 delete mCountingBuffer;
michael@0 272 mCountingBuffer = new StaticIndexBufferInterface(mRenderer);
michael@0 273 mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT);
michael@0 274
michael@0 275 void* mappedMemory = NULL;
michael@0 276 if (!mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory, NULL))
michael@0 277 {
michael@0 278 ERR("Failed to map counting buffer.");
michael@0 279 return NULL;
michael@0 280 }
michael@0 281
michael@0 282 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
michael@0 283 for(int i = 0; i < count; i++)
michael@0 284 {
michael@0 285 data[i] = i;
michael@0 286 }
michael@0 287
michael@0 288 if (!mCountingBuffer->unmapBuffer())
michael@0 289 {
michael@0 290 ERR("Failed to unmap counting buffer.");
michael@0 291 return NULL;
michael@0 292 }
michael@0 293 }
michael@0 294 }
michael@0 295 else if (mStreamingBufferInt) // 32-bit indices supported
michael@0 296 {
michael@0 297 const unsigned int spaceNeeded = count * sizeof(unsigned int);
michael@0 298
michael@0 299 if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded)
michael@0 300 {
michael@0 301 delete mCountingBuffer;
michael@0 302 mCountingBuffer = new StaticIndexBufferInterface(mRenderer);
michael@0 303 mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT);
michael@0 304
michael@0 305 void* mappedMemory = NULL;
michael@0 306 if (!mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory, NULL))
michael@0 307 {
michael@0 308 ERR("Failed to map counting buffer.");
michael@0 309 return NULL;
michael@0 310 }
michael@0 311
michael@0 312 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
michael@0 313 for(int i = 0; i < count; i++)
michael@0 314 {
michael@0 315 data[i] = i;
michael@0 316 }
michael@0 317
michael@0 318 if (!mCountingBuffer->unmapBuffer())
michael@0 319 {
michael@0 320 ERR("Failed to unmap counting buffer.");
michael@0 321 return NULL;
michael@0 322 }
michael@0 323 }
michael@0 324 }
michael@0 325 else
michael@0 326 {
michael@0 327 return NULL;
michael@0 328 }
michael@0 329
michael@0 330 return mCountingBuffer;
michael@0 331 }
michael@0 332
michael@0 333 }

mercurial