1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "test_display.h"
16 using namespace std;
17 using namespace OHOS;
18 using namespace Camera;
19
TestDisplay()20 TestDisplay::TestDisplay()
21 {
22 }
23
GetCurrentLocalTimeStamp()24 uint64_t TestDisplay::GetCurrentLocalTimeStamp()
25 {
26 std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
27 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
28 auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
29 return tmp.count();
30 }
31
SaveYUV(char * type,unsigned char * buffer,int32_t size)32 int32_t TestDisplay::SaveYUV(char* type, unsigned char* buffer, int32_t size)
33 {
34 int ret;
35 char path[PATH_MAX] = {0};
36 ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/mnt/yuv/%s_%lld.yuv", type, GetCurrentLocalTimeStamp());
37 if (ret < 0) {
38 CAMERA_LOGE("%s, sprintf_s failed, errno = %s.", __FUNCTION__, strerror(errno));
39 return -1;
40 }
41 CAMERA_LOGI("%s, save yuv to file %s", __FUNCTION__, path);
42 system("mkdir -p /mnt/yuv");
43 int imgFd = open(path, O_RDWR | O_CREAT, 00766); // 00766:文件权限
44 if (imgFd == -1) {
45 CAMERA_LOGI("%s, open file failed, errno = %s.", __FUNCTION__, strerror(errno));
46 return -1;
47 }
48 int ret = write(imgFd, buffer, size);
49 if (ret == -1) {
50 CAMERA_LOGI("%s, write file failed, error = %s", __FUNCTION__, strerror(errno));
51 close(imgFd);
52 return -1;
53 }
54 close(imgFd);
55 return 0;
56 }
57
DoFbMunmap(unsigned char * addr)58 int TestDisplay::DoFbMunmap(unsigned char* addr)
59 {
60 int rc;
61 unsigned int size = vinfo_.xres * vinfo_.yres * vinfo_.bits_per_pixel / 8; // 8:picture size;
62 CAMERA_LOGI("main test:munmapped size = %d, virt_addr = 0x%p\n", size, addr);
63 rc = (munmap(addr, finfo_.smem_len));
64 return rc;
65 }
66
DoFbMmap(int * pmemfd)67 unsigned char* TestDisplay::DoFbMmap(int* pmemfd)
68 {
69 unsigned char* ret;
70 int screensize = vinfo_.xres * vinfo_.yres * vinfo_.bits_per_pixel / 8; // 8:picture size
71 ret = (unsigned char*)mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, *pmemfd, 0);
72 if (ret == MAP_FAILED) {
73 CAMERA_LOGE("main test:do_mmap: pmem mmap() failed: %s (%d)\n", strerror(errno), errno);
74 return NULL;
75 }
76 CAMERA_LOGI("main test:do_mmap: pmem mmap fd %d ptr %p len %u\n", *pmemfd, ret, screensize);
77 return ret;
78 }
79
FBLog()80 void TestDisplay::FBLog()
81 {
82 CAMERA_LOGI("the fixed information is as follow:\n");
83 CAMERA_LOGI("id=%s\n", finfo_.id);
84 CAMERA_LOGI("sem_start=%lx\n", finfo_.smem_start);
85 CAMERA_LOGI("smem_len=%u\n", finfo_.smem_len);
86 CAMERA_LOGI("type=%u\n", finfo_.type);
87 CAMERA_LOGI("line_length=%u\n", finfo_.line_length);
88 CAMERA_LOGI("mmio_start=%lu\n", finfo_.mmio_start);
89 CAMERA_LOGI("mmio_len=%d\n", finfo_.mmio_len);
90 CAMERA_LOGI("visual=%d\n", finfo_.visual);
91
92 CAMERA_LOGI("variable information is as follow:\n");
93 CAMERA_LOGI("The xres is :%u\n", vinfo_.xres);
94 CAMERA_LOGI("The yres is :%u\n", vinfo_.yres);
95 CAMERA_LOGI("xres_virtual=%u\n", vinfo_.xres_virtual);
96 CAMERA_LOGI("yres_virtual=%u\n", vinfo_.yres_virtual);
97 CAMERA_LOGI("xoffset=%u\n", vinfo_.xoffset);
98 CAMERA_LOGI("yoffset=%u\n", vinfo_.yoffset);
99 CAMERA_LOGI("bits_per_pixel is :%u\n", vinfo_.bits_per_pixel);
100 CAMERA_LOGI("red.offset=%u\n", vinfo_.red.offset);
101 CAMERA_LOGI("red.length=%u\n", vinfo_.red.length);
102 CAMERA_LOGI("red.msb_right=%u\n", vinfo_.red.msb_right);
103 CAMERA_LOGI("green.offset=%d\n", vinfo_.green.offset);
104 CAMERA_LOGI("green.length=%d\n", vinfo_.green.length);
105 CAMERA_LOGI("green.msb_right=%d\n", vinfo_.green.msb_right);
106 CAMERA_LOGI("blue.offset=%d\n", vinfo_.blue.offset);
107 CAMERA_LOGI("blue.length=%d\n", vinfo_.blue.length);
108 CAMERA_LOGI("blue.msb_right=%d\n", vinfo_.blue.msb_right);
109 CAMERA_LOGI("transp.offset=%d\n", vinfo_.transp.offset);
110 CAMERA_LOGI("transp.length=%d\n", vinfo_.transp.length);
111 CAMERA_LOGI("transp.msb_right=%d\n", vinfo_.transp.msb_right);
112 CAMERA_LOGI("height=%x\n", vinfo_.height);
113 }
114
FBInit()115 RetCode TestDisplay::FBInit()
116 {
117 fbFd_ = open("/dev/fb0", O_RDWR);
118 if (fbFd_ < 0) {
119 CAMERA_LOGE("main test:cannot open framebuffer %s file node\n", "/dev/fb0");
120 return RC_ERROR;
121 }
122
123 if (ioctl(fbFd_, FBIOGET_VSCREENINFO, &vinfo_) < 0) {
124 CAMERA_LOGE("main test:cannot retrieve vscreenInfo!\n");
125 close(fbFd_);
126 return RC_ERROR;
127 }
128
129 if (ioctl(fbFd_, FBIOGET_FSCREENINFO, &finfo_) < 0) {
130 CAMERA_LOGE("main test:can't retrieve fscreenInfo!\n");
131 close(fbFd_);
132 return RC_ERROR;
133 }
134
135 FBLog();
136
137 CAMERA_LOGI("main test:allocating display buffer memory\n");
138 displayBuf_ = DoFbMmap(&fbFd_);
139 if (displayBuf_ == NULL) {
140 CAMERA_LOGE("main test:error displayBuf_ mmap error\n");
141 close(fbFd_);
142 return RC_ERROR;
143 }
144 return RC_OK;
145 }
146
ProcessImage(const unsigned char * p,unsigned char * fbp)147 void TestDisplay::ProcessImage(const unsigned char* p, unsigned char* fbp)
148 {
149 unsigned char* in = (unsigned char*)p;
150 int width = 640; // 640:Displays the size of the width
151 int height = 480; // 480:Displays the size of the height
152 int istride = 1280; // 1280:Initial value of span
153 int x, y, j;
154 int y0, u, v, r, g, b;
155 long location = 0;
156 int xpos = (vinfo_.xres - width) / 2;
157 int ypos = (vinfo_.yres - height) / 2;
158 int y_pos, u_pos, v_pos;
159
160 y_pos = 0; // 0:Pixel initial value
161 u_pos = 1; // 1:Pixel initial value
162 v_pos = 3; // 3:Pixel initial value
163
164 for (y = ypos; y < (height + ypos); y++) {
165 for (j = 0, x = xpos; j < width; j++, x++) {
166 location = (x + vinfo_.xoffset) * (vinfo_.bits_per_pixel / 8) + // 8:每次要处理的字节数
167 (y + vinfo_.yoffset) * finfo_.line_length; // 一次加一个y的行数
168
169 y0 = in[y_pos];
170 u = in[u_pos] - 128; // 128:display size
171 v = in[v_pos] - 128; // 128:display size
172
173 r = RANGE_LIMIT(y0 + v + ((v * 103) >> 8)); // 103,8:display range
174 g = RANGE_LIMIT(y0 - ((u * 88) >> 8) - ((v * 183) >> 8)); // 88,8,183:display range
175 b = RANGE_LIMIT(y0 + u + ((u * 198) >> 8)); // 198,8:display range
176
177 fbp[location + 1] = ((r & 0xF8) | (g >> 5)); // 5:display range
178 fbp[location + 0] = (((g & 0x1C) << 3) | (b >> 3)); // 3:display range
179
180 y_pos += 2;
181
182 if (j & 0x01) {
183 u_pos += 4;
184 v_pos += 4;
185 }
186 }
187
188 y_pos = 0; // 0:Pixel initial value
189 u_pos = 1; // 1:Pixel initial value
190 v_pos = 3; // 3:Pixel initial value
191 in += istride; // 输入的buffer一次加一行的偏移位,istride等于预览宽度*2个字节。
192 }
193 }
194
LcdDrawScreen(unsigned char * displayBuf_,unsigned char * addr)195 void TestDisplay::LcdDrawScreen(unsigned char* displayBuf_, unsigned char* addr)
196 {
197 ProcessImage(addr, displayBuf_);
198 }
199
BufferCallback(std::shared_ptr<SurfaceBuffer> buffer,int choice)200 void TestDisplay::BufferCallback(std::shared_ptr<SurfaceBuffer> buffer, int choice)
201 {
202 if (choice == preview_mode) {
203 LcdDrawScreen(displayBuf_, (unsigned char*)buffer->GetVirAddr());
204 return;
205 } else {
206 LcdDrawScreen(displayBuf_, (unsigned char*)buffer->GetVirAddr());
207 std::cout << "==========[test log] capture start saveYuv......" << std::endl;
208 SaveYUV("capture", (unsigned char*)buffer->GetVirAddr(), bufSize_);
209 std::cout << "==========[test log] capture end saveYuv......" << std::endl;
210 return;
211 }
212 }
213
Init()214 void TestDisplay::Init()
215 {
216 std::shared_ptr<Camera::IDeviceManager> deviceManager = Camera::IDeviceManager::GetInstance();
217 if (!init_flag) {
218 deviceManager->Init();
219 init_flag = 1;
220 }
221 if (cameraHost == nullptr) {
222 cameraHost = Camera::CameraHost::CreateCameraHost();
223 if (cameraHost == nullptr) {
224 std::cout << "==========[test log] CreateCameraHost failed." << std::endl;
225 return;
226 }
227 std::cout << "==========[test log] CreateCameraHost success." << std::endl;
228 }
229 if (cameraDevice == nullptr) {
230 cameraHost->GetCameraIds(cameraIds);
231 const std::shared_ptr<OHOS::Camera::ICameraDeviceCallback> callback =
232 std::make_shared<OHOS::Camera::ICameraDeviceCallback>();
233 rc = cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice);
234 if (rc != Camera::NO_ERROR || cameraDevice == nullptr) {
235 std::cout << "==========[test log] OpenCamera failed, rc = " << rc << std::endl;
236 return;
237 }
238 std::cout << "==========[test log] OpenCamera success." << std::endl;
239 }
240 }
241
UsbInit()242 void TestDisplay::UsbInit()
243 {
244 std::shared_ptr<OHOS::Camera::IDeviceManager> deviceManager = Camera::IDeviceManager::GetInstance();
245 if (!init_flag) {
246 deviceManager->Init();
247 init_flag = 1;
248 }
249 if (cameraHost == nullptr) {
250 cameraHost = Camera::CameraHost::CreateCameraHost();
251 if (cameraHost == nullptr) {
252 std::cout << "==========[test log] CreateCameraHost failed." << std::endl;
253 return;
254 }
255 std::cout << "==========[test log] CreateCameraHost success." << std::endl;
256 }
257
258 std::shared_ptr<OHOS::Camera::ICameraHostCallback> cameraHostCallback =
259 std::make_shared<OHOS::Camera::ICameraHostCallback>();
260 cameraHostCallback->OnCameraStatus = nullptr;
261 Camera::CamRetCode ret = cameraHost->SetCallback(cameraHostCallback);
262 if (ret != Camera::NO_ERROR) {
263 std::cout << "==========[test log] SetCallback failed." << std::endl;
264 return;
265 } else {
266 std::cout << "==========[test log] SetCallback success." << std::endl;
267 }
268 }
269
Close()270 void TestDisplay::Close()
271 {
272 std::cout << "==========[test log] cameraDevice->Close()." << std::endl;
273 if (cameraDevice != nullptr) {
274 cameraDevice->Close();
275 cameraDevice = nullptr;
276 }
277 }
278
OpenCamera()279 void TestDisplay::OpenCamera()
280 {
281 if (cameraDevice == nullptr) {
282 cameraHost->GetCameraIds(cameraIds);
283 const std::shared_ptr<Camera::ICameraDeviceCallback> callback =
284 std::make_shared<Camera::ICameraDeviceCallback>();
285 rc = cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice);
286 if (rc != Camera::NO_ERROR || cameraDevice == nullptr) {
287 std::cout << "==========[test log] OpenCamera failed, rc = " << rc << std::endl;
288 return;
289 }
290 std::cout << "==========[test log] OpenCamera success." << std::endl;
291 }
292 }
293
calTime(struct timeval start,struct timeval end)294 float TestDisplay::calTime(struct timeval start, struct timeval end)
295 {
296 float time_use = 0;
297 time_use = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // 1000000:time
298 return time_use;
299 }
300
AchieveStreamOperator()301 void TestDisplay::AchieveStreamOperator()
302 {
303 // 创建并获取streamOperator信息
304 std::shared_ptr<OHOS::Camera::IStreamOperatorCallback> streamOperatorCallback =
305 std::make_shared<OHOS::Camera::IStreamOperatorCallback>();
306 rc = cameraDevice->GetStreamOperator(streamOperatorCallback, streamOperator);
307 EXPECT_EQ(true, rc == Camera::NO_ERROR);
308 if (rc == Camera::NO_ERROR) {
309 std::cout << "==========[test log] AchieveStreamOperator success." << std::endl;
310 } else {
311 std::cout << "==========[test log] AchieveStreamOperator fail, rc = " << rc << std::endl;
312 }
313 }
314
StartStream(std::vector<OHOS::Camera::StreamIntent> intents)315 void TestDisplay::StartStream(std::vector<OHOS::Camera::StreamIntent> intents)
316 {
317 streamInfo = std::make_shared<OHOS::Camera::StreamInfo>();
318 streamInfoPre = std::make_shared<OHOS::Camera::StreamInfo>();
319 streamInfoCapture = std::make_shared<OHOS::Camera::StreamInfo>();
320 streamInfoVideo = std::make_shared<OHOS::Camera::StreamInfo>();
321 for (auto& intent : intents) {
322 if (intent == 0) {
323 std::shared_ptr<IBufferProducer> producer = IBufferProducer::CreateBufferQueue();
324 producer->SetQueueSize(8); // 8:set bufferQueue size
325 if (producer->GetQueueSize() != 8) { // 8:get bufferQueue size
326 std::cout << "~~~~~~~" << std::endl;
327 }
328 auto callback = [this](std::shared_ptr<SurfaceBuffer> Prebuffer) {
329 BufferCallback(Prebuffer, preview_mode);
330 return;
331 };
332 producer->SetCallback(callback);
333 streamInfo->streamId_ = streamId_preview;
334 streamInfo->width_ = 640; // 640:picture width
335 streamInfo->height_ = 480; // 480:picture height
336 streamInfo->format_ = CAMERA_FORMAT_YUYV_422_PKG;
337 streamInfo->datasapce_ = 8; // 8:picture datasapce
338 streamInfo->intent_ = intent;
339 streamInfo->tunneledMode_ = 5; // 5:tunnel mode
340 streamInfo->bufferQueue_ = producer;
341 streamInfos.push_back(streamInfo);
342 } else if (intent == 1) {
343 std::shared_ptr<IBufferProducer> producerVideo = IBufferProducer::CreateBufferQueue();
344 producerVideo->SetQueueSize(8); // 8:set bufferQueue size
345 if (producerVideo->GetQueueSize() != 8) { // 8:get bufferQueue size
346 std::cout << "~~~~~~~" << std::endl;
347 }
348 auto callbackVideo = [this](std::shared_ptr<SurfaceBuffer> VideoBuffer) {
349 BufferCallback(VideoBuffer, video_mode);
350 return;
351 };
352 producerVideo->SetCallback(callbackVideo);
353 streamInfoVideo->streamId_ = streamId_video;
354 streamInfoVideo->width_ = 640; // 640:picture width
355 streamInfoVideo->height_ = 480; // 480:picture height
356 streamInfoVideo->format_ = CAMERA_FORMAT_YUYV_422_PKG;
357 streamInfoVideo->datasapce_ = 8; // 8:picture datasapce
358 streamInfoVideo->intent_ = intent;
359 streamInfoVideo->tunneledMode_ = 5; // 5:tunnel mode
360 streamInfo->bufferQueue_ = producerVideo;
361 streamInfos.push_back(streamInfoVideo);
362 } else {
363 std::shared_ptr<IBufferProducer> producerCapture = IBufferProducer::CreateBufferQueue();
364 producerCapture->SetQueueSize(8); // 8:set bufferQueue size
365 if (producerCapture->GetQueueSize() != 8) { // 8:get bufferQueue size
366 std::cout << "~~~~~~~" << std::endl;
367 }
368 auto callbackCap = [this](std::shared_ptr<SurfaceBuffer> CapBuffer) {
369 BufferCallback(CapBuffer, capture_mode);
370 return;
371 };
372 producerCapture->SetCallback(callbackCap);
373 streamInfoCapture->streamId_ = streamId_capture;
374 streamInfoCapture->width_ = 640; // 640:picture width
375 streamInfoCapture->height_ = 480; // 480:picture height
376 streamInfoCapture->format_ = CAMERA_FORMAT_YUYV_422_PKG;
377 streamInfoCapture->datasapce_ = 8; // 8:picture datasapce
378 streamInfoCapture->intent_ = intent;
379 streamInfoCapture->tunneledMode_ = 5; // 5:tunnel mode
380 streamInfo->bufferQueue_ = producerCapture;
381 streamInfos.push_back(streamInfoCapture);
382 }
383 }
384 rc = streamOperator->CreateStreams(streamInfos);
385 EXPECT_EQ(false, rc != Camera::NO_ERROR);
386 if (rc == Camera::NO_ERROR) {
387 std::cout << "==========[test log]CreateStreams success." << std::endl;
388 } else {
389 std::cout << "==========[test log]CreateStreams fail, rc = " << rc << std::endl;
390 }
391 rc = streamOperator->CommitStreams(Camera::NORMAL, ability);
392 EXPECT_EQ(false, rc != Camera::NO_ERROR);
393 if (rc == Camera::NO_ERROR) {
394 std::cout << "==========[test log]CommitStreams success." << std::endl;
395 } else {
396 std::cout << "==========[test log]CommitStreams fail, rc = " << rc << std::endl;
397 }
398 std::vector<std::shared_ptr<Camera::StreamInfo>>().swap(streamInfos);
399 }
400
StartCapture(int streamId,int captureId,bool shutterCallback,bool isStreaming)401 void TestDisplay::StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming)
402 {
403 // 获取预览图
404 captureInfo = std::make_shared<Camera::CaptureInfo>();
405 captureInfo->streamIds_ = {streamId};
406 captureInfo->captureSetting_ = ability;
407 captureInfo->enableShutterCallback_ = shutterCallback;
408 rc = streamOperator->Capture(captureId, captureInfo, isStreaming);
409 EXPECT_EQ(true, rc == Camera::NO_ERROR);
410 if (rc == Camera::NO_ERROR) {
411 std::cout << "==========[test log]check Capture: Capture success, " << captureId << std::endl;
412 } else {
413 std::cout << "==========[test log]check Capture: Capture fail, rc = " << rc << std::endl;
414 }
415 sleep(2); // 2:sleep two second
416 }
417
StopStream(std::vector<int> & captureIds,std::vector<int> & streamIds)418 void TestDisplay::StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds)
419 {
420 if (sizeof(captureIds) > 0) {
421 for (auto &captureId : captureIds) {
422 rc = streamOperator->CancelCapture(captureId);
423 EXPECT_EQ(true, rc == Camera::NO_ERROR);
424 if (rc == Camera::NO_ERROR) {
425 std::cout << "==========[test log]check Capture: CancelCapture success," << captureId << std::endl;
426 } else {
427 std::cout << "==========[test log]check Capture: CancelCapture fail, rc = " << rc;
428 std::cout << "captureId = " << captureId << std::endl;
429 }
430 }
431 }
432 if (sizeof(streamIds) > 0) {
433 // 释放流
434 rc = streamOperator->ReleaseStreams(streamIds);
435 EXPECT_EQ(true, rc == Camera::NO_ERROR);
436 if (rc == Camera::NO_ERROR) {
437 std::cout << "==========[test log]check Capture: ReleaseStreams success." << std::endl;
438 } else {
439 std::cout << "==========[test log]check Capture: ReleaseStreams fail, rc = " << rc << std::endl;
440 }
441 }
442 }