summaryrefslogtreecommitdiffstats
path: root/gfx/layers/composite/RemoteTextureHostWrapper.cpp
blob: 6e5c946bb57e9f97ab7ea023808a3660fca3a103 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -*- 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 "RemoteTextureHostWrapper.h"

#include "mozilla/gfx/gfxVars.h"
#include "mozilla/layers/AsyncImagePipelineManager.h"
#include "mozilla/layers/CompositorThread.h"
#include "mozilla/layers/RemoteTextureMap.h"
#include "mozilla/layers/WebRenderTextureHost.h"
#include "mozilla/StaticPrefs_webgl.h"
#include "mozilla/webrender/RenderTextureHostWrapper.h"
#include "mozilla/webrender/RenderThread.h"

namespace mozilla::layers {

/* static */
RefPtr<TextureHost> RemoteTextureHostWrapper::Create(
    const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
    const base::ProcessId aForPid, const gfx::IntSize aSize,
    const TextureFlags aFlags) {
  RefPtr<TextureHost> textureHost =
      new RemoteTextureHostWrapper(aTextureId, aOwnerId, aForPid, aSize,
                                   aFlags | TextureFlags::REMOTE_TEXTURE);
  auto externalImageId = AsyncImagePipelineManager::GetNextExternalImageId();
  textureHost = new WebRenderTextureHost(aFlags, textureHost, externalImageId);
  return textureHost;
}

RemoteTextureHostWrapper::RemoteTextureHostWrapper(
    const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
    const base::ProcessId aForPid, const gfx::IntSize aSize,
    const TextureFlags aFlags)
    : TextureHost(TextureHostType::Unknown, aFlags),
      mTextureId(aTextureId),
      mOwnerId(aOwnerId),
      mForPid(aForPid),
      mSize(aSize) {
  MOZ_COUNT_CTOR(RemoteTextureHostWrapper);
}

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

bool RemoteTextureHostWrapper::IsValid() { return !!mRemoteTexture; }

gfx::YUVColorSpace RemoteTextureHostWrapper::GetYUVColorSpace() const {
  MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet");
  if (!mRemoteTexture) {
    return TextureHost::GetYUVColorSpace();
  }
  return mRemoteTexture->GetYUVColorSpace();
}

gfx::ColorDepth RemoteTextureHostWrapper::GetColorDepth() const {
  MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet");
  if (!mRemoteTexture) {
    return TextureHost::GetColorDepth();
  }
  return mRemoteTexture->GetColorDepth();
}

gfx::ColorRange RemoteTextureHostWrapper::GetColorRange() const {
  MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet");
  if (!mRemoteTexture) {
    return TextureHost::GetColorRange();
  }
  return mRemoteTexture->GetColorRange();
}

gfx::IntSize RemoteTextureHostWrapper::GetSize() const {
  MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet");
  if (!mRemoteTexture) {
    return gfx::IntSize();
  }
  return mRemoteTexture->GetSize();
}

gfx::SurfaceFormat RemoteTextureHostWrapper::GetFormat() const {
  MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet");
  if (!mRemoteTexture) {
    return gfx::SurfaceFormat::UNKNOWN;
  }
  return mRemoteTexture->GetFormat();
}

void RemoteTextureHostWrapper::CreateRenderTexture(
    const wr::ExternalImageId& aExternalImageId) {
  MOZ_ASSERT(mExternalImageId.isSome());

  MaybeCreateRenderTexture();
}

void RemoteTextureHostWrapper::MaybeCreateRenderTexture() {
  if (!mRemoteTexture) {
    return;
  }
  MOZ_ASSERT(mRemoteTexture->mExternalImageId.isSome());

  // mRemoteTexture is also used for WebRender rendering.
  auto wrappedId = mRemoteTexture->mExternalImageId.ref();
  RefPtr<wr::RenderTextureHost> texture =
      new wr::RenderTextureHostWrapper(wrappedId);
  wr::RenderThread::Get()->RegisterExternalImage(mExternalImageId.ref(),
                                                 texture.forget());
  mRenderTextureCreated = true;
}

uint32_t RemoteTextureHostWrapper::NumSubTextures() {
  if (!mRemoteTexture) {
    return 0;
  }
  return mRemoteTexture->NumSubTextures();
}

void RemoteTextureHostWrapper::PushResourceUpdates(
    wr::TransactionBuilder& aResources, ResourceUpdateOp aOp,
    const Range<wr::ImageKey>& aImageKeys, const wr::ExternalImageId& aExtID) {
  MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet");
  if (!mRemoteTexture) {
    return;
  }
  mRemoteTexture->PushResourceUpdates(aResources, aOp, aImageKeys, aExtID);
}

void RemoteTextureHostWrapper::PushDisplayItems(
    wr::DisplayListBuilder& aBuilder, const wr::LayoutRect& aBounds,
    const wr::LayoutRect& aClip, wr::ImageRendering aFilter,
    const Range<wr::ImageKey>& aImageKeys, PushDisplayItemFlagSet aFlags) {
  MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet");
  MOZ_ASSERT(aImageKeys.length() > 0);
  if (!mRemoteTexture) {
    return;
  }
  mRemoteTexture->PushDisplayItems(aBuilder, aBounds, aClip, aFilter,
                                   aImageKeys, aFlags);
}

bool RemoteTextureHostWrapper::SupportsExternalCompositing(
    WebRenderBackend aBackend) {
  if (!mRemoteTexture) {
    return false;
  }
  return mRemoteTexture->SupportsExternalCompositing(aBackend);
}

void RemoteTextureHostWrapper::UnbindTextureSource() {}

void RemoteTextureHostWrapper::NotifyNotUsed() {
  if (mRemoteTexture) {
    // Release mRemoteTexture.
    RemoteTextureMap::Get()->ReleaseRemoteTextureHost(this);
  }
  MOZ_ASSERT(!mRemoteTexture);

  RemoteTextureMap::Get()->UnregisterRemoteTextureHostWrapper(
      mTextureId, mOwnerId, mForPid);
}

TextureHostType RemoteTextureHostWrapper::GetTextureHostType() {
  if (!mRemoteTexture) {
    return TextureHostType::Unknown;
  }
  return mRemoteTexture->GetTextureHostType();
}

bool RemoteTextureHostWrapper::IsReadyForRendering() {
  return !!mRemoteTexture;
}

void RemoteTextureHostWrapper::ApplyTextureFlagsToRemoteTexture() {
  if (!mRemoteTexture) {
    return;
  }
  mRemoteTexture->SetFlags(mFlags | TextureFlags::DEALLOCATE_CLIENT);
}

TextureHost* RemoteTextureHostWrapper::GetRemoteTextureHost(
    const MonitorAutoLock& aProofOfLock) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
  return mRemoteTexture;
}

void RemoteTextureHostWrapper::SetRemoteTextureHost(
    const MonitorAutoLock& aProofOfLock, TextureHost* aTextureHost) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
  mRemoteTexture = aTextureHost;

  if (mExternalImageId.isSome() && !mRenderTextureCreated) {
    MaybeCreateRenderTexture();
  }
}

void RemoteTextureHostWrapper::ClearRemoteTextureHost(
    const MonitorAutoLock& aProofOfLoc) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
  mRemoteTexture = nullptr;
}

bool RemoteTextureHostWrapper::IsWrappingSurfaceTextureHost() {
  if (!mRemoteTexture) {
    return false;
  }
  return mRemoteTexture->IsWrappingSurfaceTextureHost();
}

bool RemoteTextureHostWrapper::NeedsDeferredDeletion() const {
  if (!mRemoteTexture) {
    return true;
  }
  return mRemoteTexture->NeedsDeferredDeletion();
}

AndroidHardwareBuffer* RemoteTextureHostWrapper::GetAndroidHardwareBuffer()
    const {
  if (!mRemoteTexture) {
    return nullptr;
  }
  return mRemoteTexture->GetAndroidHardwareBuffer();
}

}  // namespace mozilla::layers