Fence.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2012 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. #ifndef ANDROID_FENCE_H
  17. #define ANDROID_FENCE_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <ui/ANativeObjectBase.h>
  21. #include <ui/PixelFormat.h>
  22. #include <ui/Rect.h>
  23. #include <utils/Flattenable.h>
  24. #include <utils/String8.h>
  25. #include <utils/Timers.h>
  26. struct ANativeWindowBuffer;
  27. namespace android {
  28. // ===========================================================================
  29. // Fence
  30. // ===========================================================================
  31. class Fence
  32. : public LightRefBase<Fence>, public Flattenable<Fence>
  33. {
  34. public:
  35. static const sp<Fence> NO_FENCE;
  36. // TIMEOUT_NEVER may be passed to the wait method to indicate that it
  37. // should wait indefinitely for the fence to signal.
  38. enum { TIMEOUT_NEVER = -1 };
  39. // Construct a new Fence object with an invalid file descriptor. This
  40. // should be done when the Fence object will be set up by unflattening
  41. // serialized data.
  42. Fence();
  43. // Construct a new Fence object to manage a given fence file descriptor.
  44. // When the new Fence object is destructed the file descriptor will be
  45. // closed.
  46. Fence(int fenceFd);
  47. // Check whether the Fence has an open fence file descriptor. Most Fence
  48. // methods treat an invalid file descriptor just like a valid fence that
  49. // is already signalled, so using this is usually not necessary.
  50. bool isValid() const { return mFenceFd != -1; }
  51. // wait waits for up to timeout milliseconds for the fence to signal. If
  52. // the fence signals then NO_ERROR is returned. If the timeout expires
  53. // before the fence signals then -ETIME is returned. A timeout of
  54. // TIMEOUT_NEVER may be used to indicate that the call should wait
  55. // indefinitely for the fence to signal.
  56. status_t wait(int timeout);
  57. // waitForever is a convenience function for waiting forever for a fence to
  58. // signal (just like wait(TIMEOUT_NEVER)), but issuing an error to the
  59. // system log and fence state to the kernel log if the wait lasts longer
  60. // than a warning timeout.
  61. // The logname argument should be a string identifying
  62. // the caller and will be included in the log message.
  63. status_t waitForever(const char* logname);
  64. // merge combines two Fence objects, creating a new Fence object that
  65. // becomes signaled when both f1 and f2 are signaled (even if f1 or f2 is
  66. // destroyed before it becomes signaled). The name argument specifies the
  67. // human-readable name to associated with the new Fence object.
  68. static sp<Fence> merge(const String8& name, const sp<Fence>& f1,
  69. const sp<Fence>& f2);
  70. // Return a duplicate of the fence file descriptor. The caller is
  71. // responsible for closing the returned file descriptor. On error, -1 will
  72. // be returned and errno will indicate the problem.
  73. int dup() const;
  74. // getSignalTime returns the system monotonic clock time at which the
  75. // fence transitioned to the signaled state. If the fence is not signaled
  76. // then INT64_MAX is returned. If the fence is invalid or if an error
  77. // occurs then -1 is returned.
  78. nsecs_t getSignalTime() const;
  79. // Flattenable interface
  80. size_t getFlattenedSize() const;
  81. size_t getFdCount() const;
  82. status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
  83. status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
  84. private:
  85. // Only allow instantiation using ref counting.
  86. friend class LightRefBase<Fence>;
  87. ~Fence();
  88. // Disallow copying
  89. Fence(const Fence& rhs);
  90. Fence& operator = (const Fence& rhs);
  91. const Fence& operator = (const Fence& rhs) const;
  92. int mFenceFd;
  93. };
  94. }; // namespace android
  95. #endif // ANDROID_FENCE_H