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