1 /*
2 * Copyright Samsung Electronics Co.,LTD.
3 * Copyright (C) 2015 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <exynos-hwjpeg.h>
19 #include <fcntl.h>
20 #include <linux/v4l2-controls.h>
21 #include <linux/videodev2.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #include "hwjpeg-internal.h"
27
CHWJpegBase(const char * path)28 CHWJpegBase::CHWJpegBase(const char *path) : m_iFD(-1), m_uiDeviceCaps(0), m_uiAuxFlags(0) {
29 m_iFD = open(path, O_RDWR);
30 if (m_iFD < 0) ALOGERR("Failed to open '%s'", path);
31 }
32
~CHWJpegBase()33 CHWJpegBase::~CHWJpegBase() {
34 if (m_iFD >= 0) close(m_iFD);
35 }
36
SetAuxFlags(unsigned int auxflags)37 void CHWJpegBase::SetAuxFlags(unsigned int auxflags) {
38 ALOGW_IF(!!(m_uiAuxFlags & auxflags),
39 "Configuration auxiliary flags %#x overrides previous flags %#x", auxflags,
40 m_uiAuxFlags);
41
42 m_uiAuxFlags |= auxflags;
43 }
44
ClearAuxFlags(unsigned int auxflags)45 void CHWJpegBase::ClearAuxFlags(unsigned int auxflags) {
46 ALOGW_IF(!!(m_uiAuxFlags & auxflags) && ((m_uiAuxFlags & auxflags) != auxflags),
47 "Clearing auxiliary flags %#x overrides previous flags %#x", auxflags, m_uiAuxFlags);
48
49 m_uiAuxFlags &= ~auxflags;
50 }
51
Start()52 bool CStopWatch::Start() {
53 int ret = clock_gettime(CLOCK_MONOTONIC, &m_tBegin);
54 if (ret) {
55 ALOGERR("Failed to get current clock");
56 memset(&m_tBegin, 0, sizeof(m_tBegin));
57 return false;
58 }
59
60 return true;
61 }
62
GetElapsed()63 unsigned long CStopWatch::GetElapsed() {
64 timespec tp;
65 int ret = clock_gettime(CLOCK_MONOTONIC, &tp);
66 if (ret) {
67 ALOGERR("Failed to get current clock");
68 return 0;
69 }
70
71 unsigned long elapsed = (tp.tv_sec - m_tBegin.tv_sec) * 1000000;
72 return (m_tBegin.tv_nsec > tp.tv_nsec) ? elapsed - (m_tBegin.tv_nsec - tp.tv_nsec) / 1000
73 : elapsed + (tp.tv_nsec - m_tBegin.tv_nsec) / 1000;
74 }
75
GetElapsedUpdate()76 unsigned long CStopWatch::GetElapsedUpdate() {
77 timespec tp;
78 int ret = clock_gettime(CLOCK_MONOTONIC, &tp);
79 if (ret) {
80 ALOGERR("Failed to get current clock");
81 return 0;
82 }
83
84 unsigned long elapsed = (tp.tv_sec - m_tBegin.tv_sec) * 1000000;
85 elapsed = (m_tBegin.tv_nsec > tp.tv_nsec) ? elapsed - (m_tBegin.tv_nsec - tp.tv_nsec) / 1000
86 : elapsed + (tp.tv_nsec - m_tBegin.tv_nsec) / 1000;
87
88 m_tBegin = tp;
89 return elapsed;
90 }
91
WriteToFile(const char * path,const char * data,size_t len)92 bool WriteToFile(const char *path, const char *data, size_t len) {
93 int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IWGRP);
94 if (fd < 0) {
95 ALOGERR("Failed to open '%s' for write/create", path);
96 return false;
97 }
98
99 ssize_t written = write(fd, data, len);
100 close(fd);
101 if (written < 0) {
102 ALOGERR("Failed to write %zu bytes to '%s'", len, path);
103 return false;
104 }
105
106 ALOGI("%zu/%zu bytes from ptr %p are written to '%s'", written, len, data, path);
107
108 return true;
109 }
110
WriteToFile(const char * path,int dmabuf,size_t len)111 bool WriteToFile(const char *path, int dmabuf, size_t len) {
112 char *p = reinterpret_cast<char *>(
113 mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf, 0));
114 if (p == MAP_FAILED) {
115 ALOGERR("Filed to map the given dmabuf fd %d", dmabuf);
116 return false;
117 }
118
119 int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IWGRP);
120 if (fd < 0) {
121 ALOGERR("Failed to open '%s' for write/create", path);
122 munmap(p, len);
123 return false;
124 }
125
126 ssize_t written = write(fd, p, len);
127 if (written < 0)
128 ALOGERR("Failed to write %zu bytes to '%s'", len, path);
129 else
130 ALOGI("%zu/%zu bytes from dmabuf fd %d are written to '%s'", written, len, dmabuf, path);
131
132 munmap(p, len);
133 close(fd);
134
135 return true;
136 }
137