1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
#include <private/dvr/producer_buffer.h>
using android::pdx::LocalChannelHandle;
using android::pdx::LocalHandle;
using android::pdx::Status;
namespace android {
namespace dvr {
ProducerBuffer::ProducerBuffer(uint32_t width, uint32_t height, uint32_t format,
uint64_t usage, size_t user_metadata_size)
: BASE(BufferHubRPC::kClientPath) {
ATRACE_NAME("ProducerBuffer::ProducerBuffer");
ALOGD_IF(TRACE,
"ProducerBuffer::ProducerBuffer: fd=%d width=%u height=%u format=%u "
"usage=%" PRIx64 " user_metadata_size=%zu",
event_fd(), width, height, format, usage, user_metadata_size);
auto status = InvokeRemoteMethod<BufferHubRPC::CreateBuffer>(
width, height, format, usage, user_metadata_size);
if (!status) {
ALOGE(
"ProducerBuffer::ProducerBuffer: Failed to create producer buffer: %s",
status.GetErrorMessage().c_str());
Close(-status.error());
return;
}
const int ret = ImportBuffer();
if (ret < 0) {
ALOGE(
"ProducerBuffer::ProducerBuffer: Failed to import producer buffer: %s",
strerror(-ret));
Close(ret);
}
}
ProducerBuffer::ProducerBuffer(uint64_t usage, size_t size)
: BASE(BufferHubRPC::kClientPath) {
ATRACE_NAME("ProducerBuffer::ProducerBuffer");
ALOGD_IF(TRACE, "ProducerBuffer::ProducerBuffer: usage=%" PRIx64 " size=%zu",
usage, size);
const int width = static_cast<int>(size);
const int height = 1;
const int format = HAL_PIXEL_FORMAT_BLOB;
const size_t user_metadata_size = 0;
auto status = InvokeRemoteMethod<BufferHubRPC::CreateBuffer>(
width, height, format, usage, user_metadata_size);
if (!status) {
ALOGE("ProducerBuffer::ProducerBuffer: Failed to create blob: %s",
status.GetErrorMessage().c_str());
Close(-status.error());
return;
}
const int ret = ImportBuffer();
if (ret < 0) {
ALOGE(
"ProducerBuffer::ProducerBuffer: Failed to import producer buffer: %s",
strerror(-ret));
Close(ret);
}
}
ProducerBuffer::ProducerBuffer(LocalChannelHandle channel)
: BASE(std::move(channel)) {
const int ret = ImportBuffer();
if (ret < 0) {
ALOGE(
"ProducerBuffer::ProducerBuffer: Failed to import producer buffer: %s",
strerror(-ret));
Close(ret);
}
}
int ProducerBuffer::LocalPost(const DvrNativeBufferMetadata* meta,
const LocalHandle& ready_fence) {
if (const int error = CheckMetadata(meta->user_metadata_size))
return error;
// The buffer can be posted iff the buffer state for this client is gained.
uint32_t current_buffer_state =
buffer_state_->load(std::memory_order_acquire);
if (!BufferHubDefs::isClientGained(current_buffer_state,
client_state_mask())) {
ALOGE("%s: not gained, id=%d state=%" PRIx32 ".", __FUNCTION__, id(),
current_buffer_state);
return -EBUSY;
}
// Set the producer client buffer state to released, that of all other clients
// (both existing and non-existing clients) to posted.
uint32_t updated_buffer_state =
(~client_state_mask()) & BufferHubDefs::kHighBitsMask;
while (!buffer_state_->compare_exchange_weak(
current_buffer_state, updated_buffer_state, std::memory_order_acq_rel,
std::memory_order_acquire)) {
if (!BufferHubDefs::isClientGained(current_buffer_state,
client_state_mask())) {
ALOGE(
"%s: Failed to post the buffer. The buffer is no longer gained, "
"id=%d state=%" PRIx32 ".",
__FUNCTION__, id(), current_buffer_state);
return -EBUSY;
}
}
// Copy the canonical metadata.
void* metadata_ptr = reinterpret_cast<void*>(&metadata_header_->metadata);
memcpy(metadata_ptr, meta, sizeof(DvrNativeBufferMetadata));
// Copy extra user requested metadata.
if (meta->user_metadata_ptr && meta->user_metadata_size) {
void* metadata_src = reinterpret_cast<void*>(meta->user_metadata_ptr);
memcpy(user_metadata_ptr_, metadata_src, meta->user_metadata_size);
}
// Send out the acquire fence through the shared epoll fd. Note that during
// posting no consumer is not expected to be polling on the fence.
if (const int error = UpdateSharedFence(ready_fence, shared_acquire_fence_))
return error;
return 0;
}
int ProducerBuffer::Post(const LocalHandle& ready_fence, const void* meta,
size_t user_metadata_size) {
ATRACE_NAME("ProducerBuffer::Post");
// Populate cononical metadata for posting.
DvrNativeBufferMetadata canonical_meta;
canonical_meta.user_metadata_ptr = reinterpret_cast<uint64_t>(meta);
canonical_meta.user_metadata_size = user_metadata_size;
if (const int error = LocalPost(&canonical_meta, ready_fence))
return error;
return ReturnStatusOrError(InvokeRemoteMethod<BufferHubRPC::ProducerPost>(
BorrowedFence(ready_fence.Borrow())));
}
int ProducerBuffer::PostAsync(const DvrNativeBufferMetadata* meta,
const LocalHandle& ready_fence) {
ATRACE_NAME("ProducerBuffer::PostAsync");
if (const int error = LocalPost(meta, ready_fence))
return error;
return ReturnStatusOrError(SendImpulse(BufferHubRPC::ProducerPost::Opcode));
}
int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta,
LocalHandle* out_fence, bool gain_posted_buffer) {
if (!out_meta)
return -EINVAL;
uint32_t current_buffer_state =
buffer_state_->load(std::memory_order_acquire);
ALOGD_IF(TRACE, "%s: buffer=%d, state=%" PRIx32 ".", __FUNCTION__, id(),
current_buffer_state);
if (BufferHubDefs::isClientGained(current_buffer_state,
client_state_mask())) {
ALOGV("%s: already gained id=%d.", __FUNCTION__, id());
return 0;
}
if (BufferHubDefs::isAnyClientAcquired(current_buffer_state) ||
BufferHubDefs::isAnyClientGained(current_buffer_state) ||
(BufferHubDefs::isAnyClientPosted(
current_buffer_state &
active_clients_bit_mask_->load(std::memory_order_acquire)) &&
!gain_posted_buffer)) {
ALOGE("%s: not released id=%d state=%" PRIx32 ".", __FUNCTION__, id(),
current_buffer_state);
return -EBUSY;
}
// Change the buffer state to gained state.
uint32_t updated_buffer_state = client_state_mask();
while (!buffer_state_->compare_exchange_weak(
current_buffer_state, updated_buffer_state, std::memory_order_acq_rel,
std::memory_order_acquire)) {
if (BufferHubDefs::isAnyClientAcquired(current_buffer_state) ||
BufferHubDefs::isAnyClientGained(current_buffer_state) ||
(BufferHubDefs::isAnyClientPosted(
current_buffer_state &
active_clients_bit_mask_->load(std::memory_order_acquire)) &&
!gain_posted_buffer)) {
ALOGE(
"%s: Failed to gain the buffer. The buffer is no longer released. "
"id=%d state=%" PRIx32 ".",
__FUNCTION__, id(), current_buffer_state);
return -EBUSY;
}
}
// Canonical metadata is undefined on Gain. Except for user_metadata and
// release_fence_mask. Fill in the user_metadata_ptr in address space of the
// local process.
if (metadata_header_->metadata.user_metadata_size && user_metadata_ptr_) {
out_meta->user_metadata_size =
metadata_header_->metadata.user_metadata_size;
out_meta->user_metadata_ptr =
reinterpret_cast<uint64_t>(user_metadata_ptr_);
} else {
out_meta->user_metadata_size = 0;
out_meta->user_metadata_ptr = 0;
}
uint32_t current_fence_state = fence_state_->load(std::memory_order_acquire);
uint32_t current_active_clients_bit_mask =
active_clients_bit_mask_->load(std::memory_order_acquire);
// If there are release fence(s) from consumer(s), we need to return it to the
// consumer(s).
// TODO(b/112007999) add an atomic variable in metadata header in shared
// memory to indicate which client is the last producer of the buffer.
// Currently, assume the first client is the only producer to the buffer.
if (current_fence_state & current_active_clients_bit_mask &
(~BufferHubDefs::kFirstClientBitMask)) {
*out_fence = shared_release_fence_.Duplicate();
out_meta->release_fence_mask = current_fence_state &
current_active_clients_bit_mask &
(~BufferHubDefs::kFirstClientBitMask);
}
return 0;
}
int ProducerBuffer::Gain(LocalHandle* release_fence, bool gain_posted_buffer) {
ATRACE_NAME("ProducerBuffer::Gain");
DvrNativeBufferMetadata meta;
if (const int error = LocalGain(&meta, release_fence, gain_posted_buffer))
return error;
auto status = InvokeRemoteMethod<BufferHubRPC::ProducerGain>();
if (!status)
return -status.error();
return 0;
}
int ProducerBuffer::GainAsync(DvrNativeBufferMetadata* out_meta,
LocalHandle* release_fence,
bool gain_posted_buffer) {
ATRACE_NAME("ProducerBuffer::GainAsync");
if (const int error = LocalGain(out_meta, release_fence, gain_posted_buffer))
return error;
return ReturnStatusOrError(SendImpulse(BufferHubRPC::ProducerGain::Opcode));
}
int ProducerBuffer::GainAsync() {
DvrNativeBufferMetadata meta;
LocalHandle fence;
return GainAsync(&meta, &fence);
}
std::unique_ptr<ProducerBuffer> ProducerBuffer::Import(
LocalChannelHandle channel) {
ALOGD_IF(TRACE, "ProducerBuffer::Import: channel=%d", channel.value());
return ProducerBuffer::Create(std::move(channel));
}
std::unique_ptr<ProducerBuffer> ProducerBuffer::Import(
Status<LocalChannelHandle> status) {
return Import(status ? status.take()
: LocalChannelHandle{nullptr, -status.error()});
}
Status<LocalChannelHandle> ProducerBuffer::Detach() {
// TODO(b/112338294) remove after migrate producer buffer to binder
ALOGW("ProducerBuffer::Detach: not supported operation during migration");
return {};
// TODO(b/112338294) Keep here for reference. Remove it after new logic is
// written.
/* uint32_t buffer_state = buffer_state_->load(std::memory_order_acquire);
if (!BufferHubDefs::isClientGained(
buffer_state, BufferHubDefs::kFirstClientStateMask)) {
// Can only detach a ProducerBuffer when it's in gained state.
ALOGW("ProducerBuffer::Detach: The buffer (id=%d, state=0x%" PRIx32
") is not in gained state.",
id(), buffer_state);
return {};
}
Status<LocalChannelHandle> status =
InvokeRemoteMethod<BufferHubRPC::ProducerBufferDetach>();
ALOGE_IF(!status,
"ProducerBuffer::Detach: Failed to detach buffer (id=%d): %s.", id(),
status.GetErrorMessage().c_str());
return status; */
}
} // namespace dvr
} // namespace android
|