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 <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/mman.h>
21 #include <fcntl.h>
22
23 #include <linux/videodev2.h>
24 #include <linux/v4l2-controls.h>
25
26 #include <exynos-hwjpeg.h>
27 #include "hwjpeg-internal.h"
28
CHWJpegBase(const char * path)29 CHWJpegBase::CHWJpegBase(const char *path)
30 : m_iFD(-1), m_uiDeviceCaps(0), m_uiAuxFlags(0)
31 {
32 m_iFD = open(path, O_RDWR);
33 if (m_iFD < 0)
34 ALOGERR("Failed to open '%s'", path);
35 }
36
~CHWJpegBase()37 CHWJpegBase::~CHWJpegBase()
38 {
39 if (m_iFD >= 0)
40 close(m_iFD);
41 }
42
SetAuxFlags(unsigned int auxflags)43 void CHWJpegBase::SetAuxFlags(unsigned int auxflags)
44 {
45 ALOGW_IF(!!(m_uiAuxFlags & auxflags),
46 "Configuration auxiliary flags %#x overrides previous flags %#x",
47 auxflags , m_uiAuxFlags);
48
49 m_uiAuxFlags |= auxflags;
50 }
51
ClearAuxFlags(unsigned int auxflags)52 void CHWJpegBase::ClearAuxFlags(unsigned int auxflags)
53 {
54
55 ALOGW_IF(!!(m_uiAuxFlags & auxflags) && ((m_uiAuxFlags & auxflags) != auxflags),
56 "Clearing auxiliary flags %#x overrides previous flags %#x",
57 auxflags, m_uiAuxFlags);
58
59 m_uiAuxFlags &= ~auxflags;
60 }
61
Start()62 bool CStopWatch::Start()
63 {
64 int ret = clock_gettime(CLOCK_MONOTONIC, &m_tBegin);
65 if (ret) {
66 ALOGERR("Failed to get current clock");
67 memset(&m_tBegin, 0, sizeof(m_tBegin));
68 return false;
69 }
70
71 return true;
72 }
73
GetElapsed()74 unsigned long CStopWatch::GetElapsed()
75 {
76 timespec tp;
77 int ret = clock_gettime(CLOCK_MONOTONIC, &tp);
78 if (ret) {
79 ALOGERR("Failed to get current clock");
80 return 0;
81 }
82
83 unsigned long elapsed = (tp.tv_sec - m_tBegin.tv_sec) * 1000000;
84 return (m_tBegin.tv_nsec > tp.tv_nsec)
85 ? elapsed - (m_tBegin.tv_nsec - tp.tv_nsec) / 1000
86 : elapsed + (tp.tv_nsec - m_tBegin.tv_nsec) / 1000;
87 }
88
GetElapsedUpdate()89 unsigned long CStopWatch::GetElapsedUpdate()
90 {
91 timespec tp;
92 int ret = clock_gettime(CLOCK_MONOTONIC, &tp);
93 if (ret) {
94 ALOGERR("Failed to get current clock");
95 return 0;
96 }
97
98 unsigned long elapsed = (tp.tv_sec - m_tBegin.tv_sec) * 1000000;
99 elapsed = (m_tBegin.tv_nsec > tp.tv_nsec)
100 ? elapsed - (m_tBegin.tv_nsec - tp.tv_nsec) / 1000
101 : elapsed + (tp.tv_nsec - m_tBegin.tv_nsec) / 1000;
102
103 m_tBegin = tp;
104 return elapsed;
105 }
106
WriteToFile(const char * path,const char * data,size_t len)107 bool WriteToFile(const char *path, const char *data, size_t len)
108 {
109 int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IWGRP );
110 if (fd < 0) {
111 ALOGERR("Failed to open '%s' for write/create", path);
112 return false;
113 }
114
115 ssize_t written = write(fd, data, len);
116 close(fd);
117 if (written < 0) {
118 ALOGERR("Failed to write %zu bytes to '%s'", len, path);
119 return false;
120 }
121
122 ALOGI("%zu/%zu bytes from ptr %p are written to '%s'", written, len, data, path);
123
124 return true;
125 }
126
WriteToFile(const char * path,int dmabuf,size_t len)127 bool WriteToFile(const char *path, int dmabuf, size_t len)
128 {
129 char *p = reinterpret_cast<char *>(mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf, 0));
130 if (p == MAP_FAILED) {
131 ALOGERR("Filed to map the given dmabuf fd %d", dmabuf);
132 return false;
133 }
134
135 int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IWGRP );
136 if (fd < 0) {
137 ALOGERR("Failed to open '%s' for write/create", path);
138 munmap(p, len);
139 return false;
140 }
141
142 ssize_t written = write(fd, p, len);
143 if (written < 0)
144 ALOGERR("Failed to write %zu bytes to '%s'", len, path);
145 else
146 ALOGI("%zu/%zu bytes from dmabuf fd %d are written to '%s'", written, len, dmabuf, path);
147
148 munmap(p, len);
149 close(fd);
150
151 return true;
152 }
153