summaryrefslogtreecommitdiffstats
path: root/gfx/layers/composite/RemoteTextureHostWrapper.cpp
blob: b44f3bd36cc733d8858070c41d9c02a2cf61dab9 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* -*- 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 !!mRemoteTextureForDisplayList;
}

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

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

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

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

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

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

  if (mIsSyncMode) {
    // sync mode
    // mRemoteTextureForDisplayList is also used for WebRender rendering.
    auto wrappedId = mRemoteTextureForDisplayList->mExternalImageId.ref();
    RefPtr<wr::RenderTextureHost> texture =
        new wr::RenderTextureHostWrapper(wrappedId);
    wr::RenderThread::Get()->RegisterExternalImage(mExternalImageId.ref(),
                                                   texture.forget());
  } else {
    // async mode
    // mRemoteTextureForDisplayList could be previous remote texture's
    // TextureHost that is compatible to the mTextureId's TextureHost.
    // mRemoteTextureForDisplayList might not be used WebRender rendering.
    RefPtr<wr::RenderTextureHost> texture =
        new wr::RenderTextureHostWrapper(mTextureId, mOwnerId, mForPid);
    wr::RenderThread::Get()->RegisterExternalImage(mExternalImageId.ref(),
                                                   texture.forget());
  }
}

void RemoteTextureHostWrapper::MaybeDestroyRenderTexture() {
  if (mExternalImageId.isNothing() || !mRemoteTextureForDisplayList) {
    // RenderTextureHost was not created
    return;
  }
  TextureHost::DestroyRenderTexture(mExternalImageId.ref());
}

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

void RemoteTextureHostWrapper::PushResourceUpdates(
    wr::TransactionBuilder& aResources, ResourceUpdateOp aOp,
    const Range<wr::ImageKey>& aImageKeys, const wr::ExternalImageId& aExtID) {
  MOZ_ASSERT(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
  if (!mRemoteTextureForDisplayList) {
    return;
  }
  mRemoteTextureForDisplayList->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(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
  MOZ_ASSERT(aImageKeys.length() > 0);
  if (!mRemoteTextureForDisplayList) {
    return;
  }
  mRemoteTextureForDisplayList->PushDisplayItems(aBuilder, aBounds, aClip,
                                                 aFilter, aImageKeys, aFlags);
}

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

void RemoteTextureHostWrapper::UnbindTextureSource() {}

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

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

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

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

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

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

void RemoteTextureHostWrapper::SetRemoteTextureHostForDisplayList(
    const MonitorAutoLock& aProofOfLock, TextureHost* aTextureHost,
    bool aIsSyncMode) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
  mRemoteTextureForDisplayList = aTextureHost;
  mIsSyncMode = aIsSyncMode;
}

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

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

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

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

}  // namespace mozilla::layers