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 #include "CompositableHost.h"
8 #include <map>            // for _Rb_tree_iterator, map, etc
9 #include <utility>        // for pair
10 #include "ContentHost.h"  // for ContentHostDoubleBuffered, etc
11 #include "Effects.h"      // for EffectMask, Effect, etc
12 #include "gfxUtils.h"
13 #include "ImageHost.h"         // for ImageHostBuffered, etc
14 #include "TiledContentHost.h"  // for TiledContentHost
15 #include "mozilla/gfx/gfxVars.h"
16 #include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor
17 #include "mozilla/layers/TextureHost.h"     // for TextureHost, etc
18 #include "mozilla/layers/WebRenderImageHost.h"
19 #include "mozilla/RefPtr.h"   // for nsRefPtr
20 #include "nsDebug.h"          // for NS_WARNING
21 #include "nsISupportsImpl.h"  // for MOZ_COUNT_CTOR, etc
22 #include "gfxPlatform.h"      // for gfxPlatform
23 #include "IPDLActor.h"
24 
25 namespace mozilla {
26 
27 using namespace gfx;
28 
29 namespace layers {
30 
31 class Compositor;
32 
CompositableHost(const TextureInfo & aTextureInfo)33 CompositableHost::CompositableHost(const TextureInfo& aTextureInfo)
34     : mTextureInfo(aTextureInfo),
35       mCompositorBridgeID(0),
36       mLayer(nullptr),
37       mFlashCounter(0),
38       mAttached(false),
39       mKeepAttached(false) {
40   MOZ_COUNT_CTOR(CompositableHost);
41 }
42 
~CompositableHost()43 CompositableHost::~CompositableHost() { MOZ_COUNT_DTOR(CompositableHost); }
44 
UseTextureHost(const nsTArray<TimedTexture> & aTextures)45 void CompositableHost::UseTextureHost(const nsTArray<TimedTexture>& aTextures) {
46   if (mTextureSourceProvider) {
47     for (auto& texture : aTextures) {
48       texture.mTexture->SetTextureSourceProvider(mTextureSourceProvider);
49     }
50   }
51 }
52 
UseComponentAlphaTextures(TextureHost * aTextureOnBlack,TextureHost * aTextureOnWhite)53 void CompositableHost::UseComponentAlphaTextures(TextureHost* aTextureOnBlack,
54                                                  TextureHost* aTextureOnWhite) {
55   MOZ_ASSERT(aTextureOnBlack && aTextureOnWhite);
56   if (mTextureSourceProvider) {
57     aTextureOnBlack->SetTextureSourceProvider(mTextureSourceProvider);
58     aTextureOnWhite->SetTextureSourceProvider(mTextureSourceProvider);
59   }
60 }
61 
RemoveTextureHost(TextureHost * aTexture)62 void CompositableHost::RemoveTextureHost(TextureHost* aTexture) {}
63 
SetTextureSourceProvider(TextureSourceProvider * aProvider)64 void CompositableHost::SetTextureSourceProvider(
65     TextureSourceProvider* aProvider) {
66   MOZ_ASSERT(aProvider);
67   mTextureSourceProvider = aProvider;
68 }
69 
AddMaskEffect(EffectChain & aEffects,const gfx::Matrix4x4 & aTransform)70 bool CompositableHost::AddMaskEffect(EffectChain& aEffects,
71                                      const gfx::Matrix4x4& aTransform) {
72   CompositableTextureSourceRef source;
73   RefPtr<TextureHost> host = GetAsTextureHost();
74 
75   if (!host) {
76     NS_WARNING("Using compositable with no valid TextureHost as mask");
77     return false;
78   }
79 
80   if (!host->Lock()) {
81     NS_WARNING("Failed to lock the mask texture");
82     return false;
83   }
84 
85   if (!host->BindTextureSource(source)) {
86     NS_WARNING(
87         "The TextureHost was successfully locked but can't provide a "
88         "TextureSource");
89     host->Unlock();
90     return false;
91   }
92   MOZ_ASSERT(source);
93 
94   RefPtr<EffectMask> effect =
95       new EffectMask(source, source->GetSize(), aTransform);
96   aEffects.mSecondaryEffects[EffectTypes::MASK] = effect;
97   return true;
98 }
99 
RemoveMaskEffect()100 void CompositableHost::RemoveMaskEffect() {
101   RefPtr<TextureHost> host = GetAsTextureHost();
102   if (host) {
103     host->Unlock();
104   }
105 }
106 
Create(const TextureInfo & aTextureInfo,bool aUseWebRender)107 /* static */ already_AddRefed<CompositableHost> CompositableHost::Create(
108     const TextureInfo& aTextureInfo, bool aUseWebRender) {
109   RefPtr<CompositableHost> result;
110   switch (aTextureInfo.mCompositableType) {
111     case CompositableType::IMAGE_BRIDGE:
112       NS_ERROR("Cannot create an image bridge compositable this way");
113       break;
114     case CompositableType::CONTENT_TILED:
115       result = new TiledContentHost(aTextureInfo);
116       break;
117     case CompositableType::IMAGE:
118       if (aUseWebRender) {
119         result = new WebRenderImageHost(aTextureInfo);
120       } else {
121         result = new ImageHost(aTextureInfo);
122       }
123       break;
124     case CompositableType::CONTENT_SINGLE:
125       if (aUseWebRender) {
126         result = new WebRenderImageHost(aTextureInfo);
127       } else {
128         result = new ContentHostSingleBuffered(aTextureInfo);
129       }
130       break;
131     case CompositableType::CONTENT_DOUBLE:
132       MOZ_ASSERT(!aUseWebRender);
133       result = new ContentHostDoubleBuffered(aTextureInfo);
134       break;
135     default:
136       NS_ERROR("Unknown CompositableType");
137   }
138   return result.forget();
139 }
140 
DumpTextureHost(std::stringstream & aStream,TextureHost * aTexture)141 void CompositableHost::DumpTextureHost(std::stringstream& aStream,
142                                        TextureHost* aTexture) {
143   if (!aTexture) {
144     return;
145   }
146   RefPtr<gfx::DataSourceSurface> dSurf = aTexture->GetAsSurface();
147   if (!dSurf) {
148     return;
149   }
150   aStream << gfxUtils::GetAsDataURI(dSurf).get();
151 }
152 
GetLayerManager() const153 HostLayerManager* CompositableHost::GetLayerManager() const {
154   if (!mLayer || !mLayer->Manager()) {
155     return nullptr;
156   }
157   return mLayer->Manager()->AsHostLayerManager();
158 }
159 
GetTextureSourceProvider() const160 TextureSourceProvider* CompositableHost::GetTextureSourceProvider() const {
161   return mTextureSourceProvider;
162 }
163 
164 }  // namespace layers
165 }  // namespace mozilla
166