Fix build break
[platform/core/api/mediavision.git] / test / testsuites / common / visualizer / src / mv_util_visualizer_2d.cpp
1 /**
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <mv_private.h>
18 #include <grpcpp/grpcpp.h>
19
20 #include <opencv2/core.hpp>
21 #include <opencv2/imgcodecs.hpp>
22 #include <opencv2/imgproc/imgproc.hpp>
23 #include <opencv2/opencv.hpp>
24
25 #include "mv_util_visualizer_rd.grpc.pb.h"
26 #include "mv_util_visualizer_2d.h"
27
28 static int win_w = 1920;
29 static int win_h = 1080;
30 static bool initialized = false;
31
32 class ImageClientImpl
33 {
34 public:
35         ImageClientImpl(std::shared_ptr<grpc::Channel> channel) : stub_(NLImageService::NewStub(channel))
36         {}
37
38         void DrawImage(mv_source_h source)
39         {
40                 unsigned int width = 0u, height = 0u;
41                 unsigned int bufferSize = 0u;
42                 unsigned char *buffer = NULL;
43
44                 mv_source_get_width(source, &width);
45                 mv_source_get_height(source, &height);
46                 mv_source_get_buffer(source, &buffer, &bufferSize);
47
48                 cv::Mat _frame, frame;
49
50                 _frame = cv::Mat(cv::Size(width, height), CV_8UC4, buffer);
51                 cv::cvtColor(_frame, frame, cv::COLOR_RGBA2BGR);
52
53                 std::vector<int> qualityType;
54                 std::vector<uchar> buff;
55                 qualityType.push_back(cv::IMWRITE_JPEG_QUALITY);
56                 qualityType.push_back(90);
57                 cv::imencode(".jpg", frame, buff, qualityType);
58
59                 NLImageDrawRequest *request = new NLImageDrawRequest;
60                 NLImage tmp;
61                 unsigned long length = buff.size();
62
63                 std::string str(buff.begin(), buff.end());
64                 std::string NLstring(str);
65
66                 tmp.set_length(length);
67                 tmp.set_data(NLstring);
68
69                 request->mutable_image()->set_data(tmp.data());
70                 request->mutable_image()->set_length(tmp.length());
71
72                 ::Empty reply;
73
74                 grpc::ClientContext context;
75                 grpc::Status status = stub_->DrawImage(&context, *request, &reply);
76         }
77
78 private:
79         std::unique_ptr<NLImageService::Stub> stub_;
80 };
81
82 static int create_window_surface()
83 {
84         int err = MEDIA_VISION_ERROR_NONE;
85         err = egl_init_with_platform_window_surface(2, 8, 0, 0, win_w, win_h);
86         if (MEDIA_VISION_ERROR_NONE != err) {
87                 LOGE("egl_init_with_platform_window_surface: %i", err);
88                 return MEDIA_VISION_ERROR_INTERNAL;
89         }
90         err = init_2d_renderer(win_w, win_h);
91         if (MEDIA_VISION_ERROR_NONE != err) {
92                 LOGE("init_2d_renderer: %i", err);
93                 return MEDIA_VISION_ERROR_INTERNAL;
94         }
95         glClearColor(0.f, 0.f, 0.f, 1.0f);
96         glClear(GL_COLOR_BUFFER_BIT);
97         glViewport(0, 0, win_w, win_h);
98
99         return MEDIA_VISION_ERROR_NONE;
100 }
101
102 int mv_util_visualizer_2d(mv_source_h source, const char *url)
103 {
104         int texid;
105         int texw, texh;
106         int err = MEDIA_VISION_ERROR_NONE;
107         texture_2d_t captex = { 0 };
108
109         if (url != NULL) { // remote display
110                 ImageClientImpl imageChannel(grpc::CreateChannel(url, grpc::InsecureChannelCredentials()));
111                 imageChannel.DrawImage(source);
112
113                 return MEDIA_VISION_ERROR_NONE;
114         }
115
116         // target display
117         if (!initialized) {
118                 if (create_window_surface() != MEDIA_VISION_ERROR_NONE) {
119                         return MEDIA_VISION_ERROR_INTERNAL;
120                 }
121                 initialized = true;
122         }
123
124         err = load_texture(source, &texid, &texw, &texh);
125         if (MEDIA_VISION_ERROR_NONE != err) {
126                 LOGE("load_texture: %i", err);
127                 return MEDIA_VISION_ERROR_INTERNAL;
128         }
129
130         captex.texid = texid;
131         captex.width = texw;
132         captex.height = texh;
133         captex.format = pixfmt_fourcc('R', 'G', 'B', 'A');
134
135         float scale = (float) win_h / (float) texh;
136
137         err = draw_2d_texture(&captex, (win_w - scale * texw) * 0.5f, 0, scale * texw, scale * texh, 0);
138         if (MEDIA_VISION_ERROR_NONE != err) {
139                 LOGE("draw_2d_texture_ex: %i", err);
140                 goto out;
141         }
142
143         err = egl_swap();
144         if (MEDIA_VISION_ERROR_NONE != err) {
145                 LOGE("egl_swap: %i", err);
146         }
147
148 out:
149         glDeleteTextures(1, reinterpret_cast<GLuint *>(&texid));
150         return err;
151 }