Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 1 | #include <private/dvr/detached_buffer.h> |
| 2 | |
| 3 | #include <pdx/file_handle.h> |
| 4 | #include <ui/DetachedBufferHandle.h> |
| 5 | |
Jiwen 'Steve' Cai | 0728fa9 | 2018-04-24 19:03:14 -0700 | [diff] [blame] | 6 | #include <poll.h> |
| 7 | |
| 8 | using android::pdx::LocalChannelHandle; |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 9 | using android::pdx::LocalHandle; |
Jiwen 'Steve' Cai | 0728fa9 | 2018-04-24 19:03:14 -0700 | [diff] [blame] | 10 | using android::pdx::Status; |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 11 | |
| 12 | namespace android { |
| 13 | namespace dvr { |
| 14 | |
| 15 | DetachedBuffer::DetachedBuffer(uint32_t width, uint32_t height, |
| 16 | uint32_t layer_count, uint32_t format, |
| 17 | uint64_t usage, size_t user_metadata_size) { |
| 18 | ATRACE_NAME("DetachedBuffer::DetachedBuffer"); |
| 19 | ALOGD_IF(TRACE, |
| 20 | "DetachedBuffer::DetachedBuffer: width=%u height=%u layer_count=%u, " |
| 21 | "format=%u usage=%" PRIx64 " user_metadata_size=%zu", |
| 22 | width, height, layer_count, format, usage, user_metadata_size); |
| 23 | |
| 24 | auto status = client_.InvokeRemoteMethod<DetachedBufferRPC::Create>( |
| 25 | width, height, layer_count, format, usage, user_metadata_size); |
| 26 | if (!status) { |
| 27 | ALOGE( |
| 28 | "DetachedBuffer::DetachedBuffer: Failed to create detached buffer: %s", |
| 29 | status.GetErrorMessage().c_str()); |
| 30 | client_.Close(-status.error()); |
| 31 | } |
| 32 | |
| 33 | const int ret = ImportGraphicBuffer(); |
| 34 | if (ret < 0) { |
| 35 | ALOGE("DetachedBuffer::DetachedBuffer: Failed to import buffer: %s", |
| 36 | strerror(-ret)); |
| 37 | client_.Close(ret); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | DetachedBuffer::DetachedBuffer(LocalChannelHandle channel_handle) |
| 42 | : client_(std::move(channel_handle)) { |
| 43 | const int ret = ImportGraphicBuffer(); |
| 44 | if (ret < 0) { |
| 45 | ALOGE("DetachedBuffer::DetachedBuffer: Failed to import buffer: %s", |
| 46 | strerror(-ret)); |
| 47 | client_.Close(ret); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | int DetachedBuffer::ImportGraphicBuffer() { |
Jiwen 'Steve' Cai | f326fce | 2018-08-06 17:36:50 -0700 | [diff] [blame] | 52 | ATRACE_NAME("DetachedBuffer::ImportGraphicBuffer"); |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 53 | |
| 54 | auto status = client_.InvokeRemoteMethod<DetachedBufferRPC::Import>(); |
| 55 | if (!status) { |
| 56 | ALOGE("DetachedBuffer::DetachedBuffer: Failed to import GraphicBuffer: %s", |
| 57 | status.GetErrorMessage().c_str()); |
| 58 | return -status.error(); |
| 59 | } |
| 60 | |
| 61 | BufferDescription<LocalHandle> buffer_desc = status.take(); |
| 62 | if (buffer_desc.id() < 0) { |
| 63 | ALOGE("DetachedBuffer::DetachedBuffer: Received an invalid id!"); |
| 64 | return -EIO; |
| 65 | } |
| 66 | |
| 67 | // Stash the buffer id to replace the value in id_. |
| 68 | const int buffer_id = buffer_desc.id(); |
| 69 | |
| 70 | // Import the buffer. |
| 71 | IonBuffer ion_buffer; |
| 72 | ALOGD_IF(TRACE, "DetachedBuffer::DetachedBuffer: id=%d.", buffer_id); |
| 73 | |
| 74 | if (const int ret = buffer_desc.ImportBuffer(&ion_buffer)) { |
| 75 | ALOGE("Failed to import GraphicBuffer, error=%d", ret); |
| 76 | return ret; |
| 77 | } |
| 78 | |
Jiwen 'Steve' Cai | f326fce | 2018-08-06 17:36:50 -0700 | [diff] [blame] | 79 | // Import the metadata. |
| 80 | IonBuffer metadata_buffer; |
| 81 | if (const int ret = buffer_desc.ImportMetadata(&metadata_buffer)) { |
| 82 | ALOGE("Failed to import metadata buffer, error=%d", ret); |
| 83 | return ret; |
| 84 | } |
| 85 | size_t metadata_buf_size = metadata_buffer.width(); |
| 86 | if (metadata_buf_size < BufferHubDefs::kMetadataHeaderSize) { |
| 87 | ALOGE("DetachedBuffer::ImportGraphicBuffer: metadata buffer too small: %zu", |
| 88 | metadata_buf_size); |
| 89 | return -EINVAL; |
| 90 | } |
| 91 | |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 92 | // If all imports succeed, replace the previous buffer and id. |
| 93 | id_ = buffer_id; |
| 94 | buffer_ = std::move(ion_buffer); |
Jiwen 'Steve' Cai | f326fce | 2018-08-06 17:36:50 -0700 | [diff] [blame] | 95 | metadata_buffer_ = std::move(metadata_buffer); |
| 96 | user_metadata_size_ = metadata_buf_size - BufferHubDefs::kMetadataHeaderSize; |
| 97 | |
| 98 | void* metadata_ptr = nullptr; |
| 99 | if (const int ret = |
| 100 | metadata_buffer_.Lock(BufferHubDefs::kMetadataUsage, /*x=*/0, |
| 101 | /*y=*/0, metadata_buf_size, |
| 102 | /*height=*/1, &metadata_ptr)) { |
| 103 | ALOGE("DetachedBuffer::ImportGraphicBuffer: Failed to lock metadata."); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | // TODO(b/112012161) Set up shared fences. |
| 108 | |
| 109 | // Note that here the buffer state is mapped from shared memory as an atomic |
| 110 | // object. The std::atomic's constructor will not be called so that the |
| 111 | // original value stored in the memory region can be preserved. |
| 112 | metadata_header_ = static_cast<BufferHubDefs::MetadataHeader*>(metadata_ptr); |
| 113 | if (user_metadata_size_) { |
| 114 | user_metadata_ptr_ = static_cast<void*>(metadata_header_ + 1); |
| 115 | } else { |
| 116 | user_metadata_ptr_ = nullptr; |
| 117 | } |
| 118 | |
| 119 | id_ = buffer_desc.id(); |
| 120 | buffer_state_bit_ = buffer_desc.buffer_state_bit(); |
| 121 | |
| 122 | ALOGD_IF(TRACE, |
| 123 | "DetachedBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 |
| 124 | ".", |
| 125 | id(), metadata_header_->buffer_state.load()); |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Jiwen 'Steve' Cai | 0728fa9 | 2018-04-24 19:03:14 -0700 | [diff] [blame] | 129 | int DetachedBuffer::Poll(int timeout_ms) { |
| 130 | ATRACE_NAME("DetachedBuffer::Poll"); |
| 131 | pollfd p = {client_.event_fd(), POLLIN, 0}; |
| 132 | return poll(&p, 1, timeout_ms); |
| 133 | } |
| 134 | |
| 135 | Status<LocalChannelHandle> DetachedBuffer::Promote() { |
| 136 | ATRACE_NAME("DetachedBuffer::Promote"); |
| 137 | ALOGD_IF(TRACE, "DetachedBuffer::Promote: id=%d.", id_); |
| 138 | |
| 139 | auto status_or_handle = |
| 140 | client_.InvokeRemoteMethod<DetachedBufferRPC::Promote>(); |
| 141 | if (status_or_handle.ok()) { |
| 142 | // Invalidate the buffer. |
| 143 | buffer_ = {}; |
| 144 | } else { |
| 145 | ALOGE("DetachedBuffer::Promote: Failed to promote buffer (id=%d): %s.", id_, |
| 146 | status_or_handle.GetErrorMessage().c_str()); |
| 147 | } |
| 148 | return status_or_handle; |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jiwen 'Steve' Cai | 2e06c1c | 2018-07-30 21:35:32 -0700 | [diff] [blame] | 151 | Status<LocalChannelHandle> DetachedBuffer::Duplicate() { |
| 152 | ATRACE_NAME("DetachedBuffer::Duplicate"); |
| 153 | ALOGD_IF(TRACE, "DetachedBuffer::Duplicate: id=%d.", id_); |
| 154 | |
| 155 | auto status_or_handle = |
| 156 | client_.InvokeRemoteMethod<DetachedBufferRPC::Duplicate>(); |
| 157 | |
| 158 | if (!status_or_handle.ok()) { |
| 159 | ALOGE("DetachedBuffer::Duplicate: Failed to duplicate buffer (id=%d): %s.", |
| 160 | id_, status_or_handle.GetErrorMessage().c_str()); |
| 161 | } |
| 162 | return status_or_handle; |
| 163 | } |
| 164 | |
Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 165 | } // namespace dvr |
| 166 | } // namespace android |