blob: c70f18823c774a49c10445e7c3d3e7ba4e385154 [file] [log] [blame]
Jiwen 'Steve' Cai8f51ec62018-08-07 21:50:51 -07001/*
2 * Copyright (C) 2018 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
Jiwen 'Steve' Cai0728fa92018-04-24 19:03:14 -070017#include <poll.h>
18
Fan Xu069e8382018-11-16 16:28:08 -080019#include <android-base/unique_fd.h>
Fan Xu021776e2018-12-05 13:34:48 -080020#include <android/frameworks/bufferhub/1.0/IBufferHub.h>
21#include <log/log.h>
Fan Xu069e8382018-11-16 16:28:08 -080022#include <ui/BufferHubBuffer.h>
Fan Xucfbe0742018-11-21 15:03:32 -080023#include <ui/BufferHubDefs.h>
Fan Xu021776e2018-12-05 13:34:48 -080024#include <utils/Trace.h>
Fan Xu069e8382018-11-16 16:28:08 -080025
Fan Xu021776e2018-12-05 13:34:48 -080026using ::android::base::unique_fd;
27using ::android::BufferHubDefs::AnyClientAcquired;
28using ::android::BufferHubDefs::AnyClientGained;
29using ::android::BufferHubDefs::IsClientAcquired;
30using ::android::BufferHubDefs::IsClientGained;
31using ::android::BufferHubDefs::IsClientPosted;
32using ::android::BufferHubDefs::IsClientReleased;
33using ::android::frameworks::bufferhub::V1_0::BufferHubStatus;
34using ::android::frameworks::bufferhub::V1_0::BufferTraits;
35using ::android::frameworks::bufferhub::V1_0::IBufferClient;
36using ::android::frameworks::bufferhub::V1_0::IBufferHub;
37using ::android::hardware::hidl_handle;
38using ::android::hardware::graphics::common::V1_2::HardwareBufferDescription;
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070039
40namespace android {
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -070041
Fan Xu021776e2018-12-05 13:34:48 -080042std::unique_ptr<BufferHubBuffer> BufferHubBuffer::Create(uint32_t width, uint32_t height,
43 uint32_t layerCount, uint32_t format,
44 uint64_t usage, size_t userMetadataSize) {
45 auto buffer = std::unique_ptr<BufferHubBuffer>(
46 new BufferHubBuffer(width, height, layerCount, format, usage, userMetadataSize));
47 return buffer->IsValid() ? std::move(buffer) : nullptr;
Jiwen 'Steve' Cai088b3b62018-10-03 18:49:07 -070048}
49
Fan Xu021776e2018-12-05 13:34:48 -080050std::unique_ptr<BufferHubBuffer> BufferHubBuffer::Import(const native_handle_t* token) {
51 if (token == nullptr) {
52 ALOGE("%s: token cannot be nullptr!", __FUNCTION__);
53 return nullptr;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070054 }
Fan Xu021776e2018-12-05 13:34:48 -080055
56 auto buffer = std::unique_ptr<BufferHubBuffer>(new BufferHubBuffer(token));
57 return buffer->IsValid() ? std::move(buffer) : nullptr;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070058}
59
60BufferHubBuffer::BufferHubBuffer(uint32_t width, uint32_t height, uint32_t layerCount,
Fan Xu021776e2018-12-05 13:34:48 -080061 uint32_t format, uint64_t usage, size_t userMetadataSize) {
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070062 ATRACE_CALL();
Fan Xu021776e2018-12-05 13:34:48 -080063 ALOGD("%s: width=%u height=%u layerCount=%u, format=%u "
64 "usage=%" PRIx64 " mUserMetadataSize=%zu",
65 __FUNCTION__, width, height, layerCount, format, usage, userMetadataSize);
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070066
Fan Xu021776e2018-12-05 13:34:48 -080067 sp<IBufferHub> bufferhub = IBufferHub::getService();
68 if (bufferhub.get() == nullptr) {
69 ALOGE("%s: BufferHub service not found!", __FUNCTION__);
70 return;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -070071 }
72
Fan Xu021776e2018-12-05 13:34:48 -080073 AHardwareBuffer_Desc aDesc = {width, height, layerCount, format,
74 usage, /*stride=*/0UL, /*rfu0=*/0UL, /*rfu1=*/0ULL};
75 HardwareBufferDescription desc;
76 memcpy(&desc, &aDesc, sizeof(HardwareBufferDescription));
77
78 BufferHubStatus ret;
79 sp<IBufferClient> client;
80 BufferTraits bufferTraits;
81 IBufferHub::allocateBuffer_cb alloc_cb = [&](const auto& status, const auto& outClient,
82 const auto& traits) {
83 ret = status;
84 client = std::move(outClient);
85 bufferTraits = std::move(traits);
86 };
87
88 if (!bufferhub->allocateBuffer(desc, static_cast<uint32_t>(userMetadataSize), alloc_cb)
89 .isOk()) {
90 ALOGE("%s: allocateBuffer transaction failed!", __FUNCTION__);
91 return;
92 } else if (ret != BufferHubStatus::NO_ERROR) {
93 ALOGE("%s: allocateBuffer failed with error %u.", __FUNCTION__, ret);
94 return;
95 } else if (client == nullptr) {
96 ALOGE("%s: allocateBuffer got null BufferClient.", __FUNCTION__);
97 return;
98 }
99
100 const int importRet = initWithBufferTraits(bufferTraits);
101 if (importRet < 0) {
102 ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-importRet));
103 client->close();
104 }
105 mBufferClient = std::move(client);
106}
107
108BufferHubBuffer::BufferHubBuffer(const native_handle_t* token) {
109 sp<IBufferHub> bufferhub = IBufferHub::getService();
110 if (bufferhub.get() == nullptr) {
111 ALOGE("%s: BufferHub service not found!", __FUNCTION__);
112 return;
113 }
114
115 BufferHubStatus ret;
116 sp<IBufferClient> client;
117 BufferTraits bufferTraits;
118 IBufferHub::importBuffer_cb import_cb = [&](const auto& status, const auto& outClient,
119 const auto& traits) {
120 ret = status;
121 client = std::move(outClient);
122 bufferTraits = std::move(traits);
123 };
124
125 // hidl_handle(native_handle_t*) simply creates a raw pointer reference withouth ownership
126 // transfer.
127 if (!bufferhub->importBuffer(hidl_handle(token), import_cb).isOk()) {
128 ALOGE("%s: importBuffer transaction failed!", __FUNCTION__);
129 return;
130 } else if (ret != BufferHubStatus::NO_ERROR) {
131 ALOGE("%s: importBuffer failed with error %u.", __FUNCTION__, ret);
132 return;
133 } else if (client == nullptr) {
134 ALOGE("%s: importBuffer got null BufferClient.", __FUNCTION__);
135 return;
136 }
137
138 const int importRet = initWithBufferTraits(bufferTraits);
139 if (importRet < 0) {
140 ALOGE("%s: Failed to import buffer: %s", __FUNCTION__, strerror(-importRet));
141 client->close();
142 }
143 mBufferClient = std::move(client);
144}
145
146BufferHubBuffer::~BufferHubBuffer() {
147 // Close buffer client to avoid possible race condition: user could first duplicate and hold
148 // token with the original buffer gone, and then try to import the token. The close function
149 // will explicitly invalidate the token to avoid this.
150 if (mBufferClient != nullptr) {
151 if (!mBufferClient->close().isOk()) {
152 ALOGE("%s: close BufferClient transaction failed!", __FUNCTION__);
153 }
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700154 }
155}
156
Fan Xu021776e2018-12-05 13:34:48 -0800157int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) {
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700158 ATRACE_CALL();
159
Fan Xu021776e2018-12-05 13:34:48 -0800160 if (bufferTraits.bufferInfo.getNativeHandle() == nullptr) {
161 ALOGE("%s: missing buffer info handle.", __FUNCTION__);
162 return -EINVAL;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700163 }
164
Fan Xu021776e2018-12-05 13:34:48 -0800165 if (bufferTraits.bufferHandle.getNativeHandle() == nullptr) {
166 ALOGE("%s: missing gralloc handle.", __FUNCTION__);
167 return -EINVAL;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700168 }
169
Fan Xu021776e2018-12-05 13:34:48 -0800170 int bufferId = bufferTraits.bufferInfo->data[1];
171 if (bufferId < 0) {
172 ALOGE("%s: Received an invalid (negative) id!", __FUNCTION__);
173 return -EINVAL;
174 }
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700175
Fan Xu021776e2018-12-05 13:34:48 -0800176 uint32_t clientBitMask;
177 memcpy(&clientBitMask, &bufferTraits.bufferInfo->data[2], sizeof(clientBitMask));
178 if (clientBitMask == 0U) {
179 ALOGE("%s: Received a invalid client state mask!", __FUNCTION__);
180 return -EINVAL;
181 }
182
183 // Import the metadata. Dup since hidl_handle owns the fd
184 unique_fd ashmemFd(dup(bufferTraits.bufferInfo->data[0]));
185 mMetadata = BufferHubMetadata::Import(std::move(ashmemFd));
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700186
187 if (!mMetadata.IsValid()) {
Tianyu Jiangdf9d91d2018-12-17 10:48:54 -0800188 ALOGE("%s: invalid metadata.", __FUNCTION__);
Fan Xu021776e2018-12-05 13:34:48 -0800189 return -EINVAL;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700190 }
191
Fan Xu021776e2018-12-05 13:34:48 -0800192 uint32_t userMetadataSize;
193 memcpy(&userMetadataSize, &bufferTraits.bufferInfo->data[3], sizeof(userMetadataSize));
194 if (mMetadata.user_metadata_size() != userMetadataSize) {
195 ALOGE("%s: user metadata size not match: expected %u, actual %zu.", __FUNCTION__,
196 userMetadataSize, mMetadata.user_metadata_size());
197 return -EINVAL;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700198 }
199
Fan Xu021776e2018-12-05 13:34:48 -0800200 size_t metadataSize = static_cast<size_t>(mMetadata.metadata_size());
Fan Xucfbe0742018-11-21 15:03:32 -0800201 if (metadataSize < BufferHubDefs::kMetadataHeaderSize) {
Tianyu Jiangdf9d91d2018-12-17 10:48:54 -0800202 ALOGE("%s: metadata too small: %zu", __FUNCTION__, metadataSize);
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700203 return -EINVAL;
204 }
205
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800206 // Populate shortcuts to the atomics in metadata.
207 auto metadata_header = mMetadata.metadata_header();
208 buffer_state_ = &metadata_header->buffer_state;
209 fence_state_ = &metadata_header->fence_state;
210 active_clients_bit_mask_ = &metadata_header->active_clients_bit_mask;
Tianyu Jiang2ceb3202018-12-17 12:58:34 -0800211 // The C++ standard recommends (but does not require) that lock-free atomic operations are
212 // also address-free, that is, suitable for communication between processes using shared
213 // memory.
214 LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) ||
215 !std::atomic_is_lock_free(fence_state_) ||
216 !std::atomic_is_lock_free(active_clients_bit_mask_),
217 "Atomic variables in ashmen are not lock free.");
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800218
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700219 // Import the buffer: We only need to hold on the native_handle_t here so that
220 // GraphicBuffer instance can be created in future.
Fan Xu021776e2018-12-05 13:34:48 -0800221 mBufferHandle = std::move(bufferTraits.bufferHandle);
222 memcpy(&mBufferDesc, &bufferTraits.bufferDesc, sizeof(AHardwareBuffer_Desc));
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700223
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700224 mId = bufferId;
Fan Xu021776e2018-12-05 13:34:48 -0800225 mClientStateMask = clientBitMask;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700226
227 // TODO(b/112012161) Set up shared fences.
Fan Xu021776e2018-12-05 13:34:48 -0800228 ALOGD("%s: id=%d, buffer_state=%" PRIx32 ".", __FUNCTION__, mId,
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800229 buffer_state_->load(std::memory_order_acquire));
230 return 0;
231}
232
233int BufferHubBuffer::Gain() {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800234 uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800235 if (IsClientGained(current_buffer_state, mClientStateMask)) {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800236 ALOGV("%s: Buffer is already gained by this client %" PRIx32 ".", __FUNCTION__,
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800237 mClientStateMask);
238 return 0;
239 }
240 do {
241 if (AnyClientGained(current_buffer_state & (~mClientStateMask)) ||
242 AnyClientAcquired(current_buffer_state)) {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800243 ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800244 __FUNCTION__, mId, mClientStateMask, current_buffer_state);
245 return -EBUSY;
246 }
247 // Change the buffer state to gained state, whose value happens to be the same as
248 // mClientStateMask.
249 } while (!buffer_state_->compare_exchange_weak(current_buffer_state, mClientStateMask,
250 std::memory_order_acq_rel,
251 std::memory_order_acquire));
252 // TODO(b/119837586): Update fence state and return GPU fence.
253 return 0;
254}
255
256int BufferHubBuffer::Post() {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800257 uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
Fan Xu021776e2018-12-05 13:34:48 -0800258 uint32_t updated_buffer_state = (~mClientStateMask) & BufferHubDefs::kHighBitsMask;
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800259 do {
260 if (!IsClientGained(current_buffer_state, mClientStateMask)) {
261 ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d "
Tianyu Jianga99f9112018-12-13 18:23:07 -0800262 "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800263 __FUNCTION__, mId, mClientStateMask, current_buffer_state);
264 return -EBUSY;
265 }
266 // Set the producer client buffer state to released, other clients' buffer state to posted.
Tianyu Jiangec97b762019-01-07 17:14:02 -0800267 // Post to all existing and non-existing clients.
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800268 } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
269 std::memory_order_acq_rel,
270 std::memory_order_acquire));
271 // TODO(b/119837586): Update fence state and return GPU fence if needed.
272 return 0;
273}
274
275int BufferHubBuffer::Acquire() {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800276 uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800277 if (IsClientAcquired(current_buffer_state, mClientStateMask)) {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800278 ALOGV("%s: Buffer is already acquired by this client %" PRIx32 ".", __FUNCTION__,
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800279 mClientStateMask);
280 return 0;
281 }
Tianyu Jianga99f9112018-12-13 18:23:07 -0800282 uint32_t updated_buffer_state = 0U;
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800283 do {
284 if (!IsClientPosted(current_buffer_state, mClientStateMask)) {
285 ALOGE("%s: Cannot acquire a buffer that is not in posted state. buffer_id=%d "
Tianyu Jianga99f9112018-12-13 18:23:07 -0800286 "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800287 __FUNCTION__, mId, mClientStateMask, current_buffer_state);
288 return -EBUSY;
289 }
290 // Change the buffer state for this consumer from posted to acquired.
291 updated_buffer_state = current_buffer_state ^ mClientStateMask;
292 } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
293 std::memory_order_acq_rel,
294 std::memory_order_acquire));
295 // TODO(b/119837586): Update fence state and return GPU fence.
296 return 0;
297}
298
299int BufferHubBuffer::Release() {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800300 uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800301 if (IsClientReleased(current_buffer_state, mClientStateMask)) {
Tianyu Jianga99f9112018-12-13 18:23:07 -0800302 ALOGV("%s: Buffer is already released by this client %" PRIx32 ".", __FUNCTION__,
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800303 mClientStateMask);
304 return 0;
305 }
Tianyu Jianga99f9112018-12-13 18:23:07 -0800306 uint32_t updated_buffer_state = 0U;
Tianyu Jiangb08b7222018-11-16 17:55:26 -0800307 do {
308 updated_buffer_state = current_buffer_state & (~mClientStateMask);
309 } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
310 std::memory_order_acq_rel,
311 std::memory_order_acquire));
312 // TODO(b/119837586): Update fence state and return GPU fence if needed.
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700313 return 0;
314}
315
Fan Xu021776e2018-12-05 13:34:48 -0800316bool BufferHubBuffer::IsValid() const {
317 // TODO(b/68770788): check eventFd once implemented
318 return mBufferHandle.getNativeHandle() != nullptr && mId >= 0 && mClientStateMask != 0U &&
319 mMetadata.IsValid() && mBufferClient != nullptr;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700320}
321
Fan Xu021776e2018-12-05 13:34:48 -0800322// TODO(b/68770788): implement Poll() once we have event fd
323int BufferHubBuffer::Poll(int /* timeoutMs */) {
324 return -ENOSYS;
325}
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -0700326
Fan Xu021776e2018-12-05 13:34:48 -0800327native_handle_t* BufferHubBuffer::Duplicate() {
328 if (mBufferClient == nullptr) {
329 ALOGE("%s: missing BufferClient!", __FUNCTION__);
330 return nullptr;
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700331 }
Fan Xu021776e2018-12-05 13:34:48 -0800332
333 hidl_handle token;
334 BufferHubStatus ret;
335 IBufferClient::duplicate_cb dup_cb = [&](const auto& outToken, const auto& status) {
336 token = std::move(outToken);
337 ret = status;
338 };
339
340 if (!mBufferClient->duplicate(dup_cb).isOk()) {
341 ALOGE("%s: duplicate transaction failed!", __FUNCTION__);
342 return nullptr;
343 } else if (ret != BufferHubStatus::NO_ERROR) {
344 ALOGE("%s: duplicate failed with error %u.", __FUNCTION__, ret);
345 return nullptr;
346 } else if (token.getNativeHandle() == nullptr) {
347 ALOGE("%s: duplicate got null token.", __FUNCTION__);
348 return nullptr;
349 }
350
351 return native_handle_clone(token.getNativeHandle());
Jiwen 'Steve' Caia8049a22018-03-28 15:14:02 -0700352}
353
Jiwen 'Steve' Caiff675b72018-10-09 18:08:29 -0700354} // namespace android