blob: f0e37f87049d877eca898af0d5276737dcd8120a [file] [log] [blame]
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001#include <dvr/graphics.h>
2
Alex Vakulenko4fe60582017-02-02 11:35:59 -08003#include <inttypes.h>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08004#include <sys/timerfd.h>
5#include <array>
6#include <vector>
7
Alex Vakulenko4fe60582017-02-02 11:35:59 -08008#include <log/log.h>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08009#include <utils/Trace.h>
10
11#ifndef VK_USE_PLATFORM_ANDROID_KHR
12#define VK_USE_PLATFORM_ANDROID_KHR 1
13#endif
14#include <vulkan/vulkan.h>
15
Corey Tabaka2251d822017-04-20 16:04:07 -070016#include <dvr/dvr_display_types.h>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080017#include <pdx/file_handle.h>
18#include <private/dvr/clock_ns.h>
19#include <private/dvr/debug.h>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080020#include <private/dvr/frame_history.h>
21#include <private/dvr/gl_fenced_flush.h>
22#include <private/dvr/graphics/vr_gl_extensions.h>
23#include <private/dvr/graphics_private.h>
24#include <private/dvr/late_latch.h>
25#include <private/dvr/native_buffer_queue.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070026#include <private/dvr/platform_defines.h>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080027#include <private/dvr/sensor_constants.h>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080028#include <private/dvr/vsync_client.h>
29
30#include <android/native_window.h>
31
32#ifndef EGL_CONTEXT_MAJOR_VERSION
33#define EGL_CONTEXT_MAJOR_VERSION 0x3098
34#define EGL_CONTEXT_MINOR_VERSION 0x30FB
35#endif
36
Corey Tabaka2251d822017-04-20 16:04:07 -070037using android::pdx::ErrorStatus;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080038using android::pdx::LocalHandle;
39using android::pdx::LocalChannelHandle;
Corey Tabaka2251d822017-04-20 16:04:07 -070040using android::pdx::Status;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080041
Corey Tabaka2251d822017-04-20 16:04:07 -070042using android::dvr::display::DisplayClient;
43using android::dvr::display::Metrics;
44using android::dvr::display::NativeBufferQueue;
45using android::dvr::display::Surface;
46using android::dvr::display::SurfaceAttribute;
47using android::dvr::display::SurfaceAttributes;
48using android::dvr::display::SurfaceAttributeValue;
49using android::dvr::VSyncClient;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080050
51namespace {
52
Mark Urbanusa6c1f922017-03-22 13:11:51 -070053// TODO(urbanus): revisit once we have per-platform usage config in place.
Corey Tabaka2251d822017-04-20 16:04:07 -070054constexpr uint64_t kDefaultDisplaySurfaceUsage =
55 GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET |
56 GRALLOC1_PRODUCER_USAGE_PRIVATE_1 | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET |
57 GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
58constexpr uint32_t kDefaultDisplaySurfaceFormat = HAL_PIXEL_FORMAT_RGBA_8888;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080059// TODO(alexst): revisit this count when HW encode is available for casting.
Corey Tabaka2251d822017-04-20 16:04:07 -070060constexpr size_t kDefaultBufferCount = 4;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080061
62// Use with dvrBeginRenderFrame to disable EDS for the current frame.
63constexpr float32x4_t DVR_POSE_NO_EDS = {10.0f, 0.0f, 0.0f, 0.0f};
64
65// Use with dvrBeginRenderFrame to indicate that GPU late-latching is being used
66// for determining the render pose.
67constexpr float32x4_t DVR_POSE_LATE_LATCH = {20.0f, 0.0f, 0.0f, 0.0f};
68
69#ifndef NDEBUG
70
71static const char* GetGlCallbackType(GLenum type) {
72 switch (type) {
73 case GL_DEBUG_TYPE_ERROR_KHR:
74 return "ERROR";
75 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_KHR:
76 return "DEPRECATED_BEHAVIOR";
77 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_KHR:
78 return "UNDEFINED_BEHAVIOR";
79 case GL_DEBUG_TYPE_PORTABILITY_KHR:
80 return "PORTABILITY";
81 case GL_DEBUG_TYPE_PERFORMANCE_KHR:
82 return "PERFORMANCE";
83 case GL_DEBUG_TYPE_OTHER_KHR:
84 return "OTHER";
85 default:
86 return "UNKNOWN";
87 }
88}
89
90static void on_gl_error(GLenum /*source*/, GLenum type, GLuint /*id*/,
91 GLenum severity, GLsizei /*length*/,
92 const char* message, const void* /*user_param*/) {
93 char msg[400];
94 snprintf(msg, sizeof(msg), "[" __FILE__ ":%u] GL %s: %s", __LINE__,
95 GetGlCallbackType(type), message);
96 switch (severity) {
97 case GL_DEBUG_SEVERITY_LOW_KHR:
98 ALOGI("%s", msg);
99 break;
100 case GL_DEBUG_SEVERITY_MEDIUM_KHR:
101 ALOGW("%s", msg);
102 break;
103 case GL_DEBUG_SEVERITY_HIGH_KHR:
104 ALOGE("%s", msg);
105 break;
106 }
107 fprintf(stderr, "%s\n", msg);
108}
109
110#endif
111
112int DvrToHalSurfaceFormat(int dvr_surface_format) {
113 switch (dvr_surface_format) {
114 case DVR_SURFACE_FORMAT_RGBA_8888:
115 return HAL_PIXEL_FORMAT_RGBA_8888;
116 case DVR_SURFACE_FORMAT_RGB_565:
117 return HAL_PIXEL_FORMAT_RGB_565;
118 default:
119 return HAL_PIXEL_FORMAT_RGBA_8888;
120 }
121}
122
123int SelectEGLConfig(EGLDisplay dpy, EGLint* attr, unsigned format,
124 EGLConfig* config) {
125 std::array<EGLint, 4> desired_rgba;
126 switch (format) {
127 case HAL_PIXEL_FORMAT_RGBA_8888:
128 case HAL_PIXEL_FORMAT_BGRA_8888:
129 desired_rgba = {{8, 8, 8, 8}};
130 break;
131 case HAL_PIXEL_FORMAT_RGB_565:
132 desired_rgba = {{5, 6, 5, 0}};
133 break;
134 default:
135 ALOGE("Unsupported framebuffer pixel format %d", format);
136 return -1;
137 }
138
139 EGLint max_configs = 0;
140 if (eglGetConfigs(dpy, NULL, 0, &max_configs) == EGL_FALSE) {
141 ALOGE("No EGL configurations available?!");
142 return -1;
143 }
144
145 std::vector<EGLConfig> configs(max_configs);
146
147 EGLint num_configs;
148 if (eglChooseConfig(dpy, attr, &configs[0], max_configs, &num_configs) ==
149 EGL_FALSE) {
150 ALOGE("eglChooseConfig failed");
151 return -1;
152 }
153
154 std::array<EGLint, 4> config_rgba;
155 for (int i = 0; i < num_configs; i++) {
156 eglGetConfigAttrib(dpy, configs[i], EGL_RED_SIZE, &config_rgba[0]);
157 eglGetConfigAttrib(dpy, configs[i], EGL_GREEN_SIZE, &config_rgba[1]);
158 eglGetConfigAttrib(dpy, configs[i], EGL_BLUE_SIZE, &config_rgba[2]);
159 eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &config_rgba[3]);
160 if (config_rgba == desired_rgba) {
161 *config = configs[i];
162 return 0;
163 }
164 }
165
166 ALOGE("Cannot find a matching EGL config");
167 return -1;
168}
169
170void DestroyEglContext(EGLDisplay egl_display, EGLContext* egl_context) {
171 if (*egl_context != EGL_NO_CONTEXT) {
172 eglDestroyContext(egl_display, *egl_context);
173 *egl_context = EGL_NO_CONTEXT;
174 }
175}
176
177// Perform internal initialization. A GL context must be bound to the current
178// thread.
179// @param internally_created_context True if we created and own the GL context,
180// false if it was supplied by the application.
181// @return 0 if init was successful, or a negative error code on failure.
182int InitGl(bool internally_created_context) {
183 EGLDisplay egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
184 if (egl_display == EGL_NO_DISPLAY) {
185 ALOGE("eglGetDisplay failed");
186 return -EINVAL;
187 }
188
189 EGLContext egl_context = eglGetCurrentContext();
190 if (egl_context == EGL_NO_CONTEXT) {
191 ALOGE("No GL context bound");
192 return -EINVAL;
193 }
194
195 glGetError(); // Clear the error state
196 GLint major_version, minor_version;
197 glGetIntegerv(GL_MAJOR_VERSION, &major_version);
198 glGetIntegerv(GL_MINOR_VERSION, &minor_version);
199 if (glGetError() != GL_NO_ERROR) {
200 // GL_MAJOR_VERSION and GL_MINOR_VERSION were added in GLES 3. If we get an
201 // error querying them it's almost certainly because it's GLES 1 or 2.
202 ALOGE("Error getting GL version. Must be GLES 3.2 or greater.");
203 return -EINVAL;
204 }
205
206 if (major_version < 3 || (major_version == 3 && minor_version < 2)) {
207 ALOGE("Invalid GL version: %d.%d. Must be GLES 3.2 or greater.",
208 major_version, minor_version);
209 return -EINVAL;
210 }
211
212#ifndef NDEBUG
213 if (internally_created_context) {
214 // Enable verbose GL debug output.
215 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR);
216 glDebugMessageCallbackKHR(on_gl_error, NULL);
217 GLuint unused_ids = 0;
218 glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0,
219 &unused_ids, GL_TRUE);
220 }
221#else
222 (void)internally_created_context;
223#endif
224
225 load_gl_extensions();
226 return 0;
227}
228
229int CreateEglContext(EGLDisplay egl_display, DvrSurfaceParameter* parameters,
230 EGLContext* egl_context) {
231 *egl_context = EGL_NO_CONTEXT;
232
233 EGLint major, minor;
234 if (!eglInitialize(egl_display, &major, &minor)) {
235 ALOGE("Failed to initialize EGL");
236 return -ENXIO;
237 }
238
239 ALOGI("EGL version: %d.%d\n", major, minor);
240
241 int buffer_format = kDefaultDisplaySurfaceFormat;
242
243 for (auto p = parameters; p && p->key != DVR_SURFACE_PARAMETER_NONE; ++p) {
244 switch (p->key) {
245 case DVR_SURFACE_PARAMETER_FORMAT_IN:
246 buffer_format = DvrToHalSurfaceFormat(p->value);
247 break;
248 }
249 }
250
251 EGLint config_attrs[] = {EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
252 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE};
253 EGLConfig config = {0};
254
255 int ret = SelectEGLConfig(egl_display, config_attrs, buffer_format, &config);
256 if (ret < 0)
257 return ret;
258
259 ALOGI("EGL SelectEGLConfig ok.\n");
260
261 EGLint context_attrs[] = {EGL_CONTEXT_MAJOR_VERSION,
262 3,
263 EGL_CONTEXT_MINOR_VERSION,
264 2,
265#ifndef NDEBUG
266 EGL_CONTEXT_FLAGS_KHR,
267 EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR,
268#endif
269 EGL_NONE};
270
271 *egl_context =
272 eglCreateContext(egl_display, config, EGL_NO_CONTEXT, context_attrs);
273 if (*egl_context == EGL_NO_CONTEXT) {
274 ALOGE("eglCreateContext failed");
275 return -ENXIO;
276 }
277
278 ALOGI("eglCreateContext ok.\n");
279
280 if (!eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
281 *egl_context)) {
282 ALOGE("eglMakeCurrent failed");
283 DestroyEglContext(egl_display, egl_context);
284 return -EINVAL;
285 }
286
287 return 0;
288}
289
Corey Tabaka2251d822017-04-20 16:04:07 -0700290// Utility structure to hold info related to creating a surface.
291struct SurfaceResult {
292 std::shared_ptr<Surface> surface;
293 Metrics metrics;
294 uint32_t width;
295 uint32_t height;
296 uint32_t format;
297 uint64_t usage;
298 size_t capacity;
299 int geometry;
300 bool direct_surface;
301};
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800302
Corey Tabaka2251d822017-04-20 16:04:07 -0700303Status<std::tuple<std::shared_ptr<android::dvr::ProducerQueue>,
304 std::shared_ptr<android::dvr::BufferProducer>,
305 volatile DisplaySurfaceMetadata*>>
306CreateMetadataBuffer(const std::shared_ptr<Surface>& surface,
307 bool direct_surface) {
308 std::shared_ptr<android::dvr::ProducerQueue> queue;
309 std::shared_ptr<android::dvr::BufferProducer> buffer;
310
311 if (!direct_surface) {
312 auto queue_status = surface->CreateQueue(
313 sizeof(DisplaySurfaceMetadata), 1, HAL_PIXEL_FORMAT_BLOB,
314 GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET |
315 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN |
316 GRALLOC1_CONSUMER_USAGE_GPU_DATA_BUFFER,
317 1);
318 if (!queue_status) {
319 ALOGE("CreateMetadataBuffer: Failed to create queue: %s",
320 queue_status.GetErrorMessage().c_str());
321 return queue_status.error_status();
322 }
323
324 queue = queue_status.take();
325 LocalHandle fence;
326 size_t slot;
327 auto buffer_status = queue->Dequeue(-1, &slot, &fence);
328 if (!buffer_status) {
329 ALOGE("CreateMetadataBuffer: Failed to dequeue buffer: %s",
330 buffer_status.GetErrorMessage().c_str());
331 return buffer_status.error_status();
332 }
333 buffer = buffer_status.take();
334 } else {
335 buffer = android::dvr::BufferProducer::CreateUncachedBlob(
336 sizeof(DisplaySurfaceMetadata));
337 if (!buffer) {
338 ALOGE("CreateMetadataBuffer: Failed to create stand-in buffer!");
339 return ErrorStatus(ENOMEM);
340 }
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800341 }
342
Corey Tabaka2251d822017-04-20 16:04:07 -0700343 void* address = nullptr;
344 int ret =
345 buffer->GetBlobReadWritePointer(sizeof(DisplaySurfaceMetadata), &address);
346
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800347 if (ret < 0) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700348 ALOGE("CreateMetadataBuffer: Failed to map buffer: %s", strerror(-ret));
349 return ErrorStatus(-ret);
350 }
351
352 // Post the buffer so that the compositor can retrieve it from the consumer
353 // queue.
354 ret = buffer->Post<void>(LocalHandle{});
355 if (ret < 0) {
356 ALOGE("CreateMetadataBuffer: Failed to post buffer: %s", strerror(-ret));
357 return ErrorStatus(-ret);
358 }
359
360 ALOGD_IF(TRACE, "CreateMetadataBuffer: queue_id=%d buffer_id=%d address=%p",
361 queue ? queue->id() : -1, buffer->id(), address);
362 return {{std::move(queue), std::move(buffer),
363 static_cast<DisplaySurfaceMetadata*>(address)}};
364}
365
366} // anonymous namespace
367
368Status<SurfaceResult> CreateSurface(struct DvrSurfaceParameter* parameters) {
369 int error;
370 auto client = DisplayClient::Create(&error);
371 if (!client) {
372 ALOGE("CreateApplicationSurface: Failed to create display client!");
373 return ErrorStatus(error);
374 }
375
376 auto metrics_status = client->GetDisplayMetrics();
377 if (!metrics_status) {
378 ALOGE("CreateApplicationSurface: Failed to get display metrics: %s",
379 metrics_status.GetErrorMessage().c_str());
380 return metrics_status.error_status();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800381 }
382
383 // Parameters that may be modified by the parameters array. Some of these are
384 // here for future expansion.
Corey Tabaka2251d822017-04-20 16:04:07 -0700385
386 uint32_t request_width = metrics_status.get().display_width;
387 uint32_t request_height = metrics_status.get().display_width;
388 uint32_t request_format = kDefaultDisplaySurfaceFormat;
389 uint64_t request_usage = kDefaultDisplaySurfaceUsage;
390 size_t request_capacity = kDefaultBufferCount;
391 int request_geometry = DVR_SURFACE_GEOMETRY_SINGLE;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800392 bool disable_distortion = false;
393 bool disable_stabilization = false;
394 bool disable_cac = false;
Corey Tabaka2251d822017-04-20 16:04:07 -0700395 bool request_visible = false;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800396 bool vertical_flip = false;
Corey Tabaka2251d822017-04-20 16:04:07 -0700397 bool direct_surface = false;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800398 int request_z_order = 0;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800399
400 // Handle parameter inputs.
401 for (auto p = parameters; p && p->key != DVR_SURFACE_PARAMETER_NONE; ++p) {
402 switch (p->key) {
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800403 case DVR_SURFACE_PARAMETER_DISABLE_DISTORTION_IN:
404 disable_distortion = !!p->value;
405 break;
406 case DVR_SURFACE_PARAMETER_DISABLE_STABILIZATION_IN:
407 disable_stabilization = !!p->value;
408 break;
409 case DVR_SURFACE_PARAMETER_DISABLE_CAC_IN:
410 disable_cac = !!p->value;
411 break;
412 case DVR_SURFACE_PARAMETER_VISIBLE_IN:
413 request_visible = !!p->value;
414 break;
415 case DVR_SURFACE_PARAMETER_Z_ORDER_IN:
416 request_z_order = p->value;
417 break;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800418 case DVR_SURFACE_PARAMETER_VERTICAL_FLIP_IN:
419 vertical_flip = !!p->value;
420 break;
Corey Tabaka2251d822017-04-20 16:04:07 -0700421 case DVR_SURFACE_PARAMETER_DIRECT_IN:
422 direct_surface = !!p->value;
423 break;
424 case DVR_SURFACE_PARAMETER_WIDTH_IN:
425 request_width = p->value;
426 break;
427 case DVR_SURFACE_PARAMETER_HEIGHT_IN:
428 request_height = p->value;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800429 break;
430 case DVR_SURFACE_PARAMETER_FORMAT_IN:
Corey Tabaka2251d822017-04-20 16:04:07 -0700431 request_format = p->value;
432 break;
433 case DVR_SURFACE_PARAMETER_GEOMETRY_IN:
434 request_geometry = p->value;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800435 break;
436 case DVR_SURFACE_PARAMETER_ENABLE_LATE_LATCH_IN:
437 case DVR_SURFACE_PARAMETER_CREATE_GL_CONTEXT_IN:
438 case DVR_SURFACE_PARAMETER_DISPLAY_WIDTH_OUT:
439 case DVR_SURFACE_PARAMETER_DISPLAY_HEIGHT_OUT:
440 case DVR_SURFACE_PARAMETER_SURFACE_WIDTH_OUT:
441 case DVR_SURFACE_PARAMETER_SURFACE_HEIGHT_OUT:
442 case DVR_SURFACE_PARAMETER_INTER_LENS_METERS_OUT:
443 case DVR_SURFACE_PARAMETER_LEFT_FOV_LRBT_OUT:
444 case DVR_SURFACE_PARAMETER_RIGHT_FOV_LRBT_OUT:
445 case DVR_SURFACE_PARAMETER_VSYNC_PERIOD_OUT:
446 case DVR_SURFACE_PARAMETER_SURFACE_TEXTURE_TARGET_TYPE_OUT:
447 case DVR_SURFACE_PARAMETER_SURFACE_TEXTURE_TARGET_ID_OUT:
448 case DVR_SURFACE_PARAMETER_GRAPHICS_API_IN:
449 case DVR_SURFACE_PARAMETER_VK_INSTANCE_IN:
450 case DVR_SURFACE_PARAMETER_VK_PHYSICAL_DEVICE_IN:
451 case DVR_SURFACE_PARAMETER_VK_DEVICE_IN:
452 case DVR_SURFACE_PARAMETER_VK_PRESENT_QUEUE_IN:
453 case DVR_SURFACE_PARAMETER_VK_PRESENT_QUEUE_FAMILY_IN:
454 case DVR_SURFACE_PARAMETER_VK_SWAPCHAIN_IMAGE_COUNT_OUT:
455 case DVR_SURFACE_PARAMETER_VK_SWAPCHAIN_IMAGE_FORMAT_OUT:
456 break;
457 default:
Corey Tabaka2251d822017-04-20 16:04:07 -0700458 ALOGE(
459 "CreateSurface: Invalid display surface parameter: key=%d "
460 "value=%" PRId64,
461 p->key, p->value);
462 return ErrorStatus(EINVAL);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800463 }
464 }
465
Corey Tabaka2251d822017-04-20 16:04:07 -0700466 // TODO(eieio): Setup a "surface flags" attribute based on the surface
467 // parameters gathered above.
468 SurfaceAttributes surface_attributes;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800469
Corey Tabaka2251d822017-04-20 16:04:07 -0700470 surface_attributes[SurfaceAttribute::Direct] = direct_surface;
471 surface_attributes[SurfaceAttribute::Visible] = request_visible;
472 surface_attributes[SurfaceAttribute::ZOrder] = request_z_order;
473
474 auto surface_status = Surface::CreateSurface(surface_attributes);
475 if (!surface_status) {
476 ALOGE("CreateSurface: Failed to create surface: %s",
477 surface_status.GetErrorMessage().c_str());
478 return surface_status.error_status();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800479 }
480
Corey Tabaka2251d822017-04-20 16:04:07 -0700481 return {{surface_status.take(), metrics_status.get(), request_width,
482 request_height, request_format, request_usage, request_capacity,
483 request_geometry, direct_surface}};
484}
485
486// TODO(hendrikw): When we remove the calls to this in native_window.cpp, move
487// this back into the anonymous namespace
488Status<SurfaceResult> CreateApplicationSurface(
489 struct DvrSurfaceParameter* parameters) {
490 auto surface_status = CreateSurface(parameters);
491 if (!surface_status)
492 return surface_status;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800493
494 // Handle parameter output requests down here so we can return surface info.
495 for (auto p = parameters; p && p->key != DVR_SURFACE_PARAMETER_NONE; ++p) {
496 switch (p->key) {
497 case DVR_SURFACE_PARAMETER_DISPLAY_WIDTH_OUT:
Corey Tabaka2251d822017-04-20 16:04:07 -0700498 *static_cast<int32_t*>(p->value_out) =
499 surface_status.get().metrics.display_width;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800500 break;
501 case DVR_SURFACE_PARAMETER_DISPLAY_HEIGHT_OUT:
Corey Tabaka2251d822017-04-20 16:04:07 -0700502 *static_cast<int32_t*>(p->value_out) =
503 surface_status.get().metrics.display_height;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800504 break;
505 case DVR_SURFACE_PARAMETER_VSYNC_PERIOD_OUT:
Corey Tabaka2251d822017-04-20 16:04:07 -0700506 *static_cast<uint64_t*>(p->value_out) =
507 surface_status.get().metrics.vsync_period_ns;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800508 break;
Corey Tabaka2251d822017-04-20 16:04:07 -0700509 case DVR_SURFACE_PARAMETER_SURFACE_WIDTH_OUT:
510 *static_cast<uint32_t*>(p->value_out) = surface_status.get().width;
511 break;
512 case DVR_SURFACE_PARAMETER_SURFACE_HEIGHT_OUT:
513 *static_cast<uint32_t*>(p->value_out) = surface_status.get().height;
514 break;
515
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800516 default:
517 break;
518 }
519 }
520
Corey Tabaka2251d822017-04-20 16:04:07 -0700521 return surface_status;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800522}
523
Corey Tabaka2251d822017-04-20 16:04:07 -0700524extern "C" int dvrGetNativeDisplayDimensions(int* display_width,
525 int* display_height) {
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800526 int error = 0;
Corey Tabaka2251d822017-04-20 16:04:07 -0700527 auto client = DisplayClient::Create(&error);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800528 if (!client) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700529 ALOGE("dvrGetNativeDisplayDimensions: Failed to create display client!");
530 return -error;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800531 }
532
Corey Tabaka2251d822017-04-20 16:04:07 -0700533 auto metrics_status = client->GetDisplayMetrics();
534 if (!metrics_status) {
535 ALOGE("dvrGetNativeDisplayDimensions: Failed to get display metrics: %s",
536 metrics_status.GetErrorMessage().c_str());
537 return -metrics_status.error();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800538 }
539
Corey Tabaka2251d822017-04-20 16:04:07 -0700540 *display_width = static_cast<int>(metrics_status.get().display_width);
541 *display_height = static_cast<int>(metrics_status.get().display_height);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800542 return 0;
543}
544
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800545struct DvrGraphicsContext : public android::ANativeObjectBase<
546 ANativeWindow, DvrGraphicsContext,
547 android::LightRefBase<DvrGraphicsContext>> {
548 public:
549 DvrGraphicsContext();
550 ~DvrGraphicsContext();
551
552 int graphics_api; // DVR_SURFACE_GRAPHICS_API_*
553
554 // GL specific members.
555 struct {
556 EGLDisplay egl_display;
557 EGLContext egl_context;
558 bool owns_egl_context;
559 GLuint texture_id[kSurfaceViewMaxCount];
560 int texture_count;
561 GLenum texture_target_type;
562 } gl;
563
564 // VK specific members
565 struct {
566 // These objects are passed in by the application, and are NOT owned
567 // by the context.
568 VkInstance instance;
569 VkPhysicalDevice physical_device;
570 VkDevice device;
571 VkQueue present_queue;
572 uint32_t present_queue_family;
573 const VkAllocationCallbacks* allocation_callbacks;
574 // These objects are owned by the context.
575 ANativeWindow* window;
576 VkSurfaceKHR surface;
577 VkSwapchainKHR swapchain;
578 std::vector<VkImage> swapchain_images;
579 std::vector<VkImageView> swapchain_image_views;
580 } vk;
581
582 // Display surface, metrics, and buffer management members.
Corey Tabaka2251d822017-04-20 16:04:07 -0700583 std::shared_ptr<Surface> display_surface;
584 uint32_t width;
585 uint32_t height;
586 uint32_t format;
587 Metrics display_metrics;
588 std::unique_ptr<NativeBufferQueue> buffer_queue;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800589 android::dvr::NativeBufferProducer* current_buffer;
590 bool buffer_already_posted;
591
592 // Synchronization members.
593 std::unique_ptr<android::dvr::VSyncClient> vsync_client;
594 LocalHandle timerfd;
595
596 android::dvr::FrameHistory frame_history;
597
Corey Tabaka2251d822017-04-20 16:04:07 -0700598 // Metadata queue and buffer.
599 // TODO(eieio): Remove the queue once one-off buffers are supported as a
600 // surface primitive element.
601 std::shared_ptr<android::dvr::ProducerQueue> metadata_queue;
602 std::shared_ptr<android::dvr::BufferProducer> metadata_buffer;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800603 // Mapped surface metadata (ie: for pose delivery with presented frames).
Corey Tabaka2251d822017-04-20 16:04:07 -0700604 volatile DisplaySurfaceMetadata* surface_metadata;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800605
606 // LateLatch support.
607 std::unique_ptr<android::dvr::LateLatch> late_latch;
608
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800609 private:
610 // ANativeWindow function implementations
611 std::mutex lock_;
612 int Post(android::dvr::NativeBufferProducer* buffer, int fence_fd);
613 static int SetSwapInterval(ANativeWindow* window, int interval);
614 static int DequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer,
615 int* fence_fd);
616 static int QueueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer,
617 int fence_fd);
618 static int CancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer,
619 int fence_fd);
620 static int Query(const ANativeWindow* window, int what, int* value);
621 static int Perform(ANativeWindow* window, int operation, ...);
622 static int DequeueBuffer_DEPRECATED(ANativeWindow* window,
623 ANativeWindowBuffer** buffer);
624 static int CancelBuffer_DEPRECATED(ANativeWindow* window,
625 ANativeWindowBuffer* buffer);
626 static int QueueBuffer_DEPRECATED(ANativeWindow* window,
627 ANativeWindowBuffer* buffer);
628 static int LockBuffer_DEPRECATED(ANativeWindow* window,
629 ANativeWindowBuffer* buffer);
630
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800631 DvrGraphicsContext(const DvrGraphicsContext&) = delete;
632 void operator=(const DvrGraphicsContext&) = delete;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800633};
634
635DvrGraphicsContext::DvrGraphicsContext()
636 : graphics_api(DVR_GRAPHICS_API_GLES),
637 gl{},
638 vk{},
639 current_buffer(nullptr),
640 buffer_already_posted(false),
641 surface_metadata(nullptr) {
642 gl.egl_display = EGL_NO_DISPLAY;
643 gl.egl_context = EGL_NO_CONTEXT;
644 gl.owns_egl_context = true;
645 gl.texture_target_type = GL_TEXTURE_2D;
646
647 ANativeWindow::setSwapInterval = SetSwapInterval;
648 ANativeWindow::dequeueBuffer = DequeueBuffer;
649 ANativeWindow::cancelBuffer = CancelBuffer;
650 ANativeWindow::queueBuffer = QueueBuffer;
651 ANativeWindow::query = Query;
652 ANativeWindow::perform = Perform;
653
654 ANativeWindow::dequeueBuffer_DEPRECATED = DequeueBuffer_DEPRECATED;
655 ANativeWindow::cancelBuffer_DEPRECATED = CancelBuffer_DEPRECATED;
656 ANativeWindow::lockBuffer_DEPRECATED = LockBuffer_DEPRECATED;
657 ANativeWindow::queueBuffer_DEPRECATED = QueueBuffer_DEPRECATED;
658}
659
660DvrGraphicsContext::~DvrGraphicsContext() {
661 if (graphics_api == DVR_GRAPHICS_API_GLES) {
662 glDeleteTextures(gl.texture_count, gl.texture_id);
663 if (gl.owns_egl_context)
664 DestroyEglContext(gl.egl_display, &gl.egl_context);
665 } else if (graphics_api == DVR_GRAPHICS_API_VULKAN) {
666 if (vk.swapchain != VK_NULL_HANDLE) {
667 for (auto view : vk.swapchain_image_views) {
668 vkDestroyImageView(vk.device, view, vk.allocation_callbacks);
669 }
670 vkDestroySwapchainKHR(vk.device, vk.swapchain, vk.allocation_callbacks);
671 vkDestroySurfaceKHR(vk.instance, vk.surface, vk.allocation_callbacks);
672 delete vk.window;
673 }
674 }
675}
676
677int dvrGraphicsContextCreate(struct DvrSurfaceParameter* parameters,
678 DvrGraphicsContext** return_graphics_context) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700679 auto context = std::make_unique<DvrGraphicsContext>();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800680
681 // See whether we're using GL or Vulkan
682 for (auto p = parameters; p && p->key != DVR_SURFACE_PARAMETER_NONE; ++p) {
683 switch (p->key) {
684 case DVR_SURFACE_PARAMETER_GRAPHICS_API_IN:
685 context->graphics_api = p->value;
686 break;
687 }
688 }
689
690 if (context->graphics_api == DVR_GRAPHICS_API_GLES) {
691 context->gl.egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
692 if (context->gl.egl_display == EGL_NO_DISPLAY) {
693 ALOGE("eglGetDisplay failed");
694 return -ENXIO;
695 }
696
697 // See if we should create a GL context
698 for (auto p = parameters; p && p->key != DVR_SURFACE_PARAMETER_NONE; ++p) {
699 switch (p->key) {
700 case DVR_SURFACE_PARAMETER_CREATE_GL_CONTEXT_IN:
701 context->gl.owns_egl_context = p->value != 0;
702 break;
703 }
704 }
705
706 if (context->gl.owns_egl_context) {
707 int ret = CreateEglContext(context->gl.egl_display, parameters,
708 &context->gl.egl_context);
709 if (ret < 0)
710 return ret;
711 } else {
712 context->gl.egl_context = eglGetCurrentContext();
713 }
714
715 int ret = InitGl(context->gl.owns_egl_context);
716 if (ret < 0)
717 return ret;
718 } else if (context->graphics_api == DVR_GRAPHICS_API_VULKAN) {
719 for (auto p = parameters; p && p->key != DVR_SURFACE_PARAMETER_NONE; ++p) {
720 switch (p->key) {
721 case DVR_SURFACE_PARAMETER_VK_INSTANCE_IN:
722 context->vk.instance = reinterpret_cast<VkInstance>(p->value);
723 break;
724 case DVR_SURFACE_PARAMETER_VK_PHYSICAL_DEVICE_IN:
725 context->vk.physical_device =
726 reinterpret_cast<VkPhysicalDevice>(p->value);
727 break;
728 case DVR_SURFACE_PARAMETER_VK_DEVICE_IN:
729 context->vk.device = reinterpret_cast<VkDevice>(p->value);
730 break;
731 case DVR_SURFACE_PARAMETER_VK_PRESENT_QUEUE_IN:
732 context->vk.present_queue = reinterpret_cast<VkQueue>(p->value);
733 break;
734 case DVR_SURFACE_PARAMETER_VK_PRESENT_QUEUE_FAMILY_IN:
735 context->vk.present_queue_family = static_cast<uint32_t>(p->value);
736 break;
737 }
738 }
739 } else {
740 ALOGE("Error: invalid graphics API type");
741 return -EINVAL;
742 }
743
Corey Tabaka2251d822017-04-20 16:04:07 -0700744 auto surface_status = CreateApplicationSurface(parameters);
745 if (!surface_status) {
746 ALOGE("dvrGraphicsContextCreate: Failed to create surface: %s",
747 surface_status.GetErrorMessage().c_str());
748 return -surface_status.error();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800749 }
750
Corey Tabaka2251d822017-04-20 16:04:07 -0700751 auto surface_result = surface_status.take();
752
753 context->display_surface = surface_result.surface;
754 context->display_metrics = surface_result.metrics;
755 context->width = surface_result.width;
756 context->height = surface_result.height;
757 context->format = surface_result.format;
758
759 // Create an empty queue. NativeBufferQueue allocates the buffers for this
760 // queue.
761 auto queue_status = context->display_surface->CreateQueue();
762 if (!queue_status) {
763 ALOGE("dvrGraphicsContextCreate: Failed to create queue: %s",
764 queue_status.GetErrorMessage().c_str());
765 return -queue_status.error();
766 }
767
768 context->buffer_queue.reset(new NativeBufferQueue(
769 context->gl.egl_display, queue_status.take(), surface_result.width,
770 surface_result.height, surface_result.format, surface_result.usage,
771 surface_result.capacity));
772
773 // Create the metadata buffer.
774 auto metadata_status = CreateMetadataBuffer(context->display_surface,
775 surface_result.direct_surface);
776 if (!metadata_status) {
777 ALOGE("dvrGraphicsContextCreate: Failed to create metadata buffer: %s",
778 metadata_status.GetErrorMessage().c_str());
779 return -metadata_status.error();
780 }
781 std::tie(context->metadata_queue, context->metadata_buffer,
782 context->surface_metadata) = metadata_status.take();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800783
784 // The way the call sequence works we need 1 more than the buffer queue
785 // capacity to store data for all pending frames
Corey Tabaka2251d822017-04-20 16:04:07 -0700786 context->frame_history.Reset(context->buffer_queue->capacity() + 1);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800787
Corey Tabaka2251d822017-04-20 16:04:07 -0700788 context->vsync_client = VSyncClient::Create();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800789 if (!context->vsync_client) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700790 ALOGE("dvrGraphicsContextCreate: failed to create vsync client");
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800791 return -ECOMM;
792 }
793
794 context->timerfd.Reset(timerfd_create(CLOCK_MONOTONIC, 0));
795 if (!context->timerfd) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700796 ALOGE("dvrGraphicsContextCreate: timerfd_create failed because: %s",
797 strerror(errno));
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800798 return -EPERM;
799 }
800
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800801 if (context->graphics_api == DVR_GRAPHICS_API_GLES) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700802 context->gl.texture_count =
803 (surface_result.geometry == DVR_SURFACE_GEOMETRY_SEPARATE_2) ? 2 : 1;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800804
805 // Create the GL textures.
806 glGenTextures(context->gl.texture_count, context->gl.texture_id);
807
808 // We must make sure that we have at least one buffer allocated at this time
809 // so that anyone who tries to bind an FBO to context->texture_id
810 // will not get an incomplete buffer.
811 context->current_buffer = context->buffer_queue->Dequeue();
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800812 LOG_ALWAYS_FATAL_IF(context->gl.texture_count !=
813 context->current_buffer->buffer()->slice_count());
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800814 for (int i = 0; i < context->gl.texture_count; ++i) {
815 glBindTexture(context->gl.texture_target_type, context->gl.texture_id[i]);
816 glEGLImageTargetTexture2DOES(context->gl.texture_target_type,
817 context->current_buffer->image_khr(i));
818 }
819 glBindTexture(context->gl.texture_target_type, 0);
820 CHECK_GL();
821
822 bool is_late_latch = false;
823
824 // Pass back the texture target type and id.
825 for (auto p = parameters; p && p->key != DVR_SURFACE_PARAMETER_NONE; ++p) {
826 switch (p->key) {
827 case DVR_SURFACE_PARAMETER_ENABLE_LATE_LATCH_IN:
828 is_late_latch = !!p->value;
829 break;
830 case DVR_SURFACE_PARAMETER_SURFACE_TEXTURE_TARGET_TYPE_OUT:
831 *static_cast<GLenum*>(p->value_out) = context->gl.texture_target_type;
832 break;
833 case DVR_SURFACE_PARAMETER_SURFACE_TEXTURE_TARGET_ID_OUT:
834 for (int i = 0; i < context->gl.texture_count; ++i) {
835 *(static_cast<GLuint*>(p->value_out) + i) =
836 context->gl.texture_id[i];
837 }
838 break;
839 }
840 }
841
842 // Initialize late latch.
843 if (is_late_latch) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700844 LocalHandle fd = context->metadata_buffer->GetBlobFd();
845 context->late_latch.reset(
846 new android::dvr::LateLatch(true, std::move(fd)));
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800847 }
848 } else if (context->graphics_api == DVR_GRAPHICS_API_VULKAN) {
849 VkResult result = VK_SUCCESS;
850 // Create a VkSurfaceKHR from the ANativeWindow.
851 VkAndroidSurfaceCreateInfoKHR android_surface_ci = {};
852 android_surface_ci.sType =
853 VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
854 android_surface_ci.window = context.get();
855 result = vkCreateAndroidSurfaceKHR(
856 context->vk.instance, &android_surface_ci,
857 context->vk.allocation_callbacks, &context->vk.surface);
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800858 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800859 VkBool32 surface_supports_present = VK_FALSE;
860 result = vkGetPhysicalDeviceSurfaceSupportKHR(
861 context->vk.physical_device, context->vk.present_queue_family,
862 context->vk.surface, &surface_supports_present);
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800863 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800864 if (!surface_supports_present) {
865 ALOGE("Error: provided queue family (%u) does not support presentation",
866 context->vk.present_queue_family);
867 return -EPERM;
868 }
869 VkSurfaceCapabilitiesKHR surface_capabilities = {};
870 result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
871 context->vk.physical_device, context->vk.surface,
872 &surface_capabilities);
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800873 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800874 // Determine the swapchain image format.
875 uint32_t device_surface_format_count = 0;
876 result = vkGetPhysicalDeviceSurfaceFormatsKHR(
877 context->vk.physical_device, context->vk.surface,
878 &device_surface_format_count, nullptr);
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800879 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800880 std::vector<VkSurfaceFormatKHR> device_surface_formats(
881 device_surface_format_count);
882 result = vkGetPhysicalDeviceSurfaceFormatsKHR(
883 context->vk.physical_device, context->vk.surface,
884 &device_surface_format_count, device_surface_formats.data());
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800885 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
886 LOG_ALWAYS_FATAL_IF(device_surface_format_count == 0U);
887 LOG_ALWAYS_FATAL_IF(device_surface_formats[0].format ==
888 VK_FORMAT_UNDEFINED);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800889 VkSurfaceFormatKHR present_surface_format = device_surface_formats[0];
890 // Determine the swapchain present mode.
891 // TODO(cort): query device_present_modes to make sure MAILBOX is supported.
892 // But according to libvulkan, it is.
893 uint32_t device_present_mode_count = 0;
894 result = vkGetPhysicalDeviceSurfacePresentModesKHR(
895 context->vk.physical_device, context->vk.surface,
896 &device_present_mode_count, nullptr);
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800897 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800898 std::vector<VkPresentModeKHR> device_present_modes(
899 device_present_mode_count);
900 result = vkGetPhysicalDeviceSurfacePresentModesKHR(
901 context->vk.physical_device, context->vk.surface,
902 &device_present_mode_count, device_present_modes.data());
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800903 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800904 VkPresentModeKHR present_mode = VK_PRESENT_MODE_MAILBOX_KHR;
905 // Extract presentation surface extents, image count, transform, usages,
906 // etc.
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800907 LOG_ALWAYS_FATAL_IF(
908 static_cast<int>(surface_capabilities.currentExtent.width) == -1 ||
909 static_cast<int>(surface_capabilities.currentExtent.height) == -1);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800910 VkExtent2D swapchain_extent = surface_capabilities.currentExtent;
911
912 uint32_t desired_image_count = surface_capabilities.minImageCount;
913 if (surface_capabilities.maxImageCount > 0 &&
914 desired_image_count > surface_capabilities.maxImageCount) {
915 desired_image_count = surface_capabilities.maxImageCount;
916 }
917 VkSurfaceTransformFlagBitsKHR surface_transform =
918 surface_capabilities.currentTransform;
919 VkImageUsageFlags image_usage_flags =
920 surface_capabilities.supportedUsageFlags;
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800921 LOG_ALWAYS_FATAL_IF(surface_capabilities.supportedCompositeAlpha ==
922 static_cast<VkFlags>(0));
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800923 VkCompositeAlphaFlagBitsKHR composite_alpha =
924 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
925 if (!(surface_capabilities.supportedCompositeAlpha &
926 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR)) {
927 composite_alpha = VkCompositeAlphaFlagBitsKHR(
928 static_cast<int>(surface_capabilities.supportedCompositeAlpha) &
929 -static_cast<int>(surface_capabilities.supportedCompositeAlpha));
930 }
931 // Create VkSwapchainKHR
932 VkSwapchainCreateInfoKHR swapchain_ci = {};
933 swapchain_ci.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
934 swapchain_ci.pNext = nullptr;
935 swapchain_ci.surface = context->vk.surface;
936 swapchain_ci.minImageCount = desired_image_count;
937 swapchain_ci.imageFormat = present_surface_format.format;
938 swapchain_ci.imageColorSpace = present_surface_format.colorSpace;
939 swapchain_ci.imageExtent.width = swapchain_extent.width;
940 swapchain_ci.imageExtent.height = swapchain_extent.height;
941 swapchain_ci.imageUsage = image_usage_flags;
942 swapchain_ci.preTransform = surface_transform;
943 swapchain_ci.compositeAlpha = composite_alpha;
944 swapchain_ci.imageArrayLayers = 1;
945 swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
946 swapchain_ci.queueFamilyIndexCount = 0;
947 swapchain_ci.pQueueFamilyIndices = nullptr;
948 swapchain_ci.presentMode = present_mode;
949 swapchain_ci.clipped = VK_TRUE;
950 swapchain_ci.oldSwapchain = VK_NULL_HANDLE;
951 result = vkCreateSwapchainKHR(context->vk.device, &swapchain_ci,
952 context->vk.allocation_callbacks,
953 &context->vk.swapchain);
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800954 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800955 // Create swapchain image views
956 uint32_t image_count = 0;
957 result = vkGetSwapchainImagesKHR(context->vk.device, context->vk.swapchain,
958 &image_count, nullptr);
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800959 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
960 LOG_ALWAYS_FATAL_IF(image_count == 0U);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800961 context->vk.swapchain_images.resize(image_count);
962 result = vkGetSwapchainImagesKHR(context->vk.device, context->vk.swapchain,
963 &image_count,
964 context->vk.swapchain_images.data());
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800965 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800966 context->vk.swapchain_image_views.resize(image_count);
967 VkImageViewCreateInfo image_view_ci = {};
968 image_view_ci.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
969 image_view_ci.pNext = nullptr;
970 image_view_ci.flags = 0;
971 image_view_ci.format = swapchain_ci.imageFormat;
972 image_view_ci.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
973 image_view_ci.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
974 image_view_ci.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
975 image_view_ci.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
976 image_view_ci.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
977 image_view_ci.subresourceRange.baseMipLevel = 0;
978 image_view_ci.subresourceRange.levelCount = 1;
979 image_view_ci.subresourceRange.baseArrayLayer = 0;
980 image_view_ci.subresourceRange.layerCount = 1;
981 image_view_ci.viewType = VK_IMAGE_VIEW_TYPE_2D;
982 image_view_ci.image = VK_NULL_HANDLE; // filled in below
983 for (uint32_t i = 0; i < image_count; ++i) {
984 image_view_ci.image = context->vk.swapchain_images[i];
985 result = vkCreateImageView(context->vk.device, &image_view_ci,
986 context->vk.allocation_callbacks,
987 &context->vk.swapchain_image_views[i]);
Alex Vakulenko4fe60582017-02-02 11:35:59 -0800988 LOG_ALWAYS_FATAL_IF(result != VK_SUCCESS);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800989 }
990 // Fill in any requested output parameters.
991 for (auto p = parameters; p && p->key != DVR_SURFACE_PARAMETER_NONE; ++p) {
992 switch (p->key) {
993 case DVR_SURFACE_PARAMETER_VK_SWAPCHAIN_IMAGE_COUNT_OUT:
994 *static_cast<uint32_t*>(p->value_out) = image_count;
995 break;
996 case DVR_SURFACE_PARAMETER_VK_SWAPCHAIN_IMAGE_FORMAT_OUT:
997 *static_cast<VkFormat*>(p->value_out) = swapchain_ci.imageFormat;
998 break;
999 }
1000 }
1001 }
1002
1003 *return_graphics_context = context.release();
1004 return 0;
1005}
1006
1007void dvrGraphicsContextDestroy(DvrGraphicsContext* graphics_context) {
1008 delete graphics_context;
1009}
1010
1011// ANativeWindow function implementations. These should only be used
1012// by the Vulkan path.
1013int DvrGraphicsContext::Post(android::dvr::NativeBufferProducer* buffer,
1014 int fence_fd) {
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001015 LOG_ALWAYS_FATAL_IF(graphics_api != DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001016 ATRACE_NAME(__PRETTY_FUNCTION__);
1017 ALOGI_IF(TRACE, "DvrGraphicsContext::Post: buffer_id=%d, fence_fd=%d",
1018 buffer->buffer()->id(), fence_fd);
1019 ALOGW_IF(!display_surface->visible(),
1020 "DvrGraphicsContext::Post: Posting buffer on invisible surface!!!");
1021 // The NativeBufferProducer closes the fence fd, so dup it for tracking in the
1022 // frame history.
1023 frame_history.OnFrameSubmit(LocalHandle::AsDuplicate(fence_fd));
1024 int result = buffer->Post(fence_fd, 0);
1025 return result;
1026}
1027
1028int DvrGraphicsContext::SetSwapInterval(ANativeWindow* window, int interval) {
1029 ALOGI_IF(TRACE, "SetSwapInterval: window=%p interval=%d", window, interval);
1030 DvrGraphicsContext* self = getSelf(window);
1031 (void)self;
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001032 LOG_ALWAYS_FATAL_IF(self->graphics_api != DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001033 return android::NO_ERROR;
1034}
1035
1036int DvrGraphicsContext::DequeueBuffer(ANativeWindow* window,
1037 ANativeWindowBuffer** buffer,
1038 int* fence_fd) {
1039 ATRACE_NAME(__PRETTY_FUNCTION__);
1040
1041 DvrGraphicsContext* self = getSelf(window);
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001042 LOG_ALWAYS_FATAL_IF(self->graphics_api != DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001043 std::lock_guard<std::mutex> autolock(self->lock_);
1044
1045 if (!self->current_buffer) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001046 self->current_buffer = self->buffer_queue->Dequeue();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001047 }
1048 ATRACE_ASYNC_BEGIN("BufferDraw", self->current_buffer->buffer()->id());
1049 *fence_fd = self->current_buffer->ClaimReleaseFence().Release();
1050 *buffer = self->current_buffer;
1051
1052 ALOGI_IF(TRACE, "DvrGraphicsContext::DequeueBuffer: fence_fd=%d", *fence_fd);
1053 return android::NO_ERROR;
1054}
1055
1056int DvrGraphicsContext::QueueBuffer(ANativeWindow* window,
1057 ANativeWindowBuffer* buffer, int fence_fd) {
1058 ATRACE_NAME("NativeWindow::QueueBuffer");
1059 ALOGI_IF(TRACE, "NativeWindow::QueueBuffer: fence_fd=%d", fence_fd);
1060
1061 DvrGraphicsContext* self = getSelf(window);
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001062 LOG_ALWAYS_FATAL_IF(self->graphics_api != DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001063 std::lock_guard<std::mutex> autolock(self->lock_);
1064
1065 android::dvr::NativeBufferProducer* native_buffer =
1066 static_cast<android::dvr::NativeBufferProducer*>(buffer);
1067 ATRACE_ASYNC_END("BufferDraw", native_buffer->buffer()->id());
1068 bool do_post = true;
1069 if (self->buffer_already_posted) {
1070 // Check that the buffer is the one we expect, but handle it if this happens
1071 // in production by allowing this buffer to post on top of the previous one.
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001072 LOG_FATAL_IF(native_buffer != self->current_buffer);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001073 if (native_buffer == self->current_buffer) {
1074 do_post = false;
1075 if (fence_fd >= 0)
1076 close(fence_fd);
1077 }
1078 }
1079 if (do_post) {
1080 ATRACE_ASYNC_BEGIN("BufferPost", native_buffer->buffer()->id());
1081 self->Post(native_buffer, fence_fd);
1082 }
1083 self->buffer_already_posted = false;
1084 self->current_buffer = nullptr;
1085
1086 return android::NO_ERROR;
1087}
1088
1089int DvrGraphicsContext::CancelBuffer(ANativeWindow* window,
1090 ANativeWindowBuffer* buffer,
1091 int fence_fd) {
1092 ATRACE_NAME("DvrGraphicsContext::CancelBuffer");
1093 ALOGI_IF(TRACE, "DvrGraphicsContext::CancelBuffer: fence_fd: %d", fence_fd);
1094
1095 DvrGraphicsContext* self = getSelf(window);
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001096 LOG_ALWAYS_FATAL_IF(self->graphics_api != DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001097 std::lock_guard<std::mutex> autolock(self->lock_);
1098
1099 android::dvr::NativeBufferProducer* native_buffer =
1100 static_cast<android::dvr::NativeBufferProducer*>(buffer);
1101 ATRACE_ASYNC_END("BufferDraw", native_buffer->buffer()->id());
1102 ATRACE_INT("CancelBuffer", native_buffer->buffer()->id());
1103 bool do_enqueue = true;
1104 if (self->buffer_already_posted) {
1105 // Check that the buffer is the one we expect, but handle it if this happens
1106 // in production by returning this buffer to the buffer queue.
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001107 LOG_FATAL_IF(native_buffer != self->current_buffer);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001108 if (native_buffer == self->current_buffer) {
1109 do_enqueue = false;
1110 }
1111 }
1112 if (do_enqueue) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001113 self->buffer_queue->Enqueue(native_buffer);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001114 }
1115 if (fence_fd >= 0)
1116 close(fence_fd);
1117 self->buffer_already_posted = false;
1118 self->current_buffer = nullptr;
1119
1120 return android::NO_ERROR;
1121}
1122
1123int DvrGraphicsContext::Query(const ANativeWindow* window, int what,
1124 int* value) {
1125 DvrGraphicsContext* self = getSelf(const_cast<ANativeWindow*>(window));
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001126 LOG_ALWAYS_FATAL_IF(self->graphics_api != DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001127 std::lock_guard<std::mutex> autolock(self->lock_);
1128
1129 switch (what) {
1130 case NATIVE_WINDOW_WIDTH:
Corey Tabaka2251d822017-04-20 16:04:07 -07001131 *value = self->width;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001132 return android::NO_ERROR;
1133 case NATIVE_WINDOW_HEIGHT:
Corey Tabaka2251d822017-04-20 16:04:07 -07001134 *value = self->height;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001135 return android::NO_ERROR;
1136 case NATIVE_WINDOW_FORMAT:
Corey Tabaka2251d822017-04-20 16:04:07 -07001137 *value = self->format;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001138 return android::NO_ERROR;
1139 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1140 *value = 1;
1141 return android::NO_ERROR;
1142 case NATIVE_WINDOW_CONCRETE_TYPE:
1143 *value = NATIVE_WINDOW_SURFACE;
1144 return android::NO_ERROR;
1145 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
1146 *value = 1;
1147 return android::NO_ERROR;
1148 case NATIVE_WINDOW_DEFAULT_WIDTH:
Corey Tabaka2251d822017-04-20 16:04:07 -07001149 *value = self->width;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001150 return android::NO_ERROR;
1151 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Corey Tabaka2251d822017-04-20 16:04:07 -07001152 *value = self->height;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001153 return android::NO_ERROR;
1154 case NATIVE_WINDOW_TRANSFORM_HINT:
1155 *value = 0;
1156 return android::NO_ERROR;
1157 }
1158
1159 *value = 0;
1160 return android::BAD_VALUE;
1161}
1162
1163int DvrGraphicsContext::Perform(ANativeWindow* window, int operation, ...) {
1164 DvrGraphicsContext* self = getSelf(window);
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001165 LOG_ALWAYS_FATAL_IF(self->graphics_api != DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001166 std::lock_guard<std::mutex> autolock(self->lock_);
1167
1168 va_list args;
1169 va_start(args, operation);
1170
1171 // TODO(eieio): The following operations are not used at this time. They are
1172 // included here to help document which operations may be useful and what
1173 // parameters they take.
1174 switch (operation) {
1175 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: {
1176 int w = va_arg(args, int);
1177 int h = va_arg(args, int);
1178 ALOGD_IF(TRACE, "NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: w=%d h=%d", w, h);
1179 return android::NO_ERROR;
1180 }
1181
1182 case NATIVE_WINDOW_SET_BUFFERS_FORMAT: {
1183 int format = va_arg(args, int);
1184 ALOGD_IF(TRACE, "NATIVE_WINDOW_SET_BUFFERS_FORMAT: format=%d", format);
1185 return android::NO_ERROR;
1186 }
1187
1188 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: {
1189 int transform = va_arg(args, int);
1190 ALOGD_IF(TRACE, "NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: transform=%d",
1191 transform);
1192 return android::NO_ERROR;
1193 }
1194
1195 case NATIVE_WINDOW_SET_USAGE: {
1196 int usage = va_arg(args, int);
1197 ALOGD_IF(TRACE, "NATIVE_WINDOW_SET_USAGE: usage=%d", usage);
1198 return android::NO_ERROR;
1199 }
1200
1201 case NATIVE_WINDOW_CONNECT:
1202 case NATIVE_WINDOW_DISCONNECT:
1203 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
1204 case NATIVE_WINDOW_API_CONNECT:
1205 case NATIVE_WINDOW_API_DISCONNECT:
1206 // TODO(eieio): we should implement these
1207 return android::NO_ERROR;
1208
1209 case NATIVE_WINDOW_SET_BUFFER_COUNT: {
1210 int buffer_count = va_arg(args, int);
1211 ALOGD_IF(TRACE, "NATIVE_WINDOW_SET_BUFFER_COUNT: bufferCount=%d",
1212 buffer_count);
1213 return android::NO_ERROR;
1214 }
1215 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: {
1216 android_dataspace_t data_space =
1217 static_cast<android_dataspace_t>(va_arg(args, int));
1218 ALOGD_IF(TRACE, "NATIVE_WINDOW_SET_BUFFERS_DATASPACE: dataSpace=%d",
1219 data_space);
1220 return android::NO_ERROR;
1221 }
1222 case NATIVE_WINDOW_SET_SCALING_MODE: {
1223 int mode = va_arg(args, int);
1224 ALOGD_IF(TRACE, "NATIVE_WINDOW_SET_SCALING_MODE: mode=%d", mode);
1225 return android::NO_ERROR;
1226 }
1227
1228 case NATIVE_WINDOW_LOCK:
1229 case NATIVE_WINDOW_UNLOCK_AND_POST:
1230 case NATIVE_WINDOW_SET_CROP:
1231 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
1232 return android::INVALID_OPERATION;
1233 }
1234
1235 return android::NAME_NOT_FOUND;
1236}
1237
1238int DvrGraphicsContext::DequeueBuffer_DEPRECATED(ANativeWindow* window,
1239 ANativeWindowBuffer** buffer) {
1240 int fence_fd = -1;
1241 int ret = DequeueBuffer(window, buffer, &fence_fd);
1242
1243 // wait for fence
1244 if (ret == android::NO_ERROR && fence_fd != -1)
1245 close(fence_fd);
1246
1247 return ret;
1248}
1249
1250int DvrGraphicsContext::CancelBuffer_DEPRECATED(ANativeWindow* window,
1251 ANativeWindowBuffer* buffer) {
1252 return CancelBuffer(window, buffer, -1);
1253}
1254
1255int DvrGraphicsContext::QueueBuffer_DEPRECATED(ANativeWindow* window,
1256 ANativeWindowBuffer* buffer) {
1257 return QueueBuffer(window, buffer, -1);
1258}
1259
1260int DvrGraphicsContext::LockBuffer_DEPRECATED(ANativeWindow* /*window*/,
1261 ANativeWindowBuffer* /*buffer*/) {
1262 return android::NO_ERROR;
1263}
1264// End ANativeWindow implementation
1265
1266int dvrSetEdsPose(DvrGraphicsContext* graphics_context,
1267 float32x4_t render_pose_orientation,
1268 float32x4_t render_pose_translation) {
1269 ATRACE_NAME("dvrSetEdsPose");
1270 if (!graphics_context->current_buffer) {
1271 ALOGE("dvrBeginRenderFrame must be called before dvrSetEdsPose");
1272 return -EPERM;
1273 }
1274
1275 // When late-latching is enabled, the pose buffer is written by the GPU, so
1276 // we don't touch it here.
1277 float32x4_t is_late_latch = DVR_POSE_LATE_LATCH;
1278 if (render_pose_orientation[0] != is_late_latch[0]) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001279 volatile DisplaySurfaceMetadata* data = graphics_context->surface_metadata;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001280 uint32_t buffer_index =
1281 graphics_context->current_buffer->surface_buffer_index();
1282 ALOGE_IF(TRACE, "write pose index %d %f %f", buffer_index,
1283 render_pose_orientation[0], render_pose_orientation[1]);
1284 data->orientation[buffer_index] = render_pose_orientation;
1285 data->translation[buffer_index] = render_pose_translation;
1286 }
1287
1288 return 0;
1289}
1290
1291int dvrBeginRenderFrameEds(DvrGraphicsContext* graphics_context,
1292 float32x4_t render_pose_orientation,
1293 float32x4_t render_pose_translation) {
1294 ATRACE_NAME("dvrBeginRenderFrameEds");
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001295 LOG_ALWAYS_FATAL_IF(graphics_context->graphics_api != DVR_GRAPHICS_API_GLES);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001296 CHECK_GL();
1297 // Grab a buffer from the queue and set its pose.
1298 if (!graphics_context->current_buffer) {
1299 graphics_context->current_buffer =
1300 graphics_context->buffer_queue->Dequeue();
1301 }
1302
1303 int ret = dvrSetEdsPose(graphics_context, render_pose_orientation,
1304 render_pose_translation);
1305 if (ret < 0)
1306 return ret;
1307
1308 ATRACE_ASYNC_BEGIN("BufferDraw",
1309 graphics_context->current_buffer->buffer()->id());
1310
1311 {
1312 ATRACE_NAME("glEGLImageTargetTexture2DOES");
1313 // Bind the texture to the latest buffer in the queue.
1314 for (int i = 0; i < graphics_context->gl.texture_count; ++i) {
1315 glBindTexture(graphics_context->gl.texture_target_type,
1316 graphics_context->gl.texture_id[i]);
1317 glEGLImageTargetTexture2DOES(
1318 graphics_context->gl.texture_target_type,
1319 graphics_context->current_buffer->image_khr(i));
1320 }
1321 glBindTexture(graphics_context->gl.texture_target_type, 0);
1322 }
1323 CHECK_GL();
1324 return 0;
1325}
Corey Tabaka2251d822017-04-20 16:04:07 -07001326
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001327int dvrBeginRenderFrameEdsVk(DvrGraphicsContext* graphics_context,
1328 float32x4_t render_pose_orientation,
1329 float32x4_t render_pose_translation,
1330 VkSemaphore acquire_semaphore,
1331 VkFence acquire_fence,
1332 uint32_t* swapchain_image_index,
1333 VkImageView* swapchain_image_view) {
1334 ATRACE_NAME("dvrBeginRenderFrameEds");
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001335 LOG_ALWAYS_FATAL_IF(graphics_context->graphics_api !=
1336 DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001337
1338 // Acquire a swapchain image. This calls Dequeue() internally.
1339 VkResult result = vkAcquireNextImageKHR(
1340 graphics_context->vk.device, graphics_context->vk.swapchain, UINT64_MAX,
1341 acquire_semaphore, acquire_fence, swapchain_image_index);
1342 if (result != VK_SUCCESS)
1343 return -EINVAL;
1344
1345 // Set the pose pose.
1346 int ret = dvrSetEdsPose(graphics_context, render_pose_orientation,
1347 render_pose_translation);
1348 if (ret < 0)
1349 return ret;
1350 *swapchain_image_view =
1351 graphics_context->vk.swapchain_image_views[*swapchain_image_index];
1352 return 0;
1353}
1354
1355int dvrBeginRenderFrame(DvrGraphicsContext* graphics_context) {
1356 return dvrBeginRenderFrameEds(graphics_context, DVR_POSE_NO_EDS,
1357 DVR_POSE_NO_EDS);
1358}
1359int dvrBeginRenderFrameVk(DvrGraphicsContext* graphics_context,
1360 VkSemaphore acquire_semaphore, VkFence acquire_fence,
1361 uint32_t* swapchain_image_index,
1362 VkImageView* swapchain_image_view) {
1363 return dvrBeginRenderFrameEdsVk(
1364 graphics_context, DVR_POSE_NO_EDS, DVR_POSE_NO_EDS, acquire_semaphore,
1365 acquire_fence, swapchain_image_index, swapchain_image_view);
1366}
1367
1368int dvrBeginRenderFrameLateLatch(DvrGraphicsContext* graphics_context,
1369 uint32_t /*flags*/,
1370 uint32_t target_vsync_count, int num_views,
1371 const float** projection_matrices,
1372 const float** eye_from_head_matrices,
1373 const float** pose_offset_matrices,
1374 uint32_t* out_late_latch_buffer_id) {
1375 if (!graphics_context->late_latch) {
1376 return -EPERM;
1377 }
1378 if (num_views > DVR_GRAPHICS_SURFACE_MAX_VIEWS) {
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001379 ALOGE("dvrBeginRenderFrameLateLatch called with too many views.");
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001380 return -EINVAL;
1381 }
1382 dvrBeginRenderFrameEds(graphics_context, DVR_POSE_LATE_LATCH,
1383 DVR_POSE_LATE_LATCH);
1384 auto& ll = graphics_context->late_latch;
1385 // TODO(jbates) Need to change this shader so that it dumps the single
1386 // captured pose for both eyes into the display surface metadata buffer at
1387 // the right index.
1388 android::dvr::LateLatchInput input;
1389 memset(&input, 0, sizeof(input));
1390 for (int i = 0; i < num_views; ++i) {
1391 memcpy(input.proj_mat + i, *(projection_matrices + i), 16 * sizeof(float));
1392 memcpy(input.eye_from_head_mat + i, *(eye_from_head_matrices + i),
1393 16 * sizeof(float));
1394 memcpy(input.pose_offset + i, *(pose_offset_matrices + i),
1395 16 * sizeof(float));
1396 }
1397 input.pose_index =
1398 target_vsync_count & android::dvr::kPoseAsyncBufferIndexMask;
1399 input.render_pose_index =
1400 graphics_context->current_buffer->surface_buffer_index();
1401 ll->AddLateLatch(input);
1402 *out_late_latch_buffer_id = ll->output_buffer_id();
1403 return 0;
1404}
1405
1406extern "C" int dvrGraphicsWaitNextFrame(
1407 DvrGraphicsContext* graphics_context, int64_t start_delay_ns,
1408 DvrFrameSchedule* out_next_frame_schedule) {
1409 start_delay_ns = std::max(start_delay_ns, static_cast<int64_t>(0));
1410
1411 // We only do one-shot timers:
1412 int64_t wake_time_ns = 0;
1413
1414 uint32_t current_frame_vsync;
1415 int64_t current_frame_scheduled_finish_ns;
1416 int64_t vsync_period_ns;
1417
1418 int fetch_schedule_result = graphics_context->vsync_client->GetSchedInfo(
1419 &vsync_period_ns, &current_frame_scheduled_finish_ns,
1420 &current_frame_vsync);
1421 if (fetch_schedule_result == 0) {
1422 wake_time_ns = current_frame_scheduled_finish_ns + start_delay_ns;
1423 // If the last wakeup time is still in the future, use it instead to avoid
1424 // major schedule jumps when applications call WaitNextFrame with
1425 // aggressive offsets.
1426 int64_t now = android::dvr::GetSystemClockNs();
1427 if (android::dvr::TimestampGT(wake_time_ns - vsync_period_ns, now)) {
1428 wake_time_ns -= vsync_period_ns;
1429 --current_frame_vsync;
1430 }
1431 // If the next wakeup time is in the past, add a vsync period to keep the
1432 // application on schedule.
1433 if (android::dvr::TimestampLT(wake_time_ns, now)) {
1434 wake_time_ns += vsync_period_ns;
1435 ++current_frame_vsync;
1436 }
1437 } else {
1438 ALOGE("Error getting frame schedule because: %s",
1439 strerror(-fetch_schedule_result));
1440 // Sleep for a vsync period to avoid cascading failure.
1441 wake_time_ns = android::dvr::GetSystemClockNs() +
1442 graphics_context->display_metrics.vsync_period_ns;
1443 }
1444
1445 // Adjust nsec to [0..999,999,999].
1446 struct itimerspec wake_time;
1447 wake_time.it_interval.tv_sec = 0;
1448 wake_time.it_interval.tv_nsec = 0;
1449 wake_time.it_value = android::dvr::NsToTimespec(wake_time_ns);
1450 bool sleep_result =
1451 timerfd_settime(graphics_context->timerfd.Get(), TFD_TIMER_ABSTIME,
1452 &wake_time, nullptr) == 0;
1453 if (sleep_result) {
1454 ATRACE_NAME("sleep");
1455 uint64_t expirations = 0;
1456 sleep_result = read(graphics_context->timerfd.Get(), &expirations,
1457 sizeof(uint64_t)) == sizeof(uint64_t);
1458 if (!sleep_result) {
1459 ALOGE("Error: timerfd read failed");
1460 }
1461 } else {
1462 ALOGE("Error: timerfd_settime failed because: %s", strerror(errno));
1463 }
1464
1465 auto& frame_history = graphics_context->frame_history;
1466 frame_history.CheckForFinishedFrames();
1467 if (fetch_schedule_result == 0) {
1468 uint32_t next_frame_vsync =
1469 current_frame_vsync +
1470 frame_history.PredictNextFrameVsyncInterval(vsync_period_ns);
1471 int64_t next_frame_scheduled_finish =
1472 (wake_time_ns - start_delay_ns) + vsync_period_ns;
1473 frame_history.OnFrameStart(next_frame_vsync, next_frame_scheduled_finish);
1474 if (out_next_frame_schedule) {
1475 out_next_frame_schedule->vsync_count = next_frame_vsync;
1476 out_next_frame_schedule->scheduled_frame_finish_ns =
1477 next_frame_scheduled_finish;
1478 }
1479 } else {
1480 frame_history.OnFrameStart(UINT32_MAX, -1);
1481 }
1482
1483 return (fetch_schedule_result == 0 && sleep_result) ? 0 : -1;
1484}
1485
1486extern "C" void dvrGraphicsPostEarly(DvrGraphicsContext* graphics_context) {
1487 ATRACE_NAME("dvrGraphicsPostEarly");
1488 ALOGI_IF(TRACE, "dvrGraphicsPostEarly");
1489
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001490 LOG_ALWAYS_FATAL_IF(graphics_context->graphics_api != DVR_GRAPHICS_API_GLES);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001491
1492 // Note that this function can be called before or after
1493 // dvrBeginRenderFrame.
1494 if (!graphics_context->buffer_already_posted) {
1495 graphics_context->buffer_already_posted = true;
1496
1497 if (!graphics_context->current_buffer) {
1498 graphics_context->current_buffer =
1499 graphics_context->buffer_queue->Dequeue();
1500 }
1501
1502 auto buffer = graphics_context->current_buffer->buffer().get();
1503 ATRACE_ASYNC_BEGIN("BufferPost", buffer->id());
Corey Tabaka2251d822017-04-20 16:04:07 -07001504 int result = buffer->Post<void>(LocalHandle());
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001505 if (result < 0)
1506 ALOGE("Buffer post failed: %d (%s)", result, strerror(-result));
1507 }
1508}
1509
1510int dvrPresent(DvrGraphicsContext* graphics_context) {
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001511 LOG_ALWAYS_FATAL_IF(graphics_context->graphics_api != DVR_GRAPHICS_API_GLES);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001512
1513 std::array<char, 128> buf;
1514 snprintf(buf.data(), buf.size(), "dvrPresent|vsync=%d|",
1515 graphics_context->frame_history.GetCurrentFrameVsync());
1516 ATRACE_NAME(buf.data());
1517
1518 if (!graphics_context->current_buffer) {
1519 ALOGE("Error: dvrPresent called without dvrBeginRenderFrame");
1520 return -EPERM;
1521 }
1522
1523 LocalHandle fence_fd =
1524 android::dvr::CreateGLSyncAndFlush(graphics_context->gl.egl_display);
1525
1526 ALOGI_IF(TRACE, "PostBuffer: buffer_id=%d, fence_fd=%d",
1527 graphics_context->current_buffer->buffer()->id(), fence_fd.Get());
1528 ALOGW_IF(!graphics_context->display_surface->visible(),
1529 "PostBuffer: Posting buffer on invisible surface!!!");
1530
1531 auto buffer = graphics_context->current_buffer->buffer().get();
1532 ATRACE_ASYNC_END("BufferDraw", buffer->id());
1533 if (!graphics_context->buffer_already_posted) {
1534 ATRACE_ASYNC_BEGIN("BufferPost", buffer->id());
Corey Tabaka2251d822017-04-20 16:04:07 -07001535 int result = buffer->Post<void>(fence_fd);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001536 if (result < 0)
1537 ALOGE("Buffer post failed: %d (%s)", result, strerror(-result));
1538 }
1539
1540 graphics_context->frame_history.OnFrameSubmit(std::move(fence_fd));
1541 graphics_context->buffer_already_posted = false;
1542 graphics_context->current_buffer = nullptr;
1543 return 0;
1544}
1545
1546int dvrPresentVk(DvrGraphicsContext* graphics_context,
1547 VkSemaphore submit_semaphore, uint32_t swapchain_image_index) {
Alex Vakulenko4fe60582017-02-02 11:35:59 -08001548 LOG_ALWAYS_FATAL_IF(graphics_context->graphics_api !=
1549 DVR_GRAPHICS_API_VULKAN);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001550
1551 std::array<char, 128> buf;
1552 snprintf(buf.data(), buf.size(), "dvrPresent|vsync=%d|",
1553 graphics_context->frame_history.GetCurrentFrameVsync());
1554 ATRACE_NAME(buf.data());
1555
1556 if (!graphics_context->current_buffer) {
1557 ALOGE("Error: dvrPresentVk called without dvrBeginRenderFrameVk");
1558 return -EPERM;
1559 }
1560
1561 // Present the specified image. Internally, this gets a fence from the
1562 // Vulkan driver and passes it to DvrGraphicsContext::Post(),
1563 // which in turn passes it to buffer->Post() and adds it to frame_history.
1564 VkPresentInfoKHR present_info = {};
1565 present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
1566 present_info.swapchainCount = 1;
1567 present_info.pSwapchains = &graphics_context->vk.swapchain;
1568 present_info.pImageIndices = &swapchain_image_index;
1569 present_info.waitSemaphoreCount =
1570 (submit_semaphore != VK_NULL_HANDLE) ? 1 : 0;
1571 present_info.pWaitSemaphores = &submit_semaphore;
1572 VkResult result =
1573 vkQueuePresentKHR(graphics_context->vk.present_queue, &present_info);
1574 if (result != VK_SUCCESS) {
1575 return -EINVAL;
1576 }
1577
1578 return 0;
1579}
1580
1581extern "C" int dvrGetFrameScheduleResults(DvrGraphicsContext* context,
1582 DvrFrameScheduleResult* results,
1583 int in_result_count) {
1584 if (!context || !results)
1585 return -EINVAL;
1586
1587 return context->frame_history.GetPreviousFrameResults(results,
1588 in_result_count);
1589}
1590
1591extern "C" void dvrGraphicsSurfaceSetVisible(
1592 DvrGraphicsContext* graphics_context, int visible) {
1593 graphics_context->display_surface->SetVisible(visible);
1594}
1595
1596extern "C" int dvrGraphicsSurfaceGetVisible(
1597 DvrGraphicsContext* graphics_context) {
Corey Tabaka2251d822017-04-20 16:04:07 -07001598 return !!graphics_context->display_surface->visible();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001599}
1600
1601extern "C" void dvrGraphicsSurfaceSetZOrder(
1602 DvrGraphicsContext* graphics_context, int z_order) {
1603 graphics_context->display_surface->SetZOrder(z_order);
1604}
1605
1606extern "C" int dvrGraphicsSurfaceGetZOrder(
1607 DvrGraphicsContext* graphics_context) {
1608 return graphics_context->display_surface->z_order();
1609}