Bug 1849470 - Update in-tree zlib to version 1.3. r=aosmond
[gecko.git] / gfx / layers / composite / RemoteTextureHostWrapper.cpp
blobb44f3bd36cc733d8858070c41d9c02a2cf61dab9
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/. */
7 #include "RemoteTextureHostWrapper.h"
9 #include "mozilla/gfx/gfxVars.h"
10 #include "mozilla/layers/AsyncImagePipelineManager.h"
11 #include "mozilla/layers/CompositorThread.h"
12 #include "mozilla/layers/RemoteTextureMap.h"
13 #include "mozilla/layers/WebRenderTextureHost.h"
14 #include "mozilla/StaticPrefs_webgl.h"
15 #include "mozilla/webrender/RenderTextureHostWrapper.h"
16 #include "mozilla/webrender/RenderThread.h"
18 namespace mozilla::layers {
20 /* static */
21 RefPtr<TextureHost> RemoteTextureHostWrapper::Create(
22 const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
23 const base::ProcessId aForPid, const gfx::IntSize aSize,
24 const TextureFlags aFlags) {
25 RefPtr<TextureHost> textureHost =
26 new RemoteTextureHostWrapper(aTextureId, aOwnerId, aForPid, aSize,
27 aFlags | TextureFlags::REMOTE_TEXTURE);
28 auto externalImageId = AsyncImagePipelineManager::GetNextExternalImageId();
29 textureHost = new WebRenderTextureHost(aFlags, textureHost, externalImageId);
30 return textureHost;
33 RemoteTextureHostWrapper::RemoteTextureHostWrapper(
34 const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
35 const base::ProcessId aForPid, const gfx::IntSize aSize,
36 const TextureFlags aFlags)
37 : TextureHost(TextureHostType::Unknown, aFlags),
38 mTextureId(aTextureId),
39 mOwnerId(aOwnerId),
40 mForPid(aForPid),
41 mSize(aSize) {
42 MOZ_COUNT_CTOR(RemoteTextureHostWrapper);
45 RemoteTextureHostWrapper::~RemoteTextureHostWrapper() {
46 MOZ_COUNT_DTOR(RemoteTextureHostWrapper);
49 bool RemoteTextureHostWrapper::IsValid() {
50 return !!mRemoteTextureForDisplayList;
53 gfx::YUVColorSpace RemoteTextureHostWrapper::GetYUVColorSpace() const {
54 MOZ_ASSERT(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
55 if (!mRemoteTextureForDisplayList) {
56 return TextureHost::GetYUVColorSpace();
58 return mRemoteTextureForDisplayList->GetYUVColorSpace();
61 gfx::ColorDepth RemoteTextureHostWrapper::GetColorDepth() const {
62 MOZ_ASSERT(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
63 if (!mRemoteTextureForDisplayList) {
64 return TextureHost::GetColorDepth();
66 return mRemoteTextureForDisplayList->GetColorDepth();
69 gfx::ColorRange RemoteTextureHostWrapper::GetColorRange() const {
70 MOZ_ASSERT(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
71 if (!mRemoteTextureForDisplayList) {
72 return TextureHost::GetColorRange();
74 return mRemoteTextureForDisplayList->GetColorRange();
77 gfx::IntSize RemoteTextureHostWrapper::GetSize() const {
78 MOZ_ASSERT(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
79 if (!mRemoteTextureForDisplayList) {
80 return gfx::IntSize();
82 return mRemoteTextureForDisplayList->GetSize();
85 gfx::SurfaceFormat RemoteTextureHostWrapper::GetFormat() const {
86 MOZ_ASSERT(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
87 if (!mRemoteTextureForDisplayList) {
88 return gfx::SurfaceFormat::UNKNOWN;
90 return mRemoteTextureForDisplayList->GetFormat();
93 void RemoteTextureHostWrapper::CreateRenderTexture(
94 const wr::ExternalImageId& aExternalImageId) {
95 MOZ_ASSERT(mExternalImageId.isSome());
96 MOZ_ASSERT(mRemoteTextureForDisplayList);
97 MOZ_ASSERT(mRemoteTextureForDisplayList->mExternalImageId.isSome());
99 if (mIsSyncMode) {
100 // sync mode
101 // mRemoteTextureForDisplayList is also used for WebRender rendering.
102 auto wrappedId = mRemoteTextureForDisplayList->mExternalImageId.ref();
103 RefPtr<wr::RenderTextureHost> texture =
104 new wr::RenderTextureHostWrapper(wrappedId);
105 wr::RenderThread::Get()->RegisterExternalImage(mExternalImageId.ref(),
106 texture.forget());
107 } else {
108 // async mode
109 // mRemoteTextureForDisplayList could be previous remote texture's
110 // TextureHost that is compatible to the mTextureId's TextureHost.
111 // mRemoteTextureForDisplayList might not be used WebRender rendering.
112 RefPtr<wr::RenderTextureHost> texture =
113 new wr::RenderTextureHostWrapper(mTextureId, mOwnerId, mForPid);
114 wr::RenderThread::Get()->RegisterExternalImage(mExternalImageId.ref(),
115 texture.forget());
119 void RemoteTextureHostWrapper::MaybeDestroyRenderTexture() {
120 if (mExternalImageId.isNothing() || !mRemoteTextureForDisplayList) {
121 // RenderTextureHost was not created
122 return;
124 TextureHost::DestroyRenderTexture(mExternalImageId.ref());
127 uint32_t RemoteTextureHostWrapper::NumSubTextures() {
128 if (!mRemoteTextureForDisplayList) {
129 return 0;
131 return mRemoteTextureForDisplayList->NumSubTextures();
134 void RemoteTextureHostWrapper::PushResourceUpdates(
135 wr::TransactionBuilder& aResources, ResourceUpdateOp aOp,
136 const Range<wr::ImageKey>& aImageKeys, const wr::ExternalImageId& aExtID) {
137 MOZ_ASSERT(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
138 if (!mRemoteTextureForDisplayList) {
139 return;
141 mRemoteTextureForDisplayList->PushResourceUpdates(aResources, aOp, aImageKeys,
142 aExtID);
145 void RemoteTextureHostWrapper::PushDisplayItems(
146 wr::DisplayListBuilder& aBuilder, const wr::LayoutRect& aBounds,
147 const wr::LayoutRect& aClip, wr::ImageRendering aFilter,
148 const Range<wr::ImageKey>& aImageKeys, PushDisplayItemFlagSet aFlags) {
149 MOZ_ASSERT(mRemoteTextureForDisplayList, "TextureHost isn't valid yet");
150 MOZ_ASSERT(aImageKeys.length() > 0);
151 if (!mRemoteTextureForDisplayList) {
152 return;
154 mRemoteTextureForDisplayList->PushDisplayItems(aBuilder, aBounds, aClip,
155 aFilter, aImageKeys, aFlags);
158 bool RemoteTextureHostWrapper::SupportsExternalCompositing(
159 WebRenderBackend aBackend) {
160 if (!mRemoteTextureForDisplayList) {
161 return false;
163 return mRemoteTextureForDisplayList->SupportsExternalCompositing(aBackend);
166 void RemoteTextureHostWrapper::UnbindTextureSource() {}
168 void RemoteTextureHostWrapper::NotifyNotUsed() {
169 if (mRemoteTextureForDisplayList) {
170 // Release mRemoteTextureForDisplayList.
171 RemoteTextureMap::Get()->ReleaseRemoteTextureHostForDisplayList(this);
173 MOZ_ASSERT(!mRemoteTextureForDisplayList);
175 RemoteTextureMap::Get()->UnregisterRemoteTextureHostWrapper(
176 mTextureId, mOwnerId, mForPid);
179 TextureHostType RemoteTextureHostWrapper::GetTextureHostType() {
180 if (!mRemoteTextureForDisplayList) {
181 return TextureHostType::Unknown;
183 return mRemoteTextureForDisplayList->GetTextureHostType();
186 bool RemoteTextureHostWrapper::IsReadyForRendering() {
187 return !!mRemoteTextureForDisplayList;
190 void RemoteTextureHostWrapper::ApplyTextureFlagsToRemoteTexture() {
191 if (!mRemoteTextureForDisplayList) {
192 return;
194 mRemoteTextureForDisplayList->SetFlags(mFlags |
195 TextureFlags::DEALLOCATE_CLIENT);
198 TextureHost* RemoteTextureHostWrapper::GetRemoteTextureHostForDisplayList(
199 const MonitorAutoLock& aProofOfLock) {
200 MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
201 return mRemoteTextureForDisplayList;
204 void RemoteTextureHostWrapper::SetRemoteTextureHostForDisplayList(
205 const MonitorAutoLock& aProofOfLock, TextureHost* aTextureHost,
206 bool aIsSyncMode) {
207 MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
208 mRemoteTextureForDisplayList = aTextureHost;
209 mIsSyncMode = aIsSyncMode;
212 void RemoteTextureHostWrapper::ClearRemoteTextureHostForDisplayList(
213 const MonitorAutoLock& aProofOfLoc) {
214 MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
215 mRemoteTextureForDisplayList = nullptr;
218 bool RemoteTextureHostWrapper::IsWrappingSurfaceTextureHost() {
219 if (!mRemoteTextureForDisplayList) {
220 return false;
222 return mRemoteTextureForDisplayList->IsWrappingSurfaceTextureHost();
225 bool RemoteTextureHostWrapper::NeedsDeferredDeletion() const {
226 if (!mRemoteTextureForDisplayList) {
227 return true;
229 return mRemoteTextureForDisplayList->NeedsDeferredDeletion();
232 AndroidHardwareBuffer* RemoteTextureHostWrapper::GetAndroidHardwareBuffer()
233 const {
234 if (!mRemoteTextureForDisplayList) {
235 return nullptr;
237 return mRemoteTextureForDisplayList->GetAndroidHardwareBuffer();
240 } // namespace mozilla::layers