• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef EGL_CONFIG_H
17 #define EGL_CONFIG_H
18 
19 #include "EglOsApi.h"
20 #include <EGL/egl.h>
21 #include <EGL/eglext.h>
22 
23 #include <unordered_set>
24 
25 #define MIN_SWAP_INTERVAL 1
26 #define MAX_SWAP_INTERVAL 10
27 
28 #define DEBUG_CHOOSE_CONFIG 0
29 
30 #if DEBUG_CHOOSE_CONFIG
31 #include <stdio.h>
32 #define CHOOSE_CONFIG_DLOG(fmt,...) do { \
33     fprintf(stderr, "chooseconfig: " fmt "\n", ##__VA_ARGS__); \
34 } while(0) \
35 
36 #else
37 #define CHOOSE_CONFIG_DLOG(fmt,...)
38 #endif
39 
40 // A class used to model the content of an EGLConfig object.
41 // This is really a structure with several fields that can be queried
42 // individually with getConfigAttrib(), and compared for sorting according
43 // to the EGLConfig specification.
44 class EglConfig {
45 public:
46     // Return the value of a given attribute, identified by its name |attrib|
47     // (e.g. EGL_CONFIG_ID). On success, return true and sets |*val| to the
48     // attribute value. On failure (unknown |attrib| value), return false.
49     bool getConfAttrib(EGLint attrib, EGLint* val) const;
50     EGLint getConfAttrib(EGLint attrib) const;
51 
52     // Comparison operators used to sort EglConfig instances.
53     bool operator<(const EglConfig& conf) const;
54     bool operator>=(const EglConfig& conf) const;
55     bool operator==(const EglConfig& other) const;
56     // Return a 32-bit hash value, useful for keying on EglConfigs.
57     uint32_t u32hash() const;
58 
59     // Return true iff this instance is compatible with |conf|, i.e. that
60     // they have the same red/green/blue/depth/stencil sizes.
61     bool compatibleWith(const EglConfig& conf)  const; //compatibility
62 
63     // Return true iff this instance matches the minimal requirements of
64     // another EglConfig |dummy|. Any attribute value in |dummy| that isn't
65     // EGL_DONT_CARE will be tested against the corresponding value in the
66     // instance.
67     bool chosen(const EglConfig& dummy) const;
68 
69     // Return the EGL_SURFACE_TYPE value.
surfaceType()70     EGLint surfaceType() const { return m_surface_type;};
71 
72     // Return the EGL_CONFIG_ID value.
id()73     EGLint id() const { return m_config_id; }
setId(EGLint configId)74     void setId(EGLint configId) { m_config_id = configId; }
75 
76     // Return the native pixel format for this config.
nativeFormat()77     EglOS::PixelFormat* nativeFormat() const { return m_nativeFormat; }
78 
79     EglConfig(EGLint red_size,
80               EGLint green_size,
81               EGLint blue_size,
82               EGLint alpha_size,
83               EGLenum  caveat,
84               EGLint  conformant,
85               EGLint depth_size,
86               EGLint frame_buffer_level,
87               EGLint max_pbuffer_width,
88               EGLint max_pbuffer_height,
89               EGLint max_pbuffer_size,
90               EGLBoolean native_renderable,
91               EGLint renderable_type,
92               EGLint native_visual_id,
93               EGLint native_visual_type,
94               EGLint sample_buffers_num,
95               EGLint samples_per_pixel,
96               EGLint stencil_size,
97               EGLint luminance_size, // We don't really support custom luminance sizes
98               EGLint wanted_buffer_size, // nor buffer sizes (I guess normal CB size + padding),
99                                          // but we still need to properly reject configs that request illegal sizes.
100               EGLint surface_type,
101               EGLenum transparent_type,
102               EGLint trans_red_val,
103               EGLint trans_green_val,
104               EGLint trans_blue_val,
105               EGLBoolean recordable_android,
106               EGLBoolean framebuffer_target_android,
107               EglOS::PixelFormat* frmt);
108 
109     EglConfig(EGLint red_size,
110               EGLint green_size,
111               EGLint blue_size,
112               EGLint alpha_size,
113               EGLenum  caveat,
114               EGLint depth_size,
115               EGLint frame_buffer_level,
116               EGLint max_pbuffer_width,
117               EGLint max_pbuffer_height,
118               EGLint max_pbuffer_size,
119               EGLBoolean native_renderable,
120               EGLint renderable_type,
121               EGLint native_visual_id,
122               EGLint native_visual_type,
123               EGLint samples_per_pixel,
124               EGLint stencil_size,
125               EGLint surface_type,
126               EGLenum transparent_type,
127               EGLint trans_red_val,
128               EGLint trans_green_val,
129               EGLint trans_blue_val,
130               EGLBoolean recordable_android,
131               EglOS::PixelFormat* frmt);
132 
133     // Copy-constructor.
134     EglConfig(const EglConfig& conf);
135 
136     // A copy-constructor that allows one to override the configuration ID
137     // and red/green/blue/alpha sizes. Note that this is how one creates
138     // an EglConfig instance where nativeId() and id() return different
139     // values (see comment for nativeId()).
140     EglConfig(const EglConfig& conf,
141               EGLint red_size,
142               EGLint green_size,
143               EGLint blue_size,
144               EGLint alpha_size);
145 
146     // Destructor is required to get id of pixel format instance.
~EglConfig()147     ~EglConfig() {
148         delete m_nativeFormat;
149     }
150 
151     // We need to track which EGL config attributes were requested by the user
152     // versus others which are just part of the actual config itself.
153     // addWantedAttrib()/isWantedAttrib() are used in sorting configs
154     // depending on what the user requests.
addWantedAttrib(EGLint wanted)155     void addWantedAttrib(EGLint wanted) {
156         m_wantedAttribs.insert(wanted);
157     }
158 
isWantedAttrib(EGLint wanted)159     bool isWantedAttrib(EGLint wanted) const {
160         return m_wantedAttribs.count(wanted);
161     }
162 
163 private:
164     const EGLint      m_buffer_size;
165     const EGLint      m_red_size;
166     const EGLint      m_green_size;
167     const EGLint      m_blue_size;
168     const EGLint      m_alpha_size;
169     const EGLBoolean  m_bind_to_tex_rgb;
170     const EGLBoolean  m_bind_to_tex_rgba;
171     const EGLenum     m_caveat;
172     EGLint            m_config_id = EGL_DONT_CARE;
173     const EGLint      m_frame_buffer_level;
174     const EGLint      m_depth_size;
175     const EGLint      m_max_pbuffer_width;
176     const EGLint      m_max_pbuffer_height;
177     const EGLint      m_max_pbuffer_size;
178     const EGLint      m_max_swap_interval;
179     const EGLint      m_min_swap_interval;
180     const EGLBoolean  m_native_renderable;
181     const EGLint      m_renderable_type;
182     const EGLint      m_native_visual_id;
183     const EGLint      m_native_visual_type;
184     const EGLint      m_sample_buffers_num;
185     const EGLint      m_samples_per_pixel;
186     const EGLint      m_stencil_size;
187     const EGLint      m_luminance_size;
188     const EGLint      m_wanted_buffer_size;
189     const EGLint      m_surface_type;
190     const EGLenum     m_transparent_type;
191     const EGLint      m_trans_red_val;
192     const EGLint      m_trans_green_val;
193     const EGLint      m_trans_blue_val;
194     const EGLBoolean  m_recordable_android;
195     const EGLBoolean  m_framebuffer_target_android;
196     const EGLenum     m_conformant;
197 
198     EglOS::PixelFormat*  m_nativeFormat;
199     const EGLint      m_color_buffer_type;
200 
201     // Track which attribute keys are desired by a user.
202     // This dynamically affects config sorting order.
203     std::unordered_set<EGLint> m_wantedAttribs;
204 };
205 
206 namespace std {
207 template <>
208 struct hash<EglConfig> {
209     std::size_t operator()(const EglConfig& config) const {
210         return (size_t)config.u32hash();
211     }
212 };
213 } // namespace std
214 
215 #endif
216