1e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
2e5c31af7Sopenharmony_ci * drawElements Quality Program Reference Renderer
3e5c31af7Sopenharmony_ci * -----------------------------------------------
4e5c31af7Sopenharmony_ci *
5e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
8e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
9e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
10e5c31af7Sopenharmony_ci *
11e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
15e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
17e5c31af7Sopenharmony_ci * limitations under the License.
18e5c31af7Sopenharmony_ci *
19e5c31af7Sopenharmony_ci *//*!
20e5c31af7Sopenharmony_ci * \file
21e5c31af7Sopenharmony_ci * \brief Vertex packet and Vertex packet allocator
22e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "rrVertexPacket.hpp"
25e5c31af7Sopenharmony_ci
26e5c31af7Sopenharmony_cinamespace rr
27e5c31af7Sopenharmony_ci{
28e5c31af7Sopenharmony_ci
29e5c31af7Sopenharmony_ciVertexPacket::VertexPacket (void)
30e5c31af7Sopenharmony_ci{
31e5c31af7Sopenharmony_ci}
32e5c31af7Sopenharmony_ci
33e5c31af7Sopenharmony_ciVertexPacket::~VertexPacket (void)
34e5c31af7Sopenharmony_ci{
35e5c31af7Sopenharmony_ci}
36e5c31af7Sopenharmony_ci
37e5c31af7Sopenharmony_ciVertexPacketAllocator::VertexPacketAllocator (const size_t numberOfVertexOutputs)
38e5c31af7Sopenharmony_ci	: m_numberOfVertexOutputs(numberOfVertexOutputs)
39e5c31af7Sopenharmony_ci{
40e5c31af7Sopenharmony_ci}
41e5c31af7Sopenharmony_ci
42e5c31af7Sopenharmony_ciVertexPacketAllocator::~VertexPacketAllocator (void)
43e5c31af7Sopenharmony_ci{
44e5c31af7Sopenharmony_ci	for (size_t i = 0; i < m_allocations.size(); ++i)
45e5c31af7Sopenharmony_ci		delete [] m_allocations[i];
46e5c31af7Sopenharmony_ci	m_allocations.clear();
47e5c31af7Sopenharmony_ci}
48e5c31af7Sopenharmony_ci
49e5c31af7Sopenharmony_cistd::vector<VertexPacket*> VertexPacketAllocator::allocArray (size_t count)
50e5c31af7Sopenharmony_ci{
51e5c31af7Sopenharmony_ci	if (!count)
52e5c31af7Sopenharmony_ci		return std::vector<VertexPacket*>();
53e5c31af7Sopenharmony_ci
54e5c31af7Sopenharmony_ci	const size_t extraVaryings	= (m_numberOfVertexOutputs == 0) ? (0) : (m_numberOfVertexOutputs-1);
55e5c31af7Sopenharmony_ci	const size_t packetSize		= sizeof(VertexPacket) + extraVaryings * sizeof(GenericVec4);
56e5c31af7Sopenharmony_ci
57e5c31af7Sopenharmony_ci	std::vector<VertexPacket*>	retVal;
58e5c31af7Sopenharmony_ci	deInt8*						ptr = new deInt8[packetSize * count]; // throws bad_alloc => ok
59e5c31af7Sopenharmony_ci
60e5c31af7Sopenharmony_ci	// *.push_back might throw bad_alloc
61e5c31af7Sopenharmony_ci	try
62e5c31af7Sopenharmony_ci	{
63e5c31af7Sopenharmony_ci		// run ctors
64e5c31af7Sopenharmony_ci		for (size_t i = 0; i < count; ++i)
65e5c31af7Sopenharmony_ci			retVal.push_back(new (ptr + i*packetSize) VertexPacket()); // throws bad_alloc
66e5c31af7Sopenharmony_ci
67e5c31af7Sopenharmony_ci		m_allocations.push_back(ptr); // throws bad_alloc
68e5c31af7Sopenharmony_ci	}
69e5c31af7Sopenharmony_ci	catch (std::bad_alloc& )
70e5c31af7Sopenharmony_ci	{
71e5c31af7Sopenharmony_ci		delete [] ptr;
72e5c31af7Sopenharmony_ci		throw;
73e5c31af7Sopenharmony_ci	}
74e5c31af7Sopenharmony_ci
75e5c31af7Sopenharmony_ci	return retVal;
76e5c31af7Sopenharmony_ci}
77e5c31af7Sopenharmony_ci
78e5c31af7Sopenharmony_ciVertexPacket* VertexPacketAllocator::alloc (void)
79e5c31af7Sopenharmony_ci{
80e5c31af7Sopenharmony_ci	const size_t poolSize = 8;
81e5c31af7Sopenharmony_ci
82e5c31af7Sopenharmony_ci	if (m_singleAllocPool.empty())
83e5c31af7Sopenharmony_ci		m_singleAllocPool = allocArray(poolSize);
84e5c31af7Sopenharmony_ci
85e5c31af7Sopenharmony_ci	VertexPacket* packet = *--m_singleAllocPool.end();
86e5c31af7Sopenharmony_ci	m_singleAllocPool.pop_back();
87e5c31af7Sopenharmony_ci	return packet;
88e5c31af7Sopenharmony_ci}
89e5c31af7Sopenharmony_ci
90e5c31af7Sopenharmony_ci} // rr
91