huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*-------------------------------------------------------------------------
 * drawElements Quality Program Tester Core
 * ----------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Android EGL platform.
 *//*--------------------------------------------------------------------*/
 
#include "tcuAndroidPlatform.hpp"
#include "tcuAndroidUtil.hpp"
#include "gluRenderContext.hpp"
#include "egluNativeDisplay.hpp"
#include "egluNativeWindow.hpp"
#include "egluGLContextFactory.hpp"
#include "egluUtil.hpp"
#include "eglwLibrary.hpp"
#include "eglwEnums.hpp"
#include "tcuFunctionLibrary.hpp"
#include "vkWsiPlatform.hpp"
 
// Assume no call translation is needed
#include <android/native_window.h>
struct egl_native_pixmap_t;
DE_STATIC_ASSERT(sizeof(eglw::EGLNativeDisplayType) == sizeof(void*));
DE_STATIC_ASSERT(sizeof(eglw::EGLNativePixmapType) == sizeof(struct egl_native_pixmap_t*));
DE_STATIC_ASSERT(sizeof(eglw::EGLNativeWindowType) == sizeof(ANativeWindow*));
 
namespace tcu
{
namespace Android
{
 
using namespace eglw;
 
static const eglu::NativeDisplay::Capability    DISPLAY_CAPABILITIES    = eglu::NativeDisplay::CAPABILITY_GET_DISPLAY_LEGACY;
static const eglu::NativeWindow::Capability        WINDOW_CAPABILITIES        = (eglu::NativeWindow::Capability)(eglu::NativeWindow::CAPABILITY_CREATE_SURFACE_LEGACY |
                                                                                                          eglu::NativeWindow::CAPABILITY_CREATE_SURFACE_PLATFORM |
                                                                                                          eglu::NativeWindow::CAPABILITY_CREATE_SURFACE_PLATFORM_EXTENSION |
                                                                                                          eglu::NativeWindow::CAPABILITY_SET_SURFACE_SIZE |
                                                                                                          eglu::NativeWindow::CAPABILITY_GET_SCREEN_SIZE);
 
class NativeDisplay : public eglu::NativeDisplay
{
public:
                                   NativeDisplay            (void) : eglu::NativeDisplay(DISPLAY_CAPABILITIES), m_library("libEGL.so") {}
   virtual                            ~NativeDisplay            (void) {}
 
   virtual EGLNativeDisplayType    getLegacyNative            (void)            { return EGL_DEFAULT_DISPLAY;    }
   virtual const eglw::Library&    getLibrary                (void) const    { return m_library;                }
 
private:
   eglw::DefaultLibrary            m_library;
};
 
class NativeDisplayFactory : public eglu::NativeDisplayFactory
{
public:
                                   NativeDisplayFactory    (WindowRegistry& windowRegistry);
                                   ~NativeDisplayFactory    (void) {}
 
   virtual eglu::NativeDisplay*    createDisplay            (const EGLAttrib* attribList) const;
};
 
class NativeWindow : public eglu::NativeWindow
{
public:
                                   NativeWindow            (Window* window, int width, int height, int32_t format);
   virtual                            ~NativeWindow            (void);
 
   virtual EGLNativeWindowType        getLegacyNative            (void)            { return m_window->getNativeWindow();    }
   virtual EGLNativeWindowType        getPlatformExtension    (void)            { return m_window->getNativeWindow();    }
   virtual EGLNativeWindowType        getPlatformNative        (void)            { return m_window->getNativeWindow();    }
   IVec2                            getScreenSize            (void) const    { return m_window->getSize();            }
 
   void                            setSurfaceSize            (IVec2 size);
 
   virtual void                    processEvents            (void);
 
private:
   Window*                            m_window;
   int32_t                            m_format;
};
 
class NativeWindowFactory : public eglu::NativeWindowFactory
{
public:
                                   NativeWindowFactory        (WindowRegistry& windowRegistry);
                                   ~NativeWindowFactory    (void);
 
   virtual eglu::NativeWindow*        createWindow            (eglu::NativeDisplay* nativeDisplay, const eglu::WindowParams& params) const;
   virtual eglu::NativeWindow*        createWindow            (eglu::NativeDisplay* nativeDisplay, EGLDisplay display, EGLConfig config, const EGLAttrib* attribList, const eglu::WindowParams& params) const;
 
private:
   virtual eglu::NativeWindow*        createWindow            (const eglu::WindowParams& params, int32_t format) const;
 
