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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
/* -*- 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 MOZILLA_GFX_COMPOSITOROGL_H
#define MOZILLA_GFX_COMPOSITOROGL_H
#include <map>
#include <unordered_map>
#include <unordered_set>
#include "gfx2DGlue.h"
#include "GLContextTypes.h" // for GLContext, etc
#include "GLDefs.h" // for GLuint, LOCAL_GL_TEXTURE_2D, etc
#include "OGLShaderConfig.h" // for ShaderConfigOGL
#include "Units.h" // for ScreenPoint
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
#include "mozilla/Attributes.h" // for override, final
#include "mozilla/RefPtr.h" // for already_AddRefed, RefPtr
#include "mozilla/gfx/2D.h" // for DrawTarget
#include "mozilla/gfx/BaseSize.h" // for BaseSize
#include "mozilla/gfx/MatrixFwd.h" // for Matrix4x4
#include "mozilla/gfx/Point.h" // for IntSize, Point
#include "mozilla/gfx/Rect.h" // for Rect, IntRect
#include "mozilla/gfx/Triangle.h" // for Triangle
#include "mozilla/gfx/Types.h" // for Float, SurfaceFormat, etc
#include "mozilla/ipc/FileDescriptor.h"
#include "mozilla/layers/Compositor.h" // for SurfaceInitMode, Compositor, etc
#include "mozilla/layers/CompositorTypes.h" // for MaskType::MaskType::NumMaskTypes, etc
#include "mozilla/layers/LayersTypes.h"
#include "nsCOMPtr.h" // for already_AddRefed
#include "nsDebug.h" // for NS_ASSERTION, NS_WARNING
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
#include "nsTArray.h" // for AutoTArray, nsTArray, etc
#include "nsThreadUtils.h" // for nsRunnable
#include "nsXULAppAPI.h" // for XRE_GetProcessType
#include "nscore.h" // for NS_IMETHOD
class nsIWidget;
namespace mozilla {
namespace layers {
class CompositingRenderTarget;
class CompositingRenderTargetOGL;
class DataTextureSource;
class ShaderProgramOGL;
class TextureSource;
class TextureSourceOGL;
class BufferTextureHost;
struct Effect;
struct EffectChain;
class GLBlitTextureImageHelper;
/**
* Interface for pools of temporary gl textures for the compositor.
* The textures are fully owned by the pool, so the latter is responsible
* calling fDeleteTextures accordingly.
* Users of GetTexture receive a texture that is only valid for the duration
* of the current frame.
* This is primarily intended for direct texturing APIs that need to attach
* shared objects (such as an EGLImage) to a gl texture.
*/
class CompositorTexturePoolOGL {
protected:
virtual ~CompositorTexturePoolOGL() = default;
public:
NS_INLINE_DECL_REFCOUNTING(CompositorTexturePoolOGL)
virtual void Clear() = 0;
virtual GLuint GetTexture(GLenum aTarget, GLenum aEnum) = 0;
virtual void EndFrame() = 0;
};
/**
* Agressively reuses textures. One gl texture per texture unit in total.
* So far this hasn't shown the best results on b2g.
*/
class PerUnitTexturePoolOGL : public CompositorTexturePoolOGL {
public:
explicit PerUnitTexturePoolOGL(gl::GLContext* aGL);
virtual ~PerUnitTexturePoolOGL();
void Clear() override { DestroyTextures(); }
GLuint GetTexture(GLenum aTarget, GLenum aUnit) override;
void EndFrame() override {}
protected:
void DestroyTextures();
GLenum mTextureTarget;
nsTArray<GLuint> mTextures;
RefPtr<gl::GLContext> mGL;
};
// If you want to make this class not final, first remove calls to virtual
// methods (Destroy) that are made in the destructor.
class CompositorOGL final : public Compositor {
typedef mozilla::gl::GLContext GLContext;
friend class CompositingRenderTargetOGL;
std::map<ShaderConfigOGL, ShaderProgramOGL*> mPrograms;
public:
CompositorOGL(CompositorBridgeParent* aParent,
widget::CompositorWidget* aWidget, int aSurfaceWidth = -1,
int aSurfaceHeight = -1, bool aUseExternalSurfaceSize = false);
protected:
virtual ~CompositorOGL();
public:
CompositorOGL* AsCompositorOGL() override { return this; }
already_AddRefed<DataTextureSource> CreateDataTextureSource(
TextureFlags aFlags = TextureFlags::NO_FLAGS) override;
already_AddRefed<DataTextureSource> CreateDataTextureSourceAroundYCbCr(
TextureHost* aTexture) override;
already_AddRefed<DataTextureSource> CreateDataTextureSourceAround(
gfx::DataSourceSurface* aSurface) override;
bool Initialize(nsCString* const out_failureReason) override;
void Destroy() override;
TextureFactoryIdentifier GetTextureFactoryIdentifier() override {
TextureFactoryIdentifier result = TextureFactoryIdentifier(
LayersBackend::LAYERS_OPENGL, XRE_GetProcessType(), GetMaxTextureSize(),
SupportsTextureDirectMapping(), false,
mFBOTextureTarget == LOCAL_GL_TEXTURE_2D,
SupportsPartialTextureUpdate());
return result;
}
// Returns a render target for the native layer.
// aInvalidRegion is in window coordinates, i.e. in the same space as
// aNativeLayer->GetPosition().
already_AddRefed<CompositingRenderTargetOGL> RenderTargetForNativeLayer(
NativeLayer* aNativeLayer, const gfx::IntRegion& aInvalidRegion);
already_AddRefed<CompositingRenderTarget> CreateRenderTarget(
const gfx::IntRect& aRect, SurfaceInitMode aInit) override;
already_AddRefed<CompositingRenderTarget> CreateRenderTargetFromSource(
const gfx::IntRect& aRect, const CompositingRenderTarget* aSource,
const gfx::IntPoint& aSourcePoint) override;
void SetRenderTarget(CompositingRenderTarget* aSurface) override;
already_AddRefed<CompositingRenderTarget> GetCurrentRenderTarget()
const override;
already_AddRefed<CompositingRenderTarget> GetWindowRenderTarget()
const override;
bool ReadbackRenderTarget(CompositingRenderTarget* aSource,
AsyncReadbackBuffer* aDest) override;
already_AddRefed<AsyncReadbackBuffer> CreateAsyncReadbackBuffer(
const gfx::IntSize& aSize) override;
bool BlitRenderTarget(CompositingRenderTarget* aSource,
const gfx::IntSize& aSourceSize,
const gfx::IntSize& aDestSize) override;
void DrawQuad(const gfx::Rect& aRect, const gfx::IntRect& aClipRect,
const EffectChain& aEffectChain, gfx::Float aOpacity,
const gfx::Matrix4x4& aTransform,
const gfx::Rect& aVisibleRect) override;
void DrawTriangles(const nsTArray<gfx::TexturedTriangle>& aTriangles,
const gfx::Rect& aRect, const gfx::IntRect& aClipRect,
const EffectChain& aEffectChain, gfx::Float aOpacity,
const gfx::Matrix4x4& aTransform,
const gfx::Rect& aVisibleRect) override;
bool SupportsLayerGeometry() const override;
void NormalDrawingDone() override;
void EndFrame() override;
void WaitForGPU() override;
RefPtr<SurfacePoolHandle> GetSurfacePoolHandle() override;
bool SupportsPartialTextureUpdate() override;
bool CanUseCanvasLayerForSize(const gfx::IntSize& aSize) override {
if (!mGLContext) return false;
int32_t maxSize = GetMaxTextureSize();
return aSize <= gfx::IntSize(maxSize, maxSize);
}
int32_t GetMaxTextureSize() const override;
/**
* Set the size of the EGL surface we're rendering to, if we're rendering to
* an EGL surface.
*/
void SetDestinationSurfaceSize(const gfx::IntSize& aSize) override;
void MakeCurrent(MakeCurrentFlags aFlags = 0) override;
#ifdef MOZ_DUMP_PAINTING
const char* Name() const override { return "OGL"; }
#endif // MOZ_DUMP_PAINTING
LayersBackend GetBackendType() const override {
return LayersBackend::LAYERS_OPENGL;
}
void Pause() override;
bool Resume() override;
GLContext* gl() const { return mGLContext; }
GLContext* GetGLContext() const override { return mGLContext; }
#ifdef XP_DARWIN
void MaybeUnlockBeforeNextComposition(TextureHost* aTextureHost) override;
void TryUnlockTextures() override;
#endif
/**
* Clear the program state. This must be called
* before operating on the GLContext directly. */
void ResetProgram();
gfx::SurfaceFormat GetFBOFormat() const {
return gfx::SurfaceFormat::R8G8B8A8;
}
GLBlitTextureImageHelper* BlitTextureImageHelper();
/**
* The compositor provides with temporary textures for use with direct
* textruing.
*/
GLuint GetTemporaryTexture(GLenum aTarget, GLenum aUnit);
const gfx::IntSize GetDestinationSurfaceSize() const {
return gfx::IntSize(mSurfaceSize.width, mSurfaceSize.height);
}
/**
* Allow the origin of the surface to be offset so that content does not
* start at (0, 0) on the surface.
*/
void SetSurfaceOrigin(const ScreenIntPoint& aOrigin) {
mSurfaceOrigin = aOrigin;
}
// Register TextureSource which own device data that have to be deleted before
// destroying this CompositorOGL.
void RegisterTextureSource(TextureSource* aTextureSource);
void UnregisterTextureSource(TextureSource* aTextureSource);
ipc::FileDescriptor GetReleaseFence();
private:
template <typename Geometry>
void DrawGeometry(const Geometry& aGeometry, const gfx::Rect& aRect,
const gfx::IntRect& aClipRect,
const EffectChain& aEffectChain, gfx::Float aOpacity,
const gfx::Matrix4x4& aTransform,
const gfx::Rect& aVisibleRect);
void PrepareViewport(CompositingRenderTargetOGL* aRenderTarget);
bool SupportsTextureDirectMapping();
void InsertFrameDoneSync();
bool NeedToRecreateFullWindowRenderTarget() const;
/** Widget associated with this compositor */
LayoutDeviceIntSize mWidgetSize;
RefPtr<GLContext> mGLContext;
RefPtr<SurfacePoolHandle> mSurfacePoolHandle;
UniquePtr<GLBlitTextureImageHelper> mBlitTextureImageHelper;
gfx::Matrix4x4 mProjMatrix;
bool mCanRenderToDefaultFramebuffer = true;
#ifdef XP_DARWIN
nsTArray<RefPtr<BufferTextureHost>> mMaybeUnlockBeforeNextComposition;
#endif
/** The size of the surface we are rendering to */
gfx::IntSize mSurfaceSize;
/** The origin of the content on the surface */
ScreenIntPoint mSurfaceOrigin;
already_AddRefed<mozilla::gl::GLContext> CreateContext();
/** Texture target to use for FBOs */
GLenum mFBOTextureTarget;
/** Currently bound render target */
RefPtr<CompositingRenderTargetOGL> mCurrentRenderTarget;
// The 1x1 dummy render target that's the "current" render target between
// BeginFrameForNativeLayers and EndFrame but outside pairs of
// Begin/EndRenderingToNativeLayer. Created on demand.
RefPtr<CompositingRenderTarget> mNativeLayersReferenceRT;
// The render target that profiler screenshots / frame recording read from.
// This will be the actual window framebuffer when rendering to a window, and
// it will be mFullWindowRenderTarget when rendering to native layers.
RefPtr<CompositingRenderTargetOGL> mWindowRenderTarget;
// Non-null when using native layers and frame recording is requested.
// EndNormalDrawing() maintains a copy of the entire window contents in this
// render target, by copying from the native layer render targets.
RefPtr<CompositingRenderTargetOGL> mFullWindowRenderTarget;
/**
* VBO that has some basics in it for a textured quad, including vertex
* coords and texcoords.
*/
GLuint mQuadVBO;
/**
* VBO that stores dynamic triangle geometry.
*/
GLuint mTriangleVBO;
// Used to apply back-pressure in WaitForPreviousFrameDoneSync().
GLsync mPreviousFrameDoneSync;
GLsync mThisFrameDoneSync;
bool mHasBGRA;
/**
* When rendering to some EGL surfaces (e.g. on Android), we rely on being
* told about size changes (via SetSurfaceSize) rather than pulling this
* information from the widget.
*/
bool mUseExternalSurfaceSize;
/**
* Have we had DrawQuad calls since the last frame was rendered?
*/
bool mFrameInProgress;
// Only true between BeginFromeForNativeLayers and EndFrame, and only if the
// full window render target needed to be recreated in the current frame.
bool mShouldInvalidateWindow = false;
/*
* Clear aRect on current render target.
*/
void ClearRect(const gfx::Rect& aRect) override;
/* Start a new frame.
*/
Maybe<gfx::IntRect> BeginFrameForWindow(
const nsIntRegion& aInvalidRegion, const Maybe<gfx::IntRect>& aClipRect,
const gfx::IntRect& aRenderBounds,
const nsIntRegion& aOpaqueRegion) override;
Maybe<gfx::IntRect> BeginFrameForTarget(
const nsIntRegion& aInvalidRegion, const Maybe<gfx::IntRect>& aClipRect,
const gfx::IntRect& aRenderBounds, const nsIntRegion& aOpaqueRegion,
gfx::DrawTarget* aTarget, const gfx::IntRect& aTargetBounds) override;
void BeginFrameForNativeLayers() override;
Maybe<gfx::IntRect> BeginRenderingToNativeLayer(
const nsIntRegion& aInvalidRegion, const Maybe<gfx::IntRect>& aClipRect,
const nsIntRegion& aOpaqueRegion, NativeLayer* aNativeLayer) override;
void EndRenderingToNativeLayer() override;
Maybe<gfx::IntRect> BeginFrame(const nsIntRegion& aInvalidRegion,
const Maybe<gfx::IntRect>& aClipRect,
const gfx::IntRect& aRenderBounds,
const nsIntRegion& aOpaqueRegion);
ShaderConfigOGL GetShaderConfigFor(
Effect* aEffect, TextureSourceOGL* aSourceMask = nullptr,
gfx::CompositionOp aOp = gfx::CompositionOp::OP_OVER,
bool aColorMatrix = false, bool aDEAAEnabled = false) const;
ShaderProgramOGL* GetShaderProgramFor(const ShaderConfigOGL& aConfig);
void ApplyPrimitiveConfig(ShaderConfigOGL& aConfig, const gfx::Rect&) {
aConfig.SetDynamicGeometry(false);
}
void ApplyPrimitiveConfig(ShaderConfigOGL& aConfig,
const nsTArray<gfx::TexturedTriangle>&) {
aConfig.SetDynamicGeometry(true);
}
/**
* Create a FBO backed by a texture.
* Note that the texture target type will be
* of the type returned by FBOTextureTarget; different
* shaders are required to sample from the different
* texture types.
*/
void CreateFBOWithTexture(const gfx::IntRect& aRect, bool aCopyFromSource,
GLuint aSourceFrameBuffer, GLuint* aFBO,
GLuint* aTexture,
gfx::IntSize* aAllocSize = nullptr);
GLuint CreateTexture(const gfx::IntRect& aRect, bool aCopyFromSource,
GLuint aSourceFrameBuffer,
gfx::IntSize* aAllocSize = nullptr);
gfx::Point3D GetLineCoefficients(const gfx::Point& aPoint1,
const gfx::Point& aPoint2);
void ActivateProgram(ShaderProgramOGL* aProg);
void CleanupResources();
void BindAndDrawQuads(ShaderProgramOGL* aProg, int aQuads,
const gfx::Rect* aLayerRect,
const gfx::Rect* aTextureRect);
void BindAndDrawQuad(ShaderProgramOGL* aProg, const gfx::Rect& aLayerRect,
const gfx::Rect& aTextureRect = gfx::Rect(0.0f, 0.0f,
1.0f, 1.0f)) {
gfx::Rect layerRects[4];
gfx::Rect textureRects[4];
layerRects[0] = aLayerRect;
textureRects[0] = aTextureRect;
BindAndDrawQuads(aProg, 1, layerRects, textureRects);
}
void BindAndDrawGeometry(ShaderProgramOGL* aProgram, const gfx::Rect& aRect);
void BindAndDrawGeometry(ShaderProgramOGL* aProgram,
const nsTArray<gfx::TexturedTriangle>& aTriangles);
void BindAndDrawGeometryWithTextureRect(ShaderProgramOGL* aProg,
const gfx::Rect& aRect,
const gfx::Rect& aTexCoordRect,
TextureSource* aTexture);
void BindAndDrawGeometryWithTextureRect(
ShaderProgramOGL* aProg,
const nsTArray<gfx::TexturedTriangle>& aTriangles,
const gfx::Rect& aTexCoordRect, TextureSource* aTexture);
void InitializeVAO(const GLuint aAttribIndex, const GLint aComponents,
const GLsizei aStride, const size_t aOffset);
gfx::Rect GetTextureCoordinates(gfx::Rect textureRect,
TextureSource* aTexture);
/**
* Bind the texture behind the current render target as the backdrop for a
* mix-blend shader.
*/
void BindBackdrop(ShaderProgramOGL* aProgram, GLuint aBackdrop,
GLenum aTexUnit);
/**
* Copies the content of the current render target to the set transaction
* target.
*/
void CopyToTarget(gfx::DrawTarget* aTarget, const nsIntPoint& aTopLeft,
const gfx::Matrix& aWorldMatrix);
/**
* Implements the flipping of the y-axis to convert from layers/compositor
* coordinates to OpenGL coordinates.
*
* Indeed, the only coordinate system that OpenGL knows has the y-axis
* pointing upwards, but the layers/compositor coordinate system has the
* y-axis pointing downwards, for good reason as Web pages are typically
* scrolled downwards. So, some flipping has to take place; FlippedY does it.
*/
GLint FlipY(GLint y) const { return mViewportSize.height - y; }
// The DrawTarget from BeginFrameForTarget, which EndFrame needs to copy the
// window contents into.
// Only non-null between BeginFrameForTarget and EndFrame.
RefPtr<gfx::DrawTarget> mTarget;
gfx::IntRect mTargetBounds;
RefPtr<CompositorTexturePoolOGL> mTexturePool;
// The native layer that we're currently rendering to, if any.
// Non-null only between BeginFrame and EndFrame if BeginFrame has been called
// with a non-null aNativeLayer.
RefPtr<NativeLayer> mCurrentNativeLayer;
#ifdef MOZ_WIDGET_GTK
// Hold TextureSources which own device data that have to be deleted before
// destroying this CompositorOGL.
std::unordered_set<TextureSource*> mRegisteredTextureSources;
#endif
// FileDescriptor of release fence.
// Release fence is a fence that is used for waiting until usage/composite of
// AHardwareBuffer is ended. The fence is delivered to client side via
// ImageBridge. It is used only on android.
ipc::FileDescriptor mReleaseFenceFd;
bool mDestroyed;
/**
* Size of the OpenGL context's primary framebuffer in pixels. Used by
* FlipY for the y-flipping calculation and by the DEAA shader.
*/
gfx::IntSize mViewportSize;
gfx::IntRegion mCurrentFrameInvalidRegion;
ShaderProgramOGL* mCurrentProgram;
};
} // namespace layers
} // namespace mozilla
#endif /* MOZILLA_GFX_COMPOSITOROGL_H */
|