tcuSurface.hpp revision 93df37596ea66700965094b3aa2830cf4f2ca5aa
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
62private:
63	// \note Copy constructor and assignment operators are public and auto-generated
64
65	int						m_width;
66	int 					m_height;
67	std::vector<deUint32>	m_pixels;
68} DE_WARN_UNUSED_TYPE;
69
70inline void Surface::setPixel (int x, int y, RGBA col)
71{
72	DE_ASSERT(de::inBounds(x, 0, m_width) && de::inBounds(y, 0, m_height));
73
74	const int		pixOffset	= y*m_width + x;
75	deUint32*		pixAddr		= &m_pixels[pixOffset];
76
77#if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
78	*pixAddr = col.getPacked();
79#else
80	*((deUint8*)pixAddr + 0) = (deUint8)col.getRed();
81	*((deUint8*)pixAddr + 1) = (deUint8)col.getGreen();
82	*((deUint8*)pixAddr + 2) = (deUint8)col.getBlue();
83	*((deUint8*)pixAddr + 3) = (deUint8)col.getAlpha();
84#endif
85}
86
87inline RGBA Surface::getPixel (int x, int y) const
88{
89	DE_ASSERT(de::inBounds(x, 0, m_width) && de::inBounds(y, 0, m_height));
90
91	const int		pixOffset	= y*m_width + x;
92	const deUint32*	pixAddr		= &m_pixels[pixOffset];
93
94	DE_STATIC_ASSERT(RGBA::RED_SHIFT == 0 && RGBA::GREEN_SHIFT == 8 && RGBA::BLUE_SHIFT == 16 && RGBA::ALPHA_SHIFT == 24);
95
96#if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
97	return RGBA(*pixAddr);
98#else
99	const deUint8*	byteAddr	= (const deUint8*)pixAddr;
100	return RGBA(byteAddr[0], byteAddr[1], byteAddr[2], byteAddr[3]);
101#endif
102}
103
104/** Get pixel buffer access from surface. */
105inline ConstPixelBufferAccess Surface::getAccess (void) const
106{
107	return ConstPixelBufferAccess(TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), m_width, m_height, 1, m_pixels.empty() ? DE_NULL : &m_pixels[0]);
108}
109
110/** Get pixel buffer access from surface. */
111inline PixelBufferAccess Surface::getAccess (void)
112{
113	return PixelBufferAccess(TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), m_width, m_height, 1, m_pixels.empty() ? DE_NULL : &m_pixels[0]);
114}
115
116} // tcu
117
118#endif // _TCUSURFACE_HPP
119