1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef DMABufSurface_h__
#define DMABufSurface_h__
#include <stdint.h>
#include "mozilla/widget/nsWaylandDisplay.h"
#include "mozilla/widget/va_drmcommon.h"
#include "GLTypes.h"
typedef void* EGLImageKHR;
typedef void* EGLSyncKHR;
#define DMABUF_BUFFER_PLANES 4
namespace mozilla {
namespace layers {
class SurfaceDescriptor;
class SurfaceDescriptorDMABuf;
} // namespace layers
namespace gl {
class GLContext;
}
} // namespace mozilla
typedef enum {
// Use alpha pixel format
DMABUF_ALPHA = 1 << 0,
// Surface is used as texture and may be also shared
DMABUF_TEXTURE = 1 << 1,
// Use modifiers. Such dmabuf surface may have more planes
// and complex internal structure (tiling/compression/etc.)
// so we can't do direct rendering to it.
DMABUF_USE_MODIFIERS = 1 << 3,
} DMABufSurfaceFlags;
class DMABufSurfaceRGBA;
class DMABufSurfaceYUV;
class DMABufSurface {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DMABufSurface)
enum SurfaceType {
SURFACE_RGBA,
SURFACE_NV12,
SURFACE_YUV420,
};
// Import surface from SurfaceDescriptor. This is usually
// used to copy surface from another process over IPC.
// When a global reference counter was created for the surface
// (see bellow) it's automatically referenced.
static already_AddRefed<DMABufSurface> CreateDMABufSurface(
const mozilla::layers::SurfaceDescriptor& aDesc);
// Export surface to another process via. SurfaceDescriptor.
virtual bool Serialize(
mozilla::layers::SurfaceDescriptor& aOutDescriptor) = 0;
virtual int GetWidth(int aPlane = 0) = 0;
virtual int GetHeight(int aPlane = 0) = 0;
virtual mozilla::gfx::SurfaceFormat GetFormat() = 0;
virtual mozilla::gfx::SurfaceFormat GetFormatGL() = 0;
virtual bool CreateTexture(mozilla::gl::GLContext* aGLContext,
int aPlane = 0) = 0;
virtual void ReleaseTextures() = 0;
virtual GLuint GetTexture(int aPlane = 0) = 0;
virtual EGLImageKHR GetEGLImage(int aPlane = 0) = 0;
SurfaceType GetSurfaceType() { return mSurfaceType; };
virtual uint32_t GetTextureCount() = 0;
bool IsMapped(int aPlane = 0) { return (mMappedRegion[aPlane] != nullptr); };
void Unmap(int aPlane = 0);
virtual DMABufSurfaceRGBA* GetAsDMABufSurfaceRGBA() { return nullptr; }
virtual DMABufSurfaceYUV* GetAsDMABufSurfaceYUV() { return nullptr; }
virtual mozilla::gfx::YUVColorSpace GetYUVColorSpace() {
return mozilla::gfx::YUVColorSpace::UNKNOWN;
};
virtual bool IsFullRange() { return false; };
void FenceSet();
void FenceWait();
void FenceDelete();
// Set and get a global surface UID. The UID is shared across process
// and it's used to track surface lifetime in various parts of rendering
// engine.
uint32_t GetUID() const { return mUID; };
// Creates a global reference counter objects attached to the surface.
// It's created as unreferenced, i.e. IsGlobalRefSet() returns false
// right after GlobalRefCountCreate() call.
//
// The counter is shared by all surface instances across processes
// so it tracks global surface usage.
//
// The counter is automatically referenced when a new surface instance is
// created with SurfaceDescriptor (usually copied to another process over IPC)
// and it's unreferenced when surface is deleted.
//
// So without any additional GlobalRefAdd()/GlobalRefRelease() calls
// the IsGlobalRefSet() returns true if any other process use the surface.
void GlobalRefCountCreate();
// If global reference counter was created by GlobalRefCountCreate()
// returns true when there's an active surface reference.
bool IsGlobalRefSet() const;
// Add/Remove additional reference to the surface global reference counter.
void GlobalRefAdd();
void GlobalRefRelease();
// Release all underlying data.
virtual void ReleaseSurface() = 0;
#ifdef DEBUG
virtual void DumpToFile(const char* pFile){};
#endif
DMABufSurface(SurfaceType aSurfaceType);
protected:
virtual bool Create(const mozilla::layers::SurfaceDescriptor& aDesc) = 0;
bool FenceImportFromFd();
void GlobalRefCountImport(int aFd);
void GlobalRefCountDelete();
void ReleaseDMABuf();
void* MapInternal(uint32_t aX, uint32_t aY, uint32_t aWidth, uint32_t aHeight,
uint32_t* aStride, int aGbmFlags, int aPlane = 0);
virtual ~DMABufSurface();
SurfaceType mSurfaceType;
uint64_t mBufferModifier;
int mBufferPlaneCount;
int mDmabufFds[DMABUF_BUFFER_PLANES];
uint32_t mDrmFormats[DMABUF_BUFFER_PLANES];
uint32_t mStrides[DMABUF_BUFFER_PLANES];
uint32_t mOffsets[DMABUF_BUFFER_PLANES];
struct gbm_bo* mGbmBufferObject[DMABUF_BUFFER_PLANES];
void* mMappedRegion[DMABUF_BUFFER_PLANES];
void* mMappedRegionData[DMABUF_BUFFER_PLANES];
uint32_t mMappedRegionStride[DMABUF_BUFFER_PLANES];
int mSyncFd;
EGLSyncKHR mSync;
RefPtr<mozilla::gl::GLContext> mGL;
int mGlobalRefCountFd;
uint32_t mUID;
};
class DMABufSurfaceRGBA : public DMABufSurface {
public:
static already_AddRefed<DMABufSurfaceRGBA> CreateDMABufSurface(
int aWidth, int aHeight, int aDMABufSurfaceFlags);
bool Serialize(mozilla::layers::SurfaceDescriptor& aOutDescriptor);
DMABufSurfaceRGBA* GetAsDMABufSurfaceRGBA() { return this; }
void Clear();
void ReleaseSurface();
bool CopyFrom(class DMABufSurface* aSourceSurface);
int GetWidth(int aPlane = 0) { return mWidth; };
int GetHeight(int aPlane = 0) { return mHeight; };
mozilla::gfx::SurfaceFormat GetFormat();
mozilla::gfx::SurfaceFormat GetFormatGL();
bool HasAlpha();
void* MapReadOnly(uint32_t aX, uint32_t aY, uint32_t aWidth, uint32_t aHeight,
uint32_t* aStride = nullptr);
void* MapReadOnly(uint32_t* aStride = nullptr);
void* Map(uint32_t aX, uint32_t aY, uint32_t aWidth, uint32_t aHeight,
uint32_t* aStride = nullptr);
void* Map(uint32_t* aStride = nullptr);
void* GetMappedRegion(int aPlane = 0) { return mMappedRegion[aPlane]; };
uint32_t GetMappedRegionStride(int aPlane = 0) {
return mMappedRegionStride[aPlane];
};
bool CreateTexture(mozilla::gl::GLContext* aGLContext, int aPlane = 0);
void ReleaseTextures();
GLuint GetTexture(int aPlane = 0) { return mTexture; };
EGLImageKHR GetEGLImage(int aPlane = 0) { return mEGLImage; };
uint32_t GetTextureCount() { return 1; };
#ifdef DEBUG
virtual void DumpToFile(const char* pFile);
#endif
DMABufSurfaceRGBA();
private:
~DMABufSurfaceRGBA();
bool Create(int aWidth, int aHeight, int aDMABufSurfaceFlags);
bool Create(const mozilla::layers::SurfaceDescriptor& aDesc);
void ImportSurfaceDescriptor(const mozilla::layers::SurfaceDescriptor& aDesc);
private:
int mSurfaceFlags;
int mWidth;
int mHeight;
mozilla::widget::GbmFormat* mGmbFormat;
EGLImageKHR mEGLImage;
GLuint mTexture;
uint32_t mGbmBufferFlags;
};
class DMABufSurfaceYUV : public DMABufSurface {
public:
static already_AddRefed<DMABufSurfaceYUV> CreateYUVSurface(
int aWidth, int aHeight, void** aPixelData = nullptr,
int* aLineSizes = nullptr);
static already_AddRefed<DMABufSurfaceYUV> CreateYUVSurface(
const VADRMPRIMESurfaceDescriptor& aDesc);
bool Serialize(mozilla::layers::SurfaceDescriptor& aOutDescriptor);
DMABufSurfaceYUV* GetAsDMABufSurfaceYUV() { return this; };
int GetWidth(int aPlane = 0) { return mWidth[aPlane]; }
int GetHeight(int aPlane = 0) { return mHeight[aPlane]; }
mozilla::gfx::SurfaceFormat GetFormat();
mozilla::gfx::SurfaceFormat GetFormatGL();
bool CreateTexture(mozilla::gl::GLContext* aGLContext, int aPlane = 0);
void ReleaseTextures();
void ReleaseSurface();
GLuint GetTexture(int aPlane = 0) { return mTexture[aPlane]; };
EGLImageKHR GetEGLImage(int aPlane = 0) { return mEGLImage[aPlane]; };
uint32_t GetTextureCount();
void SetYUVColorSpace(mozilla::gfx::YUVColorSpace aColorSpace) {
mColorSpace = aColorSpace;
}
mozilla::gfx::YUVColorSpace GetYUVColorSpace() { return mColorSpace; }
bool IsFullRange() { return true; }
DMABufSurfaceYUV();
bool UpdateYUVData(void** aPixelData, int* aLineSizes);
bool UpdateYUVData(const VADRMPRIMESurfaceDescriptor& aDesc);
private:
~DMABufSurfaceYUV();
bool Create(const mozilla::layers::SurfaceDescriptor& aDesc);
bool Create(int aWidth, int aHeight, void** aPixelData, int* aLineSizes);
bool CreateYUVPlane(int aPlane, int aWidth, int aHeight, int aDrmFormat);
void UpdateYUVPlane(int aPlane, void* aPixelData, int aLineSize);
void ImportSurfaceDescriptor(
const mozilla::layers::SurfaceDescriptorDMABuf& aDesc);
int mWidth[DMABUF_BUFFER_PLANES];
int mHeight[DMABUF_BUFFER_PLANES];
EGLImageKHR mEGLImage[DMABUF_BUFFER_PLANES];
GLuint mTexture[DMABUF_BUFFER_PLANES];
mozilla::gfx::YUVColorSpace mColorSpace;
};
#endif
|