   WindowRegistry&                    m_windowRegistry;
};
 
// NativeWindow
 
NativeWindow::NativeWindow (Window* window, int width, int height, int32_t format)
   : eglu::NativeWindow    (WINDOW_CAPABILITIES)
   , m_window                (window)
   , m_format                (format)
{
   // Set up buffers.
   setSurfaceSize(IVec2(width, height));
}
 
NativeWindow::~NativeWindow (void)
{
   m_window->release();
}
 
void NativeWindow::processEvents (void)
{
   if (m_window->isPendingDestroy())
       throw eglu::WindowDestroyedError("Window has been destroyed");
}
 
void NativeWindow::setSurfaceSize (tcu::IVec2 size)
{
   m_window->setBuffersGeometry(size.x() != eglu::WindowParams::SIZE_DONT_CARE ? size.x() : 0,
                                size.y() != eglu::WindowParams::SIZE_DONT_CARE ? size.y() : 0,
                                m_format);
}
 
// NativeWindowFactory
 
NativeWindowFactory::NativeWindowFactory (WindowRegistry& windowRegistry)
   : eglu::NativeWindowFactory    ("default", "Default display", WINDOW_CAPABILITIES)
   , m_windowRegistry            (windowRegistry)
{
}
 
NativeWindowFactory::~NativeWindowFactory (void)
{
}
 
eglu::NativeWindow* NativeWindowFactory::createWindow (eglu::NativeDisplay* nativeDisplay, const eglu::WindowParams& params) const
{
   DE_UNREF(nativeDisplay);
   return createWindow(params, WINDOW_FORMAT_RGBA_8888);
}
 
eglu::NativeWindow* NativeWindowFactory::createWindow (eglu::NativeDisplay* nativeDisplay, EGLDisplay display, EGLConfig config, const EGLAttrib* attribList, const eglu::WindowParams& params) const
{
   const int32_t format = (int32_t)eglu::getConfigAttribInt(nativeDisplay->getLibrary(), display, config, EGL_NATIVE_VISUAL_ID);
   DE_UNREF(nativeDisplay && attribList);
   return createWindow(params, format);
}
 
eglu::NativeWindow* NativeWindowFactory::createWindow (const eglu::WindowParams& params, int32_t format) const
{
   Window* window = m_windowRegistry.tryAcquireWindow();
 
   if (!window)
       throw ResourceError("Native window is not available", DE_NULL, __FILE__, __LINE__);
 
   return new NativeWindow(window, params.width, params.height, format);
}
 
// NativeDisplayFactory
 
NativeDisplayFactory::NativeDisplayFactory (WindowRegistry& windowRegistry)
   : eglu::NativeDisplayFactory("default", "Default display", DISPLAY_CAPABILITIES)
{
   m_nativeWindowRegistry.registerFactory(new NativeWindowFactory(windowRegistry));
}
 
eglu::NativeDisplay* NativeDisplayFactory::createDisplay (const EGLAttrib* attribList) const
{
   DE_UNREF(attribList);
   return new NativeDisplay();
}
 
// Vulkan
 
class VulkanLibrary : public vk::Library
{
public:
   VulkanLibrary (void)
       : m_library    ("libvulkan.so")
       , m_driver    (m_library)
   {
   }
 
   const vk::PlatformInterface& getPlatformInterface        (void) const
   {
       return m_driver;
   }
 
   const tcu::FunctionLibrary&        getFunctionLibrary        (void) const
   {
       return m_library;
   }
 
private:
   const tcu::DynamicFunctionLibrary    m_library;
   const vk::PlatformDriver            m_driver;
};
 
DE_STATIC_ASSERT(sizeof(vk::pt::AndroidNativeWindowPtr) == sizeof(ANativeWindow*));
 
class VulkanWindow : public vk::wsi::AndroidWindowInterface
{
public:
   VulkanWindow (tcu::Android::Window& window)
       : vk::wsi::AndroidWindowInterface    (vk::pt::AndroidNativeWindowPtr(window.getNativeWindow()))
       , m_window                            (window)
   {
   }
 
