xref: /aosp_15_r20/external/deqp/framework/common/tcuPixelFormat.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _TCUPIXELFORMAT_HPP
2 #define _TCUPIXELFORMAT_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 Pixel format descriptor.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "tcuDefs.hpp"
27 #include "tcuRGBA.hpp"
28 
29 namespace tcu
30 {
31 
32 /*--------------------------------------------------------------------*//*!
33  * \brief Fixed-point render target pixel format
34  *//*--------------------------------------------------------------------*/
35 struct PixelFormat
36 {
37     int redBits;
38     int greenBits;
39     int blueBits;
40     int alphaBits;
41 
PixelFormattcu::PixelFormat42     PixelFormat(int red, int green, int blue, int alpha)
43         : redBits(red)
44         , greenBits(green)
45         , blueBits(blue)
46         , alphaBits(alpha)
47     {
48     }
49 
PixelFormattcu::PixelFormat50     PixelFormat(void) : redBits(0), greenBits(0), blueBits(0), alphaBits(0)
51     {
52     }
53 
channelThresholdtcu::PixelFormat54     static inline int channelThreshold(int bits)
55     {
56         if (bits <= 8)
57         {
58             // Threshold is 2^(8 - bits)
59             return 1 << (8 - bits);
60         }
61         else
62         {
63             // Threshold is bound by the 8-bit buffer value
64             return 1;
65         }
66     }
67 
68     /*--------------------------------------------------------------------*//*!
69      * \brief Get default threshold for per-pixel comparison for this format
70      *
71      * Per-channel threshold is 2^(8-bits). If alpha channel bits are zero,
72      * threshold for that channel is 0.
73      *//*--------------------------------------------------------------------*/
getColorThresholdtcu::PixelFormat74     inline RGBA getColorThreshold(void) const
75     {
76         return RGBA(channelThreshold(redBits), channelThreshold(greenBits), channelThreshold(blueBits),
77                     alphaBits ? channelThreshold(alphaBits) : 0);
78     }
79 
convertChanneltcu::PixelFormat80     static inline int convertChannel(int val, int bits)
81     {
82         if (bits == 0)
83         {
84             return 0;
85         }
86         else if (bits == 1)
87         {
88             return (val & 0x80) ? 0xff : 0;
89         }
90         else if (bits < 8)
91         {
92             // Emulate precision reduction by replicating the upper bits as the fractional component
93             int intComp   = val >> (8 - bits);
94             int fractComp = (intComp << (24 - bits)) | (intComp << (24 - 2 * bits)) | (intComp << (24 - 3 * bits));
95             return (intComp << (8 - bits)) | (fractComp >> (bits + 16));
96         }
97         else
98         {
99             // Bits greater than or equal to 8 will have full precision, so no reduction
100             return val;
101         }
102     }
103 
104     /*--------------------------------------------------------------------*//*!
105      * \brief Emulate reduced bit depth
106      *
107      * The color value bit depth is reduced and converted back. The lowest
108      * bits are filled by replicating the upper bits.
109      *//*--------------------------------------------------------------------*/
convertColortcu::PixelFormat110     inline RGBA convertColor(const RGBA &col) const
111     {
112         return RGBA(convertChannel(col.getRed(), redBits), convertChannel(col.getGreen(), greenBits),
113                     convertChannel(col.getBlue(), blueBits),
114                     alphaBits ? convertChannel(col.getAlpha(), alphaBits) : 0xff);
115     }
116 
operator ==tcu::PixelFormat117     inline bool operator==(const PixelFormat &other) const
118     {
119         return redBits == other.redBits && greenBits == other.greenBits && blueBits == other.blueBits &&
120                alphaBits == other.alphaBits;
121     }
122 
operator !=tcu::PixelFormat123     inline bool operator!=(const PixelFormat &other) const
124     {
125         return !(*this == other);
126     }
127 } DE_WARN_UNUSED_TYPE;
128 
129 } // namespace tcu
130 
131 #endif // _TCUPIXELFORMAT_HPP
132