xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/tests/vsync/vsync.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 // This file is included by modules that have host support but android/looper.h is not supported
22 // on host. __REMOVED_IN needs to be defined in order for android/looper.h to be compiled.
23 #ifndef __BIONIC__
24 #define __REMOVED_IN(x) __attribute__((deprecated))
25 #endif
26 #include <android/looper.h>
27 
28 #include <gui/DisplayEventReceiver.h>
29 #include <utils/Looper.h>
30 
31 using namespace android;
32 
receiver(int,int,void * data)33 int receiver(int /*fd*/, int /*events*/, void* data)
34 {
35     DisplayEventReceiver* q = (DisplayEventReceiver*)data;
36 
37     ssize_t n;
38     DisplayEventReceiver::Event buffer[1];
39 
40     static nsecs_t oldTimeStamp = 0;
41 
42     while ((n = q->getEvents(buffer, 1)) > 0) {
43         for (int i=0 ; i<n ; i++) {
44             if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
45                 printf("event vsync: count=%d\t", buffer[i].vsync.count);
46             }
47             if (oldTimeStamp) {
48                 float t = float(buffer[i].header.timestamp - oldTimeStamp) / s2ns(1);
49                 printf("%f ms (%f Hz)\n", t*1000, 1.0/t);
50             }
51             oldTimeStamp = buffer[i].header.timestamp;
52         }
53     }
54     if (n<0) {
55         printf("error reading events (%s)\n", strerror(-n));
56     }
57     return 1;
58 }
59 
main(int,char **)60 int main(int /*argc*/, char** /*argv*/)
61 {
62     DisplayEventReceiver myDisplayEvent;
63 
64     sp<Looper> loop = sp<Looper>::make(false);
65     loop->addFd(myDisplayEvent.getFd(), 0, ALOOPER_EVENT_INPUT, receiver,
66             &myDisplayEvent);
67 
68     myDisplayEvent.setVsyncRate(1);
69 
70     do {
71         //printf("about to poll...\n");
72         int32_t ret = loop->pollOnce(-1);
73         switch (ret) {
74             case ALOOPER_POLL_WAKE:
75                 //("ALOOPER_POLL_WAKE\n");
76                 break;
77             case ALOOPER_POLL_CALLBACK:
78                 //("ALOOPER_POLL_CALLBACK\n");
79                 break;
80             case ALOOPER_POLL_TIMEOUT:
81                 printf("ALOOPER_POLL_TIMEOUT\n");
82                 break;
83             case ALOOPER_POLL_ERROR:
84                 printf("ALOOPER_POLL_TIMEOUT\n");
85                 break;
86             default:
87                 printf("ugh? poll returned %d\n", ret);
88                 break;
89         }
90     } while (1);
91 
92     return 0;
93 }
94 
95 // TODO(b/129481165): remove the #pragma below and fix conversion issues
96 #pragma clang diagnostic pop // ignored "-Wconversion"
97