gfx/layers/composite/CompositableHost.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "CompositableHost.h"
michael@0 7 #include <map> // for _Rb_tree_iterator, map, etc
michael@0 8 #include <utility> // for pair
michael@0 9 #include "ContentHost.h" // for ContentHostDoubleBuffered, etc
michael@0 10 #include "Effects.h" // for EffectMask, Effect, etc
michael@0 11 #include "ImageHost.h" // for ImageHostBuffered, etc
michael@0 12 #include "TiledContentHost.h" // for TiledContentHost
michael@0 13 #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor
michael@0 14 #include "mozilla/layers/TextureHost.h" // for TextureHost, etc
michael@0 15 #include "nsAutoPtr.h" // for nsRefPtr
michael@0 16 #include "nsDebug.h" // for NS_WARNING
michael@0 17 #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
michael@0 18 #include "gfxPlatform.h" // for gfxPlatform
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21 namespace layers {
michael@0 22
michael@0 23 class Compositor;
michael@0 24
michael@0 25 /**
michael@0 26 * IPDL actor used by CompositableHost to match with its corresponding
michael@0 27 * CompositableClient on the content side.
michael@0 28 *
michael@0 29 * CompositableParent is owned by the IPDL system. It's deletion is triggered
michael@0 30 * by either the CompositableChild's deletion, or by the IPDL communication
michael@0 31 * goind down.
michael@0 32 */
michael@0 33 class CompositableParent : public PCompositableParent
michael@0 34 {
michael@0 35 public:
michael@0 36 CompositableParent(CompositableParentManager* aMgr,
michael@0 37 const TextureInfo& aTextureInfo,
michael@0 38 uint64_t aID = 0)
michael@0 39 {
michael@0 40 MOZ_COUNT_CTOR(CompositableParent);
michael@0 41 mHost = CompositableHost::Create(aTextureInfo);
michael@0 42 mHost->SetAsyncID(aID);
michael@0 43 if (aID) {
michael@0 44 CompositableMap::Set(aID, this);
michael@0 45 }
michael@0 46 }
michael@0 47
michael@0 48 ~CompositableParent()
michael@0 49 {
michael@0 50 MOZ_COUNT_DTOR(CompositableParent);
michael@0 51 CompositableMap::Erase(mHost->GetAsyncID());
michael@0 52 }
michael@0 53
michael@0 54 virtual void ActorDestroy(ActorDestroyReason why) MOZ_OVERRIDE
michael@0 55 {
michael@0 56 if (mHost) {
michael@0 57 mHost->Detach(nullptr, CompositableHost::FORCE_DETACH);
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 RefPtr<CompositableHost> mHost;
michael@0 62 };
michael@0 63
michael@0 64 CompositableHost::CompositableHost(const TextureInfo& aTextureInfo)
michael@0 65 : mTextureInfo(aTextureInfo)
michael@0 66 , mAsyncID(0)
michael@0 67 , mCompositorID(0)
michael@0 68 , mCompositor(nullptr)
michael@0 69 , mLayer(nullptr)
michael@0 70 , mFlashCounter(0)
michael@0 71 , mAttached(false)
michael@0 72 , mKeepAttached(false)
michael@0 73 {
michael@0 74 MOZ_COUNT_CTOR(CompositableHost);
michael@0 75 }
michael@0 76
michael@0 77 CompositableHost::~CompositableHost()
michael@0 78 {
michael@0 79 MOZ_COUNT_DTOR(CompositableHost);
michael@0 80 if (mBackendData) {
michael@0 81 mBackendData->ClearData();
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 PCompositableParent*
michael@0 86 CompositableHost::CreateIPDLActor(CompositableParentManager* aMgr,
michael@0 87 const TextureInfo& aTextureInfo,
michael@0 88 uint64_t aID)
michael@0 89 {
michael@0 90 return new CompositableParent(aMgr, aTextureInfo, aID);
michael@0 91 }
michael@0 92
michael@0 93 bool
michael@0 94 CompositableHost::DestroyIPDLActor(PCompositableParent* aActor)
michael@0 95 {
michael@0 96 delete aActor;
michael@0 97 return true;
michael@0 98 }
michael@0 99
michael@0 100 CompositableHost*
michael@0 101 CompositableHost::FromIPDLActor(PCompositableParent* aActor)
michael@0 102 {
michael@0 103 MOZ_ASSERT(aActor);
michael@0 104 return static_cast<CompositableParent*>(aActor)->mHost;
michael@0 105 }
michael@0 106
michael@0 107 void
michael@0 108 CompositableHost::UseTextureHost(TextureHost* aTexture)
michael@0 109 {
michael@0 110 if (!aTexture) {
michael@0 111 return;
michael@0 112 }
michael@0 113 aTexture->SetCompositor(GetCompositor());
michael@0 114 aTexture->SetCompositableBackendSpecificData(GetCompositableBackendSpecificData());
michael@0 115 }
michael@0 116
michael@0 117 void
michael@0 118 CompositableHost::UseComponentAlphaTextures(TextureHost* aTextureOnBlack,
michael@0 119 TextureHost* aTextureOnWhite)
michael@0 120 {
michael@0 121 MOZ_ASSERT(aTextureOnBlack && aTextureOnWhite);
michael@0 122 aTextureOnBlack->SetCompositor(GetCompositor());
michael@0 123 aTextureOnBlack->SetCompositableBackendSpecificData(GetCompositableBackendSpecificData());
michael@0 124 aTextureOnWhite->SetCompositor(GetCompositor());
michael@0 125 aTextureOnWhite->SetCompositableBackendSpecificData(GetCompositableBackendSpecificData());
michael@0 126 }
michael@0 127
michael@0 128 void
michael@0 129 CompositableHost::RemoveTextureHost(TextureHost* aTexture)
michael@0 130 {
michael@0 131 // Clear strong refrence to CompositableBackendSpecificData
michael@0 132 aTexture->SetCompositableBackendSpecificData(nullptr);
michael@0 133 }
michael@0 134
michael@0 135 void
michael@0 136 CompositableHost::SetCompositor(Compositor* aCompositor)
michael@0 137 {
michael@0 138 mCompositor = aCompositor;
michael@0 139 }
michael@0 140
michael@0 141 bool
michael@0 142 CompositableHost::AddMaskEffect(EffectChain& aEffects,
michael@0 143 const gfx::Matrix4x4& aTransform,
michael@0 144 bool aIs3D)
michael@0 145 {
michael@0 146 RefPtr<TextureSource> source;
michael@0 147 RefPtr<TextureHost> host = GetAsTextureHost();
michael@0 148 if (host && host->Lock()) {
michael@0 149 source = host->GetTextureSources();
michael@0 150 }
michael@0 151
michael@0 152 if (!source) {
michael@0 153 NS_WARNING("Using compositable with no texture host as mask layer");
michael@0 154 return false;
michael@0 155 }
michael@0 156
michael@0 157 RefPtr<EffectMask> effect = new EffectMask(source,
michael@0 158 source->GetSize(),
michael@0 159 aTransform);
michael@0 160 effect->mIs3D = aIs3D;
michael@0 161 aEffects.mSecondaryEffects[EFFECT_MASK] = effect;
michael@0 162 return true;
michael@0 163 }
michael@0 164
michael@0 165 void
michael@0 166 CompositableHost::RemoveMaskEffect()
michael@0 167 {
michael@0 168 RefPtr<TextureHost> host = GetAsTextureHost();
michael@0 169 if (host) {
michael@0 170 host->Unlock();
michael@0 171 }
michael@0 172 }
michael@0 173
michael@0 174 // implemented in TextureHostOGL.cpp
michael@0 175 TemporaryRef<CompositableBackendSpecificData> CreateCompositableBackendSpecificDataOGL();
michael@0 176
michael@0 177 /* static */ TemporaryRef<CompositableHost>
michael@0 178 CompositableHost::Create(const TextureInfo& aTextureInfo)
michael@0 179 {
michael@0 180 RefPtr<CompositableHost> result;
michael@0 181 switch (aTextureInfo.mCompositableType) {
michael@0 182 case BUFFER_BRIDGE:
michael@0 183 NS_ERROR("Cannot create an image bridge compositable this way");
michael@0 184 break;
michael@0 185 case BUFFER_CONTENT_INC:
michael@0 186 result = new ContentHostIncremental(aTextureInfo);
michael@0 187 break;
michael@0 188 case BUFFER_TILED:
michael@0 189 case BUFFER_SIMPLE_TILED:
michael@0 190 result = new TiledContentHost(aTextureInfo);
michael@0 191 break;
michael@0 192 case COMPOSITABLE_IMAGE:
michael@0 193 result = new ImageHost(aTextureInfo);
michael@0 194 break;
michael@0 195 case COMPOSITABLE_CONTENT_SINGLE:
michael@0 196 result = new ContentHostSingleBuffered(aTextureInfo);
michael@0 197 break;
michael@0 198 case COMPOSITABLE_CONTENT_DOUBLE:
michael@0 199 result = new ContentHostDoubleBuffered(aTextureInfo);
michael@0 200 break;
michael@0 201 default:
michael@0 202 NS_ERROR("Unknown CompositableType");
michael@0 203 }
michael@0 204 // We know that Tiled buffers don't use the compositable backend-specific
michael@0 205 // data, so don't bother creating it.
michael@0 206 if (result && aTextureInfo.mCompositableType != BUFFER_TILED) {
michael@0 207 RefPtr<CompositableBackendSpecificData> data = CreateCompositableBackendSpecificDataOGL();
michael@0 208 result->SetCompositableBackendSpecificData(data);
michael@0 209 }
michael@0 210 return result;
michael@0 211 }
michael@0 212
michael@0 213 #ifdef MOZ_DUMP_PAINTING
michael@0 214 void
michael@0 215 CompositableHost::DumpTextureHost(FILE* aFile, TextureHost* aTexture)
michael@0 216 {
michael@0 217 if (!aTexture) {
michael@0 218 return;
michael@0 219 }
michael@0 220 RefPtr<gfx::DataSourceSurface> dSurf = aTexture->GetAsSurface();
michael@0 221 if (!dSurf) {
michael@0 222 return;
michael@0 223 }
michael@0 224 gfxPlatform *platform = gfxPlatform::GetPlatform();
michael@0 225 RefPtr<gfx::DrawTarget> dt = platform->CreateDrawTargetForData(dSurf->GetData(),
michael@0 226 dSurf->GetSize(),
michael@0 227 dSurf->Stride(),
michael@0 228 dSurf->GetFormat());
michael@0 229 nsRefPtr<gfxASurface> surf = platform->GetThebesSurfaceForDrawTarget(dt);
michael@0 230 if (!surf) {
michael@0 231 return;
michael@0 232 }
michael@0 233 surf->DumpAsDataURL(aFile ? aFile : stderr);
michael@0 234 }
michael@0 235 #endif
michael@0 236
michael@0 237 namespace CompositableMap {
michael@0 238
michael@0 239 typedef std::map<uint64_t, PCompositableParent*> CompositableMap_t;
michael@0 240 static CompositableMap_t* sCompositableMap = nullptr;
michael@0 241 bool IsCreated() {
michael@0 242 return sCompositableMap != nullptr;
michael@0 243 }
michael@0 244 PCompositableParent* Get(uint64_t aID)
michael@0 245 {
michael@0 246 if (!IsCreated() || aID == 0) {
michael@0 247 return nullptr;
michael@0 248 }
michael@0 249 CompositableMap_t::iterator it = sCompositableMap->find(aID);
michael@0 250 if (it == sCompositableMap->end()) {
michael@0 251 return nullptr;
michael@0 252 }
michael@0 253 return it->second;
michael@0 254 }
michael@0 255 void Set(uint64_t aID, PCompositableParent* aParent)
michael@0 256 {
michael@0 257 if (!IsCreated() || aID == 0) {
michael@0 258 return;
michael@0 259 }
michael@0 260 (*sCompositableMap)[aID] = aParent;
michael@0 261 }
michael@0 262 void Erase(uint64_t aID)
michael@0 263 {
michael@0 264 if (!IsCreated() || aID == 0) {
michael@0 265 return;
michael@0 266 }
michael@0 267 CompositableMap_t::iterator it = sCompositableMap->find(aID);
michael@0 268 if (it != sCompositableMap->end()) {
michael@0 269 sCompositableMap->erase(it);
michael@0 270 }
michael@0 271 }
michael@0 272 void Clear()
michael@0 273 {
michael@0 274 if (!IsCreated()) {
michael@0 275 return;
michael@0 276 }
michael@0 277 sCompositableMap->clear();
michael@0 278 }
michael@0 279 void Create()
michael@0 280 {
michael@0 281 if (sCompositableMap == nullptr) {
michael@0 282 sCompositableMap = new CompositableMap_t;
michael@0 283 }
michael@0 284 }
michael@0 285 void Destroy()
michael@0 286 {
michael@0 287 Clear();
michael@0 288 delete sCompositableMap;
michael@0 289 sCompositableMap = nullptr;
michael@0 290 }
michael@0 291
michael@0 292 } // namespace CompositableMap
michael@0 293
michael@0 294 } // namespace layers
michael@0 295 } // namespace mozilla

mercurial