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
|
/* -*- 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/. */
#include "DMABUFTextureHostOGL.h"
#include "mozilla/widget/DMABufSurface.h"
#include "mozilla/webrender/RenderDMABUFTextureHost.h"
#include "mozilla/webrender/RenderThread.h"
#include "mozilla/webrender/WebRenderAPI.h"
#include "GLContextEGL.h"
namespace mozilla::layers {
DMABUFTextureHostOGL::DMABUFTextureHostOGL(TextureFlags aFlags,
const SurfaceDescriptor& aDesc)
: TextureHost(TextureHostType::DMABUF, aFlags) {
MOZ_COUNT_CTOR(DMABUFTextureHostOGL);
// DMABufSurface::CreateDMABufSurface() can fail, for instance when we're run
// out of file descriptors.
mSurface =
DMABufSurface::CreateDMABufSurface(aDesc.get_SurfaceDescriptorDMABuf());
}
DMABUFTextureHostOGL::~DMABUFTextureHostOGL() {
MOZ_COUNT_DTOR(DMABUFTextureHostOGL);
}
gfx::SurfaceFormat DMABUFTextureHostOGL::GetFormat() const {
if (!mSurface) {
return gfx::SurfaceFormat::UNKNOWN;
}
return mSurface->GetFormat();
}
gfx::YUVColorSpace DMABUFTextureHostOGL::GetYUVColorSpace() const {
if (!mSurface) {
return gfx::YUVColorSpace::Identity;
}
return mSurface->GetYUVColorSpace();
}
gfx::ColorRange DMABUFTextureHostOGL::GetColorRange() const {
if (!mSurface) {
return gfx::ColorRange::LIMITED;
}
return mSurface->IsFullRange() ? gfx::ColorRange::FULL
: gfx::ColorRange::LIMITED;
}
uint32_t DMABUFTextureHostOGL::NumSubTextures() {
return mSurface ? mSurface->GetTextureCount() : 0;
}
gfx::IntSize DMABUFTextureHostOGL::GetSize() const {
if (!mSurface) {
return gfx::IntSize();
}
return gfx::IntSize(mSurface->GetWidth(), mSurface->GetHeight());
}
gl::GLContext* DMABUFTextureHostOGL::gl() const { return nullptr; }
void DMABUFTextureHostOGL::CreateRenderTexture(
const wr::ExternalImageId& aExternalImageId) {
if (!mSurface) {
return;
}
RefPtr<wr::RenderTextureHost> texture =
new wr::RenderDMABUFTextureHost(mSurface);
wr::RenderThread::Get()->RegisterExternalImage(aExternalImageId,
texture.forget());
}
void DMABUFTextureHostOGL::PushResourceUpdates(
wr::TransactionBuilder& aResources, ResourceUpdateOp aOp,
const Range<wr::ImageKey>& aImageKeys, const wr::ExternalImageId& aExtID) {
if (!mSurface) {
return;
}
auto method = aOp == TextureHost::ADD_IMAGE
? &wr::TransactionBuilder::AddExternalImage
: &wr::TransactionBuilder::UpdateExternalImage;
auto imageType =
wr::ExternalImageType::TextureHandle(wr::ImageBufferKind::Texture2D);
switch (mSurface->GetFormat()) {
case gfx::SurfaceFormat::R8G8B8X8:
case gfx::SurfaceFormat::R8G8B8A8:
case gfx::SurfaceFormat::B8G8R8X8:
case gfx::SurfaceFormat::B8G8R8A8: {
MOZ_ASSERT(aImageKeys.length() == 1);
// XXX Add RGBA handling. Temporary hack to avoid crash
// With BGRA format setting, rendering works without problem.
wr::ImageDescriptor descriptor(GetSize(), mSurface->GetFormat());
(aResources.*method)(aImageKeys[0], descriptor, aExtID, imageType, 0);
break;
}
case gfx::SurfaceFormat::NV12: {
MOZ_ASSERT(aImageKeys.length() == 2);
MOZ_ASSERT(mSurface->GetTextureCount() == 2);
wr::ImageDescriptor descriptor0(
gfx::IntSize(mSurface->GetWidth(0), mSurface->GetHeight(0)),
gfx::SurfaceFormat::A8);
wr::ImageDescriptor descriptor1(
gfx::IntSize(mSurface->GetWidth(1), mSurface->GetHeight(1)),
gfx::SurfaceFormat::R8G8);
(aResources.*method)(aImageKeys[0], descriptor0, aExtID, imageType, 0);
(aResources.*method)(aImageKeys[1], descriptor1, aExtID, imageType, 1);
break;
}
case gfx::SurfaceFormat::YUV: {
MOZ_ASSERT(aImageKeys.length() == 3);
MOZ_ASSERT(mSurface->GetTextureCount() == 3);
wr::ImageDescriptor descriptor0(
gfx::IntSize(mSurface->GetWidth(0), mSurface->GetHeight(0)),
gfx::SurfaceFormat::A8);
wr::ImageDescriptor descriptor1(
gfx::IntSize(mSurface->GetWidth(1), mSurface->GetHeight(1)),
gfx::SurfaceFormat::A8);
(aResources.*method)(aImageKeys[0], descriptor0, aExtID, imageType, 0);
(aResources.*method)(aImageKeys[1], descriptor1, aExtID, imageType, 1);
(aResources.*method)(aImageKeys[2], descriptor1, aExtID, imageType, 2);
break;
}
default: {
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
}
}
}
void DMABUFTextureHostOGL::PushDisplayItems(
wr::DisplayListBuilder& aBuilder, const wr::LayoutRect& aBounds,
const wr::LayoutRect& aClip, wr::ImageRendering aFilter,
const Range<wr::ImageKey>& aImageKeys, PushDisplayItemFlagSet aFlags) {
if (!mSurface) {
return;
}
bool preferCompositorSurface =
aFlags.contains(PushDisplayItemFlag::PREFER_COMPOSITOR_SURFACE);
switch (mSurface->GetFormat()) {
case gfx::SurfaceFormat::R8G8B8X8:
case gfx::SurfaceFormat::R8G8B8A8:
case gfx::SurfaceFormat::B8G8R8A8:
case gfx::SurfaceFormat::B8G8R8X8: {
MOZ_ASSERT(aImageKeys.length() == 1);
aBuilder.PushImage(aBounds, aClip, true, false, aFilter, aImageKeys[0],
!(mFlags & TextureFlags::NON_PREMULTIPLIED),
wr::ColorF{1.0f, 1.0f, 1.0f, 1.0f},
preferCompositorSurface);
break;
}
case gfx::SurfaceFormat::NV12: {
MOZ_ASSERT(aImageKeys.length() == 2);
MOZ_ASSERT(mSurface->GetTextureCount() == 2);
// Those images can only be generated at present by the VAAPI H264 decoder
// which only supports 8 bits color depth.
aBuilder.PushNV12Image(aBounds, aClip, true, aImageKeys[0], aImageKeys[1],
wr::ColorDepth::Color8,
wr::ToWrYuvColorSpace(GetYUVColorSpace()),
wr::ToWrColorRange(GetColorRange()), aFilter,
preferCompositorSurface);
break;
}
case gfx::SurfaceFormat::YUV: {
MOZ_ASSERT(aImageKeys.length() == 3);
MOZ_ASSERT(mSurface->GetTextureCount() == 3);
// Those images can only be generated at present by the VAAPI vp8 decoder
// which only supports 8 bits color depth.
aBuilder.PushYCbCrPlanarImage(
aBounds, aClip, true, aImageKeys[0], aImageKeys[1], aImageKeys[2],
wr::ColorDepth::Color8, wr::ToWrYuvColorSpace(GetYUVColorSpace()),
wr::ToWrColorRange(GetColorRange()), aFilter,
preferCompositorSurface);
break;
}
default: {
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
}
}
}
} // namespace mozilla::layers
|