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