xref: /aosp_15_r20/external/deqp/framework/common/tcuSurface.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
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 "deArrayBuffer.hpp"
31 
32 namespace 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  *//*--------------------------------------------------------------------*/
44 class Surface
45 {
46 public:
47     Surface(void);
48     Surface(int width, int height);
49     ~Surface(void);
50 
51     void setSize(int width, int height);
52 
getWidth(void) const53     int getWidth(void) const
54     {
55         return m_width;
56     }
getHeight(void) const57     int getHeight(void) const
58     {
59         return m_height;
60     }
61 
62     void setPixel(int x, int y, RGBA col);
63     RGBA getPixel(int x, int y) const;
64 
65     ConstPixelBufferAccess getAccess(void) const;
66     PixelBufferAccess getAccess(void);
67 
68 private:
69     // \note Copy constructor and assignment operators are public and auto-generated
70 
71     int m_width;
72     int m_height;
73     de::ArrayBuffer<uint32_t> m_pixels;
74 } DE_WARN_UNUSED_TYPE;
75 
setPixel(int x,int y,RGBA col)76 inline void Surface::setPixel(int x, int y, RGBA col)
77 {
78     DE_ASSERT(de::inBounds(x, 0, m_width) && de::inBounds(y, 0, m_height));
79 
80     const int pixOffset = y * m_width + x;
81     uint32_t *pixAddr   = m_pixels.getElementPtr(pixOffset);
82 
83 #if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
84     *pixAddr = col.getPacked();
85 #else
86     *((uint8_t *)pixAddr + 0) = (uint8_t)col.getRed();
87     *((uint8_t *)pixAddr + 1) = (uint8_t)col.getGreen();
88     *((uint8_t *)pixAddr + 2) = (uint8_t)col.getBlue();
89     *((uint8_t *)pixAddr + 3) = (uint8_t)col.getAlpha();
90 #endif
91 }
92 
getPixel(int x,int y) const93 inline RGBA Surface::getPixel(int x, int y) const
94 {
95     DE_ASSERT(de::inBounds(x, 0, m_width) && de::inBounds(y, 0, m_height));
96 
97     const int pixOffset     = y * m_width + x;
98     const uint32_t *pixAddr = m_pixels.getElementPtr(pixOffset);
99 
100     DE_STATIC_ASSERT(RGBA::RED_SHIFT == 0 && RGBA::GREEN_SHIFT == 8 && RGBA::BLUE_SHIFT == 16 &&
101                      RGBA::ALPHA_SHIFT == 24);
102 
103 #if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
104     return RGBA(*pixAddr);
105 #else
106     const uint8_t *byteAddr   = (const uint8_t *)pixAddr;
107     return RGBA(byteAddr[0], byteAddr[1], byteAddr[2], byteAddr[3]);
108 #endif
109 }
110 
111 /** Get pixel buffer access from surface. */
getAccess(void) const112 inline ConstPixelBufferAccess Surface::getAccess(void) const
113 {
114     return ConstPixelBufferAccess(TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), m_width, m_height, 1,
115                                   m_pixels.empty() ? DE_NULL : m_pixels.getPtr());
116 }
117 
118 /** Get pixel buffer access from surface. */
getAccess(void)119 inline PixelBufferAccess Surface::getAccess(void)
120 {
121     return PixelBufferAccess(TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), m_width, m_height, 1,
122                              m_pixels.empty() ? DE_NULL : m_pixels.getPtr());
123 }
124 
125 } // namespace tcu
126 
127 #endif // _TCUSURFACE_HPP
128