Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1 | #include <EGL/egl.h> |
| 2 | #include <GLES2/gl2.h> |
| 3 | #include <math.h> |
| 4 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 5 | #include <dvr/graphics.h> |
| 6 | #include <dvr/pose_client.h> |
| 7 | #include <gtest/gtest.h> |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame^] | 8 | #include <log/log.h> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 9 | #include <private/dvr/types.h> |
| 10 | |
| 11 | using android::dvr::vec4; |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | vec4 ToVec4(float32x4_t rhs) { return vec4(rhs[0], rhs[1], rhs[2], rhs[3]); } |
| 16 | |
| 17 | } |
| 18 | |
| 19 | DvrGraphicsContext* CreateContext() { |
| 20 | DvrGraphicsContext* context = nullptr; |
| 21 | int display_width = 0, display_height = 0; |
| 22 | int surface_width = 0, surface_height = 0; |
| 23 | float inter_lens_meters = 0.0f; |
| 24 | float left_fov[4] = {0.0f}; |
| 25 | float right_fov[4] = {0.0f}; |
| 26 | int disable_warp = 0; |
| 27 | DvrSurfaceParameter surface_params[] = { |
| 28 | DVR_SURFACE_PARAMETER_IN(DISABLE_DISTORTION, disable_warp), |
| 29 | DVR_SURFACE_PARAMETER_OUT(DISPLAY_WIDTH, &display_width), |
| 30 | DVR_SURFACE_PARAMETER_OUT(DISPLAY_HEIGHT, &display_height), |
| 31 | DVR_SURFACE_PARAMETER_OUT(SURFACE_WIDTH, &surface_width), |
| 32 | DVR_SURFACE_PARAMETER_OUT(SURFACE_HEIGHT, &surface_height), |
| 33 | DVR_SURFACE_PARAMETER_OUT(INTER_LENS_METERS, &inter_lens_meters), |
| 34 | DVR_SURFACE_PARAMETER_OUT(LEFT_FOV_LRBT, left_fov), |
| 35 | DVR_SURFACE_PARAMETER_OUT(RIGHT_FOV_LRBT, right_fov), |
| 36 | DVR_SURFACE_PARAMETER_LIST_END, |
| 37 | }; |
| 38 | dvrGraphicsContextCreate(surface_params, &context); |
| 39 | return context; |
| 40 | } |
| 41 | |
| 42 | TEST(SensorAppTests, GetPose) { |
| 43 | DvrGraphicsContext* context = CreateContext(); |
| 44 | ASSERT_NE(nullptr, context); |
| 45 | DvrPose* client = dvrPoseCreate(); |
| 46 | ASSERT_NE(nullptr, client); |
| 47 | |
| 48 | DvrPoseAsync last_pose; |
| 49 | uint32_t last_vsync_count = 0; |
| 50 | for (int i = 0; i < 10; ++i) { |
| 51 | DvrFrameSchedule schedule; |
| 52 | dvrGraphicsWaitNextFrame(context, 0, &schedule); |
| 53 | DvrPoseAsync pose; |
| 54 | int ret = dvrPoseGet(client, schedule.vsync_count, &pose); |
| 55 | ASSERT_EQ(0, ret); |
| 56 | |
| 57 | // Check for unit-length quaternion to verify valid pose. |
| 58 | vec4 quaternion = ToVec4(pose.orientation); |
| 59 | float length = quaternion.norm(); |
| 60 | EXPECT_GT(0.001, fabs(1.0f - length)); |
| 61 | |
| 62 | // Check for different data each frame, but skip first few to allow |
| 63 | // startup anomalies. |
| 64 | if (i > 0) { |
| 65 | if (last_vsync_count == schedule.vsync_count) |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame^] | 66 | ALOGE("vsync did not increment: %u", schedule.vsync_count); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 67 | if (pose.timestamp_ns == last_pose.timestamp_ns) |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame^] | 68 | ALOGE("timestamp did not change: %" PRIu64, pose.timestamp_ns); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 69 | // TODO(jbates) figure out why the bots are not passing this check. |
| 70 | // EXPECT_NE(last_vsync_count, schedule.vsync_count); |
| 71 | // EXPECT_NE(pose.timestamp_ns, last_pose.timestamp_ns); |
| 72 | } |
| 73 | last_pose = pose; |
| 74 | last_vsync_count = schedule.vsync_count; |
| 75 | dvrBeginRenderFrame(context); |
| 76 | glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 77 | dvrPresent(context); |
| 78 | } |
| 79 | |
| 80 | dvrPoseDestroy(client); |
| 81 | dvrGraphicsContextDestroy(context); |
| 82 | } |
| 83 | |
| 84 | TEST(SensorAppTests, PoseRingBuffer) { |
| 85 | DvrGraphicsContext* context = CreateContext(); |
| 86 | ASSERT_NE(nullptr, context); |
| 87 | DvrPose* client = dvrPoseCreate(); |
| 88 | ASSERT_NE(nullptr, client); |
| 89 | |
| 90 | DvrPoseRingBufferInfo info; |
| 91 | int ret = dvrPoseGetRingBuffer(client, &info); |
| 92 | ASSERT_EQ(0, ret); |
| 93 | ASSERT_NE(nullptr, info.buffer); |
| 94 | EXPECT_LE(2u, info.min_future_count); |
| 95 | EXPECT_LE(8u, info.total_count); |
| 96 | |
| 97 | DvrPoseAsync last_pose; |
| 98 | uint32_t last_vsync_count = 0; |
| 99 | for (int i = 0; i < 10; ++i) { |
| 100 | DvrFrameSchedule schedule; |
| 101 | dvrGraphicsWaitNextFrame(context, 0, &schedule); |
| 102 | DvrPoseAsync pose; |
| 103 | ret = dvrPoseGet(client, schedule.vsync_count, &pose); |
| 104 | ASSERT_EQ(0, ret); |
| 105 | |
| 106 | // Check for unit-length quaternion to verify valid pose. |
| 107 | vec4 quaternion = ToVec4(pose.orientation); |
| 108 | float length = quaternion.norm(); |
| 109 | EXPECT_GT(0.001, fabs(1.0f - length)); |
| 110 | |
| 111 | // Check for different data each frame, but skip first few to allow |
| 112 | // startup anomalies. |
| 113 | if (i > 0) { |
| 114 | if (last_vsync_count == schedule.vsync_count) |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame^] | 115 | ALOGE("vsync did not increment: %u", schedule.vsync_count); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 116 | if (pose.timestamp_ns == last_pose.timestamp_ns) |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame^] | 117 | ALOGE("timestamp did not change: %" PRIu64, pose.timestamp_ns); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 118 | // TODO(jbates) figure out why the bots are not passing this check. |
| 119 | // EXPECT_NE(last_vsync_count, schedule.vsync_count); |
| 120 | // EXPECT_NE(pose.timestamp_ns, last_pose.timestamp_ns); |
| 121 | } |
| 122 | last_pose = pose; |
| 123 | last_vsync_count = schedule.vsync_count; |
| 124 | dvrBeginRenderFrame(context); |
| 125 | glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 126 | dvrPresent(context); |
| 127 | } |
| 128 | |
| 129 | dvrPoseDestroy(client); |
| 130 | dvrGraphicsContextDestroy(context); |
| 131 | } |