summaryrefslogtreecommitdiffstats
path: root/gfx/layers/composite/CompositableHost.cpp
blob: 056f26de04a21e4b305590089ab588e9f6cf587b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "CompositableHost.h"
#include <map>            // for _Rb_tree_iterator, map, etc
#include <utility>        // for pair
#include "ContentHost.h"  // for ContentHostDoubleBuffered, etc
#include "Effects.h"      // for EffectMask, Effect, etc
#include "gfxUtils.h"
#include "ImageHost.h"  // for ImageHostBuffered, etc
#include "Layers.h"
#include "TiledContentHost.h"  // for TiledContentHost
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor
#include "mozilla/layers/TextureHost.h"     // for TextureHost, etc
#include "mozilla/layers/WebRenderImageHost.h"
#include "mozilla/RefPtr.h"   // for nsRefPtr
#include "nsDebug.h"          // for NS_WARNING
#include "nsISupportsImpl.h"  // for MOZ_COUNT_CTOR, etc
#include "gfxPlatform.h"      // for gfxPlatform
#include "IPDLActor.h"

namespace mozilla {

using namespace gfx;

namespace layers {

class Compositor;

CompositableHost::CompositableHost(const TextureInfo& aTextureInfo)
    : mTextureInfo(aTextureInfo),
      mCompositorBridgeID(0),
      mLayer(nullptr),
      mFlashCounter(0),
      mAttached(false),
      mKeepAttached(false) {
  MOZ_COUNT_CTOR(CompositableHost);
}

CompositableHost::~CompositableHost() { MOZ_COUNT_DTOR(CompositableHost); }

void CompositableHost::UseTextureHost(const nsTArray<TimedTexture>& aTextures) {
  if (mTextureSourceProvider) {
    for (auto& texture : aTextures) {
      texture.mTexture->SetTextureSourceProvider(mTextureSourceProvider);
    }
  }
}

void CompositableHost::UseComponentAlphaTextures(TextureHost* aTextureOnBlack,
                                                 TextureHost* aTextureOnWhite) {
  MOZ_ASSERT(aTextureOnBlack && aTextureOnWhite);
  if (mTextureSourceProvider) {
    aTextureOnBlack->SetTextureSourceProvider(mTextureSourceProvider);
    aTextureOnWhite->SetTextureSourceProvider(mTextureSourceProvider);
  }
}

void CompositableHost::RemoveTextureHost(TextureHost* aTexture) {}

void CompositableHost::SetTextureSourceProvider(
    TextureSourceProvider* aProvider) {
  MOZ_ASSERT(aProvider);
  mTextureSourceProvider = aProvider;
}

bool CompositableHost::AddMaskEffect(EffectChain& aEffects,
                                     const gfx::Matrix4x4& aTransform) {
  CompositableTextureSourceRef source;
  RefPtr<TextureHost> host = GetAsTextureHost();

  if (!host) {
    NS_WARNING("Using compositable with no valid TextureHost as mask");
    return false;
  }

  if (!host->Lock()) {
    NS_WARNING("Failed to lock the mask texture");
    return false;
  }

  if (!host->BindTextureSource(source)) {
    NS_WARNING(
        "The TextureHost was successfully locked but can't provide a "
        "TextureSource");
    host->Unlock();
    return false;
  }
  MOZ_ASSERT(source);

  RefPtr<EffectMask> effect =
      new EffectMask(source, source->GetSize(), aTransform);
  aEffects.mSecondaryEffects[EffectTypes::MASK] = effect;
  return true;
}

void CompositableHost::RemoveMaskEffect() {
  RefPtr<TextureHost> host = GetAsTextureHost();
  if (host) {
    host->Unlock();
  }
}

/* static */
already_AddRefed<CompositableHost> CompositableHost::Create(
    const TextureInfo& aTextureInfo, bool aUseWebRender) {
  RefPtr<CompositableHost> result;
  switch (aTextureInfo.mCompositableType) {
    case CompositableType::IMAGE_BRIDGE:
      NS_ERROR("Cannot create an image bridge compositable this way");
      break;
    case CompositableType::CONTENT_TILED:
      result = new TiledContentHost(aTextureInfo);
      break;
    case CompositableType::IMAGE:
      if (aUseWebRender) {
        result = new WebRenderImageHost(aTextureInfo);
      } else {
        result = new ImageHost(aTextureInfo);
      }
      break;
    case CompositableType::CONTENT_SINGLE:
      if (aUseWebRender) {
        result = new WebRenderImageHost(aTextureInfo);
      } else {
        result = new ContentHostSingleBuffered(aTextureInfo);
      }
      break;
    case CompositableType::CONTENT_DOUBLE:
      MOZ_ASSERT(!aUseWebRender);
      result = new ContentHostDoubleBuffered(aTextureInfo);
      break;
    default:
      NS_ERROR("Unknown CompositableType");
  }
  return result.forget();
}

void CompositableHost::DumpTextureHost(std::stringstream& aStream,
                                       TextureHost* aTexture) {
  if (!aTexture) {
    return;
  }
  RefPtr<gfx::DataSourceSurface> dSurf = aTexture->GetAsSurface();
  if (!dSurf) {
    return;
  }
  aStream << gfxUtils::GetAsDataURI(dSurf).get();
}

HostLayerManager* CompositableHost::GetLayerManager() const {
  if (!mLayer || !mLayer->Manager()) {
    return nullptr;
  }
  return mLayer->Manager()->AsHostLayerManager();
}

TextureSourceProvider* CompositableHost::GetTextureSourceProvider() const {
  return mTextureSourceProvider;
}

}  // namespace layers
}  // namespace mozilla