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