1 /*
2 * Copyright (C) 2010 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
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 #include <android/looper.h>
22 #include <gui/DisplayEventReceiver.h>
23 #include <utils/Looper.h>
24
25 using namespace android;
26
receiver(int,int,void * data)27 int receiver(int /*fd*/, int /*events*/, void* data)
28 {
29 DisplayEventReceiver* q = (DisplayEventReceiver*)data;
30
31 ssize_t n;
32 DisplayEventReceiver::Event buffer[1];
33
34 static nsecs_t oldTimeStamp = 0;
35
36 while ((n = q->getEvents(buffer, 1)) > 0) {
37 for (int i=0 ; i<n ; i++) {
38 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
39 printf("event vsync: count=%d\t", buffer[i].vsync.count);
40 }
41 if (oldTimeStamp) {
42 float t = float(buffer[i].header.timestamp - oldTimeStamp) / s2ns(1);
43 printf("%f ms (%f Hz)\n", t*1000, 1.0/t);
44 }
45 oldTimeStamp = buffer[i].header.timestamp;
46 }
47 }
48 if (n<0) {
49 printf("error reading events (%s)\n", strerror(-n));
50 }
51 return 1;
52 }
53
main(int,char **)54 int main(int /*argc*/, char** /*argv*/)
55 {
56 DisplayEventReceiver myDisplayEvent;
57
58
59 sp<Looper> loop = new Looper(false);
60 loop->addFd(myDisplayEvent.getFd(), 0, ALOOPER_EVENT_INPUT, receiver,
61 &myDisplayEvent);
62
63 myDisplayEvent.setVsyncRate(1);
64
65 do {
66 //printf("about to poll...\n");
67 int32_t ret = loop->pollOnce(-1);
68 switch (ret) {
69 case ALOOPER_POLL_WAKE:
70 //("ALOOPER_POLL_WAKE\n");
71 break;
72 case ALOOPER_POLL_CALLBACK:
73 //("ALOOPER_POLL_CALLBACK\n");
74 break;
75 case ALOOPER_POLL_TIMEOUT:
76 printf("ALOOPER_POLL_TIMEOUT\n");
77 break;
78 case ALOOPER_POLL_ERROR:
79 printf("ALOOPER_POLL_TIMEOUT\n");
80 break;
81 default:
82 printf("ugh? poll returned %d\n", ret);
83 break;
84 }
85 } while (1);
86
87 return 0;
88 }
89
90 // TODO(b/129481165): remove the #pragma below and fix conversion issues
91 #pragma clang diagnostic pop // ignored "-Wconversion"
92