blob: 81d5d09826daad87ace43686e80de03cba403e35 [file] [log] [blame]
Jamie Gennisf25e1832012-06-13 16:31:43 -07001/*
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
17#ifndef ANDROID_FENCE_H
18#define ANDROID_FENCE_H
19
20#include <stdint.h>
Jamie Gennisf25e1832012-06-13 16:31:43 -070021#include <sys/types.h>
22
23#include <ui/ANativeObjectBase.h>
24#include <ui/PixelFormat.h>
25#include <ui/Rect.h>
26#include <utils/Flattenable.h>
27#include <utils/String8.h>
Jamie Gennis82dbc742012-11-08 19:23:28 -080028#include <utils/Timers.h>
Jamie Gennisf25e1832012-06-13 16:31:43 -070029
30struct ANativeWindowBuffer;
31
32namespace android {
33
34// ===========================================================================
35// Fence
36// ===========================================================================
37
38class Fence
39 : public LightRefBase<Fence>, public Flattenable
40{
41public:
Jesse Hallef194142012-06-14 14:45:17 -070042 static const sp<Fence> NO_FENCE;
Jamie Gennisf25e1832012-06-13 16:31:43 -070043
Jamie Gennis82dbc742012-11-08 19:23:28 -080044 // TIMEOUT_NEVER may be passed to the wait method to indicate that it
45 // should wait indefinitely for the fence to signal.
46 enum { TIMEOUT_NEVER = -1 };
47
Jamie Gennisf25e1832012-06-13 16:31:43 -070048 // Construct a new Fence object with an invalid file descriptor. This
49 // should be done when the Fence object will be set up by unflattening
50 // serialized data.
51 Fence();
52
53 // Construct a new Fence object to manage a given fence file descriptor.
54 // When the new Fence object is destructed the file descriptor will be
55 // closed.
56 Fence(int fenceFd);
57
Jesse Hallc777b0b2012-06-28 12:52:05 -070058 // Check whether the Fence has an open fence file descriptor. Most Fence
59 // methods treat an invalid file descriptor just like a valid fence that
60 // is already signalled, so using this is usually not necessary.
61 bool isValid() const { return mFenceFd != -1; }
62
Jamie Gennisf25e1832012-06-13 16:31:43 -070063 // wait waits for up to timeout milliseconds for the fence to signal. If
64 // the fence signals then NO_ERROR is returned. If the timeout expires
65 // before the fence signals then -ETIME is returned. A timeout of
66 // TIMEOUT_NEVER may be used to indicate that the call should wait
67 // indefinitely for the fence to signal.
Mathias Agopianb5c9dcd2012-10-09 14:38:19 -070068 status_t wait(unsigned int timeout);
Jamie Gennisf25e1832012-06-13 16:31:43 -070069
Jesse Hallba607d52012-10-01 14:05:20 -070070 // waitForever is a convenience function for waiting forever for a fence to
71 // signal (just like wait(TIMEOUT_NEVER)), but issuing an error to the
72 // system log and fence state to the kernel log if the wait lasts longer
73 // than warningTimeout. The logname argument should be a string identifying
74 // the caller and will be included in the log message.
Mathias Agopianb5c9dcd2012-10-09 14:38:19 -070075 status_t waitForever(unsigned int warningTimeout, const char* logname);
Jesse Hallba607d52012-10-01 14:05:20 -070076
Jamie Gennisf25e1832012-06-13 16:31:43 -070077 // merge combines two Fence objects, creating a new Fence object that
78 // becomes signaled when both f1 and f2 are signaled (even if f1 or f2 is
79 // destroyed before it becomes signaled). The name argument specifies the
80 // human-readable name to associated with the new Fence object.
81 static sp<Fence> merge(const String8& name, const sp<Fence>& f1,
82 const sp<Fence>& f2);
83
Jesse Hallf9783af2012-06-25 13:54:23 -070084 // Return a duplicate of the fence file descriptor. The caller is
85 // responsible for closing the returned file descriptor. On error, -1 will
86 // be returned and errno will indicate the problem.
87 int dup() const;
88
Jamie Gennis82dbc742012-11-08 19:23:28 -080089 // getSignalTime returns the system monotonic clock time at which the
90 // fence transitioned to the signaled state. If the fence is not signaled
91 // then INT64_MAX is returned. If the fence is invalid or if an error
92 // occurs then -1 is returned.
93 nsecs_t getSignalTime() const;
94
Jamie Gennisf25e1832012-06-13 16:31:43 -070095 // Flattenable interface
96 size_t getFlattenedSize() const;
97 size_t getFdCount() const;
98 status_t flatten(void* buffer, size_t size,
99 int fds[], size_t count) const;
100 status_t unflatten(void const* buffer, size_t size,
101 int fds[], size_t count);
102
103private:
104 // Only allow instantiation using ref counting.
105 friend class LightRefBase<Fence>;
106 virtual ~Fence();
107
108 // Disallow copying
109 Fence(const Fence& rhs);
110 Fence& operator = (const Fence& rhs);
111 const Fence& operator = (const Fence& rhs) const;
112
113 int mFenceFd;
114};
115
116}; // namespace android
117
118#endif // ANDROID_FENCE_H