Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/layers/composite/CompositableHost.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_BUFFERHOST_H
8
#define MOZILLA_GFX_BUFFERHOST_H
9
10
#include <stdint.h>                     // for uint64_t
11
#include <stdio.h>                      // for FILE
12
#include "gfxRect.h"                    // for gfxRect
13
#include "mozilla/Assertions.h"         // for MOZ_ASSERT, etc
14
#include "mozilla/Attributes.h"         // for override
15
#include "mozilla/RefPtr.h"             // for RefPtr, RefCounted, etc
16
#include "mozilla/gfx/MatrixFwd.h"      // for Matrix4x4
17
#include "mozilla/gfx/Point.h"          // for Point
18
#include "mozilla/gfx/Polygon.h"        // for Polygon
19
#include "mozilla/gfx/Rect.h"           // for Rect
20
#include "mozilla/gfx/Types.h"          // for SamplingFilter
21
#include "mozilla/ipc/ProtocolUtils.h"
22
#include "mozilla/layers/Compositor.h"  // for Compositor
23
#include "mozilla/layers/CompositorTypes.h"  // for TextureInfo, etc
24
#include "mozilla/layers/Effects.h"     // for Texture Effect
25
#include "mozilla/layers/LayersTypes.h"  // for LayerRenderState, etc
26
#include "mozilla/layers/LayersMessages.h"
27
#include "mozilla/layers/TextureHost.h" // for TextureHost
28
#include "mozilla/mozalloc.h"           // for operator delete
29
#include "nsCOMPtr.h"                   // for already_AddRefed
30
#include "nsRegion.h"                   // for nsIntRegion
31
#include "nscore.h"                     // for nsACString
32
#include "Units.h"                      // for CSSToScreenScale
33
34
namespace mozilla {
35
namespace gfx {
36
class DataSourceSurface;
37
} // namespace gfx
38
39
namespace layers {
40
41
class Layer;
42
class LayerComposite;
43
class ImageHost;
44
class Compositor;
45
class ThebesBufferData;
46
class TiledContentHost;
47
class CompositableParentManager;
48
class WebRenderImageHost;
49
class ContentHost;
50
class ContentHostTexture;
51
class HostLayerManager;
52
struct EffectChain;
53
54
struct ImageCompositeNotificationInfo {
55
  base::ProcessId mImageBridgeProcessId;
56
  ImageCompositeNotification mNotification;
57
};
58
59
struct AsyncCompositableRef
60
{
61
  AsyncCompositableRef()
62
   : mProcessId(mozilla::ipc::kInvalidProcessId)
63
0
  {}
64
  AsyncCompositableRef(base::ProcessId aProcessId, const CompositableHandle& aHandle)
65
   : mProcessId(aProcessId), mHandle(aHandle)
66
  {}
67
  explicit operator bool() const { return !!mHandle; }
68
  base::ProcessId mProcessId;
69
  CompositableHandle mHandle;
70
};
71
72
/**
73
 * The compositor-side counterpart to CompositableClient. Responsible for
74
 * updating textures and data about textures from IPC and how textures are
75
 * composited (tiling, double buffering, etc.).
76
 *
77
 * Update (for images/canvases) and UpdateThebes (for Thebes) are called during
78
 * the layers transaction to update the Compositbale's textures from the
79
 * content side. The actual update (and any syncronous upload) is done by the
80
 * TextureHost, but it is coordinated by the CompositableHost.
81
 *
82
 * Composite is called by the owning layer when it is composited. CompositableHost
83
 * will use its TextureHost(s) and call Compositor::DrawQuad to do the actual
84
 * rendering.
85
 */
86
class CompositableHost
87
{
88
protected:
89
  virtual ~CompositableHost();
90
91
public:
92
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositableHost)
93
  explicit CompositableHost(const TextureInfo& aTextureInfo);
94
95
  static already_AddRefed<CompositableHost> Create(const TextureInfo& aTextureInfo, bool aUseWebRender);
96
97
  virtual CompositableType GetType() = 0;
98
99
  // If base class overrides, it should still call the parent implementation
100
  virtual void SetTextureSourceProvider(TextureSourceProvider* aProvider);
101
102
  // composite the contents of this buffer host to the compositor's surface
103
  virtual void Composite(Compositor* aCompositor,
104
                         LayerComposite* aLayer,
105
                         EffectChain& aEffectChain,
106
                         float aOpacity,
107
                         const gfx::Matrix4x4& aTransform,
108
                         const gfx::SamplingFilter aSamplingFilter,
109
                         const gfx::IntRect& aClipRect,
110
                         const nsIntRegion* aVisibleRegion = nullptr,
111
                         const Maybe<gfx::Polygon>& aGeometry = Nothing()) = 0;
112
113
  /**
114
   * Update the content host.
115
   * aUpdated is the region which should be updated.
116
   */
117
  virtual bool UpdateThebes(const ThebesBufferData& aData,
118
                            const nsIntRegion& aUpdated,
119
                            const nsIntRegion& aOldValidRegionBack)
120
  {
121
    NS_ERROR("should be implemented or not used");
122
    return false;
123
  }
124
125
  /**
126
   * Returns the front buffer.
127
   * *aPictureRect (if non-null, and the returned TextureHost is non-null)
128
   * is set to the picture rect.
129
   */
130
  virtual TextureHost* GetAsTextureHost(gfx::IntRect* aPictureRect = nullptr)
131
0
  {
132
0
    return nullptr;
133
0
  }
134
135
  virtual gfx::IntSize GetImageSize()
136
0
  {
137
0
    MOZ_ASSERT(false, "Should have been overridden");
138
0
    return gfx::IntSize();
139
0
  }
140
141
  /**
142
   * Adds a mask effect using this texture as the mask, if possible.
143
   * @return true if the effect was added, false otherwise.
144
   */
145
  bool AddMaskEffect(EffectChain& aEffects,
146
                     const gfx::Matrix4x4& aTransform);
147
148
  void RemoveMaskEffect();
149
150
  TextureSourceProvider* GetTextureSourceProvider() const;
151
152
  Layer* GetLayer() const { return mLayer; }
153
  void SetLayer(Layer* aLayer) { mLayer = aLayer; }
154
155
  virtual ContentHost* AsContentHost() { return nullptr; }
156
  virtual ContentHostTexture* AsContentHostTexture() { return nullptr; }
157
  virtual ImageHost* AsImageHost() { return nullptr; }
158
  virtual TiledContentHost* AsTiledContentHost() { return nullptr; }
159
0
  virtual WebRenderImageHost* AsWebRenderImageHost() { return nullptr; }
160
161
  typedef uint32_t AttachFlags;
162
  static const AttachFlags NO_FLAGS = 0;
163
  static const AttachFlags ALLOW_REATTACH = 1;
164
  static const AttachFlags KEEP_ATTACHED = 2;
165
  static const AttachFlags FORCE_DETACH = 2;
166
167
  virtual void Attach(Layer* aLayer,
168
                      TextureSourceProvider* aProvider,
169
                      AttachFlags aFlags = NO_FLAGS)
170
0
  {
171
0
    MOZ_ASSERT(aProvider);
172
0
    NS_ASSERTION(aFlags & ALLOW_REATTACH || !mAttached,
173
0
                 "Re-attaching compositables must be explicitly authorised");
174
0
    SetTextureSourceProvider(aProvider);
175
0
    SetLayer(aLayer);
176
0
    mAttached = true;
177
0
    mKeepAttached = aFlags & KEEP_ATTACHED;
178
0
  }
179
  // Detach this compositable host from its layer.
180
  // If we are used for async video, then it is not safe to blindly detach since
181
  // we might be re-attached to a different layer. aLayer is the layer which the
182
  // caller expects us to be attached to, we will only detach if we are in fact
183
  // attached to that layer. If we are part of a normal layer, then we will be
184
  // detached in any case. if aLayer is null, then we will only detach if we are
185
  // not async.
186
  // Only force detach if the IPDL tree is being shutdown.
187
  virtual void Detach(Layer* aLayer = nullptr, AttachFlags aFlags = NO_FLAGS)
188
  {
189
    if (!mKeepAttached ||
190
        aLayer == mLayer ||
191
        aFlags & FORCE_DETACH) {
192
      SetLayer(nullptr);
193
      mAttached = false;
194
      mKeepAttached = false;
195
    }
196
  }
197
  bool IsAttached() { return mAttached; }
198
199
  virtual void Dump(std::stringstream& aStream,
200
                    const char* aPrefix="",
201
0
                    bool aDumpHtml=false) { }
202
  static void DumpTextureHost(std::stringstream& aStream, TextureHost* aTexture);
203
204
0
  virtual already_AddRefed<gfx::DataSourceSurface> GetAsSurface() { return nullptr; }
205
206
  virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix) = 0;
207
208
  struct TimedTexture {
209
    CompositableTextureHostRef mTexture;
210
    TimeStamp mTimeStamp;
211
    gfx::IntRect mPictureRect;
212
    int32_t mFrameID;
213
    int32_t mProducerID;
214
  };
215
  virtual void UseTextureHost(const nsTArray<TimedTexture>& aTextures);
216
  virtual void UseComponentAlphaTextures(TextureHost* aTextureOnBlack,
217
                                         TextureHost* aTextureOnWhite);
218
  virtual void RemoveTextureHost(TextureHost* aTexture);
219
220
  // Called every time this is composited
221
0
  void BumpFlashCounter() {
222
0
    mFlashCounter = mFlashCounter >= DIAGNOSTIC_FLASH_COUNTER_MAX
223
0
                  ? DIAGNOSTIC_FLASH_COUNTER_MAX : mFlashCounter + 1;
224
0
  }
225
226
  uint64_t GetCompositorBridgeID() const { return mCompositorBridgeID; }
227
228
  const AsyncCompositableRef& GetAsyncRef() const { return mAsyncRef; }
229
  void SetAsyncRef(const AsyncCompositableRef& aRef) { mAsyncRef = aRef; }
230
231
  void SetCompositorBridgeID(uint64_t aID) { mCompositorBridgeID = aID; }
232
233
0
  virtual bool Lock() { return false; }
234
235
0
  virtual void Unlock() { }
236
237
  virtual already_AddRefed<TexturedEffect> GenEffect(const gfx::SamplingFilter aSamplingFilter) {
238
    return nullptr;
239
  }
240
241
  /// Called when shutting down the layer tree.
242
  /// This is a good place to clear all potential gpu resources before the widget
243
  /// is is destroyed.
244
0
  virtual void CleanupResources() {}
245
246
  virtual void BindTextureSource() {}
247
248
0
  virtual uint32_t GetDroppedFrames() { return 0; }
249
250
protected:
251
  HostLayerManager* GetLayerManager() const;
252
253
protected:
254
  TextureInfo mTextureInfo;
255
  AsyncCompositableRef mAsyncRef;
256
  uint64_t mCompositorBridgeID;
257
  RefPtr<TextureSourceProvider> mTextureSourceProvider;
258
  Layer* mLayer;
259
  uint32_t mFlashCounter; // used when the pref "layers.flash-borders" is true.
260
  bool mAttached;
261
  bool mKeepAttached;
262
};
263
264
class AutoLockCompositableHost final
265
{
266
public:
267
  explicit AutoLockCompositableHost(CompositableHost* aHost)
268
    : mHost(aHost)
269
  {
270
    mSucceeded = (mHost && mHost->Lock());
271
  }
272
273
  ~AutoLockCompositableHost()
274
  {
275
    if (mSucceeded && mHost) {
276
      mHost->Unlock();
277
    }
278
  }
279
280
0
  bool Failed() const { return !mSucceeded; }
281
282
private:
283
  RefPtr<CompositableHost> mHost;
284
  bool mSucceeded;
285
};
286
287
} // namespace layers
288
} // namespace mozilla
289
290
#endif