blob: 2a91053d728c210907d33d23198354867fd15939 [file] [log] [blame]
fei.dengf7a0cd32023-08-29 09:36:37 +00001/*
2 * Copyright (C) 2021 Amlogic Corporation.
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 */
fei.dengdd910ef2024-06-07 10:25:30 +080016#include <fcntl.h>
17#include <string.h>
fei.dengf7a0cd32023-08-29 09:36:37 +000018#include "wayland_plugin.h"
19#include "wayland_display.h"
20#include "Logger.h"
21#include "Times.h"
fei.dengb9a1a572023-09-13 01:33:57 +000022#include "ErrorCode.h"
fei.dengf7a0cd32023-08-29 09:36:37 +000023
24#define TAG "rlib:wayland_plugin"
25
fei.dengb9a1a572023-09-13 01:33:57 +000026WaylandPlugin::WaylandPlugin(int logCatgory)
27 : mRenderLock("renderlock"),
28 mLogCategory(logCatgory)
fei.dengf7a0cd32023-08-29 09:36:37 +000029{
fei.dengb9a1a572023-09-13 01:33:57 +000030 mDisplay = new WaylandDisplay(this, logCatgory);
fei.dengf7a0cd32023-08-29 09:36:37 +000031 mQueue = new Tls::Queue();
32 mPaused = false;
33 mImmediatelyOutput = false;
34}
35
36WaylandPlugin::~WaylandPlugin()
37{
38 if (mDisplay) {
39 delete mDisplay;
40 }
41 if (mQueue) {
42 mQueue->flush();
43 delete mQueue;
44 mQueue = NULL;
45 }
fei.dengb9a1a572023-09-13 01:33:57 +000046 TRACE(mLogCategory,"desconstruct");
fei.dengf7a0cd32023-08-29 09:36:37 +000047}
48
49void WaylandPlugin::init()
50{
fei.dengb9a1a572023-09-13 01:33:57 +000051 INFO(mLogCategory,"\n--------------------------------\n"
fei.dengf7a0cd32023-08-29 09:36:37 +000052 "plugin : weston\n"
53 "ARCH : %s\n"
54 "branch name : %s\n"
55 "git version : %s\n"
56 "change id : %s \n"
57 "ID : %s \n"
58 "last changed: %s\n"
59 "build-time : %s\n"
60 "build-name : %s\n"
61 "--------------------------------\n",
62#if defined(__aarch64__)
63 "arm64",
64#else
65 "arm",
66#endif
67 BRANCH_NAME,
68 GIT_VERSION,
69 COMMIT_CHANGEID,
70 COMMIT_PD,
71 LAST_CHANGED,
72 BUILD_TIME,
73 BUILD_NAME
74 );
75}
76
77void WaylandPlugin::release()
78{
79}
80
81void WaylandPlugin::setCallback(void *userData, PluginCallback *callback)
82{
83 mUserData = userData;
84 mCallback = callback;
85}
86
87int WaylandPlugin::openDisplay()
88{
89 int ret;
90
fei.dengb9a1a572023-09-13 01:33:57 +000091 Tls::Mutex::Autolock _l(mRenderLock);
92 DEBUG(mLogCategory,"openDisplay");
fei.dengf7a0cd32023-08-29 09:36:37 +000093 ret = mDisplay->openDisplay();
fei.dengb9a1a572023-09-13 01:33:57 +000094 if (ret != NO_ERROR) {
95 ERROR(mLogCategory,"Error open display");
96 return ret;
fei.dengf7a0cd32023-08-29 09:36:37 +000097 }
fei.dengb9a1a572023-09-13 01:33:57 +000098 DEBUG(mLogCategory,"openDisplay end");
99 return ret;
fei.dengf7a0cd32023-08-29 09:36:37 +0000100}
101
102int WaylandPlugin::openWindow()
103{
fei.dengb9a1a572023-09-13 01:33:57 +0000104 Tls::Mutex::Autolock _l(mRenderLock);
fei.dengf7a0cd32023-08-29 09:36:37 +0000105 /* if weston can't support pts feature,
106 * we should create a post buffer thread to
107 * send buffer by mono time
108 */
fei.deng640c3c92024-04-12 08:31:19 +0000109 WaylandDisplay::AmlConfigAPIList *amlconfig = mDisplay->getAmlConfigAPIList();
110 if (!amlconfig->enableSetPts) {
fei.dengdd910ef2024-06-07 10:25:30 +0800111 DEBUG(mLogCategory,"run frame post thread");
fei.dengf7a0cd32023-08-29 09:36:37 +0000112 setThreadPriority(50);
113 run("waylandPostThread");
114 }
fei.dengb9a1a572023-09-13 01:33:57 +0000115 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000116}
117
118int WaylandPlugin::prepareFrame(RenderBuffer *buffer)
119{
120 mDisplay->prepareFrameBuffer(buffer);
fei.dengb9a1a572023-09-13 01:33:57 +0000121 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000122}
123
124int WaylandPlugin::displayFrame(RenderBuffer *buffer, int64_t displayTime)
125{
126 /* if weston can't support pts feature,
127 * push buffer to queue, the buffer will send to
128 * weston in post thread
129 */
fei.deng640c3c92024-04-12 08:31:19 +0000130 WaylandDisplay::AmlConfigAPIList *amlconfig = mDisplay->getAmlConfigAPIList();
131 if (!amlconfig->enableSetPts) {
fei.dengf7a0cd32023-08-29 09:36:37 +0000132 buffer->time = displayTime;
133 mQueue->push(buffer);
fei.dengdd910ef2024-06-07 10:25:30 +0800134 DEBUG(mLogCategory,"queue size:%d,pts:%lld us",mQueue->getCnt(),buffer->pts/1000);
fei.dengf7a0cd32023-08-29 09:36:37 +0000135 } else {
136 mDisplay->displayFrameBuffer(buffer, displayTime);
137 }
fei.dengb9a1a572023-09-13 01:33:57 +0000138 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000139}
140
141void WaylandPlugin::queueFlushCallback(void *userdata,void *data)
142{
143 WaylandPlugin* plugin = static_cast<WaylandPlugin *>(userdata);
144 plugin->handleFrameDropped((RenderBuffer *)data);
145 plugin->handleBufferRelease((RenderBuffer *)data);
146}
147
148int WaylandPlugin::flush()
149{
150 RenderBuffer *entity;
151 mQueue->flushAndCallback(this, WaylandPlugin::queueFlushCallback);
152 mDisplay->flushBuffers();
fei.dengb9a1a572023-09-13 01:33:57 +0000153 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000154}
155
156int WaylandPlugin::pause()
157{
158 mPaused = true;
fei.dengb9a1a572023-09-13 01:33:57 +0000159 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000160}
161int WaylandPlugin::resume()
162{
163 mPaused = false;
fei.dengb9a1a572023-09-13 01:33:57 +0000164 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000165}
166
167int WaylandPlugin::closeDisplay()
168{
169 RenderBuffer *entity;
fei.dengb9a1a572023-09-13 01:33:57 +0000170 Tls::Mutex::Autolock _l(mRenderLock);
fei.dengf7a0cd32023-08-29 09:36:37 +0000171 mDisplay->closeDisplay();
fei.dengf7a0cd32023-08-29 09:36:37 +0000172
fei.dengb9a1a572023-09-13 01:33:57 +0000173 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000174}
175
176int WaylandPlugin::closeWindow()
177{
fei.dengb9a1a572023-09-13 01:33:57 +0000178 Tls::Mutex::Autolock _l(mRenderLock);
fei.dengf7a0cd32023-08-29 09:36:37 +0000179 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000180 DEBUG(mLogCategory,"stop frame post thread");
fei.dengf7a0cd32023-08-29 09:36:37 +0000181 requestExitAndWait();
182 }
fei.dengdd910ef2024-06-07 10:25:30 +0800183 mQueue->flushAndCallback(this, WaylandPlugin::queueFlushCallback);
fei.dengb9a1a572023-09-13 01:33:57 +0000184 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000185}
186
187
188int WaylandPlugin::getValue(PluginKey key, void *value)
189{
190 switch (key) {
191 case PLUGIN_KEY_SELECT_DISPLAY_OUTPUT: {
192 *(int *)(value) = mDisplay->getDisplayOutput();
fei.dengb9a1a572023-09-13 01:33:57 +0000193 TRACE(mLogCategory,"get select display output:%d",*(int *)value);
fei.dengf7a0cd32023-08-29 09:36:37 +0000194 } break;
fei.dengf1f5fc32023-12-06 06:22:20 +0000195 case PLUGIN_KEY_CURRENT_OUTPUT: {
196 *(int *)(value) = mDisplay->getCurrentOutputCrtcIndex();
197 //DEBUG(mLogCategory,"get current crtc output index:%d",*(int *)value);
198 } break;
fei.dengf7a0cd32023-08-29 09:36:37 +0000199 }
fei.dengb9a1a572023-09-13 01:33:57 +0000200 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000201}
202
203int WaylandPlugin::setValue(PluginKey key, void *value)
204{
205 switch (key) {
206 case PLUGIN_KEY_WINDOW_SIZE: {
207 RenderRect* rect = static_cast<RenderRect*>(value);
208 if (mDisplay) {
209 mDisplay->setWindowSize(rect->x, rect->y, rect->w, rect->h);
210 }
211 } break;
212 case PLUGIN_KEY_FRAME_SIZE: {
213 RenderFrameSize * frameSize = static_cast<RenderFrameSize * >(value);
214 if (mDisplay) {
215 mDisplay->setFrameSize(frameSize->width, frameSize->height);
216 }
217 } break;
218 case PLUGIN_KEY_VIDEO_FORMAT: {
219 int videoFormat = *(int *)(value);
fei.dengb9a1a572023-09-13 01:33:57 +0000220 DEBUG(mLogCategory,"Set video format :%d",videoFormat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000221 mDisplay->setVideoBufferFormat((RenderVideoFormat)videoFormat);
222 } break;
223 case PLUGIN_KEY_SELECT_DISPLAY_OUTPUT: {
224 int outputIndex = *(int *)(value);
fei.dengb9a1a572023-09-13 01:33:57 +0000225 DEBUG(mLogCategory,"Set select display output :%d",outputIndex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000226 mDisplay->setDisplayOutput(outputIndex);
227 } break;
228 case PLUGIN_KEY_VIDEO_PIP: {
229 int pip = *(int *) (value);
230 pip = pip > 0? 1: 0;
231 mDisplay->setPip(pip);
232 } break;
233 case PLUGIN_KEY_IMMEDIATELY_OUTPUT: {
234 bool mImmediatelyOutput = (*(int *)(value)) > 0? true: false;
fei.dengb9a1a572023-09-13 01:33:57 +0000235 DEBUG(mLogCategory, "Set immediately output:%d",mImmediatelyOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +0000236 } break;
fei.deng640c3c92024-04-12 08:31:19 +0000237 case PLUGIN_KEY_KEEP_LAST_FRAME: {
238 int keep = *(int *) (value);
239 DEBUG(mLogCategory, "Set keep last frame:%d",keep);
240 mDisplay->setKeepLastFrame(keep);
241 } break;
fei.dengf7a0cd32023-08-29 09:36:37 +0000242 }
243 return 0;
244}
245
246void WaylandPlugin::handleBufferRelease(RenderBuffer *buffer)
247{
248 if (mCallback) {
249 mCallback->doBufferReleaseCallback(mUserData, (void *)buffer);
250 }
251}
252
253void WaylandPlugin::handleFrameDisplayed(RenderBuffer *buffer)
254{
255 if (mCallback) {
256 mCallback->doBufferDisplayedCallback(mUserData, (void *)buffer);
257 }
258}
259
260void WaylandPlugin::handleFrameDropped(RenderBuffer *buffer)
261{
262 if (mCallback) {
263 mCallback->doBufferDropedCallback(mUserData, (void *)buffer);
264 }
265}
266
fei.deng3287c082024-04-23 09:29:22 +0000267void WaylandPlugin::handleMsgNotify(int type, void *detail)
268{
269 if (mCallback) {
270 mCallback->doMsgCallback(mUserData, type, detail);
271 }
272}
273
fei.dengf7a0cd32023-08-29 09:36:37 +0000274void WaylandPlugin::readyToRun()
275{
276}
277
278bool WaylandPlugin::threadLoop()
279{
280 RenderBuffer *curFrameEntity = NULL;
281 RenderBuffer *expiredFrameEntity = NULL;
282 int64_t nowMonotime = Tls::Times::getSystemTimeUs();
283
284 //if queue is empty or paused, loop next
285 if (mQueue->isEmpty() || mPaused) {
286 goto tag_next;
287 }
288
fei.deng1cfb2752023-10-26 08:01:25 +0000289 //if weston has no wl_outout,it means weston can't display frames
290 //so we should display buffer to display to drop buffers
291 if (mDisplay->getWlOutput() == NULL) {
292 mQueue->pop((void **)&expiredFrameEntity);
293 goto tag_post;
294 }
295
fei.dengf7a0cd32023-08-29 09:36:37 +0000296 //if weston obtains a buffer rendering,we can't send buffer to weston
297 if (mDisplay->isRedrawingPending()) {
298 goto tag_next;
299 }
300
301 //we output video frame asap
302 if (mImmediatelyOutput) {
303 //pop the peeked frame
304 mQueue->pop((void **)&expiredFrameEntity);
305 goto tag_post;
306 }
307
308 while (mQueue->peek((void **)&curFrameEntity, 0) == Q_OK)
309 {
310 //no frame expired,loop next
311 if (nowMonotime < curFrameEntity->time) {
312 break;
313 }
314
315 //pop the peeked frame
316 mQueue->pop((void **)&curFrameEntity);
317
318 //drop last expired frame,got a new expired frame
319 if (expiredFrameEntity) {
fei.dengb9a1a572023-09-13 01:33:57 +0000320 WARNING(mLogCategory,"drop,now:%lld,display:%lld(pts:%lld ms),n-d:%lld ms",
fei.dengf7a0cd32023-08-29 09:36:37 +0000321 nowMonotime,expiredFrameEntity->time,expiredFrameEntity->pts/1000000,
322 (nowMonotime - expiredFrameEntity->time)/1000);
323 handleFrameDropped(expiredFrameEntity);
324 handleBufferRelease(expiredFrameEntity);
325 expiredFrameEntity = NULL;
326 }
327
328 expiredFrameEntity = curFrameEntity;
329 }
330
331tag_post:
332 if (!expiredFrameEntity) {
333 //TRACE(mLogCategory,"no frame expire");
334 goto tag_next;
335 }
336
337 if (mDisplay) {
fei.dengdd910ef2024-06-07 10:25:30 +0800338 TRACE(mLogCategory,"post,now:%lld,display:%lld(pts:%lld ms),n-d::%lld ms, size:%d",
fei.dengf7a0cd32023-08-29 09:36:37 +0000339 nowMonotime,expiredFrameEntity->time,expiredFrameEntity->pts/1000000,
fei.dengdd910ef2024-06-07 10:25:30 +0800340 (nowMonotime - expiredFrameEntity->time)/1000,mQueue->getCnt());
fei.dengf7a0cd32023-08-29 09:36:37 +0000341 mDisplay->displayFrameBuffer(expiredFrameEntity, expiredFrameEntity->time);
342 }
343
344tag_next:
345 usleep(4*1000);
346 return true;
347}
348
349void *makePluginInstance(int id)
350{
fei.dengdd910ef2024-06-07 10:25:30 +0800351 int fd= -1;
352 const char *levelPath = "/run/rlib_plugin_level";
353
fei.dengb9a1a572023-09-13 01:33:57 +0000354 int category =Logger_init(id);
fei.dengc4677852023-10-09 07:21:04 +0000355 char *env = getenv("VIDEO_RENDER_PLUGIN_LOG_LEVEL");
fei.dengf7a0cd32023-08-29 09:36:37 +0000356 if (env) {
357 int level = atoi(env);
358 Logger_set_level(level);
fei.dengdd910ef2024-06-07 10:25:30 +0800359 INFO(category,"env set VIDEO_RENDER_PLUGIN_LOG_LEVEL=%d",level);
fei.dengf7a0cd32023-08-29 09:36:37 +0000360 }
fei.dengdd910ef2024-06-07 10:25:30 +0800361
362 //get log level from /run/rlib_plugin_level
363 fd= open(levelPath, O_RDONLY|O_CLOEXEC);
364 if ( fd >= 0 )
365 {
366 char valstr[64];
367 uint32_t val= 0;
368 int nRead;
369
370 memset(valstr, 0, sizeof(valstr));
371 nRead = read(fd, valstr, sizeof(valstr) - 1);
372 valstr[strlen(valstr)] = '\0';
373 if (sscanf(valstr, "%u", &val) > 0)
374 {
375 Logger_set_level(val);
376 INFO(category,"set VIDEO_RENDER_LOG_LEVEL=%d",val);
377 }
378 close(fd);
379 fd = -1;
380 }
381 Logger_set_level(4);
382
fei.dengb9a1a572023-09-13 01:33:57 +0000383 WaylandPlugin *pluginInstance = new WaylandPlugin(category);
fei.dengf7a0cd32023-08-29 09:36:37 +0000384 return static_cast<void *>(pluginInstance);
385}
386
387void destroyPluginInstance(void * plugin)
388{
fei.dengb9a1a572023-09-13 01:33:57 +0000389 int category;
390
fei.dengf7a0cd32023-08-29 09:36:37 +0000391 WaylandPlugin *pluginInstance = static_cast<WaylandPlugin *>(plugin);
fei.dengb9a1a572023-09-13 01:33:57 +0000392 category = pluginInstance->getLogCategory();
fei.dengf7a0cd32023-08-29 09:36:37 +0000393 delete pluginInstance;
fei.dengb9a1a572023-09-13 01:33:57 +0000394 Logger_exit(category);
fei.dengf7a0cd32023-08-29 09:36:37 +0000395}