1 /*
2  * Copyright (C) 2021 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 #define LOG_TAG "CachedStreamBuffer"
17 #include "cached_stream_buffer.h"
18 #include <hardware/gralloc.h>
19 #include <log/log.h>
20 #include <sync/sync.h>
21 
22 namespace android::hardware::camera::device::V3_4::implementation {
23 
24 namespace {
25 HandleImporter g_importer;
26 }
27 
ReleaseFence(int fence_fd)28 ReleaseFence::ReleaseFence(int fence_fd) : handle_(nullptr) {
29   if (fence_fd >= 0) {
30     handle_ = native_handle_create(/*numFds*/ 1, /*numInts*/ 0);
31     handle_->data[0] = fence_fd;
32   }
33 }
34 
~ReleaseFence()35 ReleaseFence::~ReleaseFence() {
36   if (handle_ != nullptr) {
37     native_handle_close(handle_);
38     native_handle_delete(handle_);
39   }
40 }
41 
CachedStreamBuffer()42 CachedStreamBuffer::CachedStreamBuffer()
43     : buffer_(nullptr), buffer_id_(0), stream_id_(0), acquire_fence_(-1) {}
44 
CachedStreamBuffer(const StreamBuffer & buffer)45 CachedStreamBuffer::CachedStreamBuffer(const StreamBuffer& buffer)
46     : buffer_(buffer.buffer.getNativeHandle()),
47       buffer_id_(buffer.bufferId),
48       stream_id_(buffer.streamId),
49       acquire_fence_(-1) {
50   g_importer.importBuffer(buffer_);
51   g_importer.importFence(buffer.acquireFence, acquire_fence_);
52 }
53 
CachedStreamBuffer(CachedStreamBuffer && from)54 CachedStreamBuffer::CachedStreamBuffer(CachedStreamBuffer&& from) noexcept {
55   buffer_ = from.buffer_;
56   buffer_id_ = from.buffer_id_;
57   stream_id_ = from.stream_id_;
58   acquire_fence_ = from.acquire_fence_;
59   from.acquire_fence_ = -1;
60   from.buffer_ = nullptr;
61 }
62 
operator =(CachedStreamBuffer && from)63 CachedStreamBuffer& CachedStreamBuffer::operator=(
64     CachedStreamBuffer&& from) noexcept {
65   if (this != &from) {
66     buffer_ = from.buffer_;
67     buffer_id_ = from.buffer_id_;
68     stream_id_ = from.stream_id_;
69     acquire_fence_ = from.acquire_fence_;
70     from.acquire_fence_ = -1;
71     from.buffer_ = nullptr;
72   }
73   return *this;
74 }
75 
~CachedStreamBuffer()76 CachedStreamBuffer::~CachedStreamBuffer() {
77   if (buffer_ != nullptr) {
78     g_importer.freeBuffer(buffer_);
79   }
80   g_importer.closeFence(acquire_fence_);
81 }
82 
importFence(const native_handle_t * fence_handle)83 void CachedStreamBuffer::importFence(const native_handle_t* fence_handle) {
84   g_importer.closeFence(acquire_fence_);
85   g_importer.importFence(fence_handle, acquire_fence_);
86 }
87 
acquireAsYUV(int32_t width,int32_t height,int timeout_ms)88 YCbCrLayout CachedStreamBuffer::acquireAsYUV(int32_t width, int32_t height,
89                                              int timeout_ms) {
90   if (acquire_fence_ >= 0) {
91     if (sync_wait(acquire_fence_, timeout_ms)) {
92       ALOGW("%s: timeout while waiting acquire fence", __FUNCTION__);
93       return {};
94     } else {
95       ::close(acquire_fence_);
96       acquire_fence_ = -1;
97     }
98   }
99   android::Rect region{0, 0, width, height};
100   android_ycbcr result =
101       g_importer.lockYCbCr(buffer_, GRALLOC_USAGE_SW_WRITE_OFTEN, region);
102   if (result.ystride > UINT32_MAX || result.cstride > UINT32_MAX ||
103       result.chroma_step > UINT32_MAX) {
104     ALOGE(
105         "%s: lockYCbCr failed. Unexpected values! ystride: %zu cstride: %zu "
106         "chroma_step: %zu",
107         __FUNCTION__, result.ystride, result.cstride, result.chroma_step);
108     return {};
109   }
110   return {.y = result.y,
111           .cb = result.cb,
112           .cr = result.cr,
113           .yStride = static_cast<uint32_t>(result.ystride),
114           .cStride = static_cast<uint32_t>(result.cstride),
115           .chromaStep = static_cast<uint32_t>(result.chroma_step)};
116 }
117 
acquireAsBlob(int32_t size,int timeout_ms)118 void* CachedStreamBuffer::acquireAsBlob(int32_t size, int timeout_ms) {
119   if (acquire_fence_ >= 0) {
120     if (sync_wait(acquire_fence_, timeout_ms)) {
121       return nullptr;
122     } else {
123       ::close(acquire_fence_);
124       acquire_fence_ = -1;
125     }
126   }
127   return g_importer.lock(buffer_, GRALLOC_USAGE_SW_WRITE_OFTEN, size);
128 }
129 
release()130 int CachedStreamBuffer::release() { return g_importer.unlock(buffer_); }
131 
132 }  // namespace android::hardware::camera::device::V3_4::implementation
133