RemoteTextureHostWrapper.cpp (7331B)
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/. */ 6 7 #include "RemoteTextureHostWrapper.h" 8 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" 17 18 namespace mozilla::layers { 19 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; 31 } 32 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); 43 } 44 45 RemoteTextureHostWrapper::~RemoteTextureHostWrapper() { 46 MOZ_COUNT_DTOR(RemoteTextureHostWrapper); 47 } 48 49 bool RemoteTextureHostWrapper::IsValid() { return !!mRemoteTexture; } 50 51 gfx::YUVColorSpace RemoteTextureHostWrapper::GetYUVColorSpace() const { 52 MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet"); 53 if (!mRemoteTexture) { 54 return TextureHost::GetYUVColorSpace(); 55 } 56 return mRemoteTexture->GetYUVColorSpace(); 57 } 58 59 gfx::ColorDepth RemoteTextureHostWrapper::GetColorDepth() const { 60 MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet"); 61 if (!mRemoteTexture) { 62 return TextureHost::GetColorDepth(); 63 } 64 return mRemoteTexture->GetColorDepth(); 65 } 66 67 gfx::ColorRange RemoteTextureHostWrapper::GetColorRange() const { 68 MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet"); 69 if (!mRemoteTexture) { 70 return TextureHost::GetColorRange(); 71 } 72 return mRemoteTexture->GetColorRange(); 73 } 74 75 gfx::IntSize RemoteTextureHostWrapper::GetSize() const { 76 MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet"); 77 if (!mRemoteTexture) { 78 return gfx::IntSize(); 79 } 80 return mRemoteTexture->GetSize(); 81 } 82 83 gfx::SurfaceFormat RemoteTextureHostWrapper::GetFormat() const { 84 MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet"); 85 if (!mRemoteTexture) { 86 return gfx::SurfaceFormat::UNKNOWN; 87 } 88 return mRemoteTexture->GetFormat(); 89 } 90 91 void RemoteTextureHostWrapper::CreateRenderTexture( 92 const wr::ExternalImageId& aExternalImageId) { 93 MOZ_ASSERT(mExternalImageId.isSome()); 94 95 MaybeCreateRenderTexture(); 96 } 97 98 void RemoteTextureHostWrapper::MaybeCreateRenderTexture() { 99 if (!mRemoteTexture) { 100 return; 101 } 102 MOZ_ASSERT(mRemoteTexture->mExternalImageId.isSome()); 103 104 // mRemoteTexture is also used for WebRender rendering. 105 auto wrappedId = mRemoteTexture->mExternalImageId.ref(); 106 RefPtr<wr::RenderTextureHost> texture = 107 new wr::RenderTextureHostWrapper(wrappedId); 108 wr::RenderThread::Get()->RegisterExternalImage(mExternalImageId.ref(), 109 texture.forget()); 110 mRenderTextureCreated = true; 111 } 112 113 uint32_t RemoteTextureHostWrapper::NumSubTextures() { 114 if (!mRemoteTexture) { 115 return 0; 116 } 117 return mRemoteTexture->NumSubTextures(); 118 } 119 120 void RemoteTextureHostWrapper::PushResourceUpdates( 121 wr::TransactionBuilder& aResources, ResourceUpdateOp aOp, 122 const Range<wr::ImageKey>& aImageKeys, const wr::ExternalImageId& aExtID) { 123 MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet"); 124 if (!mRemoteTexture) { 125 return; 126 } 127 mRemoteTexture->PushResourceUpdates(aResources, aOp, aImageKeys, aExtID); 128 } 129 130 void RemoteTextureHostWrapper::PushDisplayItems( 131 wr::DisplayListBuilder& aBuilder, const wr::LayoutRect& aBounds, 132 const wr::LayoutRect& aClip, wr::ImageRendering aFilter, 133 const Range<wr::ImageKey>& aImageKeys, PushDisplayItemFlagSet aFlags) { 134 MOZ_ASSERT(mRemoteTexture, "TextureHost isn't valid yet"); 135 MOZ_ASSERT(aImageKeys.length() > 0); 136 if (!mRemoteTexture) { 137 return; 138 } 139 mRemoteTexture->PushDisplayItems(aBuilder, aBounds, aClip, aFilter, 140 aImageKeys, aFlags); 141 } 142 143 bool RemoteTextureHostWrapper::SupportsExternalCompositing( 144 WebRenderBackend aBackend) { 145 if (!mRemoteTexture) { 146 return false; 147 } 148 return mRemoteTexture->SupportsExternalCompositing(aBackend); 149 } 150 151 void RemoteTextureHostWrapper::UnbindTextureSource() {} 152 153 void RemoteTextureHostWrapper::NotifyNotUsed() { 154 if (mRemoteTexture) { 155 // Release mRemoteTexture. 156 RemoteTextureMap::Get()->ReleaseRemoteTextureHost(this); 157 } 158 MOZ_ASSERT(!mRemoteTexture); 159 160 RemoteTextureMap::Get()->UnregisterRemoteTextureHostWrapper( 161 mTextureId, mOwnerId, mForPid); 162 } 163 164 void RemoteTextureHostWrapper::SetReadFence(Fence* aReadFence) { 165 if (mRemoteTexture) { 166 mRemoteTexture->SetReadFence(aReadFence); 167 } 168 } 169 170 TextureHostType RemoteTextureHostWrapper::GetTextureHostType() { 171 if (!mRemoteTexture) { 172 return TextureHostType::Unknown; 173 } 174 return mRemoteTexture->GetTextureHostType(); 175 } 176 177 bool RemoteTextureHostWrapper::IsReadyForRendering() { 178 return !!mRemoteTexture; 179 } 180 181 void RemoteTextureHostWrapper::ApplyTextureFlagsToRemoteTexture() { 182 if (!mRemoteTexture) { 183 return; 184 } 185 mRemoteTexture->SetFlags(mFlags | TextureFlags::DEALLOCATE_CLIENT); 186 } 187 188 TextureHost* RemoteTextureHostWrapper::GetRemoteTextureHost( 189 const MonitorAutoLock& aProofOfLock) { 190 MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread()); 191 return mRemoteTexture; 192 } 193 194 void RemoteTextureHostWrapper::SetRemoteTextureHost( 195 const MonitorAutoLock& aProofOfLock, TextureHost* aTextureHost) { 196 MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread()); 197 mRemoteTexture = aTextureHost; 198 199 if (mExternalImageId.isSome() && !mRenderTextureCreated) { 200 MaybeCreateRenderTexture(); 201 } 202 } 203 204 void RemoteTextureHostWrapper::ClearRemoteTextureHost( 205 const MonitorAutoLock& aProofOfLoc) { 206 MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread()); 207 mRemoteTexture = nullptr; 208 } 209 210 bool RemoteTextureHostWrapper::IsWrappingSurfaceTextureHost() { 211 if (!mRemoteTexture) { 212 return false; 213 } 214 return mRemoteTexture->IsWrappingSurfaceTextureHost(); 215 } 216 217 bool RemoteTextureHostWrapper::NeedsDeferredDeletion() const { 218 if (!mRemoteTexture) { 219 return true; 220 } 221 return mRemoteTexture->NeedsDeferredDeletion(); 222 } 223 224 AndroidHardwareBuffer* RemoteTextureHostWrapper::GetAndroidHardwareBuffer() 225 const { 226 if (!mRemoteTexture) { 227 return nullptr; 228 } 229 return mRemoteTexture->GetAndroidHardwareBuffer(); 230 } 231 232 } // namespace mozilla::layers