Bug 1849470 - Update in-tree zlib to version 1.3. r=aosmond
[gecko.git] / gfx / layers / client / CompositableClient.cpp
blob9178f8f5c17345228b9da1ad1c6faa2519333ba5
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/. */
7 #include "mozilla/layers/CompositableClient.h"
9 #include <stdint.h> // for uint64_t, uint32_t
11 #include "gfxPlatform.h" // for gfxPlatform
12 #include "mozilla/layers/CompositableForwarder.h"
13 #include "mozilla/layers/ImageBridgeChild.h"
14 #include "mozilla/layers/TextureClient.h" // for TextureClient, etc
15 #include "mozilla/layers/TextureClientOGL.h"
16 #include "mozilla/layers/TextureClientRecycleAllocator.h"
17 #include "mozilla/mozalloc.h" // for operator delete, etc
18 #ifdef XP_WIN
19 # include "gfxWindowsPlatform.h" // for gfxWindowsPlatform
20 # include "mozilla/layers/TextureD3D11.h"
21 #endif
22 #include "IPDLActor.h"
23 #include "gfxUtils.h"
25 namespace mozilla {
26 namespace layers {
28 using namespace mozilla::gfx;
30 void CompositableClient::InitIPDL(const CompositableHandle& aHandle) {
31 MOZ_ASSERT(aHandle);
33 mForwarder->AssertInForwarderThread();
35 mHandle = aHandle;
36 mIsAsync |= !NS_IsMainThread();
37 // By simply taking the lock on mTextureClientRecycler we force memory barrier
38 // on mHandle and mIsAsync which makes them behave as Atomic as they are only
39 // ever set in this method.
40 auto l = mTextureClientRecycler.Lock();
43 CompositableClient::CompositableClient(CompositableForwarder* aForwarder,
44 TextureFlags aTextureFlags)
45 : mForwarder(aForwarder),
46 mTextureFlags(aTextureFlags),
47 mTextureClientRecycler("CompositableClient::mTextureClientRecycler"),
48 mIsAsync(false) {}
50 CompositableClient::~CompositableClient() { Destroy(); }
52 LayersBackend CompositableClient::GetCompositorBackendType() const {
53 return mForwarder->GetCompositorBackendType();
56 bool CompositableClient::Connect(ImageContainer* aImageContainer) {
57 MOZ_ASSERT(!mHandle);
58 if (!GetForwarder() || mHandle) {
59 return false;
62 GetForwarder()->AssertInForwarderThread();
63 GetForwarder()->Connect(this, aImageContainer);
64 return true;
67 bool CompositableClient::IsConnected() const {
68 // CanSend() is only reliable in the same thread as the IPDL channel.
69 mForwarder->AssertInForwarderThread();
70 return !!mHandle;
73 void CompositableClient::Destroy() {
74 auto l = mTextureClientRecycler.Lock();
75 if (*l) {
76 (*l)->Destroy();
79 if (mHandle) {
80 mForwarder->ReleaseCompositable(mHandle);
81 mHandle = CompositableHandle();
85 CompositableHandle CompositableClient::GetAsyncHandle() const {
86 if (mIsAsync) {
87 return mHandle;
89 return CompositableHandle();
92 already_AddRefed<TextureClient> CompositableClient::CreateBufferTextureClient(
93 gfx::SurfaceFormat aFormat, gfx::IntSize aSize,
94 gfx::BackendType aMoz2DBackend, TextureFlags aTextureFlags) {
95 return TextureClient::CreateForRawBufferAccess(GetForwarder(), aFormat, aSize,
96 aMoz2DBackend,
97 aTextureFlags | mTextureFlags);
100 already_AddRefed<TextureClient>
101 CompositableClient::CreateTextureClientForDrawing(
102 gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector,
103 TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) {
104 return TextureClient::CreateForDrawing(
105 GetForwarder(), aFormat, aSize, aSelector, aTextureFlags | mTextureFlags,
106 aAllocFlags);
109 already_AddRefed<TextureClient>
110 CompositableClient::CreateTextureClientFromSurface(
111 gfx::SourceSurface* aSurface, BackendSelector aSelector,
112 TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) {
113 return TextureClient::CreateFromSurface(GetForwarder(), aSurface, aSelector,
114 aTextureFlags | mTextureFlags,
115 aAllocFlags);
118 bool CompositableClient::AddTextureClient(TextureClient* aClient) {
119 if (!aClient) {
120 return false;
122 aClient->SetAddedToCompositableClient();
123 return aClient->InitIPDLActor(mForwarder);
126 void CompositableClient::ClearCachedResources() {
127 auto l = mTextureClientRecycler.Lock();
128 if (*l) {
129 (*l)->ShrinkToMinimumSize();
133 void CompositableClient::HandleMemoryPressure() {
134 auto l = mTextureClientRecycler.Lock();
135 if (*l) {
136 (*l)->ShrinkToMinimumSize();
140 void CompositableClient::RemoveTexture(TextureClient* aTexture) {
141 mForwarder->RemoveTextureFromCompositable(this, aTexture);
144 TextureClientRecycleAllocator* CompositableClient::GetTextureClientRecycler() {
145 auto l = mTextureClientRecycler.Lock();
146 if (*l) {
147 return *l;
150 if (!mForwarder || !mForwarder->GetTextureForwarder()) {
151 return nullptr;
154 *l = new layers::TextureClientRecycleAllocator(mForwarder);
155 return *l;
158 void CompositableClient::DumpTextureClient(std::stringstream& aStream,
159 TextureClient* aTexture,
160 TextureDumpMode aCompress) {
161 if (!aTexture) {
162 return;
164 RefPtr<gfx::DataSourceSurface> dSurf = aTexture->GetAsSurface();
165 if (!dSurf) {
166 return;
168 if (aCompress == TextureDumpMode::Compress) {
169 aStream << gfxUtils::GetAsLZ4Base64Str(dSurf).get();
170 } else {
171 aStream << gfxUtils::GetAsDataURI(dSurf).get();
175 AutoRemoveTexture::~AutoRemoveTexture() {
176 if (mCompositable && mTexture && mCompositable->IsConnected()) {
177 mCompositable->RemoveTexture(mTexture);
181 } // namespace layers
182 } // namespace mozilla