vsync.cpp 2.5 KB

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