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
19 # include "gfxWindowsPlatform.h" // for gfxWindowsPlatform
20 # include "mozilla/layers/TextureD3D11.h"
22 #include "IPDLActor.h"
28 using namespace mozilla::gfx
;
30 void CompositableClient::InitIPDL(const CompositableHandle
& aHandle
) {
33 mForwarder
->AssertInForwarderThread();
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"),
50 CompositableClient::~CompositableClient() { Destroy(); }
52 LayersBackend
CompositableClient::GetCompositorBackendType() const {
53 return mForwarder
->GetCompositorBackendType();
56 bool CompositableClient::Connect(ImageContainer
* aImageContainer
) {
58 if (!GetForwarder() || mHandle
) {
62 GetForwarder()->AssertInForwarderThread();
63 GetForwarder()->Connect(this, aImageContainer
);
67 bool CompositableClient::IsConnected() const {
68 // CanSend() is only reliable in the same thread as the IPDL channel.
69 mForwarder
->AssertInForwarderThread();
73 void CompositableClient::Destroy() {
74 auto l
= mTextureClientRecycler
.Lock();
80 mForwarder
->ReleaseCompositable(mHandle
);
81 mHandle
= CompositableHandle();
85 CompositableHandle
CompositableClient::GetAsyncHandle() const {
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
,
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
,
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
,
118 bool CompositableClient::AddTextureClient(TextureClient
* aClient
) {
122 aClient
->SetAddedToCompositableClient();
123 return aClient
->InitIPDLActor(mForwarder
);
126 void CompositableClient::ClearCachedResources() {
127 auto l
= mTextureClientRecycler
.Lock();
129 (*l
)->ShrinkToMinimumSize();
133 void CompositableClient::HandleMemoryPressure() {
134 auto l
= mTextureClientRecycler
.Lock();
136 (*l
)->ShrinkToMinimumSize();
140 void CompositableClient::RemoveTexture(TextureClient
* aTexture
) {
141 mForwarder
->RemoveTextureFromCompositable(this, aTexture
);
144 TextureClientRecycleAllocator
* CompositableClient::GetTextureClientRecycler() {
145 auto l
= mTextureClientRecycler
.Lock();
150 if (!mForwarder
|| !mForwarder
->GetTextureForwarder()) {
154 *l
= new layers::TextureClientRecycleAllocator(mForwarder
);
158 void CompositableClient::DumpTextureClient(std::stringstream
& aStream
,
159 TextureClient
* aTexture
,
160 TextureDumpMode aCompress
) {
164 RefPtr
<gfx::DataSourceSurface
> dSurf
= aTexture
->GetAsSurface();
168 if (aCompress
== TextureDumpMode::Compress
) {
169 aStream
<< gfxUtils::GetAsLZ4Base64Str(dSurf
).get();
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