   ~VulkanWindow (void)
   {
       m_window.release();
   }
 
private:
   tcu::Android::Window&    m_window;
};
 
class VulkanDisplay : public vk::wsi::Display
{
public:
   VulkanDisplay (WindowRegistry& windowRegistry)
       : m_windowRegistry(windowRegistry)
   {
   }
 
   vk::wsi::Window* createWindow (const Maybe<UVec2>& initialSize) const
   {
       Window* const    window    = m_windowRegistry.tryAcquireWindow();
 
       if (window)
       {
           try
           {
               if (initialSize)
                   window->setBuffersGeometry((int)initialSize->x(), (int)initialSize->y(), WINDOW_FORMAT_RGBA_8888);
 
               return new VulkanWindow(*window);
           }
           catch (...)
           {
               window->release();
               throw;
           }
       }
       else
           TCU_THROW(ResourceError, "Native window is not available");
   }
 
private:
   WindowRegistry&        m_windowRegistry;
};
 
static size_t getTotalSystemMemory (ANativeActivity* activity)
{
   const size_t    MiB        = (size_t)(1<<20);
 
   try
   {
       const size_t    cddRequiredSize    = getCDDRequiredSystemMemory(activity);
 
       print("Device has at least %.2f MiB total system memory per Android CDD\n", double(cddRequiredSize) / double(MiB));
 
       return cddRequiredSize;
   }
   catch (const std::exception& e)
   {
       // Use relatively high fallback size to encourage CDD-compliant behavior
       const size_t    fallbackSize    = (sizeof(void*) == sizeof(deUint64)) ? 2048*MiB : 1024*MiB;
 
       print("WARNING: Failed to determine system memory size required by CDD: %s\n", e.what());
       print("WARNING: Using fall-back size of %.2f MiB\n", double(fallbackSize) / double(MiB));
 
       return fallbackSize;
   }
}
 
// Platform
 
Platform::Platform (NativeActivity& activity)
   : m_activity            (activity)
   , m_totalSystemMemory    (getTotalSystemMemory(activity.getNativeActivity()))
{
   m_nativeDisplayFactoryRegistry.registerFactory(new NativeDisplayFactory(m_windowRegistry));
   m_contextFactoryRegistry.registerFactory(new eglu::GLContextFactory(m_nativeDisplayFactoryRegistry));
}
 
Platform::~Platform (void)
{
}
 
bool Platform::processEvents (void)
{
   m_windowRegistry.garbageCollect();
   return true;
}
 
vk::Library* Platform::createLibrary (void) const
{
   return new VulkanLibrary();
}
 
void Platform::describePlatform (std::ostream& dst) const
{
   tcu::Android::describePlatform(m_activity.getNativeActivity(), dst);
}
 
void Platform::getMemoryLimits (vk::PlatformMemoryLimits& limits) const
{
   // Worst-case estimates
   const size_t    MiB                = (size_t)(1<<20);
   const size_t    baseMemUsage    = 400*MiB;
   const double    safeUsageRatio    = 0.25;
 
   limits.totalSystemMemory                    = de::max((size_t)(double(deInt64(m_totalSystemMemory)-deInt64(baseMemUsage)) * safeUsageRatio), 16*MiB);
 
   // Assume UMA architecture
   limits.totalDeviceLocalMemory                = 0;
 
   // Reasonable worst-case estimates
   limits.deviceMemoryAllocationGranularity    = 64*1024;
   limits.devicePageSize                        = 4096;
   limits.devicePageTableEntrySize                = 8;
   limits.devicePageTableHierarchyLevels        = 3;
}
 
vk::wsi::Display* Platform::createWsiDisplay (vk::wsi::Type wsiType) const
{
   if (wsiType == vk::wsi::TYPE_ANDROID)
       return new VulkanDisplay(const_cast<WindowRegistry&>(m_windowRegistry));
   else
       TCU_THROW(NotSupportedError, "WSI type not supported on Android");
}
 
bool Platform::hasDisplay (vk::wsi::Type wsiType) const
{
   if (wsiType == vk::wsi::TYPE_ANDROID)
       return true;
 
   return false;
}
 
} // Android
} // tcu