native_buffer.h revision 4d3590f3fd0fd65f4e8758d3228de9f55cf135d0
1#ifndef ANDROID_DVR_NATIVE_BUFFER_H_ 2#define ANDROID_DVR_NATIVE_BUFFER_H_ 3 4#include <EGL/egl.h> 5#include <EGL/eglext.h> 6#include <log/log.h> 7#include <system/window.h> 8#include <ui/ANativeObjectBase.h> 9#include <utils/RefBase.h> 10 11#include <private/dvr/buffer_hub_client.h> 12 13namespace android { 14namespace dvr { 15 16// ANativeWindowBuffer is the abstraction Android HALs and frameworks use to 17// pass around hardware graphics buffers. The following classes implement this 18// abstraction with different DVR backing buffers, all of which provide 19// different semantics on top of ion/gralloc buffers. 20 21// An implementation of ANativeWindowBuffer backed by an IonBuffer. 22class NativeBuffer 23 : public android::ANativeObjectBase<ANativeWindowBuffer, NativeBuffer, 24 android::LightRefBase<NativeBuffer>> { 25 public: 26 static constexpr int kEmptyFence = -1; 27 28 explicit NativeBuffer(const std::shared_ptr<IonBuffer>& buffer) 29 : BASE(), buffer_(buffer), fence_(kEmptyFence) { 30 ANativeWindowBuffer::width = buffer->width(); 31 ANativeWindowBuffer::height = buffer->height(); 32 ANativeWindowBuffer::stride = buffer->stride(); 33 ANativeWindowBuffer::format = buffer->format(); 34 ANativeWindowBuffer::usage = buffer->usage(); 35 handle = buffer_->handle(); 36 } 37 38 virtual ~NativeBuffer() {} 39 40 std::shared_ptr<IonBuffer> buffer() { return buffer_; } 41 int fence() const { return fence_.Get(); } 42 43 void SetFence(int fence) { fence_.Reset(fence); } 44 45 private: 46 friend class android::LightRefBase<NativeBuffer>; 47 48 std::shared_ptr<IonBuffer> buffer_; 49 pdx::LocalHandle fence_; 50 51 NativeBuffer(const NativeBuffer&) = delete; 52 void operator=(NativeBuffer&) = delete; 53}; 54 55// NativeBufferProducer is an implementation of ANativeWindowBuffer backed by a 56// BufferProducer. 57class NativeBufferProducer : public android::ANativeObjectBase< 58 ANativeWindowBuffer, NativeBufferProducer, 59 android::LightRefBase<NativeBufferProducer>> { 60 public: 61 static constexpr int kEmptyFence = -1; 62 63 NativeBufferProducer(const std::shared_ptr<BufferProducer>& buffer, 64 EGLDisplay display, uint32_t surface_buffer_index) 65 : BASE(), 66 buffer_(buffer), 67 surface_buffer_index_(surface_buffer_index), 68 display_(display) { 69 ANativeWindowBuffer::width = buffer_->width(); 70 ANativeWindowBuffer::height = buffer_->height(); 71 ANativeWindowBuffer::stride = buffer_->stride(); 72 ANativeWindowBuffer::format = buffer_->format(); 73 ANativeWindowBuffer::usage = buffer_->usage(); 74 handle = buffer_->native_handle(); 75 } 76 77 explicit NativeBufferProducer(const std::shared_ptr<BufferProducer>& buffer) 78 : NativeBufferProducer(buffer, nullptr, 0) {} 79 80 virtual ~NativeBufferProducer() { 81 for (EGLImageKHR egl_image : egl_images_) { 82 if (egl_image != EGL_NO_IMAGE_KHR) 83 eglDestroyImageKHR(display_, egl_image); 84 } 85 } 86 87 EGLImageKHR image_khr(int index) const { return egl_images_[index]; } 88 std::shared_ptr<BufferProducer> buffer() const { return buffer_; } 89 int release_fence() const { return release_fence_.Get(); } 90 uint32_t surface_buffer_index() const { return surface_buffer_index_; } 91 92 // Return the release fence, passing ownership to the caller. 93 pdx::LocalHandle ClaimReleaseFence() { return std::move(release_fence_); } 94 95 // Post the buffer consumer, closing the acquire and release fences. 96 int Post(int acquire_fence, uint64_t sequence) { 97 release_fence_.Close(); 98 return buffer_->Post(pdx::LocalHandle(acquire_fence), sequence); 99 } 100 101 // Gain the buffer producer, closing the previous release fence if valid. 102 int Gain() { return buffer_->Gain(&release_fence_); } 103 104 // Asynchronously gain the buffer, closing the previous release fence. 105 int GainAsync() { 106 release_fence_.Close(); 107 return buffer_->GainAsync(); 108 } 109 110 private: 111 friend class android::LightRefBase<NativeBufferProducer>; 112 113 std::shared_ptr<BufferProducer> buffer_; 114 pdx::LocalHandle release_fence_; 115 std::vector<EGLImageKHR> egl_images_; 116 uint32_t surface_buffer_index_; 117 EGLDisplay display_; 118 119 NativeBufferProducer(const NativeBufferProducer&) = delete; 120 void operator=(NativeBufferProducer&) = delete; 121}; 122 123// NativeBufferConsumer is an implementation of ANativeWindowBuffer backed by a 124// BufferConsumer. 125class NativeBufferConsumer : public android::ANativeObjectBase< 126 ANativeWindowBuffer, NativeBufferConsumer, 127 android::LightRefBase<NativeBufferConsumer>> { 128 public: 129 static constexpr int kEmptyFence = -1; 130 131 explicit NativeBufferConsumer(const std::shared_ptr<BufferConsumer>& buffer) 132 : BASE(), buffer_(buffer), acquire_fence_(kEmptyFence), sequence_(0) { 133 ANativeWindowBuffer::width = buffer_->width(); 134 ANativeWindowBuffer::height = buffer_->height(); 135 ANativeWindowBuffer::stride = buffer_->stride(); 136 ANativeWindowBuffer::format = buffer_->format(); 137 ANativeWindowBuffer::usage = buffer_->usage(); 138 handle = buffer_->native_handle(); 139 } 140 141 virtual ~NativeBufferConsumer() {} 142 143 std::shared_ptr<BufferConsumer> buffer() const { return buffer_; } 144 int acquire_fence() const { return acquire_fence_.Get(); } 145 uint64_t sequence() const { return sequence_; } 146 147 // Return the acquire fence, passing ownership to the caller. 148 pdx::LocalHandle ClaimAcquireFence() { return std::move(acquire_fence_); } 149 150 // Acquire the underlying buffer consumer, closing the previous acquire fence 151 // if valid. 152 int Acquire() { return buffer_->Acquire(&acquire_fence_, &sequence_); } 153 154 // Release the buffer consumer, closing the acquire and release fences if 155 // valid. 156 int Release(int release_fence) { 157 acquire_fence_.Close(); 158 sequence_ = 0; 159 return buffer_->Release(pdx::LocalHandle(release_fence)); 160 } 161 162 private: 163 friend class android::LightRefBase<NativeBufferConsumer>; 164 165 std::shared_ptr<BufferConsumer> buffer_; 166 pdx::LocalHandle acquire_fence_; 167 uint64_t sequence_; 168 169 NativeBufferConsumer(const NativeBufferConsumer&) = delete; 170 void operator=(NativeBufferConsumer&) = delete; 171}; 172 173} // namespace dvr 174} // namespace android 175 176#endif // ANDROID_DVR_NATIVE_BUFFER_H_ 177