summaryrefslogtreecommitdiff
path: root/libhwjpeg/LibScalerForJpeg.cpp
diff options
context:
space:
mode:
authorCho KyongHo <pullip.cho@samsung.com>2019-08-14 20:14:39 +0900
committerCho KyongHo <pullip.cho@samsung.com>2020-02-20 21:34:34 -0800
commit01ca34c1bb0969c968e2ab109675243c097045d7 (patch)
tree902ea9c7b7bbda502437f91b7afe8e4e45c6e8f7 /libhwjpeg/LibScalerForJpeg.cpp
parenta6b44cc59409195144498eb568742a59983c2571 (diff)
libhwjpeg: add device object for thunbmail scaler
Redundancies in LibScalerForJpeg is introduced by repeated calls to V4L2 API. Device class provides simple functions that replaces V4L2 API for LibScalerForJpeg and hides redudancies. Change-Id: Ic56b157e61617a6f34a312dfac7eedbfa6c4b635 Signed-off-by: Cho KyongHo <pullip.cho@samsung.com>
Diffstat (limited to 'libhwjpeg/LibScalerForJpeg.cpp')
-rw-r--r--libhwjpeg/LibScalerForJpeg.cpp215
1 files changed, 106 insertions, 109 deletions
diff --git a/libhwjpeg/LibScalerForJpeg.cpp b/libhwjpeg/LibScalerForJpeg.cpp
index 10c7b76..cab9ae4 100644
--- a/libhwjpeg/LibScalerForJpeg.cpp
+++ b/libhwjpeg/LibScalerForJpeg.cpp
@@ -29,31 +29,6 @@ static const char *getBufTypeString(unsigned int buftype)
return "unknown";
}
-LibScalerForJpeg::LibScalerForJpeg()
-{
- m_fdScaler = open(SCALER_DEV_NODE, O_RDWR);
-
- if (m_fdScaler < 0) {
- ALOGE("Failed to open %s", SCALER_DEV_NODE);
- return;
- }
-
- mSrcImage.init(m_fdScaler);
- mDstImage.init(m_fdScaler);
-
- ALOGD("LibScalerForJpeg Created: %p", this);
-}
-
-LibScalerForJpeg::~LibScalerForJpeg()
-{
- if (m_fdScaler > 0)
- close(m_fdScaler);
-
- m_fdScaler = -1;
-
- ALOGD("LibScalerForJpeg Destroyed: %p", this);
-}
-
bool LibScalerForJpeg::RunStream(int srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, size_t dstLen)
{
if (!mSrcImage.begin(V4L2_MEMORY_DMABUF) || !mDstImage.begin(V4L2_MEMORY_DMABUF))
@@ -76,29 +51,12 @@ bool LibScalerForJpeg::Image::set(unsigned int width, unsigned int height, unsig
return true;
if (memoryType != 0) {
- v4l2_requestbuffers reqbufs{};
-
- reqbufs.type = bufferType;
- reqbufs.memory = memoryType;
- reqbufs.count = 0;
-
- if (ioctl(mDevFd, VIDIOC_REQBUFS, &reqbufs) < 0) {
- ALOGE("Failed to REQBUFS for(0) %s", getBufTypeString(bufferType));
+ if (!mDevice.requestBuffers(bufferType, memoryType, 0))
return false;
- }
}
- v4l2_format fmt{};
-
- fmt.type = bufferType;
- fmt.fmt.pix_mp.pixelformat = format;
- fmt.fmt.pix_mp.width = width;
- fmt.fmt.pix_mp.height = height;
-
- if (ioctl(mDevFd, VIDIOC_S_FMT, &fmt) < 0) {
- ALOGE("Failed to S_FMT for %s", getBufTypeString(bufferType));
+ if (!mDevice.setFormat(bufferType, format, width, height))
return false;
- }
memoryType = 0; // new reqbufs is required.
@@ -108,32 +66,16 @@ bool LibScalerForJpeg::Image::set(unsigned int width, unsigned int height, unsig
bool LibScalerForJpeg::Image::begin(unsigned int memtype)
{
if (memoryType != memtype) {
- v4l2_requestbuffers reqbufs{};
-
- reqbufs.type = bufferType;
-
if (memoryType != 0) {
- reqbufs.memory = memoryType;
- reqbufs.count = 0;
-
- if (ioctl(mDevFd, VIDIOC_REQBUFS, &reqbufs) < 0) {
- ALOGERR("Failed to REQBUFS(0) for %s", getBufTypeString(bufferType));
+ if (!mDevice.requestBuffers(bufferType, memoryType, 0))
return false;
- }
}
- reqbufs.memory = memtype;
- reqbufs.count = 1;
-
- if (ioctl(mDevFd, VIDIOC_REQBUFS, &reqbufs) < 0) {
- ALOGERR("Failed to REQBUFS(1) for %s with %d", getBufTypeString(bufferType), reqbufs.memory);
+ if (!mDevice.requestBuffers(bufferType, memtype, 1))
return false;
- }
- if (ioctl(mDevFd, VIDIOC_STREAMON, &bufferType) < 0) {
- ALOGERR("Failed to STREAMON for %s", getBufTypeString(bufferType));
+ if (!mDevice.streamOn(bufferType))
return false;
- }
memoryType = memtype;
}
@@ -143,104 +85,159 @@ bool LibScalerForJpeg::Image::begin(unsigned int memtype)
bool LibScalerForJpeg::Image::cancelBuffer()
{
- if (ioctl(mDevFd, VIDIOC_STREAMOFF, &bufferType) < 0) {
- ALOGE("Failed to STREAMOFF for %s", getBufTypeString(bufferType));
+ if (!mDevice.streamOff(bufferType))
return false;
- }
- if (ioctl(mDevFd, VIDIOC_STREAMON, &bufferType) < 0) {
- ALOGE("Failed to STREAMON for %s", getBufTypeString(bufferType));
+ if (!mDevice.streamOn(bufferType))
return false;
- }
return true;
}
-bool LibScalerForJpeg::Image::queueBuffer(int buf, size_t len)
+LibScalerForJpeg::Device::Device()
{
- v4l2_buffer buffer{};
- v4l2_plane plane[SCALER_MAX_PLANES];
+ mFd = ::open(SCALER_DEV_NODE, O_RDWR);
+ if (mFd < 0)
+ ALOGERR("failed to open %s", SCALER_DEV_NODE);
+}
- memset(&plane, 0, sizeof(plane));
+LibScalerForJpeg::Device::~Device()
+{
+ if (mFd >= 0)
+ ::close(mFd);
+}
- buffer.type = bufferType;
- buffer.memory = memoryType;
- buffer.length = 1;
- buffer.m.planes = plane;
- plane[0].m.fd = buf;
- plane[0].length = len;
+bool LibScalerForJpeg::Device::requestBuffers(unsigned int buftype, unsigned int memtype, unsigned int count)
+{
+ v4l2_requestbuffers reqbufs{};
- if (ioctl(mDevFd, VIDIOC_QBUF, &buffer) < 0) {
- ALOGERR("Failed to QBUF for %s", getBufTypeString(bufferType));
+ reqbufs.type = buftype;
+ reqbufs.memory = memtype;
+ reqbufs.count = count;
+
+ if (ioctl(mFd, VIDIOC_REQBUFS, &reqbufs) < 0) {
+ ALOGERR("failed REQBUFS(%s, mem=%d, count=%d)", getBufTypeString(buftype), memtype, count);
return false;
}
return true;
}
-bool LibScalerForJpeg::Image::queueBuffer(int buf[SCALER_MAX_PLANES], int len[SCALER_MAX_PLANES])
+bool LibScalerForJpeg::Device::setFormat(unsigned int buftype, unsigned int format, unsigned int width, unsigned int height)
{
- v4l2_buffer buffer{};
- v4l2_plane plane[SCALER_MAX_PLANES];
+ v4l2_format fmt{};
- memset(&plane, 0, sizeof(plane));
+ fmt.type = buftype;
+ fmt.fmt.pix_mp.pixelformat = format;
+ fmt.fmt.pix_mp.width = width;
+ fmt.fmt.pix_mp.height = height;
- buffer.type = bufferType;
- buffer.memory = memoryType;
- buffer.length = SCALER_MAX_PLANES;
- for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) {
- plane[i].m.fd = buf[i];
- plane[i].length = len[i];
+ if (ioctl(mFd, VIDIOC_S_FMT, &fmt) < 0) {
+ ALOGERR("failed S_FMT(%s, fmt=h'%x, %ux%u)", getBufTypeString(buftype), format, width, height);
+ return false;
+ }
+
+ return true;
+}
+
+bool LibScalerForJpeg::Device::streamOn(unsigned int buftype)
+{
+ if (ioctl(mFd, VIDIOC_STREAMON, &buftype) < 0) {
+ ALOGERR("failed STREAMON for %s", getBufTypeString(buftype));
+ return false;
}
- buffer.m.planes = plane;
- if (ioctl(mDevFd, VIDIOC_QBUF, &buffer) < 0) {
- ALOGERR("Failed to QBUF for %s", getBufTypeString(bufferType));
+ return true;
+}
+
+bool LibScalerForJpeg::Device::streamOff(unsigned int buftype)
+{
+ if (ioctl(mFd, VIDIOC_STREAMOFF, &buftype) < 0) {
+ ALOGERR("failed STREAMOFF for %s", getBufTypeString(buftype));
return false;
}
return true;
}
-bool LibScalerForJpeg::Image::queueBuffer(char *buf[SCALER_MAX_PLANES], int len[SCALER_MAX_PLANES])
+bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, std::function<void(v4l2_buffer &)> bufferFiller)
{
v4l2_buffer buffer{};
v4l2_plane plane[SCALER_MAX_PLANES];
memset(&plane, 0, sizeof(plane));
- buffer.type = bufferType;
- buffer.memory = memoryType;
- buffer.length = SCALER_MAX_PLANES;
- for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) {
- plane[i].m.userptr = reinterpret_cast<unsigned long>(buf[i]);
- plane[i].length = len[i];
- }
+ buffer.type = buftype;
buffer.m.planes = plane;
- if (ioctl(mDevFd, VIDIOC_QBUF, &buffer) < 0) {
- ALOGERR("Failed to QBUF for %s", getBufTypeString(bufferType));
+ bufferFiller(buffer);
+
+ return ioctl(mFd, VIDIOC_QBUF, &buffer) >= 0;
+}
+
+bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, int buf[SCALER_MAX_PLANES], int len[SCALER_MAX_PLANES])
+{
+ if (!queueBuffer(buftype, [buf, len] (v4l2_buffer &buffer) {
+ buffer.memory = V4L2_MEMORY_DMABUF;
+ buffer.length = SCALER_MAX_PLANES;
+ for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) {
+ buffer.m.planes[i].m.fd = buf[i];
+ buffer.m.planes[i].length = len[i];
+ } })) {
+ ALOGERR("failed QBUF(%s, fd[]=%d %d, len[0]=%d %d)", getBufTypeString(buftype), buf[0], buf[1], len[0], len[1]);
+ return false;
+ }
+
+ return true;
+}
+
+bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, char *buf[SCALER_MAX_PLANES], int len[SCALER_MAX_PLANES])
+{
+ if (!queueBuffer(buftype, [buf, len] (v4l2_buffer &buffer) {
+ buffer.memory = V4L2_MEMORY_USERPTR;
+ buffer.length = SCALER_MAX_PLANES;
+ for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) {
+ buffer.m.planes[i].m.userptr = reinterpret_cast<unsigned long>(buf[i]);
+ buffer.m.planes[i].length = len[i];
+ } })) {
+ ALOGERR("failed QBUF(%s, ptr[]=%p %p, len[0]=%d %d)", getBufTypeString(buftype), buf[0], buf[1], len[0], len[1]);
+ return false;
+ }
+
+ return true;
+}
+
+bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, int buf, int len)
+{
+ if (!queueBuffer(buftype, [buf, len] (v4l2_buffer &buffer)
+ {
+ buffer.memory = V4L2_MEMORY_DMABUF;
+ buffer.length = 1;
+ buffer.m.planes[0].m.fd = buf;
+ buffer.m.planes[0].length = len;
+ })) {
+ ALOGERR("failed QBUF(%s, fd=%d, len=%d", getBufTypeString(buftype), buf, len);
return false;
}
return true;
}
-bool LibScalerForJpeg::Image::dequeueBuffer()
+bool LibScalerForJpeg::Device::dequeueBuffer(unsigned int buftype, unsigned int memtype)
{
v4l2_buffer buffer{};
v4l2_plane plane[SCALER_MAX_PLANES];
memset(&plane, 0, sizeof(plane));
- buffer.type = bufferType;
- buffer.memory = memoryType;
+ buffer.type = buftype;
+ buffer.memory = memtype;
buffer.length = SCALER_MAX_PLANES;
buffer.m.planes = plane;
- if (ioctl(mDevFd, VIDIOC_DQBUF, &buffer) < 0 ) {
- ALOGERR("Failed to DQBuf %s", getBufTypeString(bufferType));
+ if (ioctl(mFd, VIDIOC_DQBUF, &buffer) < 0 ) {
+ ALOGERR("failed DQBUF(%s)", getBufTypeString(buftype));
return false;
}