Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/layers/TextureHostOGL.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef MOZILLA_GFX_TEXTUREOGL_H
8
#define MOZILLA_GFX_TEXTUREOGL_H
9
10
#include <stddef.h>                     // for size_t
11
#include <stdint.h>                     // for uint64_t
12
#include "CompositableHost.h"
13
#include "GLContextTypes.h"             // for GLContext
14
#include "GLDefs.h"                     // for GLenum, LOCAL_GL_CLAMP_TO_EDGE, etc
15
#include "GLTextureImage.h"             // for TextureImage
16
#include "gfxTypes.h"
17
#include "mozilla/GfxMessageUtils.h"    // for gfxContentType
18
#include "mozilla/Assertions.h"         // for MOZ_ASSERT, etc
19
#include "mozilla/Attributes.h"         // for override
20
#include "mozilla/RefPtr.h"             // for RefPtr
21
#include "mozilla/gfx/Matrix.h"         // for Matrix4x4
22
#include "mozilla/gfx/Point.h"          // for IntSize, IntPoint
23
#include "mozilla/gfx/Types.h"          // for SurfaceFormat, etc
24
#include "mozilla/layers/CompositorOGL.h"  // for CompositorOGL
25
#include "mozilla/layers/CompositorTypes.h"  // for TextureFlags
26
#include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor
27
#include "mozilla/layers/TextureHost.h"  // for TextureHost, etc
28
#include "mozilla/mozalloc.h"           // for operator delete, etc
29
#include "nsCOMPtr.h"                   // for already_AddRefed
30
#include "nsDebug.h"                    // for NS_WARNING
31
#include "nsISupportsImpl.h"            // for TextureImage::Release, etc
32
#include "nsRegionFwd.h"                // for nsIntRegion
33
#include "OGLShaderProgram.h"           // for ShaderProgramType, etc
34
35
#ifdef MOZ_WIDGET_ANDROID
36
#include "GeneratedJNIWrappers.h"
37
#include "AndroidSurfaceTexture.h"
38
#endif
39
40
namespace mozilla {
41
namespace gfx {
42
class DataSourceSurface;
43
} // namespace gfx
44
45
namespace layers {
46
47
class Compositor;
48
class CompositorOGL;
49
class TextureImageTextureSourceOGL;
50
class GLTextureSource;
51
52
inline void ApplySamplingFilterToBoundTexture(gl::GLContext* aGL,
53
                                              gfx::SamplingFilter aSamplingFilter,
54
                                              GLuint aTarget = LOCAL_GL_TEXTURE_2D)
55
0
{
56
0
  GLenum filter =
57
0
    (aSamplingFilter == gfx::SamplingFilter::POINT ? LOCAL_GL_NEAREST : LOCAL_GL_LINEAR);
58
0
59
0
  aGL->fTexParameteri(aTarget, LOCAL_GL_TEXTURE_MIN_FILTER, filter);
60
0
  aGL->fTexParameteri(aTarget, LOCAL_GL_TEXTURE_MAG_FILTER, filter);
61
0
}
62
63
/*
64
 * TextureHost implementations for the OpenGL backend.
65
 *
66
 * Note that it is important to be careful about the ownership model with
67
 * the OpenGL backend, due to some widget limitation on Linux: before
68
 * the nsBaseWidget associated with our OpenGL context has been completely
69
 * deleted, every resource belonging to the OpenGL context MUST have been
70
 * released. At the moment the teardown sequence happens in the middle of
71
 * the nsBaseWidget's destructor, meaning that at a given moment we must be
72
 * able to easily find and release all the GL resources.
73
 * The point is: be careful about the ownership model and limit the number
74
 * of objects sharing references to GL resources to make the tear down
75
 * sequence as simple as possible.
76
 */
77
78
/**
79
 * TextureSourceOGL provides the necessary API for CompositorOGL to composite
80
 * a TextureSource.
81
 */
82
class TextureSourceOGL
83
{
84
public:
85
  TextureSourceOGL()
86
    : mCachedSamplingFilter(gfx::SamplingFilter::GOOD)
87
    , mHasCachedSamplingFilter(false)
88
0
  {}
89
90
  virtual bool IsValid() const = 0;
91
92
  virtual void BindTexture(GLenum aTextureUnit,
93
                           gfx::SamplingFilter aSamplingFilter) = 0;
94
95
  virtual gfx::IntSize GetSize() const = 0;
96
97
0
  virtual GLenum GetTextureTarget() const { return LOCAL_GL_TEXTURE_2D; }
98
99
  virtual gfx::SurfaceFormat GetFormat() const = 0;
100
101
  virtual GLenum GetWrapMode() const = 0;
102
103
0
  virtual gfx::Matrix4x4 GetTextureTransform() { return gfx::Matrix4x4(); }
104
105
0
  virtual TextureImageTextureSourceOGL* AsTextureImageTextureSource() { return nullptr; }
106
107
0
  virtual GLTextureSource* AsGLTextureSource() { return nullptr; }
108
109
  void SetSamplingFilter(gl::GLContext* aGL, gfx::SamplingFilter aSamplingFilter)
110
  {
111
    if (mHasCachedSamplingFilter &&
112
        mCachedSamplingFilter == aSamplingFilter) {
113
      return;
114
    }
115
    mHasCachedSamplingFilter = true;
116
    mCachedSamplingFilter = aSamplingFilter;
117
    ApplySamplingFilterToBoundTexture(aGL, aSamplingFilter, GetTextureTarget());
118
  }
119
120
  void ClearCachedFilter() { mHasCachedSamplingFilter = false; }
121
122
private:
123
  gfx::SamplingFilter mCachedSamplingFilter;
124
  bool mHasCachedSamplingFilter;
125
};
126
127
/**
128
 * A TextureSource backed by a TextureImage.
129
 *
130
 * Depending on the underlying TextureImage, may support texture tiling, so
131
 * make sure to check AsBigImageIterator() and use the texture accordingly.
132
 *
133
 * This TextureSource can be used without a TextureHost and manage it's own
134
 * GL texture(s).
135
 */
136
class TextureImageTextureSourceOGL final : public DataTextureSource
137
                                         , public TextureSourceOGL
138
                                         , public BigImageIterator
139
{
140
public:
141
  explicit TextureImageTextureSourceOGL(CompositorOGL *aCompositor,
142
                                        TextureFlags aFlags = TextureFlags::DEFAULT)
143
    : mGL(aCompositor->gl())
144
    , mFlags(aFlags)
145
    , mIterating(false)
146
  {}
147
148
  virtual const char* Name() const override { return "TextureImageTextureSourceOGL"; }
149
  // DataTextureSource
150
151
  virtual bool Update(gfx::DataSourceSurface* aSurface,
152
                      nsIntRegion* aDestRegion = nullptr,
153
                      gfx::IntPoint* aSrcOffset = nullptr) override;
154
155
  void EnsureBuffer(const gfx::IntSize& aSize,
156
                    gfxContentType aContentType);
157
158
  virtual TextureImageTextureSourceOGL* AsTextureImageTextureSource() override { return this; }
159
160
  // TextureSource
161
162
  virtual void DeallocateDeviceData() override
163
  {
164
    mTexImage = nullptr;
165
    SetUpdateSerial(0);
166
  }
167
168
  virtual TextureSourceOGL* AsSourceOGL() override { return this; }
169
170
  virtual void BindTexture(GLenum aTextureUnit,
171
                           gfx::SamplingFilter aSamplingFilter) override;
172
173
  virtual gfx::IntSize GetSize() const override;
174
175
  virtual gfx::SurfaceFormat GetFormat() const override;
176
177
  virtual bool IsValid() const override { return !!mTexImage; }
178
179
  virtual void SetTextureSourceProvider(TextureSourceProvider* aProvider) override;
180
181
  virtual GLenum GetWrapMode() const override
182
  {
183
    return mTexImage->GetWrapMode();
184
  }
185
186
  // BigImageIterator
187
188
  virtual BigImageIterator* AsBigImageIterator() override { return this; }
189
190
  virtual void BeginBigImageIteration() override
191
  {
192
    mTexImage->BeginBigImageIteration();
193
    mIterating = true;
194
  }
195
196
  virtual void EndBigImageIteration() override
197
  {
198
    mIterating = false;
199
  }
200
201
  virtual gfx::IntRect GetTileRect() override;
202
203
  virtual size_t GetTileCount() override
204
  {
205
    return mTexImage->GetTileCount();
206
  }
207
208
  virtual bool NextTile() override
209
  {
210
    return mTexImage->NextTile();
211
  }
212
213
protected:
214
  RefPtr<gl::TextureImage> mTexImage;
215
  RefPtr<gl::GLContext> mGL;
216
  TextureFlags mFlags;
217
  bool mIterating;
218
};
219
220
/**
221
 * A texture source for GL textures.
222
 *
223
 * It does not own any GL texture, and attaches its shared handle to one of
224
 * the compositor's temporary textures when binding.
225
 *
226
 * The shared texture handle is owned by the TextureHost.
227
 */
228
class GLTextureSource : public DataTextureSource
229
                      , public TextureSourceOGL
230
{
231
public:
232
  GLTextureSource(TextureSourceProvider* aProvider,
233
                  GLuint aTextureHandle,
234
                  GLenum aTarget,
235
                  gfx::IntSize aSize,
236
                  gfx::SurfaceFormat aFormat);
237
238
  virtual ~GLTextureSource();
239
240
  virtual const char* Name() const override { return "GLTextureSource"; }
241
242
  virtual GLTextureSource* AsGLTextureSource() override { return this; }
243
244
  virtual TextureSourceOGL* AsSourceOGL() override { return this; }
245
246
  virtual void BindTexture(GLenum activetex,
247
                           gfx::SamplingFilter aSamplingFilter) override;
248
249
  virtual bool IsValid() const override;
250
251
  virtual gfx::IntSize GetSize() const override { return mSize; }
252
253
  virtual gfx::SurfaceFormat GetFormat() const override { return mFormat; }
254
255
  virtual GLenum GetTextureTarget() const override { return mTextureTarget; }
256
257
  virtual GLenum GetWrapMode() const override { return LOCAL_GL_CLAMP_TO_EDGE; }
258
259
  virtual void DeallocateDeviceData() override;
260
261
  virtual void SetTextureSourceProvider(TextureSourceProvider* aProvider) override;
262
263
0
  void SetSize(gfx::IntSize aSize) { mSize = aSize; }
264
265
0
  void SetFormat(gfx::SurfaceFormat aFormat) { mFormat = aFormat; }
266
267
0
  GLuint GetTextureHandle() const { return mTextureHandle; }
268
269
  gl::GLContext* gl() const {
270
    return mGL;
271
  }
272
273
  virtual bool Update(gfx::DataSourceSurface* aSurface,
274
                      nsIntRegion* aDestRegion = nullptr,
275
                      gfx::IntPoint* aSrcOffset = nullptr) override
276
  {
277
    return false;
278
  }
279
280
protected:
281
  void DeleteTextureHandle();
282
283
  RefPtr<gl::GLContext> mGL;
284
  RefPtr<CompositorOGL> mCompositor;
285
  GLuint mTextureHandle;
286
  GLenum mTextureTarget;
287
  gfx::IntSize mSize;
288
  gfx::SurfaceFormat mFormat;
289
};
290
291
// This texture source try to wrap "aSurface" in ctor for compositor direct
292
// access. Since we can't know the timing for gpu buffer access, the surface
293
// should be alive until the ~ClientStorageTextureSource(). And if we try to
294
// update the surface we mapped before, we need to call Sync() to make sure
295
// the surface is not used by compositor.
296
class DirectMapTextureSource : public GLTextureSource
297
{
298
public:
299
  DirectMapTextureSource(TextureSourceProvider* aProvider,
300
                         gfx::DataSourceSurface* aSurface);
301
302
  virtual bool Update(gfx::DataSourceSurface* aSurface,
303
                      nsIntRegion* aDestRegion = nullptr,
304
                      gfx::IntPoint* aSrcOffset = nullptr) override;
305
306
  virtual bool IsDirectMap() override { return true; }
307
308
  // If aBlocking is false, check if this texture is no longer being used
309
  // by the GPU - if aBlocking is true, this will block until the GPU is
310
  // done with it.
311
  virtual bool Sync(bool aBlocking) override;
312
313
private:
314
  bool UpdateInternal(gfx::DataSourceSurface* aSurface,
315
                      nsIntRegion* aDestRegion,
316
                      gfx::IntPoint* aSrcOffset,
317
                      bool aInit);
318
};
319
320
class GLTextureHost : public TextureHost
321
{
322
public:
323
  GLTextureHost(TextureFlags aFlags,
324
                GLuint aTextureHandle,
325
                GLenum aTarget,
326
                GLsync aSync,
327
                gfx::IntSize aSize,
328
                bool aHasAlpha);
329
330
  virtual ~GLTextureHost();
331
332
  // We don't own anything.
333
  virtual void DeallocateDeviceData() override {}
334
335
  virtual void SetTextureSourceProvider(TextureSourceProvider* aProvider) override;
336
337
  virtual bool Lock() override;
338
339
  virtual void Unlock() override {}
340
341
  virtual gfx::SurfaceFormat GetFormat() const override;
342
343
  virtual bool BindTextureSource(CompositableTextureSourceRef& aTexture) override
344
  {
345
    aTexture = mTextureSource;
346
    return !!aTexture;
347
  }
348
349
  virtual already_AddRefed<gfx::DataSourceSurface> GetAsSurface() override
350
  {
351
    return nullptr; // XXX - implement this (for MOZ_DUMP_PAINTING)
352
  }
353
354
  gl::GLContext* gl() const;
355
356
  virtual gfx::IntSize GetSize() const override { return mSize; }
357
358
  virtual const char* Name() override { return "GLTextureHost"; }
359
360
protected:
361
  const GLuint mTexture;
362
  const GLenum mTarget;
363
  GLsync mSync;
364
  const gfx::IntSize mSize;
365
  const bool mHasAlpha;
366
  RefPtr<GLTextureSource> mTextureSource;
367
};
368
369
////////////////////////////////////////////////////////////////////////
370
// SurfaceTexture
371
372
#ifdef MOZ_WIDGET_ANDROID
373
374
class SurfaceTextureSource : public TextureSource
375
                           , public TextureSourceOGL
376
{
377
public:
378
  SurfaceTextureSource(TextureSourceProvider* aProvider,
379
                       java::GeckoSurfaceTexture::Ref& aSurfTex,
380
                       gfx::SurfaceFormat aFormat,
381
                       GLenum aTarget,
382
                       GLenum aWrapMode,
383
                       gfx::IntSize aSize,
384
                       bool aIgnoreTransform);
385
386
  virtual const char* Name() const override { return "SurfaceTextureSource"; }
387
388
  virtual TextureSourceOGL* AsSourceOGL() override { return this; }
389
390
  virtual void BindTexture(GLenum activetex,
391
                           gfx::SamplingFilter aSamplingFilter) override;
392
393
  virtual bool IsValid() const override;
394
395
  virtual gfx::IntSize GetSize() const override { return mSize; }
396
397
  virtual gfx::SurfaceFormat GetFormat() const override { return mFormat; }
398
399
  virtual gfx::Matrix4x4 GetTextureTransform() override;
400
401
  virtual GLenum GetTextureTarget() const override { return mTextureTarget; }
402
403
  virtual GLenum GetWrapMode() const override { return mWrapMode; }
404
405
  virtual void DeallocateDeviceData() override;
406
407
  virtual void SetTextureSourceProvider(TextureSourceProvider* aProvider) override;
408
409
  gl::GLContext* gl() const {
410
    return mGL;
411
  }
412
413
protected:
414
  RefPtr<gl::GLContext> mGL;
415
  mozilla::java::GeckoSurfaceTexture::GlobalRef mSurfTex;
416
  const gfx::SurfaceFormat mFormat;
417
  const GLenum mTextureTarget;
418
  const GLenum mWrapMode;
419
  const gfx::IntSize mSize;
420
  const bool mIgnoreTransform;
421
};
422
423
class SurfaceTextureHost : public TextureHost
424
{
425
public:
426
  SurfaceTextureHost(TextureFlags aFlags,
427
                     mozilla::java::GeckoSurfaceTexture::Ref& aSurfTex,
428
                     gfx::IntSize aSize,
429
                     gfx::SurfaceFormat aFormat,
430
                     bool aContinuousUpdate,
431
                     bool aIgnoreTransform);
432
433
  virtual ~SurfaceTextureHost();
434
435
  virtual void PrepareTextureSource(CompositableTextureSourceRef& aTexture) override;
436
437
  virtual void DeallocateDeviceData() override;
438
439
  virtual void SetTextureSourceProvider(TextureSourceProvider* aProvider) override;
440
441
  virtual bool Lock() override;
442
443
  virtual gfx::SurfaceFormat GetFormat() const override;
444
445
  virtual void NotifyNotUsed() override;
446
447
  virtual bool BindTextureSource(CompositableTextureSourceRef& aTexture) override
448
  {
449
    aTexture = mTextureSource;
450
    return !!aTexture;
451
  }
452
453
  virtual already_AddRefed<gfx::DataSourceSurface> GetAsSurface() override
454
  {
455
    return nullptr; // XXX - implement this (for MOZ_DUMP_PAINTING)
456
  }
457
458
  gl::GLContext* gl() const;
459
460
  virtual gfx::IntSize GetSize() const override { return mSize; }
461
462
  virtual const char* Name() override { return "SurfaceTextureHost"; }
463
464
protected:
465
  bool EnsureAttached();
466
467
  mozilla::java::GeckoSurfaceTexture::GlobalRef mSurfTex;
468
  const gfx::IntSize mSize;
469
  const gfx::SurfaceFormat mFormat;
470
  bool mContinuousUpdate;
471
  const bool mIgnoreTransform;
472
  RefPtr<CompositorOGL> mCompositor;
473
  RefPtr<SurfaceTextureSource> mTextureSource;
474
};
475
476
#endif // MOZ_WIDGET_ANDROID
477
478
////////////////////////////////////////////////////////////////////////
479
// EGLImage
480
481
class EGLImageTextureSource : public TextureSource
482
                            , public TextureSourceOGL
483
{
484
public:
485
  EGLImageTextureSource(TextureSourceProvider* aProvider,
486
                        EGLImage aImage,
487
                        gfx::SurfaceFormat aFormat,
488
                        GLenum aTarget,
489
                        GLenum aWrapMode,
490
                        gfx::IntSize aSize);
491
492
  virtual const char* Name() const override { return "EGLImageTextureSource"; }
493
494
  virtual TextureSourceOGL* AsSourceOGL() override { return this; }
495
496
  virtual void BindTexture(GLenum activetex,
497
                           gfx::SamplingFilter aSamplingFilter) override;
498
499
  virtual bool IsValid() const override;
500
501
  virtual gfx::IntSize GetSize() const override { return mSize; }
502
503
  virtual gfx::SurfaceFormat GetFormat() const override { return mFormat; }
504
505
  virtual gfx::Matrix4x4 GetTextureTransform() override;
506
507
  virtual GLenum GetTextureTarget() const override { return mTextureTarget; }
508
509
  virtual GLenum GetWrapMode() const override { return mWrapMode; }
510
511
  // We don't own anything.
512
  virtual void DeallocateDeviceData() override {}
513
514
  virtual void SetTextureSourceProvider(TextureSourceProvider* aProvider) override;
515
516
  gl::GLContext* gl() const {
517
    return mGL;
518
  }
519
520
protected:
521
  RefPtr<gl::GLContext> mGL;
522
  RefPtr<CompositorOGL> mCompositor;
523
  const EGLImage mImage;
524
  const gfx::SurfaceFormat mFormat;
525
  const GLenum mTextureTarget;
526
  const GLenum mWrapMode;
527
  const gfx::IntSize mSize;
528
};
529
530
class EGLImageTextureHost final : public TextureHost
531
{
532
public:
533
  EGLImageTextureHost(TextureFlags aFlags,
534
                     EGLImage aImage,
535
                     EGLSync aSync,
536
                     gfx::IntSize aSize,
537
                     bool hasAlpha);
538
539
  virtual ~EGLImageTextureHost();
540
541
  // We don't own anything.
542
  virtual void DeallocateDeviceData() override {}
543
544
  void SetTextureSourceProvider(TextureSourceProvider* aProvider) override;
545
546
  virtual bool Lock() override;
547
548
  virtual void Unlock() override;
549
550
  virtual gfx::SurfaceFormat GetFormat() const override;
551
552
  virtual bool BindTextureSource(CompositableTextureSourceRef& aTexture) override
553
  {
554
    aTexture = mTextureSource;
555
    return !!aTexture;
556
  }
557
558
  virtual already_AddRefed<gfx::DataSourceSurface> GetAsSurface() override
559
  {
560
    return nullptr; // XXX - implement this (for MOZ_DUMP_PAINTING)
561
  }
562
563
  gl::GLContext* gl() const;
564
565
  virtual gfx::IntSize GetSize() const override { return mSize; }
566
567
  virtual const char* Name() override { return "EGLImageTextureHost"; }
568
569
protected:
570
  const EGLImage mImage;
571
  const EGLSync mSync;
572
  const gfx::IntSize mSize;
573
  const bool mHasAlpha;
574
  RefPtr<EGLImageTextureSource> mTextureSource;
575
};
576
577
} // namespace layers
578
} // namespace mozilla
579
580
#endif /* MOZILLA_GFX_TEXTUREOGL_H */