blob: ee6807c3464a1e12e8fa08515a7b0f188c431b90 [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 {
fei.dengae8c90a2024-06-27 13:39:53 +0800136 buffer->time = displayTime;
fei.dengf7a0cd32023-08-29 09:36:37 +0000137 mDisplay->displayFrameBuffer(buffer, displayTime);
138 }
fei.dengb9a1a572023-09-13 01:33:57 +0000139 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000140}
141
142void WaylandPlugin::queueFlushCallback(void *userdata,void *data)
143{
144 WaylandPlugin* plugin = static_cast<WaylandPlugin *>(userdata);
145 plugin->handleFrameDropped((RenderBuffer *)data);
146 plugin->handleBufferRelease((RenderBuffer *)data);
147}
148
149int WaylandPlugin::flush()
150{
151 RenderBuffer *entity;
152 mQueue->flushAndCallback(this, WaylandPlugin::queueFlushCallback);
153 mDisplay->flushBuffers();
fei.dengb9a1a572023-09-13 01:33:57 +0000154 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000155}
156
157int WaylandPlugin::pause()
158{
159 mPaused = true;
fei.dengb9a1a572023-09-13 01:33:57 +0000160 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000161}
162int WaylandPlugin::resume()
163{
164 mPaused = false;
fei.dengb9a1a572023-09-13 01:33:57 +0000165 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000166}
167
168int WaylandPlugin::closeDisplay()
169{
170 RenderBuffer *entity;
fei.dengb9a1a572023-09-13 01:33:57 +0000171 Tls::Mutex::Autolock _l(mRenderLock);
fei.dengf7a0cd32023-08-29 09:36:37 +0000172 mDisplay->closeDisplay();
fei.dengf7a0cd32023-08-29 09:36:37 +0000173
fei.dengb9a1a572023-09-13 01:33:57 +0000174 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000175}
176
177int WaylandPlugin::closeWindow()
178{
fei.dengb9a1a572023-09-13 01:33:57 +0000179 Tls::Mutex::Autolock _l(mRenderLock);
fei.dengf7a0cd32023-08-29 09:36:37 +0000180 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000181 DEBUG(mLogCategory,"stop frame post thread");
fei.dengf7a0cd32023-08-29 09:36:37 +0000182 requestExitAndWait();
183 }
fei.dengdd910ef2024-06-07 10:25:30 +0800184 mQueue->flushAndCallback(this, WaylandPlugin::queueFlushCallback);
fei.dengb9a1a572023-09-13 01:33:57 +0000185 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000186}
187
188
189int WaylandPlugin::getValue(PluginKey key, void *value)
190{
191 switch (key) {
192 case PLUGIN_KEY_SELECT_DISPLAY_OUTPUT: {
193 *(int *)(value) = mDisplay->getDisplayOutput();
fei.dengb9a1a572023-09-13 01:33:57 +0000194 TRACE(mLogCategory,"get select display output:%d",*(int *)value);
fei.dengf7a0cd32023-08-29 09:36:37 +0000195 } break;
fei.dengf1f5fc32023-12-06 06:22:20 +0000196 case PLUGIN_KEY_CURRENT_OUTPUT: {
197 *(int *)(value) = mDisplay->getCurrentOutputCrtcIndex();
198 //DEBUG(mLogCategory,"get current crtc output index:%d",*(int *)value);
199 } break;
fei.dengf7a0cd32023-08-29 09:36:37 +0000200 }
fei.dengb9a1a572023-09-13 01:33:57 +0000201 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000202}
203
204int WaylandPlugin::setValue(PluginKey key, void *value)
205{
206 switch (key) {
207 case PLUGIN_KEY_WINDOW_SIZE: {
208 RenderRect* rect = static_cast<RenderRect*>(value);
209 if (mDisplay) {
210 mDisplay->setWindowSize(rect->x, rect->y, rect->w, rect->h);
211 }
212 } break;
213 case PLUGIN_KEY_FRAME_SIZE: {
214 RenderFrameSize * frameSize = static_cast<RenderFrameSize * >(value);
215 if (mDisplay) {
216 mDisplay->setFrameSize(frameSize->width, frameSize->height);
217 }
218 } break;
219 case PLUGIN_KEY_VIDEO_FORMAT: {
220 int videoFormat = *(int *)(value);
fei.dengb9a1a572023-09-13 01:33:57 +0000221 DEBUG(mLogCategory,"Set video format :%d",videoFormat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000222 mDisplay->setVideoBufferFormat((RenderVideoFormat)videoFormat);
223 } break;
224 case PLUGIN_KEY_SELECT_DISPLAY_OUTPUT: {
225 int outputIndex = *(int *)(value);
fei.dengb9a1a572023-09-13 01:33:57 +0000226 DEBUG(mLogCategory,"Set select display output :%d",outputIndex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000227 mDisplay->setDisplayOutput(outputIndex);
228 } break;
229 case PLUGIN_KEY_VIDEO_PIP: {
230 int pip = *(int *) (value);
231 pip = pip > 0? 1: 0;
232 mDisplay->setPip(pip);
233 } break;
234 case PLUGIN_KEY_IMMEDIATELY_OUTPUT: {
235 bool mImmediatelyOutput = (*(int *)(value)) > 0? true: false;
fei.dengb9a1a572023-09-13 01:33:57 +0000236 DEBUG(mLogCategory, "Set immediately output:%d",mImmediatelyOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +0000237 } break;
fei.deng640c3c92024-04-12 08:31:19 +0000238 case PLUGIN_KEY_KEEP_LAST_FRAME: {
239 int keep = *(int *) (value);
240 DEBUG(mLogCategory, "Set keep last frame:%d",keep);
241 mDisplay->setKeepLastFrame(keep);
242 } break;
fei.denga4abbd52024-07-11 19:17:50 +0800243 case PLUGIN_KEY_FORCE_ASPECT_RATIO: {
244 int forceAspectRatio = *(int *)(value);
245 DEBUG(mLogCategory, "force aspect ratio:%d, TODO ",forceAspectRatio);
246 } break;
247 case PLUGIN_KEY_PIXEL_ASPECT_RATIO: {
248 double ratio = *(double *)(value);
249 INFO(mLogCategory,"pixel aspect ratio :%f",ratio);
250 mDisplay->setPixelAspectRatio((double)ratio);
251 } break;
fei.dengf7a0cd32023-08-29 09:36:37 +0000252 }
253 return 0;
254}
255
256void WaylandPlugin::handleBufferRelease(RenderBuffer *buffer)
257{
258 if (mCallback) {
259 mCallback->doBufferReleaseCallback(mUserData, (void *)buffer);
260 }
261}
262
263void WaylandPlugin::handleFrameDisplayed(RenderBuffer *buffer)
264{
265 if (mCallback) {
266 mCallback->doBufferDisplayedCallback(mUserData, (void *)buffer);
267 }
268}
269
270void WaylandPlugin::handleFrameDropped(RenderBuffer *buffer)
271{
272 if (mCallback) {
273 mCallback->doBufferDropedCallback(mUserData, (void *)buffer);
274 }
275}
276
fei.deng3287c082024-04-23 09:29:22 +0000277void WaylandPlugin::handleMsgNotify(int type, void *detail)
278{
279 if (mCallback) {
280 mCallback->doMsgCallback(mUserData, type, detail);
281 }
282}
283
fei.dengf7a0cd32023-08-29 09:36:37 +0000284void WaylandPlugin::readyToRun()
285{
286}
287
288bool WaylandPlugin::threadLoop()
289{
290 RenderBuffer *curFrameEntity = NULL;
291 RenderBuffer *expiredFrameEntity = NULL;
292 int64_t nowMonotime = Tls::Times::getSystemTimeUs();
293
294 //if queue is empty or paused, loop next
295 if (mQueue->isEmpty() || mPaused) {
296 goto tag_next;
297 }
298
fei.deng1cfb2752023-10-26 08:01:25 +0000299 //if weston has no wl_outout,it means weston can't display frames
300 //so we should display buffer to display to drop buffers
301 if (mDisplay->getWlOutput() == NULL) {
302 mQueue->pop((void **)&expiredFrameEntity);
303 goto tag_post;
304 }
305
fei.dengf7a0cd32023-08-29 09:36:37 +0000306 //if weston obtains a buffer rendering,we can't send buffer to weston
307 if (mDisplay->isRedrawingPending()) {
308 goto tag_next;
309 }
310
311 //we output video frame asap
312 if (mImmediatelyOutput) {
313 //pop the peeked frame
314 mQueue->pop((void **)&expiredFrameEntity);
315 goto tag_post;
316 }
317
318 while (mQueue->peek((void **)&curFrameEntity, 0) == Q_OK)
319 {
320 //no frame expired,loop next
321 if (nowMonotime < curFrameEntity->time) {
322 break;
323 }
324
325 //pop the peeked frame
326 mQueue->pop((void **)&curFrameEntity);
327
328 //drop last expired frame,got a new expired frame
329 if (expiredFrameEntity) {
fei.dengb9a1a572023-09-13 01:33:57 +0000330 WARNING(mLogCategory,"drop,now:%lld,display:%lld(pts:%lld ms),n-d:%lld ms",
fei.dengf7a0cd32023-08-29 09:36:37 +0000331 nowMonotime,expiredFrameEntity->time,expiredFrameEntity->pts/1000000,
332 (nowMonotime - expiredFrameEntity->time)/1000);
333 handleFrameDropped(expiredFrameEntity);
334 handleBufferRelease(expiredFrameEntity);
335 expiredFrameEntity = NULL;
336 }
337
338 expiredFrameEntity = curFrameEntity;
339 }
340
341tag_post:
342 if (!expiredFrameEntity) {
343 //TRACE(mLogCategory,"no frame expire");
344 goto tag_next;
345 }
346
347 if (mDisplay) {
fei.dengdd910ef2024-06-07 10:25:30 +0800348 TRACE(mLogCategory,"post,now:%lld,display:%lld(pts:%lld ms),n-d::%lld ms, size:%d",
fei.dengf7a0cd32023-08-29 09:36:37 +0000349 nowMonotime,expiredFrameEntity->time,expiredFrameEntity->pts/1000000,
fei.dengdd910ef2024-06-07 10:25:30 +0800350 (nowMonotime - expiredFrameEntity->time)/1000,mQueue->getCnt());
fei.dengf7a0cd32023-08-29 09:36:37 +0000351 mDisplay->displayFrameBuffer(expiredFrameEntity, expiredFrameEntity->time);
352 }
353
354tag_next:
355 usleep(4*1000);
356 return true;
357}
358
359void *makePluginInstance(int id)
360{
fei.dengdd910ef2024-06-07 10:25:30 +0800361 int fd= -1;
362 const char *levelPath = "/run/rlib_plugin_level";
363
fei.dengb9a1a572023-09-13 01:33:57 +0000364 int category =Logger_init(id);
fei.dengc4677852023-10-09 07:21:04 +0000365 char *env = getenv("VIDEO_RENDER_PLUGIN_LOG_LEVEL");
fei.dengf7a0cd32023-08-29 09:36:37 +0000366 if (env) {
367 int level = atoi(env);
368 Logger_set_level(level);
fei.dengdd910ef2024-06-07 10:25:30 +0800369 INFO(category,"env set VIDEO_RENDER_PLUGIN_LOG_LEVEL=%d",level);
fei.dengf7a0cd32023-08-29 09:36:37 +0000370 }
fei.dengdd910ef2024-06-07 10:25:30 +0800371
372 //get log level from /run/rlib_plugin_level
373 fd= open(levelPath, O_RDONLY|O_CLOEXEC);
374 if ( fd >= 0 )
375 {
376 char valstr[64];
377 uint32_t val= 0;
378 int nRead;
379
380 memset(valstr, 0, sizeof(valstr));
381 nRead = read(fd, valstr, sizeof(valstr) - 1);
382 valstr[strlen(valstr)] = '\0';
383 if (sscanf(valstr, "%u", &val) > 0)
384 {
385 Logger_set_level(val);
386 INFO(category,"set VIDEO_RENDER_LOG_LEVEL=%d",val);
387 }
388 close(fd);
389 fd = -1;
390 }
391 Logger_set_level(4);
392
fei.dengb9a1a572023-09-13 01:33:57 +0000393 WaylandPlugin *pluginInstance = new WaylandPlugin(category);
fei.dengf7a0cd32023-08-29 09:36:37 +0000394 return static_cast<void *>(pluginInstance);
395}
396
397void destroyPluginInstance(void * plugin)
398{
fei.dengb9a1a572023-09-13 01:33:57 +0000399 int category;
400
fei.dengf7a0cd32023-08-29 09:36:37 +0000401 WaylandPlugin *pluginInstance = static_cast<WaylandPlugin *>(plugin);
fei.dengb9a1a572023-09-13 01:33:57 +0000402 category = pluginInstance->getLogCategory();
fei.dengf7a0cd32023-08-29 09:36:37 +0000403 delete pluginInstance;
fei.dengb9a1a572023-09-13 01:33:57 +0000404 Logger_exit(category);
fei.dengf7a0cd32023-08-29 09:36:37 +0000405}