fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | #include "wayland_plugin.h" |
| 17 | #include "wayland_display.h" |
| 18 | #include "Logger.h" |
| 19 | #include "Times.h" |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 20 | #include "ErrorCode.h" |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 21 | |
| 22 | #define TAG "rlib:wayland_plugin" |
| 23 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 24 | WaylandPlugin::WaylandPlugin(int logCatgory) |
| 25 | : mRenderLock("renderlock"), |
| 26 | mLogCategory(logCatgory) |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 27 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 28 | mDisplay = new WaylandDisplay(this, logCatgory); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 29 | mQueue = new Tls::Queue(); |
| 30 | mPaused = false; |
| 31 | mImmediatelyOutput = false; |
| 32 | } |
| 33 | |
| 34 | WaylandPlugin::~WaylandPlugin() |
| 35 | { |
| 36 | if (mDisplay) { |
| 37 | delete mDisplay; |
| 38 | } |
| 39 | if (mQueue) { |
| 40 | mQueue->flush(); |
| 41 | delete mQueue; |
| 42 | mQueue = NULL; |
| 43 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 44 | TRACE(mLogCategory,"desconstruct"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void WaylandPlugin::init() |
| 48 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 49 | INFO(mLogCategory,"\n--------------------------------\n" |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 50 | "plugin : weston\n" |
| 51 | "ARCH : %s\n" |
| 52 | "branch name : %s\n" |
| 53 | "git version : %s\n" |
| 54 | "change id : %s \n" |
| 55 | "ID : %s \n" |
| 56 | "last changed: %s\n" |
| 57 | "build-time : %s\n" |
| 58 | "build-name : %s\n" |
| 59 | "--------------------------------\n", |
| 60 | #if defined(__aarch64__) |
| 61 | "arm64", |
| 62 | #else |
| 63 | "arm", |
| 64 | #endif |
| 65 | BRANCH_NAME, |
| 66 | GIT_VERSION, |
| 67 | COMMIT_CHANGEID, |
| 68 | COMMIT_PD, |
| 69 | LAST_CHANGED, |
| 70 | BUILD_TIME, |
| 71 | BUILD_NAME |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | void WaylandPlugin::release() |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | void WaylandPlugin::setCallback(void *userData, PluginCallback *callback) |
| 80 | { |
| 81 | mUserData = userData; |
| 82 | mCallback = callback; |
| 83 | } |
| 84 | |
| 85 | int WaylandPlugin::openDisplay() |
| 86 | { |
| 87 | int ret; |
| 88 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 89 | Tls::Mutex::Autolock _l(mRenderLock); |
| 90 | DEBUG(mLogCategory,"openDisplay"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 91 | ret = mDisplay->openDisplay(); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 92 | if (ret != NO_ERROR) { |
| 93 | ERROR(mLogCategory,"Error open display"); |
| 94 | return ret; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 95 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 96 | DEBUG(mLogCategory,"openDisplay end"); |
| 97 | return ret; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | int WaylandPlugin::openWindow() |
| 101 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 102 | Tls::Mutex::Autolock _l(mRenderLock); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 103 | /* if weston can't support pts feature, |
| 104 | * we should create a post buffer thread to |
| 105 | * send buffer by mono time |
| 106 | */ |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 107 | WaylandDisplay::AmlConfigAPIList *amlconfig = mDisplay->getAmlConfigAPIList(); |
| 108 | if (!amlconfig->enableSetPts) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 109 | DEBUG(mLogCategory,"run frame post thread"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 110 | setThreadPriority(50); |
| 111 | run("waylandPostThread"); |
| 112 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 113 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int WaylandPlugin::prepareFrame(RenderBuffer *buffer) |
| 117 | { |
| 118 | mDisplay->prepareFrameBuffer(buffer); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 119 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int WaylandPlugin::displayFrame(RenderBuffer *buffer, int64_t displayTime) |
| 123 | { |
| 124 | /* if weston can't support pts feature, |
| 125 | * push buffer to queue, the buffer will send to |
| 126 | * weston in post thread |
| 127 | */ |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 128 | WaylandDisplay::AmlConfigAPIList *amlconfig = mDisplay->getAmlConfigAPIList(); |
| 129 | if (!amlconfig->enableSetPts) { |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 130 | buffer->time = displayTime; |
| 131 | mQueue->push(buffer); |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 132 | DEBUG(mLogCategory,"queue size:%d",mQueue->getCnt()); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 133 | } else { |
| 134 | mDisplay->displayFrameBuffer(buffer, displayTime); |
| 135 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 136 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void WaylandPlugin::queueFlushCallback(void *userdata,void *data) |
| 140 | { |
| 141 | WaylandPlugin* plugin = static_cast<WaylandPlugin *>(userdata); |
| 142 | plugin->handleFrameDropped((RenderBuffer *)data); |
| 143 | plugin->handleBufferRelease((RenderBuffer *)data); |
| 144 | } |
| 145 | |
| 146 | int WaylandPlugin::flush() |
| 147 | { |
| 148 | RenderBuffer *entity; |
| 149 | mQueue->flushAndCallback(this, WaylandPlugin::queueFlushCallback); |
| 150 | mDisplay->flushBuffers(); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 151 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | int WaylandPlugin::pause() |
| 155 | { |
| 156 | mPaused = true; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 157 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 158 | } |
| 159 | int WaylandPlugin::resume() |
| 160 | { |
| 161 | mPaused = false; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 162 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | int WaylandPlugin::closeDisplay() |
| 166 | { |
| 167 | RenderBuffer *entity; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 168 | Tls::Mutex::Autolock _l(mRenderLock); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 169 | mDisplay->closeDisplay(); |
| 170 | while (mQueue->pop((void **)&entity) == Q_OK) |
| 171 | { |
| 172 | handleBufferRelease(entity); |
| 173 | } |
| 174 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 175 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int WaylandPlugin::closeWindow() |
| 179 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 180 | Tls::Mutex::Autolock _l(mRenderLock); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 181 | if (isRunning()) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 182 | DEBUG(mLogCategory,"stop frame post thread"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 183 | requestExitAndWait(); |
| 184 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 185 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
| 189 | int WaylandPlugin::getValue(PluginKey key, void *value) |
| 190 | { |
| 191 | switch (key) { |
| 192 | case PLUGIN_KEY_SELECT_DISPLAY_OUTPUT: { |
| 193 | *(int *)(value) = mDisplay->getDisplayOutput(); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 194 | TRACE(mLogCategory,"get select display output:%d",*(int *)value); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 195 | } break; |
fei.deng | f1f5fc3 | 2023-12-06 06:22:20 +0000 | [diff] [blame] | 196 | case PLUGIN_KEY_CURRENT_OUTPUT: { |
| 197 | *(int *)(value) = mDisplay->getCurrentOutputCrtcIndex(); |
| 198 | //DEBUG(mLogCategory,"get current crtc output index:%d",*(int *)value); |
| 199 | } break; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 200 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 201 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int 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.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 221 | DEBUG(mLogCategory,"Set video format :%d",videoFormat); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 222 | mDisplay->setVideoBufferFormat((RenderVideoFormat)videoFormat); |
| 223 | } break; |
| 224 | case PLUGIN_KEY_SELECT_DISPLAY_OUTPUT: { |
| 225 | int outputIndex = *(int *)(value); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 226 | DEBUG(mLogCategory,"Set select display output :%d",outputIndex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 227 | 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.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 236 | DEBUG(mLogCategory, "Set immediately output:%d",mImmediatelyOutput); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 237 | } break; |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 238 | 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.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | void WaylandPlugin::handleBufferRelease(RenderBuffer *buffer) |
| 248 | { |
| 249 | if (mCallback) { |
| 250 | mCallback->doBufferReleaseCallback(mUserData, (void *)buffer); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void WaylandPlugin::handleFrameDisplayed(RenderBuffer *buffer) |
| 255 | { |
| 256 | if (mCallback) { |
| 257 | mCallback->doBufferDisplayedCallback(mUserData, (void *)buffer); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void WaylandPlugin::handleFrameDropped(RenderBuffer *buffer) |
| 262 | { |
| 263 | if (mCallback) { |
| 264 | mCallback->doBufferDropedCallback(mUserData, (void *)buffer); |
| 265 | } |
| 266 | } |
| 267 | |
fei.deng | 3287c08 | 2024-04-23 09:29:22 +0000 | [diff] [blame^] | 268 | void WaylandPlugin::handleMsgNotify(int type, void *detail) |
| 269 | { |
| 270 | if (mCallback) { |
| 271 | mCallback->doMsgCallback(mUserData, type, detail); |
| 272 | } |
| 273 | } |
| 274 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 275 | void WaylandPlugin::readyToRun() |
| 276 | { |
| 277 | } |
| 278 | |
| 279 | bool WaylandPlugin::threadLoop() |
| 280 | { |
| 281 | RenderBuffer *curFrameEntity = NULL; |
| 282 | RenderBuffer *expiredFrameEntity = NULL; |
| 283 | int64_t nowMonotime = Tls::Times::getSystemTimeUs(); |
| 284 | |
| 285 | //if queue is empty or paused, loop next |
| 286 | if (mQueue->isEmpty() || mPaused) { |
| 287 | goto tag_next; |
| 288 | } |
| 289 | |
fei.deng | 1cfb275 | 2023-10-26 08:01:25 +0000 | [diff] [blame] | 290 | //if weston has no wl_outout,it means weston can't display frames |
| 291 | //so we should display buffer to display to drop buffers |
| 292 | if (mDisplay->getWlOutput() == NULL) { |
| 293 | mQueue->pop((void **)&expiredFrameEntity); |
| 294 | goto tag_post; |
| 295 | } |
| 296 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 297 | //if weston obtains a buffer rendering,we can't send buffer to weston |
| 298 | if (mDisplay->isRedrawingPending()) { |
| 299 | goto tag_next; |
| 300 | } |
| 301 | |
| 302 | //we output video frame asap |
| 303 | if (mImmediatelyOutput) { |
| 304 | //pop the peeked frame |
| 305 | mQueue->pop((void **)&expiredFrameEntity); |
| 306 | goto tag_post; |
| 307 | } |
| 308 | |
| 309 | while (mQueue->peek((void **)&curFrameEntity, 0) == Q_OK) |
| 310 | { |
| 311 | //no frame expired,loop next |
| 312 | if (nowMonotime < curFrameEntity->time) { |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | //pop the peeked frame |
| 317 | mQueue->pop((void **)&curFrameEntity); |
| 318 | |
| 319 | //drop last expired frame,got a new expired frame |
| 320 | if (expiredFrameEntity) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 321 | WARNING(mLogCategory,"drop,now:%lld,display:%lld(pts:%lld ms),n-d:%lld ms", |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 322 | nowMonotime,expiredFrameEntity->time,expiredFrameEntity->pts/1000000, |
| 323 | (nowMonotime - expiredFrameEntity->time)/1000); |
| 324 | handleFrameDropped(expiredFrameEntity); |
| 325 | handleBufferRelease(expiredFrameEntity); |
| 326 | expiredFrameEntity = NULL; |
| 327 | } |
| 328 | |
| 329 | expiredFrameEntity = curFrameEntity; |
| 330 | } |
| 331 | |
| 332 | tag_post: |
| 333 | if (!expiredFrameEntity) { |
| 334 | //TRACE(mLogCategory,"no frame expire"); |
| 335 | goto tag_next; |
| 336 | } |
| 337 | |
| 338 | if (mDisplay) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 339 | TRACE(mLogCategory,"post,now:%lld,display:%lld(pts:%lld ms),n-d::%lld ms", |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 340 | nowMonotime,expiredFrameEntity->time,expiredFrameEntity->pts/1000000, |
| 341 | (nowMonotime - expiredFrameEntity->time)/1000); |
| 342 | mDisplay->displayFrameBuffer(expiredFrameEntity, expiredFrameEntity->time); |
| 343 | } |
| 344 | |
| 345 | tag_next: |
| 346 | usleep(4*1000); |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | void *makePluginInstance(int id) |
| 351 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 352 | int category =Logger_init(id); |
fei.deng | c467785 | 2023-10-09 07:21:04 +0000 | [diff] [blame] | 353 | char *env = getenv("VIDEO_RENDER_PLUGIN_LOG_LEVEL"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 354 | if (env) { |
| 355 | int level = atoi(env); |
| 356 | Logger_set_level(level); |
fei.deng | c467785 | 2023-10-09 07:21:04 +0000 | [diff] [blame] | 357 | INFO(category,"VIDEO_RENDER_PLUGIN_LOG_LEVEL=%d",level); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 358 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 359 | WaylandPlugin *pluginInstance = new WaylandPlugin(category); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 360 | return static_cast<void *>(pluginInstance); |
| 361 | } |
| 362 | |
| 363 | void destroyPluginInstance(void * plugin) |
| 364 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 365 | int category; |
| 366 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 367 | WaylandPlugin *pluginInstance = static_cast<WaylandPlugin *>(plugin); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 368 | category = pluginInstance->getLogCategory(); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 369 | delete pluginInstance; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 370 | Logger_exit(category); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 371 | } |