blob: 27019b1a23f6e9de2ae1f334c66f0dad0e404920 [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 */
16#include <sys/types.h>
17#include <sys/socket.h>
18#include <unistd.h>
19#include <fcntl.h>
20#include <errno.h>
21#include <sys/epoll.h>
22#include <string.h>
23#include <stdlib.h>
24#include <wayland-client.h>
25#include "xdg-shell-client-protocol.h"
26#include "viewporter-client-protocol.h"
27#include "linux-dmabuf-unstable-v1-client-protocol.h"
28#include "fullscreen-shell-unstable-v1-client-protocol.h"
29#include "wayland_display.h"
30#include "wayland_buffer.h"
31#include "Logger.h"
32#include "wayland_videoformat.h"
33#include "wayland_dma.h"
34#include "wayland_shm.h"
35#include "Utils.h"
fei.dengb9a1a572023-09-13 01:33:57 +000036#include "ErrorCode.h"
fei.dengf7a0cd32023-08-29 09:36:37 +000037
38#define TAG "rlib:wayland_buffer"
39
fei.dengb9a1a572023-09-13 01:33:57 +000040WaylandBuffer::WaylandBuffer(WaylandDisplay *display, int logCategory)
41 : mDisplay(display),
42 mLogCategory(logCategory)
fei.dengf7a0cd32023-08-29 09:36:37 +000043{
44 mRenderBuffer = NULL;
45 mWaylandWlWrap = NULL;
46 mUsedByCompositor = false;
47 mRedrawingPending = false;
48 mRealTime = -1;
49 mBufferFormat = VIDEO_FORMAT_UNKNOWN;
50 mFrameWidth = 0;
51 mFrameHeight = 0;
52}
53
54WaylandBuffer::~WaylandBuffer()
55{
56 /*if weston obtains the wl_buffer,we need
57 * notify user to release renderBuffer*/
58 if (mRenderBuffer) {
59 mDisplay->handleBufferReleaseCallback(this);
60 mRenderBuffer = NULL;
61 }
62 if (mWaylandWlWrap) {
63 delete mWaylandWlWrap;
64 mWaylandWlWrap = NULL;
65 }
66}
67
68void WaylandBuffer::bufferRelease (void *data, struct wl_buffer *wl_buffer)
69{
70 WaylandBuffer* waylandBuffer = static_cast<WaylandBuffer*>(data);
fei.dengb9a1a572023-09-13 01:33:57 +000071 TRACE(waylandBuffer->mLogCategory,"--wl_buffer:%p,renderBuffer:%p",wl_buffer,waylandBuffer->mRenderBuffer);
fei.dengf7a0cd32023-08-29 09:36:37 +000072 waylandBuffer->mUsedByCompositor = false;
73 //sometimes this callback be called twice
74 //this cause double free,so check mRenderBuffer
75 if (waylandBuffer->mRenderBuffer) {
76 waylandBuffer->mDisplay->handleBufferReleaseCallback(waylandBuffer);
77 waylandBuffer->mRenderBuffer = NULL;
fei.dengae8c90a2024-06-27 13:39:53 +080078 waylandBuffer->mRealTime = -1;
fei.dengf7a0cd32023-08-29 09:36:37 +000079 }
80}
81
82void WaylandBuffer::bufferdroped (void *data, struct wl_buffer *wl_buffer)
83{
84 WaylandBuffer* waylandBuffer = static_cast<WaylandBuffer*>(data);
fei.dengb9a1a572023-09-13 01:33:57 +000085 WARNING(waylandBuffer->mLogCategory,"--droped wl_buffer:%p,renderBuffer:%p",wl_buffer,waylandBuffer->mRenderBuffer);
fei.dengf7a0cd32023-08-29 09:36:37 +000086 waylandBuffer->mUsedByCompositor = false;
87
88 if (waylandBuffer->mRenderBuffer) {
89 waylandBuffer->mDisplay->handleFrameDropedCallback(waylandBuffer);
90 waylandBuffer->mDisplay->handleBufferReleaseCallback(waylandBuffer);
91 waylandBuffer->mRenderBuffer = NULL;
92 }
93}
94
fei.deng640c3c92024-04-12 08:31:19 +000095static const struct wl_buffer_listener buffer_with_drop_listener = {
96 WaylandBuffer::bufferRelease,
97 WaylandBuffer::bufferdroped,
98};
99
100static const struct wl_buffer_listener buffer_listener = {
101 WaylandBuffer::bufferRelease
102};
fei.dengf7a0cd32023-08-29 09:36:37 +0000103
104/*if we commit buffers to weston too fast,it causes weston can't invoke this callback*/
105void WaylandBuffer::frameDisplayedCallback(void *data, struct wl_callback *callback, uint32_t time)
106{
107 WaylandBuffer* waylandBuffer = static_cast<WaylandBuffer*>(data);
108 waylandBuffer->mDisplay->setRedrawingPending(false);
109 waylandBuffer->mLock.lock();
110 bool redrawing = waylandBuffer->mRedrawingPending;
111 waylandBuffer->mRedrawingPending = false;
112 waylandBuffer->mLock.unlock();
113 if (waylandBuffer->mRenderBuffer && redrawing) {
fei.dengdd910ef2024-06-07 10:25:30 +0800114 TRACE(waylandBuffer->mLogCategory,"renderBuffer:%p, pts:%lld us",waylandBuffer->mRenderBuffer,waylandBuffer->mRenderBuffer->pts/1000);
fei.dengf7a0cd32023-08-29 09:36:37 +0000115 waylandBuffer->mDisplay->handleFrameDisplayedCallback(waylandBuffer);
116 }
117 wl_callback_destroy (callback);
118}
119
120static const struct wl_callback_listener frame_callback_listener = {
121 WaylandBuffer::frameDisplayedCallback
122};
123
124int WaylandBuffer::constructWlBuffer(RenderBuffer *buf)
125{
126 struct wl_buffer * wlbuffer = NULL;
fei.deng640c3c92024-04-12 08:31:19 +0000127 WaylandDisplay::AmlConfigAPIList *amlConfigAPI = mDisplay->getAmlConfigAPIList();
fei.dengf7a0cd32023-08-29 09:36:37 +0000128
129 mRenderBuffer = buf;
130 if (mWaylandWlWrap) {
fei.dengb9a1a572023-09-13 01:33:57 +0000131 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000132 }
133
134 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
fei.dengb9a1a572023-09-13 01:33:57 +0000135 WaylandDmaBuffer *waylanddma = new WaylandDmaBuffer(mDisplay, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +0000136 wlbuffer = waylanddma->constructWlBuffer(&buf->dma, mBufferFormat);
137 if (!wlbuffer) {
138 delete waylanddma;
fei.dengb9a1a572023-09-13 01:33:57 +0000139 ERROR(mLogCategory,"create wl_buffer fail");
140 return ERROR_INVALID_OPERATION;
fei.dengf7a0cd32023-08-29 09:36:37 +0000141 }
142 mWaylandWlWrap = waylanddma;
143 mFrameWidth = buf->dma.width;
144 mFrameHeight = buf->dma.height;
145 }
146
147 /*register buffer release listen*/
fei.deng640c3c92024-04-12 08:31:19 +0000148 if (amlConfigAPI->enableDropFrame) {
149 wl_buffer_add_listener (wlbuffer, &buffer_with_drop_listener, this);
150 } else {
151 wl_buffer_add_listener (wlbuffer, &buffer_listener, this);
152 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000153
fei.dengb9a1a572023-09-13 01:33:57 +0000154 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000155}
156
157struct wl_buffer *WaylandBuffer::getWlBuffer()
158{
159 if (mWaylandWlWrap) {
160 return mWaylandWlWrap->getWlBuffer();
161 } else {
162 return NULL;
163 }
164}
165
166void WaylandBuffer::attach(struct wl_surface *surface)
167{
168 struct wl_callback *callback;
169 struct wl_buffer *wlbuffer = NULL;
170 if (mUsedByCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000171 DEBUG(mLogCategory,"buffer used by compositor");
fei.dengf7a0cd32023-08-29 09:36:37 +0000172 return;
173 }
174
175 //callback when this frame displayed
176 callback = wl_surface_frame (surface);
177 wl_callback_add_listener (callback, &frame_callback_listener, this);
178 mDisplay->setRedrawingPending(true);
179
180 wlbuffer = getWlBuffer();
181 if (wlbuffer) {
182 wl_surface_attach (surface, wlbuffer, 0, 0);
183 }
184
185 mUsedByCompositor = true;
186 mRedrawingPending = true;
187}