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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/* -*- 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 "GLContext.h" // for GLContext, etc
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
#include "mozilla/layers/ISurfaceAllocator.h"
#include "mozilla/layers/TextureClientOGL.h"
#include "mozilla/gfx/2D.h" // for Factory
#include "mozilla/gfx/Point.h" // for IntSize
#include "GLLibraryEGL.h"
#ifdef MOZ_WIDGET_ANDROID
# include <jni.h>
# include <android/native_window.h>
# include <android/native_window_jni.h>
# include <sys/socket.h>
# include "mozilla/ipc/FileDescriptor.h"
# include "mozilla/java/GeckoSurfaceWrappers.h"
# include "mozilla/java/SurfaceAllocatorWrappers.h"
# include "mozilla/layers/AndroidHardwareBuffer.h"
# include "mozilla/UniquePtrExtensions.h"
#endif
using namespace mozilla::gl;
namespace mozilla {
namespace layers {
class CompositableForwarder;
////////////////////////////////////////////////////////////////////////
// AndroidSurface
#ifdef MOZ_WIDGET_ANDROID
already_AddRefed<TextureClient> AndroidSurfaceTextureData::CreateTextureClient(
AndroidSurfaceTextureHandle aHandle, gfx::IntSize aSize, bool aContinuous,
gl::OriginPos aOriginPos, bool aHasAlpha, bool aForceBT709ColorSpace,
Maybe<gfx::Matrix4x4> aTransformOverride, LayersIPCChannel* aAllocator,
TextureFlags aFlags) {
if (aOriginPos == gl::OriginPos::BottomLeft) {
aFlags |= TextureFlags::ORIGIN_BOTTOM_LEFT;
}
return TextureClient::CreateWithData(
new AndroidSurfaceTextureData(aHandle, aSize, aContinuous, aHasAlpha,
aForceBT709ColorSpace, aTransformOverride),
aFlags, aAllocator);
}
AndroidSurfaceTextureData::AndroidSurfaceTextureData(
AndroidSurfaceTextureHandle aHandle, gfx::IntSize aSize, bool aContinuous,
bool aHasAlpha, bool aForceBT709ColorSpace,
Maybe<gfx::Matrix4x4> aTransformOverride)
: mHandle(aHandle),
mSize(aSize),
mContinuous(aContinuous),
mHasAlpha(aHasAlpha),
mForceBT709ColorSpace(aForceBT709ColorSpace),
mTransformOverride(aTransformOverride) {
MOZ_ASSERT(mHandle);
}
AndroidSurfaceTextureData::~AndroidSurfaceTextureData() {}
void AndroidSurfaceTextureData::FillInfo(TextureData::Info& aInfo) const {
aInfo.size = mSize;
aInfo.format = gfx::SurfaceFormat::UNKNOWN;
aInfo.hasSynchronization = false;
aInfo.supportsMoz2D = false;
aInfo.canExposeMappedData = false;
}
bool AndroidSurfaceTextureData::Serialize(SurfaceDescriptor& aOutDescriptor) {
aOutDescriptor = SurfaceTextureDescriptor(
mHandle, mSize,
mHasAlpha ? gfx::SurfaceFormat::R8G8B8A8 : gfx::SurfaceFormat::R8G8B8X8,
mContinuous, mForceBT709ColorSpace, mTransformOverride);
return true;
}
#endif // MOZ_WIDGET_ANDROID
////////////////////////////////////////////////////////////////////////
// AndroidNativeWindow
#ifdef MOZ_WIDGET_ANDROID
AndroidNativeWindowTextureData* AndroidNativeWindowTextureData::Create(
gfx::IntSize aSize, gfx::SurfaceFormat aFormat) {
if (aFormat != gfx::SurfaceFormat::R8G8B8A8 &&
aFormat != gfx::SurfaceFormat::R8G8B8X8 &&
aFormat != gfx::SurfaceFormat::B8G8R8A8 &&
aFormat != gfx::SurfaceFormat::B8G8R8X8 &&
aFormat != gfx::SurfaceFormat::R5G6B5_UINT16) {
return nullptr;
}
auto surface =
java::GeckoSurface::LocalRef(java::SurfaceAllocator::AcquireSurface(
aSize.width, aSize.height, true /* single-buffer mode */));
if (surface) {
return new AndroidNativeWindowTextureData(surface, aSize, aFormat);
}
return nullptr;
}
AndroidNativeWindowTextureData::AndroidNativeWindowTextureData(
java::GeckoSurface::Param aSurface, gfx::IntSize aSize,
gfx::SurfaceFormat aFormat)
: mSurface(aSurface), mIsLocked(false), mSize(aSize), mFormat(aFormat) {
mNativeWindow = ANativeWindow_fromSurface(jni::GetEnvForThread(),
mSurface->GetSurface().Get());
MOZ_ASSERT(mNativeWindow, "Failed to create NativeWindow.");
// SurfaceTextures don't technically support BGR, but we can just pretend to
// be RGB.
int32_t format = WINDOW_FORMAT_RGBA_8888;
switch (aFormat) {
case gfx::SurfaceFormat::R8G8B8A8:
case gfx::SurfaceFormat::B8G8R8A8:
format = WINDOW_FORMAT_RGBA_8888;
break;
case gfx::SurfaceFormat::R8G8B8X8:
case gfx::SurfaceFormat::B8G8R8X8:
format = WINDOW_FORMAT_RGBX_8888;
break;
case gfx::SurfaceFormat::R5G6B5_UINT16:
format = WINDOW_FORMAT_RGB_565;
break;
default:
MOZ_ASSERT(false, "Unsupported AndroidNativeWindowTextureData format.");
}
DebugOnly<int32_t> r = ANativeWindow_setBuffersGeometry(
mNativeWindow, mSize.width, mSize.height, format);
MOZ_ASSERT(r == 0, "ANativeWindow_setBuffersGeometry failed.");
// Ideally here we'd call ANativeWindow_setBuffersTransform() with the
// identity transform, but that is only available on api level >= 26.
// Instead use SurfaceDescriptor's transformOverride flag when serializing.
}
void AndroidNativeWindowTextureData::FillInfo(TextureData::Info& aInfo) const {
aInfo.size = mSize;
aInfo.format = mFormat;
aInfo.hasSynchronization = false;
aInfo.supportsMoz2D = true;
aInfo.canExposeMappedData = false;
aInfo.canConcurrentlyReadLock = false;
}
bool AndroidNativeWindowTextureData::Serialize(
SurfaceDescriptor& aOutDescriptor) {
aOutDescriptor = SurfaceTextureDescriptor(
mSurface->GetHandle(), mSize, mFormat, false /* not continuous */,
false /* do not override colorspace */,
Some(gfx::Matrix4x4()) /* always use identity transform */);
return true;
}
bool AndroidNativeWindowTextureData::Lock(OpenMode) {
// ANativeWindows can only be locked and unlocked a single time, after which
// we must wait until they receive ownership back from the host.
// Therefore we must only actually call ANativeWindow_lock() once per cycle.
if (!mIsLocked) {
int32_t r = ANativeWindow_lock(mNativeWindow, &mBuffer, nullptr);
if (r == -ENOMEM) {
return false;
} else if (r < 0) {
MOZ_CRASH("ANativeWindow_lock failed.");
}
mIsLocked = true;
}
return true;
}
void AndroidNativeWindowTextureData::Unlock() {
// The TextureClient may want to call Lock again before handing ownership
// to the host, so we cannot call ANativeWindow_unlockAndPost yet.
}
void AndroidNativeWindowTextureData::Forget(LayersIPCChannel*) {
MOZ_ASSERT(!mIsLocked,
"ANativeWindow should not be released while locked.\n");
ANativeWindow_release(mNativeWindow);
mNativeWindow = nullptr;
java::SurfaceAllocator::DisposeSurface(mSurface);
mSurface = nullptr;
}
already_AddRefed<gfx::DrawTarget>
AndroidNativeWindowTextureData::BorrowDrawTarget() {
const int bpp = (mFormat == gfx::SurfaceFormat::R5G6B5_UINT16) ? 2 : 4;
return gfx::Factory::CreateDrawTargetForData(
gfx::BackendType::SKIA, static_cast<unsigned char*>(mBuffer.bits),
gfx::IntSize(mBuffer.width, mBuffer.height), mBuffer.stride * bpp,
mFormat, true);
}
void AndroidNativeWindowTextureData::OnForwardedToHost() {
if (mIsLocked) {
int32_t r = ANativeWindow_unlockAndPost(mNativeWindow);
if (r < 0) {
MOZ_CRASH("ANativeWindow_unlockAndPost failed\n.");
}
mIsLocked = false;
}
}
AndroidHardwareBufferTextureData* AndroidHardwareBufferTextureData::Create(
gfx::IntSize aSize, gfx::SurfaceFormat aFormat) {
RefPtr<AndroidHardwareBuffer> buffer =
AndroidHardwareBuffer::Create(aSize, aFormat);
if (!buffer) {
return nullptr;
}
return new AndroidHardwareBufferTextureData(buffer, aSize, aFormat);
}
AndroidHardwareBufferTextureData::AndroidHardwareBufferTextureData(
AndroidHardwareBuffer* aAndroidHardwareBuffer, gfx::IntSize aSize,
gfx::SurfaceFormat aFormat)
: mAndroidHardwareBuffer(aAndroidHardwareBuffer),
mSize(aSize),
mFormat(aFormat),
mAddress(nullptr),
mIsLocked(false) {}
AndroidHardwareBufferTextureData::~AndroidHardwareBufferTextureData() {}
void AndroidHardwareBufferTextureData::FillInfo(
TextureData::Info& aInfo) const {
aInfo.size = mSize;
aInfo.format = mFormat;
aInfo.hasSynchronization = true;
aInfo.supportsMoz2D = true;
aInfo.canExposeMappedData = false;
aInfo.canConcurrentlyReadLock = true;
}
bool AndroidHardwareBufferTextureData::Serialize(
SurfaceDescriptor& aOutDescriptor) {
aOutDescriptor = SurfaceDescriptorAndroidHardwareBuffer(
mAndroidHardwareBuffer->mId, mSize, mFormat);
return true;
}
bool AndroidHardwareBufferTextureData::Lock(OpenMode aMode) {
if (!mIsLocked) {
MOZ_ASSERT(!mAddress);
uint64_t usage = 0;
if (aMode & OpenMode::OPEN_READ) {
usage |= AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN;
}
if (aMode & OpenMode::OPEN_WRITE) {
usage |= AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
}
int ret = mAndroidHardwareBuffer->Lock(usage, 0, &mAddress);
if (ret) {
mAddress = nullptr;
return false;
}
mIsLocked = true;
}
return true;
}
void AndroidHardwareBufferTextureData::Unlock() {
// The TextureClient may want to call Lock again before handing ownership
// to the host, so we cannot call AHardwareBuffer_unlock yet.
}
void AndroidHardwareBufferTextureData::Forget(LayersIPCChannel*) {
MOZ_ASSERT(!mIsLocked);
mAndroidHardwareBuffer = nullptr;
mAddress = nullptr;
}
already_AddRefed<gfx::DrawTarget>
AndroidHardwareBufferTextureData::BorrowDrawTarget() {
MOZ_ASSERT(mIsLocked);
const int bpp = (mFormat == gfx::SurfaceFormat::R5G6B5_UINT16) ? 2 : 4;
return gfx::Factory::CreateDrawTargetForData(
gfx::BackendType::SKIA, static_cast<unsigned char*>(mAddress),
gfx::IntSize(mAndroidHardwareBuffer->mSize.width,
mAndroidHardwareBuffer->mSize.height),
mAndroidHardwareBuffer->mStride * bpp, mFormat, true);
}
void AndroidHardwareBufferTextureData::OnForwardedToHost() {
if (mIsLocked) {
mAndroidHardwareBuffer->Unlock();
mAddress = nullptr;
mIsLocked = false;
}
}
TextureFlags AndroidHardwareBufferTextureData::GetTextureFlags() const {
return TextureFlags::WAIT_HOST_USAGE_END;
}
Maybe<uint64_t> AndroidHardwareBufferTextureData::GetBufferId() const {
return Some(mAndroidHardwareBuffer->mId);
}
mozilla::ipc::FileDescriptor
AndroidHardwareBufferTextureData::GetAcquireFence() {
if (!mAndroidHardwareBuffer) {
return ipc::FileDescriptor();
}
return mAndroidHardwareBuffer->GetAcquireFence();
}
#endif // MOZ_WIDGET_ANDROID
} // namespace layers
} // namespace mozilla
|