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_BUFFERCLIENT_H
7 : #define MOZILLA_GFX_BUFFERCLIENT_H
8 :
9 : #include <stdint.h> // for uint64_t
10 : #include <vector> // for vector
11 : #include <map> // for map
12 : #include "mozilla/Assertions.h" // for MOZ_CRASH
13 : #include "mozilla/RefPtr.h" // for already_AddRefed, RefCounted
14 : #include "mozilla/gfx/Types.h" // for SurfaceFormat
15 : #include "mozilla/layers/CompositorTypes.h"
16 : #include "mozilla/layers/LayersTypes.h" // for LayersBackend, TextureDumpMode
17 : #include "mozilla/layers/TextureClient.h" // for TextureClient
18 : #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
19 :
20 : namespace mozilla {
21 : namespace layers {
22 :
23 : class CompositableClient;
24 : class ImageBridgeChild;
25 : class ImageContainer;
26 : class CompositableForwarder;
27 : class CompositableChild;
28 : class TextureClientRecycleAllocator;
29 : class ContentClientRemote;
30 :
31 : /**
32 : * CompositableClient manages the texture-specific logic for composite layers,
33 : * independently of the layer. It is the content side of a CompositableClient/
34 : * CompositableHost pair.
35 : *
36 : * CompositableClient's purpose is to send texture data to the compositor side
37 : * along with any extra information about how the texture is to be composited.
38 : * Things like opacity or transformation belong to layer and not compositable.
39 : *
40 : * Since Compositables are independent of layers it is possible to create one,
41 : * connect it to the compositor side, and start sending images to it. This alone
42 : * is arguably not very useful, but it means that as long as a shadow layer can
43 : * do the proper magic to find a reference to the right CompositableHost on the
44 : * Compositor side, a Compositable client can be used outside of the main
45 : * shadow layer forwarder machinery that is used on the main thread.
46 : *
47 : * The first step is to create a Compositable client and call Connect().
48 : * Connect() creates the underlying IPDL actor (see CompositableChild) and the
49 : * corresponding CompositableHost on the other side.
50 : *
51 : * To do in-transaction texture transfer (the default), call
52 : * ShadowLayerForwarder::Attach(CompositableClient*, ShadowableLayer*). This
53 : * will let the LayerComposite on the compositor side know which CompositableHost
54 : * to use for compositing.
55 : *
56 : * To do async texture transfer (like async-video), the CompositableClient
57 : * should be created with a different CompositableForwarder (like
58 : * ImageBridgeChild) and attachment is done with
59 : * CompositableForwarder::AttachAsyncCompositable that takes an identifier
60 : * instead of a CompositableChild, since the CompositableClient is not managed
61 : * by this layer forwarder (the matching uses a global map on the compositor side,
62 : * see CompositableMap in ImageBridgeParent.cpp)
63 : *
64 : * Subclasses: Painted layers use ContentClients, ImageLayers use ImageClients,
65 : * Canvas layers use CanvasClients (but ImageHosts). We have a different subclass
66 : * where we have a different way of interfacing with the textures - in terms of
67 : * drawing into the compositable and/or passing its contents to the compostior.
68 : */
69 : class CompositableClient
70 : {
71 : protected:
72 : virtual ~CompositableClient();
73 :
74 : public:
75 152 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositableClient)
76 :
77 : explicit CompositableClient(CompositableForwarder* aForwarder, TextureFlags aFlags = TextureFlags::NO_FLAGS);
78 :
79 0 : virtual void Dump(std::stringstream& aStream,
80 : const char* aPrefix="",
81 : bool aDumpHtml=false,
82 0 : TextureDumpMode aCompress=TextureDumpMode::Compress) {};
83 :
84 : virtual TextureInfo GetTextureInfo() const = 0;
85 :
86 : LayersBackend GetCompositorBackendType() const;
87 :
88 : already_AddRefed<TextureClient>
89 : CreateBufferTextureClient(gfx::SurfaceFormat aFormat,
90 : gfx::IntSize aSize,
91 : gfx::BackendType aMoz2dBackend = gfx::BackendType::NONE,
92 : TextureFlags aFlags = TextureFlags::DEFAULT);
93 :
94 : already_AddRefed<TextureClient>
95 : CreateTextureClientForDrawing(gfx::SurfaceFormat aFormat,
96 : gfx::IntSize aSize,
97 : BackendSelector aSelector,
98 : TextureFlags aTextureFlags,
99 : TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT);
100 :
101 : already_AddRefed<TextureClient>
102 : CreateTextureClientFromSurface(gfx::SourceSurface* aSurface,
103 : BackendSelector aSelector,
104 : TextureFlags aTextureFlags,
105 : TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT);
106 :
107 : /**
108 : * Establishes the connection with compositor side through IPDL
109 : */
110 : virtual bool Connect(ImageContainer* aImageContainer = nullptr);
111 :
112 : void Destroy();
113 :
114 : bool IsConnected() const;
115 :
116 130 : CompositableForwarder* GetForwarder() const
117 : {
118 130 : return mForwarder;
119 : }
120 :
121 : /**
122 : * This identifier is what lets us attach async compositables with a shadow
123 : * layer. It is not used if the compositable is used with the regular shadow
124 : * layer forwarder.
125 : *
126 : * If this returns empty, it means the compositable is not async (it is used
127 : * on the main thread).
128 : */
129 : CompositableHandle GetAsyncHandle() const;
130 :
131 : /**
132 : * Handle for IPDL communication.
133 : */
134 121 : CompositableHandle GetIPCHandle() const {
135 121 : return mHandle;
136 : }
137 :
138 : /**
139 : * Tells the Compositor to create a TextureHost for this TextureClient.
140 : */
141 : virtual bool AddTextureClient(TextureClient* aClient);
142 :
143 : /**
144 : * A hook for the when the Compositable is detached from it's layer.
145 : */
146 19 : virtual void OnDetach() {}
147 :
148 : /**
149 : * Clear any resources that are not immediately necessary. This may be called
150 : * in low-memory conditions.
151 : */
152 : virtual void ClearCachedResources();
153 :
154 : /**
155 : * Shrink memory usage.
156 : * Called when "memory-pressure" is observed.
157 : */
158 : virtual void HandleMemoryPressure();
159 :
160 : /**
161 : * Should be called when deataching a TextureClient from a Compositable, because
162 : * some platforms need to do some extra book keeping when this happens.
163 : *
164 : * See AutoRemoveTexture to automatically invoke this at the end of a scope.
165 : */
166 : virtual void RemoveTexture(TextureClient* aTexture);
167 :
168 0 : virtual ContentClientRemote* AsContentClientRemote() { return nullptr; }
169 :
170 : void InitIPDL(const CompositableHandle& aHandle);
171 :
172 0 : TextureFlags GetTextureFlags() const { return mTextureFlags; }
173 :
174 : TextureClientRecycleAllocator* GetTextureClientRecycler();
175 :
176 0 : bool HasTextureClientRecycler() { return !!mTextureClientRecycler; }
177 :
178 : static void DumpTextureClient(std::stringstream& aStream,
179 : TextureClient* aTexture,
180 : TextureDumpMode aCompress);
181 : protected:
182 : RefPtr<CompositableForwarder> mForwarder;
183 : // Some layers may want to enforce some flags to all their textures
184 : // (like disallowing tiling)
185 : TextureFlags mTextureFlags;
186 : RefPtr<TextureClientRecycleAllocator> mTextureClientRecycler;
187 :
188 : CompositableHandle mHandle;
189 : bool mIsAsync;
190 :
191 : friend class CompositableChild;
192 : };
193 :
194 : /**
195 : * Helper to call RemoveTexture at the end of a scope.
196 : */
197 : struct AutoRemoveTexture
198 : {
199 0 : explicit AutoRemoveTexture(CompositableClient* aCompositable,
200 : TextureClient* aTexture = nullptr)
201 0 : : mTexture(aTexture)
202 0 : , mCompositable(aCompositable)
203 0 : {}
204 :
205 : ~AutoRemoveTexture();
206 :
207 : RefPtr<TextureClient> mTexture;
208 : private:
209 : CompositableClient* mCompositable;
210 : };
211 :
212 : } // namespace layers
213 : } // namespace mozilla
214 :
215 : #endif
|