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
|
#include <private/dvr/ion_buffer.h>
#include <log/log.h>
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include <utils/Trace.h>
#include <mutex>
namespace {
constexpr uint32_t kDefaultGraphicBufferLayerCount = 1;
} // anonymous namespace
namespace android {
namespace dvr {
IonBuffer::IonBuffer() : IonBuffer(nullptr, 0, 0, 0, 0, 0, 0) {}
IonBuffer::IonBuffer(uint32_t width, uint32_t height, uint32_t format,
uint64_t usage)
: IonBuffer() {
Alloc(width, height, kDefaultGraphicBufferLayerCount, format, usage);
}
IonBuffer::IonBuffer(buffer_handle_t handle, uint32_t width, uint32_t height,
uint32_t stride, uint32_t format, uint64_t usage)
: IonBuffer(handle, width, height, kDefaultGraphicBufferLayerCount, stride,
format, usage) {}
IonBuffer::IonBuffer(buffer_handle_t handle, uint32_t width, uint32_t height,
uint32_t layer_count, uint32_t stride, uint32_t format,
uint64_t usage)
: buffer_(nullptr) {
ALOGD_IF(TRACE,
"IonBuffer::IonBuffer: handle=%p width=%u height=%u layer_count=%u "
"stride=%u format=%u usage=%" PRIx64,
handle, width, height, layer_count, stride, format, usage);
if (handle != 0) {
Import(handle, width, height, layer_count, stride, format, usage);
}
}
IonBuffer::~IonBuffer() {
ALOGD_IF(TRACE,
"IonBuffer::~IonBuffer: handle=%p width=%u height=%u stride=%u "
"format=%u usage=%" PRIx64,
handle(), width(), height(), stride(), format(), usage());
FreeHandle();
}
IonBuffer::IonBuffer(IonBuffer&& other) noexcept : IonBuffer() {
*this = std::move(other);
}
IonBuffer& IonBuffer::operator=(IonBuffer&& other) noexcept {
ALOGD_IF(TRACE, "IonBuffer::operator=: handle_=%p other.handle_=%p", handle(),
other.handle());
if (this != &other) {
buffer_ = other.buffer_;
other.FreeHandle();
}
return *this;
}
void IonBuffer::FreeHandle() {
if (buffer_.get()) {
// GraphicBuffer unregisters and cleans up the handle if needed
buffer_ = nullptr;
}
}
int IonBuffer::Alloc(uint32_t width, uint32_t height, uint32_t layer_count,
uint32_t format, uint64_t usage) {
ALOGD_IF(TRACE,
"IonBuffer::Alloc: width=%u height=%u layer_count=%u format=%u "
"usage=%" PRIx64, width, height, layer_count, format, usage);
sp<GraphicBuffer> buffer =
new GraphicBuffer(width, height, format, layer_count, usage);
if (buffer->initCheck() != OK) {
ALOGE("IonBuffer::Aloc: Failed to allocate buffer");
return -EINVAL;
} else {
buffer_ = buffer;
return 0;
}
}
void IonBuffer::Reset(buffer_handle_t handle, uint32_t width, uint32_t height,
uint32_t layer_count, uint32_t stride, uint32_t format,
uint64_t usage) {
ALOGD_IF(TRACE,
"IonBuffer::Reset: handle=%p width=%u height=%u layer_count=%u "
"stride=%u format=%u usage=%" PRIx64,
handle, width, height, layer_count, stride, format, usage);
Import(handle, width, height, layer_count, stride, format, usage);
}
int IonBuffer::Import(buffer_handle_t handle, uint32_t width, uint32_t height,
uint32_t layer_count, uint32_t stride, uint32_t format,
uint64_t usage) {
ATRACE_NAME("IonBuffer::Import1");
ALOGD_IF(TRACE,
"IonBuffer::Import: handle=%p width=%u height=%u layer_count=%u "
"stride=%u format=%u usage=%" PRIx64,
handle, width, height, layer_count, stride, format, usage);
FreeHandle();
sp<GraphicBuffer> buffer =
new GraphicBuffer(handle, GraphicBuffer::TAKE_UNREGISTERED_HANDLE, width,
height, format, layer_count, usage, stride);
if (buffer->initCheck() != OK) {
ALOGE("IonBuffer::Import: Failed to import buffer");
return -EINVAL;
} else {
buffer_ = buffer;
return 0;
}
}
int IonBuffer::Import(const int* fd_array, int fd_count, const int* int_array,
int int_count, uint32_t width, uint32_t height,
uint32_t layer_count, uint32_t stride, uint32_t format,
uint64_t usage) {
ATRACE_NAME("IonBuffer::Import2");
ALOGD_IF(TRACE,
"IonBuffer::Import: fd_count=%d int_count=%d width=%u height=%u "
"layer_count=%u stride=%u format=%u usage=%" PRIx64,
fd_count, int_count, width, height, layer_count, stride, format,
usage);
if (fd_count < 0 || int_count < 0) {
ALOGE("IonBuffer::Import: invalid arguments.");
return -EINVAL;
}
native_handle_t* handle = native_handle_create(fd_count, int_count);
if (!handle) {
ALOGE("IonBuffer::Import: failed to create new native handle.");
return -ENOMEM;
}
// Copy fd_array into the first part of handle->data and int_array right
// after it.
memcpy(handle->data, fd_array, sizeof(int) * fd_count);
memcpy(handle->data + fd_count, int_array, sizeof(int) * int_count);
const int ret =
Import(handle, width, height, layer_count, stride, format, usage);
if (ret < 0) {
ALOGE("IonBuffer::Import: failed to import raw native handle: %s",
strerror(-ret));
native_handle_close(handle);
native_handle_delete(handle);
}
return ret;
}
int IonBuffer::Duplicate(const IonBuffer* other) {
if (!other->handle())
return -EINVAL;
const int fd_count = other->handle()->numFds;
const int int_count = other->handle()->numInts;
if (fd_count < 0 || int_count < 0)
return -EINVAL;
native_handle_t* handle = native_handle_create(fd_count, int_count);
if (!handle) {
ALOGE("IonBuffer::Duplicate: Failed to create new native handle.");
return -ENOMEM;
}
// Duplicate the file descriptors from the other native handle.
for (int i = 0; i < fd_count; i++)
handle->data[i] = dup(other->handle()->data[i]);
// Copy the ints after the file descriptors.
memcpy(handle->data + fd_count, other->handle()->data + fd_count,
sizeof(int) * int_count);
const int ret =
Import(handle, other->width(), other->height(), other->layer_count(),
other->stride(), other->format(), other->usage());
if (ret < 0) {
ALOGE("IonBuffer::Duplicate: Failed to import duplicate native handle: %s",
strerror(-ret));
native_handle_close(handle);
native_handle_delete(handle);
}
return ret;
}
int IonBuffer::Lock(uint32_t usage, int x, int y, int width, int height,
void** address) {
ATRACE_NAME("IonBuffer::Lock");
ALOGD_IF(TRACE,
"IonBuffer::Lock: handle=%p usage=%d x=%d y=%d width=%d height=%d "
"address=%p",
handle(), usage, x, y, width, height, address);
status_t err =
buffer_->lock(usage, Rect(x, y, x + width, y + height), address);
if (err != OK)
return -EINVAL;
else
return 0;
}
int IonBuffer::LockYUV(uint32_t usage, int x, int y, int width, int height,
struct android_ycbcr* yuv) {
ATRACE_NAME("IonBuffer::LockYUV");
ALOGD_IF(TRACE,
"IonBuffer::Lock: handle=%p usage=%d x=%d y=%d width=%d height=%d",
handle(), usage, x, y, width, height);
status_t err =
buffer_->lockYCbCr(usage, Rect(x, y, x + width, y + height), yuv);
if (err != OK)
return -EINVAL;
else
return 0;
}
int IonBuffer::Unlock() {
ATRACE_NAME("IonBuffer::Unlock");
ALOGD_IF(TRACE, "IonBuffer::Unlock: handle=%p", handle());
status_t err = buffer_->unlock();
if (err != OK)
return -EINVAL;
else
return 0;
}
} // namespace dvr
} // namespace android
|