xref: /aosp_15_r20/external/deqp/framework/common/tcuPixelFormat.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1*35238bceSAndroid Build Coastguard Worker #ifndef _TCUPIXELFORMAT_HPP
2*35238bceSAndroid Build Coastguard Worker #define _TCUPIXELFORMAT_HPP
3*35238bceSAndroid Build Coastguard Worker /*-------------------------------------------------------------------------
4*35238bceSAndroid Build Coastguard Worker  * drawElements Quality Program Tester Core
5*35238bceSAndroid Build Coastguard Worker  * ----------------------------------------
6*35238bceSAndroid Build Coastguard Worker  *
7*35238bceSAndroid Build Coastguard Worker  * Copyright 2014 The Android Open Source Project
8*35238bceSAndroid Build Coastguard Worker  *
9*35238bceSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
10*35238bceSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
11*35238bceSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
12*35238bceSAndroid Build Coastguard Worker  *
13*35238bceSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
14*35238bceSAndroid Build Coastguard Worker  *
15*35238bceSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
16*35238bceSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
17*35238bceSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18*35238bceSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
19*35238bceSAndroid Build Coastguard Worker  * limitations under the License.
20*35238bceSAndroid Build Coastguard Worker  *
21*35238bceSAndroid Build Coastguard Worker  *//*!
22*35238bceSAndroid Build Coastguard Worker  * \file
23*35238bceSAndroid Build Coastguard Worker  * \brief Pixel format descriptor.
24*35238bceSAndroid Build Coastguard Worker  *//*--------------------------------------------------------------------*/
25*35238bceSAndroid Build Coastguard Worker 
26*35238bceSAndroid Build Coastguard Worker #include "tcuDefs.hpp"
27*35238bceSAndroid Build Coastguard Worker #include "tcuRGBA.hpp"
28*35238bceSAndroid Build Coastguard Worker 
29*35238bceSAndroid Build Coastguard Worker namespace tcu
30*35238bceSAndroid Build Coastguard Worker {
31*35238bceSAndroid Build Coastguard Worker 
32*35238bceSAndroid Build Coastguard Worker /*--------------------------------------------------------------------*//*!
33*35238bceSAndroid Build Coastguard Worker  * \brief Fixed-point render target pixel format
34*35238bceSAndroid Build Coastguard Worker  *//*--------------------------------------------------------------------*/
35*35238bceSAndroid Build Coastguard Worker struct PixelFormat
36*35238bceSAndroid Build Coastguard Worker {
37*35238bceSAndroid Build Coastguard Worker     int redBits;
38*35238bceSAndroid Build Coastguard Worker     int greenBits;
39*35238bceSAndroid Build Coastguard Worker     int blueBits;
40*35238bceSAndroid Build Coastguard Worker     int alphaBits;
41*35238bceSAndroid Build Coastguard Worker 
PixelFormattcu::PixelFormat42*35238bceSAndroid Build Coastguard Worker     PixelFormat(int red, int green, int blue, int alpha)
43*35238bceSAndroid Build Coastguard Worker         : redBits(red)
44*35238bceSAndroid Build Coastguard Worker         , greenBits(green)
45*35238bceSAndroid Build Coastguard Worker         , blueBits(blue)
46*35238bceSAndroid Build Coastguard Worker         , alphaBits(alpha)
47*35238bceSAndroid Build Coastguard Worker     {
48*35238bceSAndroid Build Coastguard Worker     }
49*35238bceSAndroid Build Coastguard Worker 
PixelFormattcu::PixelFormat50*35238bceSAndroid Build Coastguard Worker     PixelFormat(void) : redBits(0), greenBits(0), blueBits(0), alphaBits(0)
51*35238bceSAndroid Build Coastguard Worker     {
52*35238bceSAndroid Build Coastguard Worker     }
53*35238bceSAndroid Build Coastguard Worker 
channelThresholdtcu::PixelFormat54*35238bceSAndroid Build Coastguard Worker     static inline int channelThreshold(int bits)
55*35238bceSAndroid Build Coastguard Worker     {
56*35238bceSAndroid Build Coastguard Worker         if (bits <= 8)
57*35238bceSAndroid Build Coastguard Worker         {
58*35238bceSAndroid Build Coastguard Worker             // Threshold is 2^(8 - bits)
59*35238bceSAndroid Build Coastguard Worker             return 1 << (8 - bits);
60*35238bceSAndroid Build Coastguard Worker         }
61*35238bceSAndroid Build Coastguard Worker         else
62*35238bceSAndroid Build Coastguard Worker         {
63*35238bceSAndroid Build Coastguard Worker             // Threshold is bound by the 8-bit buffer value
64*35238bceSAndroid Build Coastguard Worker             return 1;
65*35238bceSAndroid Build Coastguard Worker         }
66*35238bceSAndroid Build Coastguard Worker     }
67*35238bceSAndroid Build Coastguard Worker 
68*35238bceSAndroid Build Coastguard Worker     /*--------------------------------------------------------------------*//*!
69*35238bceSAndroid Build Coastguard Worker      * \brief Get default threshold for per-pixel comparison for this format
70*35238bceSAndroid Build Coastguard Worker      *
71*35238bceSAndroid Build Coastguard Worker      * Per-channel threshold is 2^(8-bits). If alpha channel bits are zero,
72*35238bceSAndroid Build Coastguard Worker      * threshold for that channel is 0.
73*35238bceSAndroid Build Coastguard Worker      *//*--------------------------------------------------------------------*/
getColorThresholdtcu::PixelFormat74*35238bceSAndroid Build Coastguard Worker     inline RGBA getColorThreshold(void) const
75*35238bceSAndroid Build Coastguard Worker     {
76*35238bceSAndroid Build Coastguard Worker         return RGBA(channelThreshold(redBits), channelThreshold(greenBits), channelThreshold(blueBits),
77*35238bceSAndroid Build Coastguard Worker                     alphaBits ? channelThreshold(alphaBits) : 0);
78*35238bceSAndroid Build Coastguard Worker     }
79*35238bceSAndroid Build Coastguard Worker 
convertChanneltcu::PixelFormat80*35238bceSAndroid Build Coastguard Worker     static inline int convertChannel(int val, int bits)
81*35238bceSAndroid Build Coastguard Worker     {
82*35238bceSAndroid Build Coastguard Worker         if (bits == 0)
83*35238bceSAndroid Build Coastguard Worker         {
84*35238bceSAndroid Build Coastguard Worker             return 0;
85*35238bceSAndroid Build Coastguard Worker         }
86*35238bceSAndroid Build Coastguard Worker         else if (bits == 1)
87*35238bceSAndroid Build Coastguard Worker         {
88*35238bceSAndroid Build Coastguard Worker             return (val & 0x80) ? 0xff : 0;
89*35238bceSAndroid Build Coastguard Worker         }
90*35238bceSAndroid Build Coastguard Worker         else if (bits < 8)
91*35238bceSAndroid Build Coastguard Worker         {
92*35238bceSAndroid Build Coastguard Worker             // Emulate precision reduction by replicating the upper bits as the fractional component
93*35238bceSAndroid Build Coastguard Worker             int intComp   = val >> (8 - bits);
94*35238bceSAndroid Build Coastguard Worker             int fractComp = (intComp << (24 - bits)) | (intComp << (24 - 2 * bits)) | (intComp << (24 - 3 * bits));
95*35238bceSAndroid Build Coastguard Worker             return (intComp << (8 - bits)) | (fractComp >> (bits + 16));
96*35238bceSAndroid Build Coastguard Worker         }
97*35238bceSAndroid Build Coastguard Worker         else
98*35238bceSAndroid Build Coastguard Worker         {
99*35238bceSAndroid Build Coastguard Worker             // Bits greater than or equal to 8 will have full precision, so no reduction
100*35238bceSAndroid Build Coastguard Worker             return val;
101*35238bceSAndroid Build Coastguard Worker         }
102*35238bceSAndroid Build Coastguard Worker     }
103*35238bceSAndroid Build Coastguard Worker 
104*35238bceSAndroid Build Coastguard Worker     /*--------------------------------------------------------------------*//*!
105*35238bceSAndroid Build Coastguard Worker      * \brief Emulate reduced bit depth
106*35238bceSAndroid Build Coastguard Worker      *
107*35238bceSAndroid Build Coastguard Worker      * The color value bit depth is reduced and converted back. The lowest
108*35238bceSAndroid Build Coastguard Worker      * bits are filled by replicating the upper bits.
109*35238bceSAndroid Build Coastguard Worker      *//*--------------------------------------------------------------------*/
convertColortcu::PixelFormat110*35238bceSAndroid Build Coastguard Worker     inline RGBA convertColor(const RGBA &col) const
111*35238bceSAndroid Build Coastguard Worker     {
112*35238bceSAndroid Build Coastguard Worker         return RGBA(convertChannel(col.getRed(), redBits), convertChannel(col.getGreen(), greenBits),
113*35238bceSAndroid Build Coastguard Worker                     convertChannel(col.getBlue(), blueBits),
114*35238bceSAndroid Build Coastguard Worker                     alphaBits ? convertChannel(col.getAlpha(), alphaBits) : 0xff);
115*35238bceSAndroid Build Coastguard Worker     }
116*35238bceSAndroid Build Coastguard Worker 
operator ==tcu::PixelFormat117*35238bceSAndroid Build Coastguard Worker     inline bool operator==(const PixelFormat &other) const
118*35238bceSAndroid Build Coastguard Worker     {
119*35238bceSAndroid Build Coastguard Worker         return redBits == other.redBits && greenBits == other.greenBits && blueBits == other.blueBits &&
120*35238bceSAndroid Build Coastguard Worker                alphaBits == other.alphaBits;
121*35238bceSAndroid Build Coastguard Worker     }
122*35238bceSAndroid Build Coastguard Worker 
operator !=tcu::PixelFormat123*35238bceSAndroid Build Coastguard Worker     inline bool operator!=(const PixelFormat &other) const
124*35238bceSAndroid Build Coastguard Worker     {
125*35238bceSAndroid Build Coastguard Worker         return !(*this == other);
126*35238bceSAndroid Build Coastguard Worker     }
127*35238bceSAndroid Build Coastguard Worker } DE_WARN_UNUSED_TYPE;
128*35238bceSAndroid Build Coastguard Worker 
129*35238bceSAndroid Build Coastguard Worker } // namespace tcu
130*35238bceSAndroid Build Coastguard Worker 
131*35238bceSAndroid Build Coastguard Worker #endif // _TCUPIXELFORMAT_HPP
132