summaryrefslogtreecommitdiff
path: root/libshims
diff options
context:
space:
mode:
authorkoron393 <koron393@gmail.com>2019-05-07 02:49:25 +0900
committerArian <arian.kulmer@web.de>2019-10-25 22:16:13 +0200
commiteb4c130ccf05799fe7c20a4737f21dd63c0dbed5 (patch)
tree4845e3f7e06d660b0def654c9a294bd489dc2744 /libshims
parentfd201d3c1df49d4af95d2c48709b5a98b6e6d8e9 (diff)
shinano-common: libshim_camera: Update GraphicBuffer sources to Pie
* Copy from Pie framework sources. Change-Id: I903eb0c3c80a752d4a8da44dec47e348c7dd23fc Signed-off-by: Nikhil Punathil <nikhilpe@gmail.com>
Diffstat (limited to 'libshims')
-rw-r--r--libshims/include/ui/GraphicBuffer.h144
-rw-r--r--libshims/include/ui/GraphicBufferAllocator.h50
-rw-r--r--libshims/include/ui/GraphicBufferMapper.h52
-rw-r--r--libshims/include/utils/Looper.h27
-rw-r--r--libshims/include/utils/VectorImpl.h38
-rw-r--r--libshims/ui/GraphicBuffer.cpp356
-rw-r--r--libshims/ui/GraphicBufferAllocator.cpp115
-rw-r--r--libshims/ui/GraphicBufferMapper.cpp169
-rw-r--r--libshims/utils/Looper.cpp249
-rw-r--r--libshims/utils/VectorImpl.cpp46
10 files changed, 854 insertions, 392 deletions
diff --git a/libshims/include/ui/GraphicBuffer.h b/libshims/include/ui/GraphicBuffer.h
index e1e6a64..cc38982 100644
--- a/libshims/include/ui/GraphicBuffer.h
+++ b/libshims/include/ui/GraphicBuffer.h
@@ -20,18 +20,21 @@
#include <stdint.h>
#include <sys/types.h>
+#include <string>
+
#include <ui/ANativeObjectBase.h>
#include <ui/PixelFormat.h>
#include <ui/Rect.h>
#include <utils/Flattenable.h>
#include <utils/RefBase.h>
-#include <hardware/gralloc.h>
+#include <nativebase/nativebase.h>
-struct ANativeWindowBuffer;
+#include <hardware/gralloc.h>
namespace android {
+class DetachedBufferHandle;
class GraphicBufferMapper;
// ===========================================================================
@@ -39,7 +42,7 @@ class GraphicBufferMapper;
// ===========================================================================
class GraphicBuffer
- : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >,
+ : public ANativeObjectBase<ANativeWindowBuffer, GraphicBuffer, RefBase>,
public Flattenable<GraphicBuffer>
{
friend class Flattenable<GraphicBuffer>;
@@ -66,21 +69,72 @@ public:
USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER,
USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK,
+
+ USAGE_CURSOR = GRALLOC_USAGE_CURSOR,
};
+ static sp<GraphicBuffer> from(ANativeWindowBuffer *);
+
+
+ // Create a GraphicBuffer to be unflatten'ed into or be reallocated.
GraphicBuffer();
- // creates w * h buffer
+ // Create a GraphicBuffer by allocating and managing a buffer internally.
+ // This function is privileged. See reallocate for details.
GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
- uint32_t inUsage);
-
- // create a buffer from an existing handle
+ uint32_t inLayerCount, uint64_t inUsage,
+ std::string requestorName = "<Unknown>");
+
+ // Create a GraphicBuffer from an existing handle.
+ enum HandleWrapMethod : uint8_t {
+ // Wrap and use the handle directly. It assumes the handle has been
+ // registered and never fails. The handle must have a longer lifetime
+ // than this wrapping GraphicBuffer.
+ //
+ // This can be used when, for example, you want to wrap a handle that
+ // is already managed by another GraphicBuffer.
+ WRAP_HANDLE,
+
+ // Take ownership of the handle and use it directly. It assumes the
+ // handle has been registered and never fails.
+ //
+ // This can be used to manage an already registered handle with
+ // GraphicBuffer.
+ TAKE_HANDLE,
+
+ // Take onwership of an unregistered handle and use it directly. It
+ // can fail when the buffer does not register. There is no ownership
+ // transfer on failures.
+ //
+ // This can be used to, for example, create a GraphicBuffer from a
+ // handle returned by Parcel::readNativeHandle.
+ TAKE_UNREGISTERED_HANDLE,
+
+ // Make a clone of the handle and use the cloned handle. It can fail
+ // when cloning fails or when the buffer does not register. There is
+ // never ownership transfer.
+ //
+ // This can be used to create a GraphicBuffer from a handle that
+ // cannot be used directly, such as one from hidl_handle.
+ CLONE_HANDLE,
+ };
+ GraphicBuffer(const native_handle_t* handle, HandleWrapMethod method,
+ uint32_t width, uint32_t height,
+ PixelFormat format, uint32_t layerCount,
+ uint64_t usage, uint32_t stride);
+
+ // These functions are deprecated because they only take 32 bits of usage
+ GraphicBuffer(const native_handle_t* handle, HandleWrapMethod method,
+ uint32_t width, uint32_t height,
+ PixelFormat format, uint32_t layerCount,
+ uint32_t usage, uint32_t stride)
+ : GraphicBuffer(handle, method, width, height, format, layerCount,
+ static_cast<uint64_t>(usage), stride) {}
GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
- uint32_t inUsage, uint32_t inStride, native_handle_t* inHandle,
- bool keepOwnership);
-
- // create a buffer from an existing ANativeWindowBuffer
- GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
+ uint32_t inLayerCount, uint32_t inUsage, uint32_t inStride,
+ native_handle_t* inHandle, bool keepOwnership);
+ GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
+ uint32_t inUsage, std::string requestorName = "<Unknown>");
// return status
status_t initCheck() const;
@@ -88,12 +142,25 @@ public:
uint32_t getWidth() const { return static_cast<uint32_t>(width); }
uint32_t getHeight() const { return static_cast<uint32_t>(height); }
uint32_t getStride() const { return static_cast<uint32_t>(stride); }
- uint32_t getUsage() const { return static_cast<uint32_t>(usage); }
+ uint64_t getUsage() const { return usage; }
PixelFormat getPixelFormat() const { return format; }
+ uint32_t getLayerCount() const { return static_cast<uint32_t>(layerCount); }
Rect getBounds() const { return Rect(width, height); }
+ uint64_t getId() const { return mId; }
+
+ uint32_t getGenerationNumber() const { return mGenerationNumber; }
+ void setGenerationNumber(uint32_t generation) {
+ mGenerationNumber = generation;
+ }
+ // This function is privileged. It requires access to the allocator
+ // device or service, which usually involves adding suitable selinux
+ // rules.
status_t reallocate(uint32_t inWidth, uint32_t inHeight,
- PixelFormat inFormat, uint32_t inUsage);
+ PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage);
+
+ bool needsReallocation(uint32_t inWidth, uint32_t inHeight,
+ PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage);
status_t lock(uint32_t inUsage, void** vaddr);
status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr);
@@ -102,6 +169,16 @@ public:
status_t lockYCbCr(uint32_t inUsage, const Rect& rect,
android_ycbcr *ycbcr);
status_t unlock();
+ status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd);
+ status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr,
+ int fenceFd);
+ status_t lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
+ const Rect& rect, void** vaddr, int fenceFd);
+ status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr,
+ int fenceFd);
+ status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
+ android_ycbcr *ycbcr, int fenceFd);
+ status_t unlockAsync(int *fenceFd);
ANativeWindowBuffer* getNativeBuffer() const;
@@ -114,6 +191,11 @@ public:
status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
+ // Sets and takes DetachedBuffer. Should only be called from BufferHub.
+ bool isDetachedBuffer() const;
+ status_t setDetachedBufferHandle(std::unique_ptr<DetachedBufferHandle> detachedBuffer);
+ std::unique_ptr<DetachedBufferHandle> takeDetachedBufferHandle();
+
private:
~GraphicBuffer();
@@ -140,17 +222,41 @@ private:
GraphicBuffer& operator = (const GraphicBuffer& rhs);
const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
- status_t initSize(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
- uint32_t inUsage);
+ status_t initWithSize(uint32_t inWidth, uint32_t inHeight,
+ PixelFormat inFormat, uint32_t inLayerCount,
+ uint64_t inUsage, std::string requestorName);
+
+ status_t initWithHandle(const native_handle_t* handle,
+ HandleWrapMethod method, uint32_t width, uint32_t height,
+ PixelFormat format, uint32_t layerCount,
+ uint64_t usage, uint32_t stride);
void free_handle();
GraphicBufferMapper& mBufferMapper;
ssize_t mInitCheck;
- // If we're wrapping another buffer then this reference will make sure it
- // doesn't get freed.
- sp<ANativeWindowBuffer> mWrappedBuffer;
+ // numbers of fds/ints in native_handle_t to flatten
+ uint32_t mTransportNumFds;
+ uint32_t mTransportNumInts;
+
+ uint64_t mId;
+
+ // Stores the generation number of this buffer. If this number does not
+ // match the BufferQueue's internal generation number (set through
+ // IGBP::setGenerationNumber), attempts to attach the buffer will fail.
+ uint32_t mGenerationNumber;
+
+ // Stores a BufferHub handle that can be used to re-attach this GraphicBuffer back into a
+ // BufferHub producer/consumer set. In terms of GraphicBuffer's relationship with BufferHub,
+ // there are three different modes:
+ // 1. Legacy mode: GraphicBuffer is not backed by BufferHub and mDetachedBufferHandle must be
+ // invalid.
+ // 2. Detached mode: GraphicBuffer is backed by BufferHub, but not part of a producer/consumer
+ // set. In this mode, mDetachedBufferHandle must be valid.
+ // 3. Attached mode: GraphicBuffer is backed by BufferHub and it's part of a producer/consumer
+ // set. In this mode, mDetachedBufferHandle must be invalid.
+ std::unique_ptr<DetachedBufferHandle> mDetachedBufferHandle;
};
}; // namespace android
diff --git a/libshims/include/ui/GraphicBufferAllocator.h b/libshims/include/ui/GraphicBufferAllocator.h
index b6094a7..14a865e 100644
--- a/libshims/include/ui/GraphicBufferAllocator.h
+++ b/libshims/include/ui/GraphicBufferAllocator.h
@@ -20,49 +20,36 @@
#include <stdint.h>
+#include <memory>
+#include <string>
+
#include <cutils/native_handle.h>
+#include <ui/PixelFormat.h>
+
#include <utils/Errors.h>
#include <utils/KeyedVector.h>
-#include <utils/threads.h>
+#include <utils/Mutex.h>
#include <utils/Singleton.h>
-#include <ui/PixelFormat.h>
-
-#include <hardware/gralloc.h>
-
-
namespace android {
-// ---------------------------------------------------------------------------
+namespace Gralloc2 {
+class Allocator;
+}
+
+class GraphicBufferMapper;
class String8;
class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
{
public:
- enum {
- USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
- USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
- USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
- USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
-
- USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
- USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
- USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
- USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
-
- USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
-
- USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
- USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
- USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
- USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK
- };
-
static inline GraphicBufferAllocator& get() { return getInstance(); }
- status_t alloc(uint32_t w, uint32_t h, PixelFormat format, int usage,
- buffer_handle_t* handle, int32_t* stride);
+ status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
+ uint32_t layerCount, uint64_t usage,
+ buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
+ std::string requestorName);
status_t free(buffer_handle_t handle);
@@ -75,8 +62,10 @@ private:
uint32_t height;
uint32_t stride;
PixelFormat format;
- uint32_t usage;
+ uint32_t layerCount;
+ uint64_t usage;
size_t size;
+ std::string requestorName;
};
static Mutex sLock;
@@ -86,7 +75,8 @@ private:
GraphicBufferAllocator();
~GraphicBufferAllocator();
- alloc_device_t *mAllocDev;
+ GraphicBufferMapper& mMapper;
+ const std::unique_ptr<const Gralloc2::Allocator> mAllocator;
};
// ---------------------------------------------------------------------------
diff --git a/libshims/include/ui/GraphicBufferMapper.h b/libshims/include/ui/GraphicBufferMapper.h
index fbfdfc3..7cf003d 100644
--- a/libshims/include/ui/GraphicBufferMapper.h
+++ b/libshims/include/ui/GraphicBufferMapper.h
@@ -20,43 +20,77 @@
#include <stdint.h>
#include <sys/types.h>
+#include <memory>
+
+#include <ui/PixelFormat.h>
#include <utils/Singleton.h>
-#include <hardware/gralloc.h>
+// Needed by code that still uses the GRALLOC_USAGE_* constants.
+// when/if we get rid of gralloc, we should provide aliases or fix call sites.
+#include <hardware/gralloc.h>
-struct gralloc_module_t;
namespace android {
// ---------------------------------------------------------------------------
+namespace Gralloc2 {
+class Mapper;
+}
+
class Rect;
class GraphicBufferMapper : public Singleton<GraphicBufferMapper>
{
public:
+ static void preloadHal();
static inline GraphicBufferMapper& get() { return getInstance(); }
- status_t registerBuffer(buffer_handle_t handle);
+ // The imported outHandle must be freed with freeBuffer when no longer
+ // needed. rawHandle is owned by the caller.
+ status_t importBuffer(buffer_handle_t rawHandle,
+ uint32_t width, uint32_t height, uint32_t layerCount,
+ PixelFormat format, uint64_t usage, uint32_t stride,
+ buffer_handle_t* outHandle);
+
+ status_t freeBuffer(buffer_handle_t handle);
- status_t unregisterBuffer(buffer_handle_t handle);
+ void getTransportSize(buffer_handle_t handle,
+ uint32_t* outTransportNumFds, uint32_t* outTransportNumInts);
status_t lock(buffer_handle_t handle,
- int usage, const Rect& bounds, void** vaddr);
+ uint32_t usage, const Rect& bounds, void** vaddr);
status_t lockYCbCr(buffer_handle_t handle,
- int usage, const Rect& bounds, android_ycbcr *ycbcr);
+ uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr);
status_t unlock(buffer_handle_t handle);
- // dumps information about the mapping of this handle
- void dump(buffer_handle_t handle);
+ status_t lockAsync(buffer_handle_t handle,
+ uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd);
+
+ status_t lockAsync(buffer_handle_t handle,
+ uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds,
+ void** vaddr, int fenceFd);
+
+ status_t lockAsyncYCbCr(buffer_handle_t handle,
+ uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr,
+ int fenceFd);
+
+ status_t unlockAsync(buffer_handle_t handle, int *fenceFd);
+
+ const Gralloc2::Mapper& getGrallocMapper() const
+ {
+ return *mMapper;
+ }
private:
friend class Singleton<GraphicBufferMapper>;
+
GraphicBufferMapper();
- gralloc_module_t const *mAllocMod;
+
+ const std::unique_ptr<const Gralloc2::Mapper> mMapper;
};
// ---------------------------------------------------------------------------
diff --git a/libshims/include/utils/Looper.h b/libshims/include/utils/Looper.h
index 88e13cf..a62e67f 100644
--- a/libshims/include/utils/Looper.h
+++ b/libshims/include/utils/Looper.h
@@ -49,7 +49,7 @@ typedef int (*Looper_callbackFunc)(int fd, int events, void* data);
*/
struct Message {
Message() : what(0) { }
- Message(int what) : what(what) { }
+ Message(int w) : what(w) { }
/* The message type. (interpretation is left up to the handler) */
int what;
@@ -66,7 +66,7 @@ struct Message {
*/
class MessageHandler : public virtual RefBase {
protected:
- virtual ~MessageHandler() { }
+ virtual ~MessageHandler();
public:
/**
@@ -97,7 +97,7 @@ private:
*/
class LooperCallback : public virtual RefBase {
protected:
- virtual ~LooperCallback() { }
+ virtual ~LooperCallback();
public:
/**
@@ -420,8 +420,12 @@ private:
struct Request {
int fd;
int ident;
+ int events;
+ int seq;
sp<LooperCallback> callback;
void* data;
+
+ void initEventItem(struct epoll_event* eventItem) const;
};
struct Response {
@@ -432,8 +436,8 @@ private:
struct MessageEnvelope {
MessageEnvelope() : uptime(0) { }
- MessageEnvelope(nsecs_t uptime, const sp<MessageHandler> handler,
- const Message& message) : uptime(uptime), handler(handler), message(message) {
+ MessageEnvelope(nsecs_t u, const sp<MessageHandler> h,
+ const Message& m) : uptime(u), handler(h), message(m) {
}
nsecs_t uptime;
@@ -443,8 +447,7 @@ private:
const bool mAllowNonCallbacks; // immutable
- int mWakeReadPipeFd; // immutable
- int mWakeWritePipeFd; // immutable
+ int mWakeEventFd; // immutable
Mutex mLock;
Vector<MessageEnvelope> mMessageEnvelopes; // guarded by mLock
@@ -454,10 +457,12 @@ private:
// any use of it is racy anyway.
volatile bool mPolling;
- int mEpollFd; // immutable
+ int mEpollFd; // guarded by mLock but only modified on the looper thread
+ bool mEpollRebuildRequired; // guarded by mLock
// Locked list of file descriptor monitoring requests.
KeyedVector<int, Request> mRequests; // guarded by mLock
+ int mNextRequestSeq;
// This state is only used privately by pollOnce and does not require a lock since
// it runs on a single thread.
@@ -466,13 +471,17 @@ private:
nsecs_t mNextMessageUptime; // set to LLONG_MAX when none
int pollInner(int timeoutMillis);
+ int removeFd(int fd, int seq);
void awoken();
void pushResponse(int events, const Request& request);
+ void rebuildEpollLocked();
+ void scheduleEpollRebuildLocked();
static void initTLSKey();
static void threadDestructor(void *st);
+ static void initEpollEvent(struct epoll_event* eventItem);
};
} // namespace android
-#endif // UTILS_LOOPER_H \ No newline at end of file
+#endif // UTILS_LOOPER_H
diff --git a/libshims/include/utils/VectorImpl.h b/libshims/include/utils/VectorImpl.h
index f5fa742..4dd91fd 100644
--- a/libshims/include/utils/VectorImpl.h
+++ b/libshims/include/utils/VectorImpl.h
@@ -53,12 +53,12 @@ public:
/*! must be called from subclasses destructor */
void finish_vector();
- VectorImpl& operator = (const VectorImpl& rhs);
-
+ VectorImpl& operator = (const VectorImpl& rhs);
+
/*! C-style array access */
inline const void* arrayImpl() const { return mStorage; }
void* editArrayImpl();
-
+
/*! vector stats */
inline size_t size() const { return mCount; }
inline bool isEmpty() const { return mCount == 0; }
@@ -71,7 +71,7 @@ public:
ssize_t appendVector(const VectorImpl& vector);
ssize_t insertArrayAt(const void* array, size_t index, size_t length);
ssize_t appendArray(const void* array, size_t length);
-
+
/*! add/insert/replace items */
ssize_t insertAt(size_t where, size_t numItems = 1);
ssize_t insertAt(const void* item, size_t where, size_t numItems = 1);
@@ -105,16 +105,7 @@ protected:
virtual void do_splat(void* dest, const void* item, size_t num) const = 0;
virtual void do_move_forward(void* dest, const void* from, size_t num) const = 0;
virtual void do_move_backward(void* dest, const void* from, size_t num) const = 0;
-
- virtual void reservedVectorImpl1();
- virtual void reservedVectorImpl2();
- virtual void reservedVectorImpl3();
- virtual void reservedVectorImpl4();
- virtual void reservedVectorImpl5();
- virtual void reservedVectorImpl6();
- virtual void reservedVectorImpl7();
- virtual void reservedVectorImpl8();
-
+
private:
void* _grow(size_t where, size_t amount);
void _shrink(size_t where, size_t amount);
@@ -141,10 +132,10 @@ class SortedVectorImpl : public VectorImpl
{
public:
SortedVectorImpl(size_t itemSize, uint32_t flags);
- SortedVectorImpl(const VectorImpl& rhs);
+ explicit SortedVectorImpl(const VectorImpl& rhs);
virtual ~SortedVectorImpl();
-
- SortedVectorImpl& operator = (const SortedVectorImpl& rhs);
+
+ SortedVectorImpl& operator = (const SortedVectorImpl& rhs);
//! finds the index of an item
ssize_t indexOf(const void* item) const;
@@ -158,22 +149,13 @@ public:
//! merges a vector into this one
ssize_t merge(const VectorImpl& vector);
ssize_t merge(const SortedVectorImpl& vector);
-
+
//! removes an item
ssize_t remove(const void* item);
-
+
protected:
virtual int do_compare(const void* lhs, const void* rhs) const = 0;
- virtual void reservedSortedVectorImpl1();
- virtual void reservedSortedVectorImpl2();
- virtual void reservedSortedVectorImpl3();
- virtual void reservedSortedVectorImpl4();
- virtual void reservedSortedVectorImpl5();
- virtual void reservedSortedVectorImpl6();
- virtual void reservedSortedVectorImpl7();
- virtual void reservedSortedVectorImpl8();
-
private:
ssize_t _indexOrderOf(const void* item, size_t* order = 0) const;
diff --git a/libshims/ui/GraphicBuffer.cpp b/libshims/ui/GraphicBuffer.cpp
index dad41b4..7670ac6 100644
--- a/libshims/ui/GraphicBuffer.cpp
+++ b/libshims/ui/GraphicBuffer.cpp
@@ -16,17 +16,16 @@
#define LOG_TAG "GraphicBuffer"
-#include <stdlib.h>
-#include <stdint.h>
-#include <sys/types.h>
+#include <ui/GraphicBuffer.h>
-#include <utils/Errors.h>
-#include <utils/Log.h>
+#include <cutils/atomic.h>
-#include <ui/GraphicBuffer.h>
+#include <grallocusage/GrallocUsageConversion.h>
+
+#include <ui/DetachedBufferHandle.h>
+#include <ui/Gralloc2.h>
#include <ui/GraphicBufferAllocator.h>
#include <ui/GraphicBufferMapper.h>
-#include <ui/PixelFormat.h>
namespace android {
@@ -34,58 +33,65 @@ namespace android {
// Buffer and implementation of ANativeWindowBuffer
// ===========================================================================
+static uint64_t getUniqueId() {
+ static volatile int32_t nextId = 0;
+ uint64_t id = static_cast<uint64_t>(getpid()) << 32;
+ id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
+ return id;
+}
+
+sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
+ return static_cast<GraphicBuffer *>(anwb);
+}
+
GraphicBuffer::GraphicBuffer()
: BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
- mInitCheck(NO_ERROR)
+ mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
{
width =
height =
stride =
format =
+ usage_deprecated = 0;
usage = 0;
+ layerCount = 0;
handle = NULL;
}
+// deprecated
GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
- PixelFormat inFormat, uint32_t inUsage)
- : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
- mInitCheck(NO_ERROR)
+ PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
+ : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
{
- width =
- height =
- stride =
- format =
- usage = 0;
- handle = NULL;
- mInitCheck = initSize(inWidth, inHeight, inFormat, inUsage);
}
GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
- PixelFormat inFormat, uint32_t inUsage, uint32_t inStride,
- native_handle_t* inHandle, bool keepOwnership)
- : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
- mBufferMapper(GraphicBufferMapper::get()),
- mInitCheck(NO_ERROR)
+ PixelFormat inFormat, uint32_t inLayerCount, uint64_t usage, std::string requestorName)
+ : GraphicBuffer()
{
- width = static_cast<int>(inWidth);
- height = static_cast<int>(inHeight);
- stride = static_cast<int>(inStride);
- format = inFormat;
- usage = static_cast<int>(inUsage);
- handle = inHandle;
-}
-
-GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
- : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
- mBufferMapper(GraphicBufferMapper::get()),
- mInitCheck(NO_ERROR), mWrappedBuffer(buffer)
+ mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount,
+ usage, std::move(requestorName));
+}
+
+// deprecated
+GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
+ PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
+ uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
+ : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
+ inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
+ inStride)
+{
+}
+
+GraphicBuffer::GraphicBuffer(const native_handle_t* handle,
+ HandleWrapMethod method, uint32_t width, uint32_t height,
+ PixelFormat format, uint32_t layerCount,
+ uint64_t usage,
+ uint32_t stride)
+ : GraphicBuffer()
{
- width = buffer->width;
- height = buffer->height;
- stride = buffer->stride;
- format = buffer->format;
- usage = buffer->usage;
- handle = buffer->handle;
+ mInitCheck = initWithHandle(handle, method, width, height, format,
+ layerCount, usage, stride);
}
GraphicBuffer::~GraphicBuffer()
@@ -98,15 +104,12 @@ GraphicBuffer::~GraphicBuffer()
void GraphicBuffer::free_handle()
{
if (mOwner == ownHandle) {
- mBufferMapper.unregisterBuffer(handle);
- native_handle_close(handle);
- native_handle_delete(const_cast<native_handle*>(handle));
+ mBufferMapper.freeBuffer(handle);
} else if (mOwner == ownData) {
GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
allocator.free(handle);
}
-
- mWrappedBuffer = 0;
+ handle = NULL;
}
status_t GraphicBuffer::initCheck() const {
@@ -126,7 +129,7 @@ ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
}
status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
- PixelFormat inFormat, uint32_t inUsage)
+ PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
{
if (mOwner != ownData)
return INVALID_OPERATION;
@@ -135,7 +138,8 @@ status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
static_cast<int>(inWidth) == width &&
static_cast<int>(inHeight) == height &&
inFormat == format &&
- static_cast<int>(inUsage) == usage)
+ inLayerCount == layerCount &&
+ inUsage == usage)
return NO_ERROR;
if (handle) {
@@ -143,26 +147,83 @@ status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
allocator.free(handle);
handle = 0;
}
- return initSize(inWidth, inHeight, inFormat, inUsage);
+ return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
+}
+
+bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
+ PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
+{
+ if (static_cast<int>(inWidth) != width) return true;
+ if (static_cast<int>(inHeight) != height) return true;
+ if (inFormat != format) return true;
+ if (inLayerCount != layerCount) return true;
+ if ((usage & inUsage) != inUsage) return true;
+ return false;
}
-status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
- PixelFormat inFormat, uint32_t inUsage)
+status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
+ PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
+ std::string requestorName)
{
GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
- int outStride = 0;
- status_t err = allocator.alloc(inWidth, inHeight, inFormat, inUsage,
- &handle, &outStride);
+ uint32_t outStride = 0;
+ status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
+ inUsage, &handle, &outStride, mId,
+ std::move(requestorName));
if (err == NO_ERROR) {
+ mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
+
width = static_cast<int>(inWidth);
height = static_cast<int>(inHeight);
format = inFormat;
- usage = static_cast<int>(inUsage);
+ layerCount = inLayerCount;
+ usage = inUsage;
+ usage_deprecated = int(usage);
stride = static_cast<int>(outStride);
}
return err;
}
+status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
+ HandleWrapMethod method, uint32_t width, uint32_t height,
+ PixelFormat format, uint32_t layerCount, uint64_t usage,
+ uint32_t stride)
+{
+ ANativeWindowBuffer::width = static_cast<int>(width);
+ ANativeWindowBuffer::height = static_cast<int>(height);
+ ANativeWindowBuffer::stride = static_cast<int>(stride);
+ ANativeWindowBuffer::format = format;
+ ANativeWindowBuffer::usage = usage;
+ ANativeWindowBuffer::usage_deprecated = int(usage);
+
+ ANativeWindowBuffer::layerCount = layerCount;
+
+ mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
+
+ if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
+ buffer_handle_t importedHandle;
+ status_t err = mBufferMapper.importBuffer(handle, width, height,
+ layerCount, format, usage, stride, &importedHandle);
+ if (err != NO_ERROR) {
+ initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
+
+ return err;
+ }
+
+ if (method == TAKE_UNREGISTERED_HANDLE) {
+ native_handle_close(handle);
+ native_handle_delete(const_cast<native_handle_t*>(handle));
+ }
+
+ handle = importedHandle;
+ mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
+ }
+
+ ANativeWindowBuffer::handle = handle;
+
+ return NO_ERROR;
+}
+
status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
{
const Rect lockBounds(width, height);
@@ -210,12 +271,68 @@ status_t GraphicBuffer::unlock()
return res;
}
+status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
+{
+ const Rect lockBounds(width, height);
+ status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
+ return res;
+}
+
+status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
+ void** vaddr, int fenceFd)
+{
+ return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
+}
+
+status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
+ uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
+{
+ if (rect.left < 0 || rect.right > width ||
+ rect.top < 0 || rect.bottom > height) {
+ ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
+ rect.left, rect.top, rect.right, rect.bottom,
+ width, height);
+ return BAD_VALUE;
+ }
+ status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
+ inConsumerUsage, rect, vaddr, fenceFd);
+ return res;
+}
+
+status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
+ int fenceFd)
+{
+ const Rect lockBounds(width, height);
+ status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
+ return res;
+}
+
+status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
+ android_ycbcr* ycbcr, int fenceFd)
+{
+ if (rect.left < 0 || rect.right > width ||
+ rect.top < 0 || rect.bottom > height) {
+ ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
+ rect.left, rect.top, rect.right, rect.bottom,
+ width, height);
+ return BAD_VALUE;
+ }
+ status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
+ return res;
+}
+
+status_t GraphicBuffer::unlockAsync(int *fenceFd)
+{
+ status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
+ return res;
+}
+
size_t GraphicBuffer::getFlattenedSize() const {
- return static_cast<size_t>(8 + (handle ? handle->numInts : 0)) * sizeof(int);
+ return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
}
size_t GraphicBuffer::getFdCount() const {
- return static_cast<size_t>(handle ? handle->numFds : 0);
+ return static_cast<size_t>(handle ? mTransportNumFds : 0);
}
status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
@@ -225,30 +342,34 @@ status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t&
size_t fdCountNeeded = GraphicBuffer::getFdCount();
if (count < fdCountNeeded) return NO_MEMORY;
- int* buf = static_cast<int*>(buffer);
- buf[0] = 'GBFR';
+ int32_t* buf = static_cast<int32_t*>(buffer);
+ buf[0] = 'GB01';
buf[1] = width;
buf[2] = height;
buf[3] = stride;
buf[4] = format;
- buf[5] = usage;
- buf[6] = 0;
- buf[7] = 0;
+ buf[5] = static_cast<int32_t>(layerCount);
+ buf[6] = int(usage); // low 32-bits
+ buf[7] = static_cast<int32_t>(mId >> 32);
+ buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
+ buf[9] = static_cast<int32_t>(mGenerationNumber);
+ buf[10] = 0;
+ buf[11] = 0;
+ buf[12] = int(usage >> 32); // high 32-bits
if (handle) {
- buf[6] = handle->numFds;
- buf[7] = handle->numInts;
- memcpy(fds, handle->data,
- static_cast<size_t>(handle->numFds) * sizeof(int));
- memcpy(&buf[8], handle->data + handle->numFds,
- static_cast<size_t>(handle->numInts) * sizeof(int));
+ buf[10] = int32_t(mTransportNumFds);
+ buf[11] = int32_t(mTransportNumInts);
+ memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
+ memcpy(buf + 13, handle->data + handle->numFds,
+ static_cast<size_t>(mTransportNumInts) * sizeof(int));
}
- buffer = static_cast<void*>(static_cast<int*>(buffer) + sizeNeeded);
+ buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
size -= sizeNeeded;
if (handle) {
- fds += handle->numFds;
- count -= static_cast<size_t>(handle->numFds);
+ fds += mTransportNumFds;
+ count -= static_cast<size_t>(mTransportNumFds);
}
return NO_ERROR;
@@ -256,28 +377,44 @@ status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t&
status_t GraphicBuffer::unflatten(
void const*& buffer, size_t& size, int const*& fds, size_t& count) {
- if (size < 8 * sizeof(int)) return NO_MEMORY;
+ if (size < 12 * sizeof(int)) {
+ android_errorWriteLog(0x534e4554, "114223584");
+ return NO_MEMORY;
+ }
int const* buf = static_cast<int const*>(buffer);
- if (buf[0] != 'GBFR') return BAD_TYPE;
- const size_t numFds = static_cast<size_t>(buf[6]);
- const size_t numInts = static_cast<size_t>(buf[7]);
+ // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
+ // see H2BGraphicBufferProducer.cpp
+ uint32_t flattenWordCount = 0;
+ if (buf[0] == 'GB01') {
+ // new version with 64-bits usage bits
+ flattenWordCount = 13;
+ } else if (buf[0] == 'GBFR') {
+ // old version, when usage bits were 32-bits
+ flattenWordCount = 12;
+ } else {
+ return BAD_TYPE;
+ }
+
+ const size_t numFds = static_cast<size_t>(buf[10]);
+ const size_t numInts = static_cast<size_t>(buf[11]);
// Limit the maxNumber to be relatively small. The number of fds or ints
// should not come close to this number, and the number itself was simply
// chosen to be high enough to not cause issues and low enough to prevent
// overflow problems.
const size_t maxNumber = 4096;
- if (numFds >= maxNumber || numInts >= (maxNumber - 10)) {
- width = height = stride = format = usage = 0;
+ if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
+ width = height = stride = format = usage_deprecated = 0;
+ layerCount = 0;
+ usage = 0;
handle = NULL;
- ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
- numFds, numInts);
+ ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
return BAD_VALUE;
}
- const size_t sizeNeeded = (8 + numInts) * sizeof(int);
+ const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
if (size < sizeNeeded) return NO_MEMORY;
size_t fdCountNeeded = numFds;
@@ -293,37 +430,60 @@ status_t GraphicBuffer::unflatten(
height = buf[2];
stride = buf[3];
format = buf[4];
- usage = buf[5];
+ layerCount = static_cast<uintptr_t>(buf[5]);
+ usage_deprecated = buf[6];
+ if (flattenWordCount == 13) {
+ usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
+ } else {
+ usage = uint64_t(usage_deprecated);
+ }
native_handle* h = native_handle_create(
static_cast<int>(numFds), static_cast<int>(numInts));
if (!h) {
- width = height = stride = format = usage = 0;
+ width = height = stride = format = usage_deprecated = 0;
+ layerCount = 0;
+ usage = 0;
handle = NULL;
ALOGE("unflatten: native_handle_create failed");
return NO_MEMORY;
}
memcpy(h->data, fds, numFds * sizeof(int));
- memcpy(h->data + numFds, &buf[8], numInts * sizeof(int));
+ memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
handle = h;
} else {
- width = height = stride = format = usage = 0;
+ width = height = stride = format = usage_deprecated = 0;
+ layerCount = 0;
+ usage = 0;
handle = NULL;
}
+ mId = static_cast<uint64_t>(buf[7]) << 32;
+ mId |= static_cast<uint32_t>(buf[8]);
+
+ mGenerationNumber = static_cast<uint32_t>(buf[9]);
+
mOwner = ownHandle;
if (handle != 0) {
- status_t err = mBufferMapper.registerBuffer(handle);
+ buffer_handle_t importedHandle;
+ status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
+ uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
if (err != NO_ERROR) {
- width = height = stride = format = usage = 0;
+ width = height = stride = format = usage_deprecated = 0;
+ layerCount = 0;
+ usage = 0;
handle = NULL;
- ALOGE("unflatten: registerBuffer failed: %s (%d)",
- strerror(-err), err);
+ ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
return err;
}
+
+ native_handle_close(handle);
+ native_handle_delete(const_cast<native_handle_t*>(handle));
+ handle = importedHandle;
+ mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
}
- buffer = static_cast<void const*>(static_cast<int const*>(buffer) + sizeNeeded);
+ buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
size -= sizeNeeded;
fds += numFds;
count -= numFds;
@@ -331,6 +491,24 @@ status_t GraphicBuffer::unflatten(
return NO_ERROR;
}
+bool GraphicBuffer::isDetachedBuffer() const {
+ return mDetachedBufferHandle && mDetachedBufferHandle->isValid();
+}
+
+status_t GraphicBuffer::setDetachedBufferHandle(std::unique_ptr<DetachedBufferHandle> channel) {
+ if (isDetachedBuffer()) {
+ ALOGW("setDetachedBuffer: there is already a BufferHub channel associated with this "
+ "GraphicBuffer. Replacing the old one.");
+ }
+
+ mDetachedBufferHandle = std::move(channel);
+ return NO_ERROR;
+}
+
+std::unique_ptr<DetachedBufferHandle> GraphicBuffer::takeDetachedBufferHandle() {
+ return std::move(mDetachedBufferHandle);
+}
+
// ---------------------------------------------------------------------------
}; // namespace android
diff --git a/libshims/ui/GraphicBufferAllocator.cpp b/libshims/ui/GraphicBufferAllocator.cpp
index 4f93fb6..eaba1ed 100644
--- a/libshims/ui/GraphicBufferAllocator.cpp
+++ b/libshims/ui/GraphicBufferAllocator.cpp
@@ -18,13 +18,19 @@
#define LOG_TAG "GraphicBufferAllocator"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
-#include <cutils/log.h>
+#include <ui/GraphicBufferAllocator.h>
+
+#include <stdio.h>
+
+#include <grallocusage/GrallocUsageConversion.h>
+#include <log/log.h>
#include <utils/Singleton.h>
#include <utils/String8.h>
#include <utils/Trace.h>
-#include <ui/GraphicBufferAllocator.h>
+#include <ui/Gralloc2.h>
+#include <ui/GraphicBufferMapper.h>
namespace android {
// ---------------------------------------------------------------------------
@@ -36,20 +42,13 @@ KeyedVector<buffer_handle_t,
GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
GraphicBufferAllocator::GraphicBufferAllocator()
- : mAllocDev(0)
+ : mMapper(GraphicBufferMapper::getInstance()),
+ mAllocator(std::make_unique<Gralloc2::Allocator>(
+ mMapper.getGrallocMapper()))
{
- hw_module_t const* module;
- int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
- ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
- if (err == 0) {
- gralloc_open(module, &mAllocDev);
- }
}
-GraphicBufferAllocator::~GraphicBufferAllocator()
-{
- gralloc_close(mAllocDev);
-}
+GraphicBufferAllocator::~GraphicBufferAllocator() {}
void GraphicBufferAllocator::dump(String8& result) const
{
@@ -64,23 +63,26 @@ void GraphicBufferAllocator::dump(String8& result) const
for (size_t i=0 ; i<c ; i++) {
const alloc_rec_t& rec(list.valueAt(i));
if (rec.size) {
- snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n",
- list.keyAt(i), rec.size/1024.0f,
- rec.width, rec.stride, rec.height, rec.format, rec.usage);
+ snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64
+ " | %s\n",
+ list.keyAt(i), rec.size/1024.0,
+ rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
+ rec.usage, rec.requestorName.c_str());
} else {
- snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x\n",
+ snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64
+ " | %s\n",
list.keyAt(i),
- rec.width, rec.stride, rec.height, rec.format, rec.usage);
+ rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
+ rec.usage, rec.requestorName.c_str());
}
result.append(buffer);
total += rec.size;
}
- snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f);
+ snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0);
result.append(buffer);
- if (mAllocDev->common.version >= 1 && mAllocDev->dump) {
- mAllocDev->dump(mAllocDev, buffer, SIZE);
- result.append(buffer);
- }
+
+ std::string deviceDump = mAllocator->dumpDebugInfo();
+ result.append(deviceDump.c_str(), deviceDump.size());
}
void GraphicBufferAllocator::dumpToSystemLog()
@@ -90,9 +92,10 @@ void GraphicBufferAllocator::dumpToSystemLog()
ALOGD("%s", s.string());
}
-status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
- PixelFormat format, int usage, buffer_handle_t* handle,
- int32_t* stride)
+status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height,
+ PixelFormat format, uint32_t layerCount, uint64_t usage,
+ buffer_handle_t* handle, uint32_t* stride,
+ uint64_t /*graphicBufferId*/, std::string requestorName)
{
ATRACE_CALL();
@@ -101,58 +104,56 @@ status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
if (!width || !height)
width = height = 1;
- // we have a h/w allocator and h/w buffer is requested
- status_t err;
+ // Ensure that layerCount is valid.
+ if (layerCount < 1)
+ layerCount = 1;
- // Filter out any usage bits that should not be passed to the gralloc module
- usage &= GRALLOC_USAGE_ALLOC_MASK;
+ Gralloc2::IMapper::BufferDescriptorInfo info = {};
+ info.width = width;
+ info.height = height;
+ info.layerCount = layerCount;
+ info.format = static_cast<Gralloc2::PixelFormat>(format);
+ info.usage = usage;
- int outStride = 0;
- err = mAllocDev->alloc(mAllocDev, static_cast<int>(width),
- static_cast<int>(height), format, static_cast<int>(usage), handle,
- &outStride);
- *stride = static_cast<uint32_t>(outStride);
-
- ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
- width, height, format, usage, err, strerror(-err));
-
- if (err == NO_ERROR) {
+ Gralloc2::Error error = mAllocator->allocate(info, stride, handle);
+ if (error == Gralloc2::Error::NONE) {
Mutex::Autolock _l(sLock);
KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
- int bpp = bytesPerPixel(format);
- if (bpp < 0) {
- // probably a HAL custom format. in any case, we don't know
- // what its pixel size is.
- bpp = 0;
- }
+ uint32_t bpp = bytesPerPixel(format);
alloc_rec_t rec;
rec.width = width;
rec.height = height;
rec.stride = *stride;
rec.format = format;
+ rec.layerCount = layerCount;
rec.usage = usage;
rec.size = static_cast<size_t>(height * (*stride) * bpp);
+ rec.requestorName = std::move(requestorName);
list.add(*handle, rec);
- }
- return err;
+ return NO_ERROR;
+ } else {
+ ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
+ "usage %" PRIx64 ": %d",
+ width, height, layerCount, format, usage,
+ error);
+ return NO_MEMORY;
+ }
}
status_t GraphicBufferAllocator::free(buffer_handle_t handle)
{
ATRACE_CALL();
- status_t err;
- err = mAllocDev->free(mAllocDev, handle);
+ // We allocated a buffer from the allocator and imported it into the
+ // mapper to get the handle. We just need to free the handle now.
+ mMapper.freeBuffer(handle);
- ALOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
- if (err == NO_ERROR) {
- Mutex::Autolock _l(sLock);
- KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
- list.removeItem(handle);
- }
+ Mutex::Autolock _l(sLock);
+ KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
+ list.removeItem(handle);
- return err;
+ return NO_ERROR;
}
// ---------------------------------------------------------------------------
diff --git a/libshims/ui/GraphicBufferMapper.cpp b/libshims/ui/GraphicBufferMapper.cpp
index 18be8ae..2d8e582 100644
--- a/libshims/ui/GraphicBufferMapper.cpp
+++ b/libshims/ui/GraphicBufferMapper.cpp
@@ -16,103 +16,172 @@
#define LOG_TAG "GraphicBufferMapper"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
+//#define LOG_NDEBUG 0
-#include <stdint.h>
-#include <errno.h>
+#include <ui/GraphicBufferMapper.h>
+
+#include <grallocusage/GrallocUsageConversion.h>
+// We would eliminate the non-conforming zero-length array, but we can't since
+// this is effectively included from the Linux kernel
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wzero-length-array"
#include <sync/sync.h>
+#pragma clang diagnostic pop
-#include <utils/Errors.h>
#include <utils/Log.h>
#include <utils/Trace.h>
-#include <ui/GraphicBufferMapper.h>
-#include <ui/Rect.h>
-
-#include <hardware/gralloc.h>
+#include <ui/Gralloc2.h>
+#include <ui/GraphicBuffer.h>
+#include <system/graphics.h>
namespace android {
// ---------------------------------------------------------------------------
ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
+void GraphicBufferMapper::preloadHal() {
+ Gralloc2::Mapper::preload();
+}
+
GraphicBufferMapper::GraphicBufferMapper()
- : mAllocMod(0)
+ : mMapper(std::make_unique<const Gralloc2::Mapper>())
{
- hw_module_t const* module;
- int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
- ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
- if (err == 0) {
- mAllocMod = reinterpret_cast<gralloc_module_t const *>(module);
- }
}
-status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
+status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
+ uint32_t width, uint32_t height, uint32_t layerCount,
+ PixelFormat format, uint64_t usage, uint32_t stride,
+ buffer_handle_t* outHandle)
{
ATRACE_CALL();
- status_t err;
- err = mAllocMod->registerBuffer(mAllocMod, handle);
+ buffer_handle_t bufferHandle;
+ Gralloc2::Error error = mMapper->importBuffer(
+ hardware::hidl_handle(rawHandle), &bufferHandle);
+ if (error != Gralloc2::Error::NONE) {
+ ALOGW("importBuffer(%p) failed: %d", rawHandle, error);
+ return static_cast<status_t>(error);
+ }
- ALOGW_IF(err, "registerBuffer(%p) failed %d (%s)",
- handle, err, strerror(-err));
- return err;
+ Gralloc2::IMapper::BufferDescriptorInfo info = {};
+ info.width = width;
+ info.height = height;
+ info.layerCount = layerCount;
+ info.format = static_cast<Gralloc2::PixelFormat>(format);
+ info.usage = usage;
+
+ error = mMapper->validateBufferSize(bufferHandle, info, stride);
+ if (error != Gralloc2::Error::NONE) {
+ ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error);
+ freeBuffer(bufferHandle);
+ return static_cast<status_t>(error);
+ }
+
+ *outHandle = bufferHandle;
+
+ return NO_ERROR;
+}
+
+void GraphicBufferMapper::getTransportSize(buffer_handle_t handle,
+ uint32_t* outTransportNumFds, uint32_t* outTransportNumInts)
+{
+ mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts);
}
-status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle)
+status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
{
ATRACE_CALL();
- status_t err;
- err = mAllocMod->unregisterBuffer(mAllocMod, handle);
+ mMapper->freeBuffer(handle);
+
+ return NO_ERROR;
+}
- ALOGW_IF(err, "unregisterBuffer(%p) failed %d (%s)",
- handle, err, strerror(-err));
- return err;
+static inline Gralloc2::IMapper::Rect asGralloc2Rect(const Rect& rect) {
+ Gralloc2::IMapper::Rect outRect{};
+ outRect.left = rect.left;
+ outRect.top = rect.top;
+ outRect.width = rect.width();
+ outRect.height = rect.height();
+ return outRect;
}
-status_t GraphicBufferMapper::lock(buffer_handle_t handle,
- int usage, const Rect& bounds, void** vaddr)
+status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
+ const Rect& bounds, void** vaddr)
+{
+ return lockAsync(handle, usage, bounds, vaddr, -1);
+}
+
+status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
+ const Rect& bounds, android_ycbcr *ycbcr)
+{
+ return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
+}
+
+status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
+{
+ int32_t fenceFd = -1;
+ status_t error = unlockAsync(handle, &fenceFd);
+ if (error == NO_ERROR && fenceFd >= 0) {
+ sync_wait(fenceFd, -1);
+ close(fenceFd);
+ }
+ return error;
+}
+
+status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
+ uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
+{
+ return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd);
+}
+
+status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
+ uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds,
+ void** vaddr, int fenceFd)
{
ATRACE_CALL();
- status_t err;
- err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
- bounds.left, bounds.top, bounds.width(), bounds.height(),
- vaddr);
+ const uint64_t usage = static_cast<uint64_t>(
+ android_convertGralloc1To0Usage(producerUsage, consumerUsage));
+ Gralloc2::Error error = mMapper->lock(handle, usage,
+ asGralloc2Rect(bounds), fenceFd, vaddr);
+
+ ALOGW_IF(error != Gralloc2::Error::NONE, "lock(%p, ...) failed: %d",
+ handle, error);
- ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
- return err;
+ return static_cast<status_t>(error);
}
-status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle,
- int usage, const Rect& bounds, android_ycbcr *ycbcr)
+status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
+ uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
{
ATRACE_CALL();
- status_t err;
- if (mAllocMod->lock_ycbcr == NULL) {
- return -EINVAL; // do not log failure
+ Gralloc2::YCbCrLayout layout;
+ Gralloc2::Error error = mMapper->lock(handle, usage,
+ asGralloc2Rect(bounds), fenceFd, &layout);
+ if (error == Gralloc2::Error::NONE) {
+ ycbcr->y = layout.y;
+ ycbcr->cb = layout.cb;
+ ycbcr->cr = layout.cr;
+ ycbcr->ystride = static_cast<size_t>(layout.yStride);
+ ycbcr->cstride = static_cast<size_t>(layout.cStride);
+ ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
}
- err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
- bounds.left, bounds.top, bounds.width(), bounds.height(),
- ycbcr);
-
- ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
- return err;
+ return static_cast<status_t>(error);
}
-status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
+status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
{
ATRACE_CALL();
- status_t err;
- err = mAllocMod->unlock(mAllocMod, handle);
+ *fenceFd = mMapper->unlock(handle);
- ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
- return err;
+ return NO_ERROR;
}
// ---------------------------------------------------------------------------
diff --git a/libshims/utils/Looper.cpp b/libshims/utils/Looper.cpp
index 4bab6d5..6c57b2e 100644
--- a/libshims/utils/Looper.cpp
+++ b/libshims/utils/Looper.cpp
@@ -13,14 +13,8 @@
// Debugs callback registration and invocation.
#define DEBUG_CALLBACKS 0
-#include <cutils/log.h>
#include <utils/Looper.h>
-#include <utils/Timers.h>
-
-#include <unistd.h>
-#include <fcntl.h>
-#include <limits.h>
-
+#include <sys/eventfd.h>
namespace android {
@@ -68,41 +62,22 @@ static pthread_key_t gTLSKey = 0;
Looper::Looper(bool allowNonCallbacks) :
mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
- mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
- int wakeFds[2];
- int result = pipe(wakeFds);
- LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
-
- mWakeReadPipeFd = wakeFds[0];
- mWakeWritePipeFd = wakeFds[1];
-
- result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
- LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
- errno);
-
- result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
- LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
- errno);
-
- mPolling = false;
-
- // Allocate the epoll instance and register the wake pipe.
- mEpollFd = epoll_create(EPOLL_SIZE_HINT);
- LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
-
- struct epoll_event eventItem;
- memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
- eventItem.events = EPOLLIN;
- eventItem.data.fd = mWakeReadPipeFd;
- result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, & eventItem);
- LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
- errno);
+ mPolling(false), mEpollFd(-1), mEpollRebuildRequired(false),
+ mNextRequestSeq(0), mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
+ mWakeEventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
+ LOG_ALWAYS_FATAL_IF(mWakeEventFd < 0, "Could not make wake event fd: %s",
+ strerror(errno));
+
+ AutoMutex _l(mLock);
+ rebuildEpollLocked();
}
Looper::~Looper() {
- close(mWakeReadPipeFd);
- close(mWakeWritePipeFd);
- close(mEpollFd);
+ close(mWakeEventFd);
+ mWakeEventFd = -1;
+ if (mEpollFd >= 0) {
+ close(mEpollFd);
+ }
}
void Looper::initTLSKey() {
@@ -156,6 +131,50 @@ bool Looper::getAllowNonCallbacks() const {
return mAllowNonCallbacks;
}
+void Looper::rebuildEpollLocked() {
+ // Close old epoll instance if we have one.
+ if (mEpollFd >= 0) {
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ rebuildEpollLocked - rebuilding epoll set", this);
+#endif
+ close(mEpollFd);
+ }
+
+ // Allocate the new epoll instance and register the wake pipe.
+ mEpollFd = epoll_create(EPOLL_SIZE_HINT);
+ LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno));
+
+ struct epoll_event eventItem;
+ memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
+ eventItem.events = EPOLLIN;
+ eventItem.data.fd = mWakeEventFd;
+ int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeEventFd, & eventItem);
+ LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake event fd to epoll instance: %s",
+ strerror(errno));
+
+ for (size_t i = 0; i < mRequests.size(); i++) {
+ const Request& request = mRequests.valueAt(i);
+ struct epoll_event eventItem;
+ request.initEventItem(&eventItem);
+
+ int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, request.fd, & eventItem);
+ if (epollResult < 0) {
+ ALOGE("Error adding epoll events for fd %d while rebuilding epoll set: %s",
+ request.fd, strerror(errno));
+ }
+ }
+}
+
+void Looper::scheduleEpollRebuildLocked() {
+ if (!mEpollRebuildRequired) {
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ scheduleEpollRebuildLocked - scheduling epoll set rebuild", this);
+#endif
+ mEpollRebuildRequired = true;
+ wake();
+ }
+}
+
int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
int result = 0;
for (;;) {
@@ -206,7 +225,7 @@ int Looper::pollInner(int timeoutMillis) {
timeoutMillis = messageTimeoutMillis;
}
#if DEBUG_POLL_AND_WAKE
- ALOGD("%p ~ pollOnce - next message in %lldns, adjusted timeout: timeoutMillis=%d",
+ ALOGD("%p ~ pollOnce - next message in %" PRId64 "ns, adjusted timeout: timeoutMillis=%d",
this, mNextMessageUptime - now, timeoutMillis);
#endif
}
@@ -228,12 +247,19 @@ int Looper::pollInner(int timeoutMillis) {
// Acquire lock.
mLock.lock();
+ // Rebuild epoll set if needed.
+ if (mEpollRebuildRequired) {
+ mEpollRebuildRequired = false;
+ rebuildEpollLocked();
+ goto Done;
+ }
+
// Check for poll error.
if (eventCount < 0) {
if (errno == EINTR) {
goto Done;
}
- ALOGW("Poll failed with an unexpected error, errno=%d", errno);
+ ALOGW("Poll failed with an unexpected error: %s", strerror(errno));
result = POLL_ERROR;
goto Done;
}
@@ -255,11 +281,11 @@ int Looper::pollInner(int timeoutMillis) {
for (int i = 0; i < eventCount; i++) {
int fd = eventItems[i].data.fd;
uint32_t epollEvents = eventItems[i].events;
- if (fd == mWakeReadPipeFd) {
+ if (fd == mWakeEventFd) {
if (epollEvents & EPOLLIN) {
awoken();
} else {
- ALOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents);
+ ALOGW("Ignoring unexpected epoll events 0x%x on wake event fd.", epollEvents);
}
} else {
ssize_t requestIndex = mRequests.indexOfKey(fd);
@@ -326,10 +352,14 @@ Done: ;
ALOGD("%p ~ pollOnce - invoking fd event callback %p: fd=%d, events=0x%x, data=%p",
this, response.request.callback.get(), fd, events, data);
#endif
+ // Invoke the callback. Note that the file descriptor may be closed by
+ // the callback (and potentially even reused) before the function returns so
+ // we need to be a little careful when removing the file descriptor afterwards.
int callbackResult = response.request.callback->handleEvent(fd, events, data);
if (callbackResult == 0) {
- removeFd(fd);
+ removeFd(fd, response.request.seq);
}
+
// Clear the callback reference in the response structure promptly because we
// will not clear the response vector itself until the next poll.
response.request.callback.clear();
@@ -370,14 +400,12 @@ void Looper::wake() {
ALOGD("%p ~ wake", this);
#endif
- ssize_t nWrite;
- do {
- nWrite = write(mWakeWritePipeFd, "W", 1);
- } while (nWrite == -1 && errno == EINTR);
-
- if (nWrite != 1) {
+ uint64_t inc = 1;
+ ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd, &inc, sizeof(uint64_t)));
+ if (nWrite != sizeof(uint64_t)) {
if (errno != EAGAIN) {
- ALOGW("Could not write wake signal, errno=%d", errno);
+ LOG_ALWAYS_FATAL("Could not write wake signal to fd %d: %s",
+ mWakeEventFd, strerror(errno));
}
}
}
@@ -387,11 +415,8 @@ void Looper::awoken() {
ALOGD("%p ~ awoken", this);
#endif
- char buffer[16];
- ssize_t nRead;
- do {
- nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
- } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
+ uint64_t counter;
+ TEMP_FAILURE_RETRY(read(mWakeEventFd, &counter, sizeof(uint64_t)));
}
void Looper::pushResponse(int events, const Request& request) {
@@ -425,37 +450,62 @@ int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callb
ident = POLL_CALLBACK;
}
- int epollEvents = 0;
- if (events & EVENT_INPUT) epollEvents |= EPOLLIN;
- if (events & EVENT_OUTPUT) epollEvents |= EPOLLOUT;
-
{ // acquire lock
AutoMutex _l(mLock);
Request request;
request.fd = fd;
request.ident = ident;
+ request.events = events;
+ request.seq = mNextRequestSeq++;
request.callback = callback;
request.data = data;
+ if (mNextRequestSeq == -1) mNextRequestSeq = 0; // reserve sequence number -1
struct epoll_event eventItem;
- memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
- eventItem.events = epollEvents;
- eventItem.data.fd = fd;
+ request.initEventItem(&eventItem);
ssize_t requestIndex = mRequests.indexOfKey(fd);
if (requestIndex < 0) {
int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
if (epollResult < 0) {
- ALOGE("Error adding epoll events for fd %d, errno=%d", fd, errno);
+ ALOGE("Error adding epoll events for fd %d: %s", fd, strerror(errno));
return -1;
}
mRequests.add(fd, request);
} else {
int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_MOD, fd, & eventItem);
if (epollResult < 0) {
- ALOGE("Error modifying epoll events for fd %d, errno=%d", fd, errno);
- return -1;
+ if (errno == ENOENT) {
+ // Tolerate ENOENT because it means that an older file descriptor was
+ // closed before its callback was unregistered and meanwhile a new
+ // file descriptor with the same number has been created and is now
+ // being registered for the first time. This error may occur naturally
+ // when a callback has the side-effect of closing the file descriptor
+ // before returning and unregistering itself. Callback sequence number
+ // checks further ensure that the race is benign.
+ //
+ // Unfortunately due to kernel limitations we need to rebuild the epoll
+ // set from scratch because it may contain an old file handle that we are
+ // now unable to remove since its file descriptor is no longer valid.
+ // No such problem would have occurred if we were using the poll system
+ // call instead, but that approach carries others disadvantages.
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ addFd - EPOLL_CTL_MOD failed due to file descriptor "
+ "being recycled, falling back on EPOLL_CTL_ADD: %s",
+ this, strerror(errno));
+#endif
+ epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
+ if (epollResult < 0) {
+ ALOGE("Error modifying or adding epoll events for fd %d: %s",
+ fd, strerror(errno));
+ return -1;
+ }
+ scheduleEpollRebuildLocked();
+ } else {
+ ALOGE("Error modifying epoll events for fd %d: %s", fd, strerror(errno));
+ return -1;
+ }
}
mRequests.replaceValueAt(requestIndex, request);
}
@@ -464,8 +514,12 @@ int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callb
}
int Looper::removeFd(int fd) {
+ return removeFd(fd, -1);
+}
+
+int Looper::removeFd(int fd, int seq) {
#if DEBUG_CALLBACKS
- ALOGD("%p ~ removeFd - fd=%d", this, fd);
+ ALOGD("%p ~ removeFd - fd=%d, seq=%d", this, fd, seq);
#endif
{ // acquire lock
@@ -475,13 +529,48 @@ int Looper::removeFd(int fd) {
return 0;
}
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, NULL);
- if (epollResult < 0) {
- ALOGE("Error removing epoll events for fd %d, errno=%d", fd, errno);
- return -1;
+ // Check the sequence number if one was given.
+ if (seq != -1 && mRequests.valueAt(requestIndex).seq != seq) {
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ removeFd - sequence number mismatch, oldSeq=%d",
+ this, mRequests.valueAt(requestIndex).seq);
+#endif
+ return 0;
}
+ // Always remove the FD from the request map even if an error occurs while
+ // updating the epoll set so that we avoid accidentally leaking callbacks.
mRequests.removeItemsAt(requestIndex);
+
+ int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, NULL);
+ if (epollResult < 0) {
+ if (seq != -1 && (errno == EBADF || errno == ENOENT)) {
+ // Tolerate EBADF or ENOENT when the sequence number is known because it
+ // means that the file descriptor was closed before its callback was
+ // unregistered. This error may occur naturally when a callback has the
+ // side-effect of closing the file descriptor before returning and
+ // unregistering itself.
+ //
+ // Unfortunately due to kernel limitations we need to rebuild the epoll
+ // set from scratch because it may contain an old file handle that we are
+ // now unable to remove since its file descriptor is no longer valid.
+ // No such problem would have occurred if we were using the poll system
+ // call instead, but that approach carries others disadvantages.
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ removeFd - EPOLL_CTL_DEL failed due to file descriptor "
+ "being closed: %s", this, strerror(errno));
+#endif
+ scheduleEpollRebuildLocked();
+ } else {
+ // Some other error occurred. This is really weird because it means
+ // our list of callbacks got out of sync with the epoll set somehow.
+ // We defensively rebuild the epoll set to avoid getting spurious
+ // notifications with nowhere to go.
+ ALOGE("Error removing epoll events for fd %d: %s", fd, strerror(errno));
+ scheduleEpollRebuildLocked();
+ return -1;
+ }
+ }
} // release lock
return 1;
}
@@ -500,7 +589,7 @@ void Looper::sendMessageDelayed(nsecs_t uptimeDelay, const sp<MessageHandler>& h
void Looper::sendMessageAtTime(nsecs_t uptime, const sp<MessageHandler>& handler,
const Message& message) {
#if DEBUG_CALLBACKS
- ALOGD("%p ~ sendMessageAtTime - uptime=%lld, handler=%p, what=%d",
+ ALOGD("%p ~ sendMessageAtTime - uptime=%" PRId64 ", handler=%p, what=%d",
this, uptime, handler.get(), message.what);
#endif
@@ -570,4 +659,18 @@ bool Looper::isPolling() const {
return mPolling;
}
-} // namespace android \ No newline at end of file
+void Looper::Request::initEventItem(struct epoll_event* eventItem) const {
+ int epollEvents = 0;
+ if (events & EVENT_INPUT) epollEvents |= EPOLLIN;
+ if (events & EVENT_OUTPUT) epollEvents |= EPOLLOUT;
+
+ memset(eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
+ eventItem->events = epollEvents;
+ eventItem->data.fd = fd;
+}
+
+MessageHandler::~MessageHandler() { }
+
+LooperCallback::~LooperCallback() { }
+
+} // namespace android
diff --git a/libshims/utils/VectorImpl.cpp b/libshims/utils/VectorImpl.cpp
index 936cd37..ef3277f 100644
--- a/libshims/utils/VectorImpl.cpp
+++ b/libshims/utils/VectorImpl.cpp
@@ -16,16 +16,17 @@
#define LOG_TAG "Vector"
-#include <string.h>
-#include <stdlib.h>
+#include <utils/VectorImpl.h>
+
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <log/log.h>
+
#include <safe_iop.h>
-#include <SharedBuffer.h>
-#include <utils/Errors.h>
-#include <utils/VectorImpl.h>
+#include "SharedBuffer.h"
/*****************************************************************************/
@@ -62,7 +63,7 @@ VectorImpl::~VectorImpl()
"[%p] subclasses of VectorImpl must call finish_vector()"
" in their destructor. Leaking %d bytes.",
this, (int)(mCount*mItemSize));
- // We can't call _do_destroy() here because the vtable is already gone.
+ // We can't call _do_destroy() here because the vtable is already gone.
}
VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
@@ -198,13 +199,16 @@ status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
_do_copy(temp, item, 1);
ssize_t j = i-1;
- void* next = reinterpret_cast<char*>(array) + mItemSize*(i);
+ void* next = reinterpret_cast<char*>(array) + mItemSize*(i);
do {
_do_destroy(next, 1);
_do_copy(next, curr, 1);
next = curr;
--j;
- curr = reinterpret_cast<char*>(array) + mItemSize*(j);
+ curr = NULL;
+ if (j >= 0) {
+ curr = reinterpret_cast<char*>(array) + mItemSize*(j);
+ }
} while (j>=0 && (cmp(curr, temp, state) > 0));
_do_destroy(next, 1);
@@ -212,7 +216,7 @@ status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
}
i++;
}
-
+
if (temp) {
_do_destroy(temp, 1);
free(temp);
@@ -368,7 +372,7 @@ void VectorImpl::release_storage()
if (sb->release(SharedBuffer::eKeepStorage) == 1) {
_do_destroy(mStorage, mCount);
SharedBuffer::dealloc(sb);
- }
+ }
}
}
@@ -552,15 +556,6 @@ void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) con
do_move_backward(dest, from, num);
}
-void VectorImpl::reservedVectorImpl1() { }
-void VectorImpl::reservedVectorImpl2() { }
-void VectorImpl::reservedVectorImpl3() { }
-void VectorImpl::reservedVectorImpl4() { }
-void VectorImpl::reservedVectorImpl5() { }
-void VectorImpl::reservedVectorImpl6() { }
-void VectorImpl::reservedVectorImpl7() { }
-void VectorImpl::reservedVectorImpl8() { }
-
/*****************************************************************************/
SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags)
@@ -596,6 +591,10 @@ size_t SortedVectorImpl::orderOf(const void* item) const
ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const
{
+ if (order) *order = 0;
+ if (isEmpty()) {
+ return NAME_NOT_FOUND;
+ }
// binary search
ssize_t err = NAME_NOT_FOUND;
ssize_t l = 0;
@@ -676,15 +675,6 @@ ssize_t SortedVectorImpl::remove(const void* item)
return i;
}
-void SortedVectorImpl::reservedSortedVectorImpl1() { };
-void SortedVectorImpl::reservedSortedVectorImpl2() { };
-void SortedVectorImpl::reservedSortedVectorImpl3() { };
-void SortedVectorImpl::reservedSortedVectorImpl4() { };
-void SortedVectorImpl::reservedSortedVectorImpl5() { };
-void SortedVectorImpl::reservedSortedVectorImpl6() { };
-void SortedVectorImpl::reservedSortedVectorImpl7() { };
-void SortedVectorImpl::reservedSortedVectorImpl8() { };
-
/*****************************************************************************/
}; // namespace android