blob: f75730a573f1ab3ee08675323dac0b5045233ccf [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;
78 }
79}
80
81void WaylandBuffer::bufferdroped (void *data, struct wl_buffer *wl_buffer)
82{
83 WaylandBuffer* waylandBuffer = static_cast<WaylandBuffer*>(data);
fei.dengb9a1a572023-09-13 01:33:57 +000084 WARNING(waylandBuffer->mLogCategory,"--droped wl_buffer:%p,renderBuffer:%p",wl_buffer,waylandBuffer->mRenderBuffer);
fei.dengf7a0cd32023-08-29 09:36:37 +000085 waylandBuffer->mUsedByCompositor = false;
86
87 if (waylandBuffer->mRenderBuffer) {
88 waylandBuffer->mDisplay->handleFrameDropedCallback(waylandBuffer);
89 waylandBuffer->mDisplay->handleBufferReleaseCallback(waylandBuffer);
90 waylandBuffer->mRenderBuffer = NULL;
91 }
92}
93
fei.deng640c3c92024-04-12 08:31:19 +000094static const struct wl_buffer_listener buffer_with_drop_listener = {
95 WaylandBuffer::bufferRelease,
96 WaylandBuffer::bufferdroped,
97};
98
99static const struct wl_buffer_listener buffer_listener = {
100 WaylandBuffer::bufferRelease
101};
fei.dengf7a0cd32023-08-29 09:36:37 +0000102
103/*if we commit buffers to weston too fast,it causes weston can't invoke this callback*/
104void WaylandBuffer::frameDisplayedCallback(void *data, struct wl_callback *callback, uint32_t time)
105{
106 WaylandBuffer* waylandBuffer = static_cast<WaylandBuffer*>(data);
107 waylandBuffer->mDisplay->setRedrawingPending(false);
108 waylandBuffer->mLock.lock();
109 bool redrawing = waylandBuffer->mRedrawingPending;
110 waylandBuffer->mRedrawingPending = false;
111 waylandBuffer->mLock.unlock();
112 if (waylandBuffer->mRenderBuffer && redrawing) {
fei.dengdd910ef2024-06-07 10:25:30 +0800113 TRACE(waylandBuffer->mLogCategory,"renderBuffer:%p, pts:%lld us",waylandBuffer->mRenderBuffer,waylandBuffer->mRenderBuffer->pts/1000);
fei.dengf7a0cd32023-08-29 09:36:37 +0000114 waylandBuffer->mDisplay->handleFrameDisplayedCallback(waylandBuffer);
115 }
116 wl_callback_destroy (callback);
117}
118
119static const struct wl_callback_listener frame_callback_listener = {
120 WaylandBuffer::frameDisplayedCallback
121};
122
123int WaylandBuffer::constructWlBuffer(RenderBuffer *buf)
124{
125 struct wl_buffer * wlbuffer = NULL;
fei.deng640c3c92024-04-12 08:31:19 +0000126 WaylandDisplay::AmlConfigAPIList *amlConfigAPI = mDisplay->getAmlConfigAPIList();
fei.dengf7a0cd32023-08-29 09:36:37 +0000127
128 mRenderBuffer = buf;
129 if (mWaylandWlWrap) {
fei.dengb9a1a572023-09-13 01:33:57 +0000130 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000131 }
132
133 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
fei.dengb9a1a572023-09-13 01:33:57 +0000134 WaylandDmaBuffer *waylanddma = new WaylandDmaBuffer(mDisplay, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +0000135 wlbuffer = waylanddma->constructWlBuffer(&buf->dma, mBufferFormat);
136 if (!wlbuffer) {
137 delete waylanddma;
fei.dengb9a1a572023-09-13 01:33:57 +0000138 ERROR(mLogCategory,"create wl_buffer fail");
139 return ERROR_INVALID_OPERATION;
fei.dengf7a0cd32023-08-29 09:36:37 +0000140 }
141 mWaylandWlWrap = waylanddma;
142 mFrameWidth = buf->dma.width;
143 mFrameHeight = buf->dma.height;
144 }
145
146 /*register buffer release listen*/
fei.deng640c3c92024-04-12 08:31:19 +0000147 if (amlConfigAPI->enableDropFrame) {
148 wl_buffer_add_listener (wlbuffer, &buffer_with_drop_listener, this);
149 } else {
150 wl_buffer_add_listener (wlbuffer, &buffer_listener, this);
151 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000152
fei.dengb9a1a572023-09-13 01:33:57 +0000153 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000154}
155
156struct wl_buffer *WaylandBuffer::getWlBuffer()
157{
158 if (mWaylandWlWrap) {
159 return mWaylandWlWrap->getWlBuffer();
160 } else {
161 return NULL;
162 }
163}
164
165void WaylandBuffer::attach(struct wl_surface *surface)
166{
167 struct wl_callback *callback;
168 struct wl_buffer *wlbuffer = NULL;
169 if (mUsedByCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000170 DEBUG(mLogCategory,"buffer used by compositor");
fei.dengf7a0cd32023-08-29 09:36:37 +0000171 return;
172 }
173
174 //callback when this frame displayed
175 callback = wl_surface_frame (surface);
176 wl_callback_add_listener (callback, &frame_callback_listener, this);
177 mDisplay->setRedrawingPending(true);
178
179 wlbuffer = getWlBuffer();
180 if (wlbuffer) {
181 wl_surface_attach (surface, wlbuffer, 0, 0);
182 }
183
184 mUsedByCompositor = true;
185 mRedrawingPending = true;
186}