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
|
/* -*- 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 "RenderBufferTextureHost.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/layers/ImageDataSerializer.h"
namespace mozilla {
namespace wr {
RenderBufferTextureHost::RenderBufferTextureHost(
uint8_t* aBuffer, const layers::BufferDescriptor& aDescriptor)
: mBuffer(aBuffer),
mDescriptor(aDescriptor),
mMap(),
mYMap(),
mCbMap(),
mCrMap(),
mLocked(false) {
MOZ_COUNT_CTOR_INHERITED(RenderBufferTextureHost, RenderTextureHost);
switch (mDescriptor.type()) {
case layers::BufferDescriptor::TYCbCrDescriptor: {
const layers::YCbCrDescriptor& ycbcr = mDescriptor.get_YCbCrDescriptor();
mSize = ycbcr.display().Size();
mFormat = gfx::SurfaceFormat::YUV;
break;
}
case layers::BufferDescriptor::TRGBDescriptor: {
const layers::RGBDescriptor& rgb = mDescriptor.get_RGBDescriptor();
mSize = rgb.size();
mFormat = rgb.format();
break;
}
default:
gfxCriticalError() << "Bad buffer host descriptor "
<< (int)mDescriptor.type();
MOZ_CRASH("GFX: Bad descriptor");
}
}
RenderBufferTextureHost::~RenderBufferTextureHost() {
MOZ_COUNT_DTOR_INHERITED(RenderBufferTextureHost, RenderTextureHost);
}
wr::WrExternalImage RenderBufferTextureHost::Lock(uint8_t aChannelIndex,
gl::GLContext* aGL) {
if (!mLocked) {
if (!GetBuffer()) {
if (!mDestroyed) {
// We hit some problems to get the shmem.
gfxCriticalNote << "GetBuffer Failed";
}
return InvalidToWrExternalImage();
}
if (mFormat != gfx::SurfaceFormat::YUV) {
mSurface = gfx::Factory::CreateWrappingDataSourceSurface(
GetBuffer(),
layers::ImageDataSerializer::GetRGBStride(
mDescriptor.get_RGBDescriptor()),
mSize, mFormat);
if (NS_WARN_IF(!mSurface)) {
gfxCriticalNote << "DataSourceSurface is null";
return InvalidToWrExternalImage();
}
if (NS_WARN_IF(
!mSurface->Map(gfx::DataSourceSurface::MapType::READ, &mMap))) {
mSurface = nullptr;
gfxCriticalNote << "Failed to map Surface";
return InvalidToWrExternalImage();
}
} else {
const layers::YCbCrDescriptor& desc = mDescriptor.get_YCbCrDescriptor();
auto cbcrSize = layers::ImageDataSerializer::GetCroppedCbCrSize(desc);
mYSurface = gfx::Factory::CreateWrappingDataSourceSurface(
layers::ImageDataSerializer::GetYChannel(GetBuffer(), desc),
desc.yStride(), desc.display().Size(), gfx::SurfaceFormat::A8);
mCbSurface = gfx::Factory::CreateWrappingDataSourceSurface(
layers::ImageDataSerializer::GetCbChannel(GetBuffer(), desc),
desc.cbCrStride(), cbcrSize, gfx::SurfaceFormat::A8);
mCrSurface = gfx::Factory::CreateWrappingDataSourceSurface(
layers::ImageDataSerializer::GetCrChannel(GetBuffer(), desc),
desc.cbCrStride(), cbcrSize, gfx::SurfaceFormat::A8);
if (NS_WARN_IF(!mYSurface || !mCbSurface || !mCrSurface)) {
mYSurface = mCbSurface = mCrSurface = nullptr;
gfxCriticalNote << "YCbCr Surface is null";
return InvalidToWrExternalImage();
}
if (NS_WARN_IF(
!mYSurface->Map(gfx::DataSourceSurface::MapType::READ, &mYMap) ||
!mCbSurface->Map(gfx::DataSourceSurface::MapType::READ,
&mCbMap) ||
!mCrSurface->Map(gfx::DataSourceSurface::MapType::READ,
&mCrMap))) {
mYSurface = mCbSurface = mCrSurface = nullptr;
gfxCriticalNote << "Failed to map YCbCr Surface";
return InvalidToWrExternalImage();
}
}
mLocked = true;
}
RenderBufferData data = GetBufferDataForRender(aChannelIndex);
return RawDataToWrExternalImage(data.mData, data.mBufferSize);
}
void RenderBufferTextureHost::Unlock() {
if (mLocked) {
if (mSurface) {
mSurface->Unmap();
mSurface = nullptr;
} else if (mYSurface) {
mYSurface->Unmap();
mCbSurface->Unmap();
mCrSurface->Unmap();
mYSurface = mCbSurface = mCrSurface = nullptr;
}
mLocked = false;
}
}
RenderBufferTextureHost::RenderBufferData
RenderBufferTextureHost::GetBufferDataForRender(uint8_t aChannelIndex) {
MOZ_ASSERT(mFormat != gfx::SurfaceFormat::YUV || aChannelIndex < 3);
MOZ_ASSERT(mFormat == gfx::SurfaceFormat::YUV || aChannelIndex < 1);
MOZ_ASSERT(mLocked);
if (mFormat != gfx::SurfaceFormat::YUV) {
MOZ_ASSERT(mSurface);
return RenderBufferData(mMap.mData,
mMap.mStride * mSurface->GetSize().height);
} else {
MOZ_ASSERT(mYSurface && mCbSurface && mCrSurface);
switch (aChannelIndex) {
case 0:
return RenderBufferData(mYMap.mData,
mYMap.mStride * mYSurface->GetSize().height);
break;
case 1:
return RenderBufferData(mCbMap.mData,
mCbMap.mStride * mCbSurface->GetSize().height);
break;
case 2:
return RenderBufferData(mCrMap.mData,
mCrMap.mStride * mCrSurface->GetSize().height);
break;
default:
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
return RenderBufferData(nullptr, 0);
}
}
}
size_t RenderBufferTextureHost::GetPlaneCount() const {
switch (mDescriptor.type()) {
case layers::BufferDescriptor::TYCbCrDescriptor:
return 3;
default:
return 1;
}
}
gfx::SurfaceFormat RenderBufferTextureHost::GetFormat() const {
switch (mDescriptor.type()) {
case layers::BufferDescriptor::TYCbCrDescriptor:
return gfx::SurfaceFormat::YUV;
default:
return mDescriptor.get_RGBDescriptor().format();
}
}
gfx::ColorDepth RenderBufferTextureHost::GetColorDepth() const {
switch (mDescriptor.type()) {
case layers::BufferDescriptor::TYCbCrDescriptor:
return mDescriptor.get_YCbCrDescriptor().colorDepth();
default:
return gfx::ColorDepth::COLOR_8;
}
}
gfx::YUVRangedColorSpace RenderBufferTextureHost::GetYUVColorSpace() const {
switch (mDescriptor.type()) {
case layers::BufferDescriptor::TYCbCrDescriptor:
return gfx::GetYUVRangedColorSpace(mDescriptor.get_YCbCrDescriptor());
default:
return gfx::YUVRangedColorSpace::Default;
}
}
bool RenderBufferTextureHost::MapPlane(RenderCompositor* aCompositor,
uint8_t aChannelIndex,
PlaneInfo& aPlaneInfo) {
if (!mBuffer) {
if (!mDestroyed) {
// We hit some problems to get the shmem.
gfxCriticalNote << "GetBuffer Failed";
}
return false;
}
switch (mDescriptor.type()) {
case layers::BufferDescriptor::TYCbCrDescriptor: {
const layers::YCbCrDescriptor& desc = mDescriptor.get_YCbCrDescriptor();
switch (aChannelIndex) {
case 0:
aPlaneInfo.mData =
layers::ImageDataSerializer::GetYChannel(mBuffer, desc);
aPlaneInfo.mStride = desc.yStride();
aPlaneInfo.mSize = desc.display().Size();
break;
case 1:
aPlaneInfo.mData =
layers::ImageDataSerializer::GetCbChannel(mBuffer, desc);
aPlaneInfo.mStride = desc.cbCrStride();
aPlaneInfo.mSize =
layers::ImageDataSerializer::GetCroppedCbCrSize(desc);
break;
case 2:
aPlaneInfo.mData =
layers::ImageDataSerializer::GetCrChannel(mBuffer, desc);
aPlaneInfo.mStride = desc.cbCrStride();
aPlaneInfo.mSize =
layers::ImageDataSerializer::GetCroppedCbCrSize(desc);
break;
}
break;
}
default: {
const layers::RGBDescriptor& desc = mDescriptor.get_RGBDescriptor();
aPlaneInfo.mData = mBuffer;
aPlaneInfo.mStride = layers::ImageDataSerializer::GetRGBStride(desc);
aPlaneInfo.mSize = desc.size();
break;
}
}
return true;
}
void RenderBufferTextureHost::UnmapPlanes() {}
void RenderBufferTextureHost::Destroy() {
mBuffer = nullptr;
mDestroyed = true;
}
} // namespace wr
} // namespace mozilla
|