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