1#ifndef _TCUSURFACE_HPP
2#define _TCUSURFACE_HPP
3/*-------------------------------------------------------------------------
4 * drawElements Quality Program Tester Core
5 * ----------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief RGBA8888 surface class.
24 *//*--------------------------------------------------------------------*/
25
26#include "tcuDefs.hpp"
27#include "tcuRGBA.hpp"
28#include "tcuTexture.hpp"
29
30#include <vector>
31
32namespace tcu
33{
34
35/*--------------------------------------------------------------------*//*!
36 * \brief RGBA8888 surface
37 *
38 * Surface provides basic pixel storage functionality. Only single format
39 * (RGBA8888) is supported.
40 *
41 * PixelBufferAccess (see tcuTexture.h) provides much more flexible API
42 * for handling various pixel formats. This is mainly a convinience class.
43 *//*--------------------------------------------------------------------*/
44class Surface
45{
46public:
47							Surface				(void);
48							Surface				(int width, int height);
49							~Surface			(void);
50
51	void					setSize				(int width, int height);
52
53	int						getWidth			(void) const { return m_width;  }
54	int						getHeight			(void) const { return m_height; }
55
56	void					setPixel			(int x, int y, RGBA col);
57	RGBA					getPixel			(int x, int y) const;
58
59	ConstPixelBufferAccess	getAccess			(void) const;
60	PixelBufferAccess		getAccess			(void);
61
62	ConstPixelBufferAccess	getSubAccess		(int x, int y, int width, int height) const;
63	PixelBufferAccess		getSubAccess		(int x, int y, int width, int height);
64
65private:
66	// \note Copy constructor and assignment operators are public and auto-generated
67
68	int						m_width;
69	int 					m_height;
70	std::vector<deUint32>	m_pixels;
71};
72
73inline void Surface::setPixel (int x, int y, RGBA col)
74{
75	DE_ASSERT(de::inBounds(x, 0, m_width) && de::inBounds(y, 0, m_height));
76
77	const int		pixOffset	= y*m_width + x;
78	deUint32*		pixAddr		= &m_pixels[pixOffset];
79
80#if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
81	*pixAddr = col.getPacked();
82#else
83	*((deUint8*)pixAddr + 0) = (deUint8)col.getRed();
84	*((deUint8*)pixAddr + 1) = (deUint8)col.getGreen();
85	*((deUint8*)pixAddr + 2) = (deUint8)col.getBlue();
86	*((deUint8*)pixAddr + 3) = (deUint8)col.getAlpha();
87#endif
88}
89
90inline RGBA Surface::getPixel (int x, int y) const
91{
92	DE_ASSERT(de::inBounds(x, 0, m_width) && de::inBounds(y, 0, m_height));
93
94	const int		pixOffset	= y*m_width + x;
95	const deUint32*	pixAddr		= &m_pixels[pixOffset];
96
97	DE_STATIC_ASSERT(RGBA::RED_SHIFT == 0 && RGBA::GREEN_SHIFT == 8 && RGBA::BLUE_SHIFT == 16 && RGBA::ALPHA_SHIFT == 24);
98
99#if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
100	return RGBA(*pixAddr);
101#else
102	const deUint8*	byteAddr	= (const deUint8*)pixAddr;
103	return RGBA(byteAddr[0], byteAddr[1], byteAddr[2], byteAddr[3]);
104#endif
105}
106
107
108/** Get pixel sub buffer access from surface. */
109inline ConstPixelBufferAccess Surface::getSubAccess (int x, int y, int width, int height) const
110{
111	DE_ASSERT(x >= 0);
112	DE_ASSERT(width >= 1);
113	DE_ASSERT(x < m_width);
114
115	DE_ASSERT(y >= 0);
116	DE_ASSERT(height >= 1);
117	DE_ASSERT(y < m_height);
118
119	DE_ASSERT(x + width <= m_width);
120	DE_ASSERT(y + height <= m_height);
121
122	const deUint8* ptr = (m_pixels.empty() ? NULL : ((deUint8*)&m_pixels[0]) + 4 * (x + y * m_width));
123	return ConstPixelBufferAccess(TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), width, height, 1, m_width*4, 0, ptr);
124}
125
126/** Get pixel sub buffer access from surface. */
127inline PixelBufferAccess Surface::getSubAccess (int x, int y, int width, int height)
128{
129	DE_ASSERT(x >= 0);
130	DE_ASSERT(width >= 1);
131	DE_ASSERT(x < m_width);
132
133	DE_ASSERT(y >= 0);
134	DE_ASSERT(height >= 1);
135	DE_ASSERT(y < m_height);
136
137	DE_ASSERT(x + width <= m_width);
138	DE_ASSERT(y + height <= m_height);
139
140	deUint8* ptr = (m_pixels.empty() ? NULL : ((deUint8*)&m_pixels[0]) + 4 * (x + y * m_width));
141	return PixelBufferAccess(TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), width, height, 1, m_width*4, 0, ptr);
142}
143
144/** Get pixel buffer access from surface. */
145inline ConstPixelBufferAccess Surface::getAccess (void) const
146{
147	return ConstPixelBufferAccess(TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), m_width, m_height, 1, m_pixels.empty() ? DE_NULL : &m_pixels[0]);
148}
149
150/** Get pixel buffer access from surface. */
151inline PixelBufferAccess Surface::getAccess (void)
152{
153	return PixelBufferAccess(TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), m_width, m_height, 1, m_pixels.empty() ? DE_NULL : &m_pixels[0]);
154}
155
156} // tcu
157
158#endif // _TCUSURFACE_HPP
159