2017-10-28 02:10:06 +03:00
|
|
|
/* -*- 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
|
2017-03-07 13:37:28 +03:00
|
|
|
* 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 "WebRenderTextureHost.h"
|
|
|
|
|
2017-03-09 05:10:09 +03:00
|
|
|
#include "mozilla/layers/ImageDataSerializer.h"
|
2017-03-31 17:29:14 +03:00
|
|
|
#include "mozilla/layers/LayersSurfaces.h"
|
2017-03-07 13:37:28 +03:00
|
|
|
#include "mozilla/webrender/RenderThread.h"
|
2019-04-26 10:48:54 +03:00
|
|
|
#include "mozilla/webrender/WebRenderAPI.h"
|
2017-03-07 13:37:28 +03:00
|
|
|
|
2019-04-04 01:05:27 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
# include "mozilla/layers/TextureHostOGL.h"
|
|
|
|
#endif
|
|
|
|
|
2017-03-07 13:37:28 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2019-04-26 10:48:54 +03:00
|
|
|
class ScheduleNofityForUse : public wr::NotificationHandler {
|
|
|
|
public:
|
|
|
|
explicit ScheduleNofityForUse(uint64_t aExternalImageId)
|
|
|
|
: mExternalImageId(aExternalImageId) {}
|
|
|
|
|
|
|
|
virtual void Notify(wr::Checkpoint aCheckpoint) override {
|
|
|
|
if (aCheckpoint == wr::Checkpoint::FrameTexturesUpdated) {
|
|
|
|
MOZ_ASSERT(wr::RenderThread::IsInRenderThread());
|
|
|
|
wr::RenderThread::Get()->NofityForUse(mExternalImageId);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aCheckpoint == wr::Checkpoint::TransactionDropped);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
uint64_t mExternalImageId;
|
|
|
|
};
|
|
|
|
|
2017-03-31 17:29:14 +03:00
|
|
|
WebRenderTextureHost::WebRenderTextureHost(
|
|
|
|
const SurfaceDescriptor& aDesc, TextureFlags aFlags, TextureHost* aTexture,
|
2017-04-20 04:24:13 +03:00
|
|
|
wr::ExternalImageId& aExternalImageId)
|
|
|
|
: TextureHost(aFlags), mExternalImageId(aExternalImageId) {
|
2017-06-07 18:44:05 +03:00
|
|
|
// The wrapped textureHost will be used in WebRender, and the WebRender could
|
|
|
|
// run at another thread. It's hard to control the life-time when gecko
|
|
|
|
// receives PTextureParent destroy message. It's possible that textureHost is
|
|
|
|
// still used by WebRender. So, we only accept the textureHost without
|
|
|
|
// DEALLOCATE_CLIENT flag here. If the buffer deallocation is controlled by
|
|
|
|
// parent, we could do something to make sure the wrapped textureHost is not
|
|
|
|
// used by WebRender and then release it.
|
|
|
|
MOZ_ASSERT(!(aFlags & TextureFlags::DEALLOCATE_CLIENT));
|
|
|
|
|
2017-03-07 13:37:28 +03:00
|
|
|
MOZ_COUNT_CTOR(WebRenderTextureHost);
|
|
|
|
mWrappedTextureHost = aTexture;
|
|
|
|
|
2017-03-31 17:29:14 +03:00
|
|
|
CreateRenderTextureHost(aDesc, aTexture);
|
2017-03-07 13:37:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WebRenderTextureHost::~WebRenderTextureHost() {
|
|
|
|
MOZ_COUNT_DTOR(WebRenderTextureHost);
|
2017-04-19 12:59:53 +03:00
|
|
|
wr::RenderThread::Get()->UnregisterExternalImage(
|
|
|
|
wr::AsUint64(mExternalImageId));
|
2017-03-07 13:37:28 +03:00
|
|
|
}
|
|
|
|
|
2017-03-31 17:29:14 +03:00
|
|
|
void WebRenderTextureHost::CreateRenderTextureHost(
|
|
|
|
const layers::SurfaceDescriptor& aDesc, TextureHost* aTexture) {
|
2017-06-15 12:07:46 +03:00
|
|
|
MOZ_ASSERT(aTexture);
|
2017-03-31 17:29:14 +03:00
|
|
|
|
2017-06-15 12:07:46 +03:00
|
|
|
aTexture->CreateRenderTexture(mExternalImageId);
|
2017-03-31 17:29:14 +03:00
|
|
|
}
|
|
|
|
|
2017-03-07 13:37:28 +03:00
|
|
|
bool WebRenderTextureHost::Lock() {
|
2019-06-10 15:36:14 +03:00
|
|
|
MOZ_ASSERT(!mWrappedTextureHost.get() ||
|
|
|
|
mWrappedTextureHost->AsBufferTextureHost());
|
|
|
|
|
|
|
|
if (mWrappedTextureHost && mWrappedTextureHost->AsBufferTextureHost()) {
|
|
|
|
return mWrappedTextureHost->Lock();
|
|
|
|
}
|
2017-03-07 13:37:28 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebRenderTextureHost::Unlock() {
|
2019-06-10 15:36:14 +03:00
|
|
|
MOZ_ASSERT(!mWrappedTextureHost.get() ||
|
|
|
|
mWrappedTextureHost->AsBufferTextureHost());
|
|
|
|
|
|
|
|
if (mWrappedTextureHost && mWrappedTextureHost->AsBufferTextureHost()) {
|
|
|
|
mWrappedTextureHost->Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebRenderTextureHost::PrepareTextureSource(
|
|
|
|
CompositableTextureSourceRef& aTexture) {
|
|
|
|
MOZ_ASSERT(!mWrappedTextureHost.get() ||
|
|
|
|
mWrappedTextureHost->AsBufferTextureHost());
|
|
|
|
|
|
|
|
if (mWrappedTextureHost && mWrappedTextureHost->AsBufferTextureHost()) {
|
|
|
|
mWrappedTextureHost->PrepareTextureSource(aTexture);
|
|
|
|
}
|
2017-03-07 13:37:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebRenderTextureHost::BindTextureSource(
|
|
|
|
CompositableTextureSourceRef& aTexture) {
|
2019-06-10 15:36:14 +03:00
|
|
|
MOZ_ASSERT(!mWrappedTextureHost.get() ||
|
|
|
|
mWrappedTextureHost->AsBufferTextureHost());
|
|
|
|
|
|
|
|
if (mWrappedTextureHost && mWrappedTextureHost->AsBufferTextureHost()) {
|
|
|
|
return mWrappedTextureHost->BindTextureSource(aTexture);
|
|
|
|
}
|
2017-03-07 13:37:28 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:36:14 +03:00
|
|
|
void WebRenderTextureHost::UnbindTextureSource() {
|
|
|
|
if (mWrappedTextureHost && mWrappedTextureHost->AsBufferTextureHost()) {
|
|
|
|
mWrappedTextureHost->UnbindTextureSource();
|
|
|
|
}
|
2019-06-13 00:29:47 +03:00
|
|
|
// Handle read unlock
|
|
|
|
TextureHost::UnbindTextureSource();
|
2019-06-10 15:36:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebRenderTextureHost::SetTextureSourceProvider(
|
|
|
|
TextureSourceProvider* aProvider) {
|
|
|
|
// During using WebRender, only BasicCompositor could exist
|
|
|
|
MOZ_ASSERT(!aProvider || aProvider->AsBasicCompositor());
|
|
|
|
MOZ_ASSERT(!mWrappedTextureHost.get() ||
|
|
|
|
mWrappedTextureHost->AsBufferTextureHost());
|
|
|
|
|
|
|
|
if (mWrappedTextureHost && mWrappedTextureHost->AsBufferTextureHost()) {
|
|
|
|
mWrappedTextureHost->SetTextureSourceProvider(aProvider);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 13:37:28 +03:00
|
|
|
already_AddRefed<gfx::DataSourceSurface> WebRenderTextureHost::GetAsSurface() {
|
|
|
|
if (!mWrappedTextureHost) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mWrappedTextureHost->GetAsSurface();
|
|
|
|
}
|
|
|
|
|
2019-04-11 15:41:33 +03:00
|
|
|
gfx::YUVColorSpace WebRenderTextureHost::GetYUVColorSpace() const {
|
2017-03-07 13:37:28 +03:00
|
|
|
if (mWrappedTextureHost) {
|
|
|
|
return mWrappedTextureHost->GetYUVColorSpace();
|
|
|
|
}
|
2019-04-11 15:41:33 +03:00
|
|
|
return gfx::YUVColorSpace::UNKNOWN;
|
2017-03-07 13:37:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::IntSize WebRenderTextureHost::GetSize() const {
|
|
|
|
if (!mWrappedTextureHost) {
|
|
|
|
return gfx::IntSize();
|
|
|
|
}
|
|
|
|
return mWrappedTextureHost->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::SurfaceFormat WebRenderTextureHost::GetFormat() const {
|
|
|
|
if (!mWrappedTextureHost) {
|
|
|
|
return gfx::SurfaceFormat::UNKNOWN;
|
|
|
|
}
|
|
|
|
return mWrappedTextureHost->GetFormat();
|
|
|
|
}
|
|
|
|
|
2019-04-04 01:05:27 +03:00
|
|
|
void WebRenderTextureHost::NotifyNotUsed() {
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
if (mWrappedTextureHost && mWrappedTextureHost->AsSurfaceTextureHost()) {
|
|
|
|
wr::RenderThread::Get()->NotifyNotUsed(wr::AsUint64(mExternalImageId));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
TextureHost::NotifyNotUsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebRenderTextureHost::PrepareForUse() {
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
if (mWrappedTextureHost && mWrappedTextureHost->AsSurfaceTextureHost()) {
|
|
|
|
// Call PrepareForUse on render thread.
|
|
|
|
// See RenderAndroidSurfaceTextureHostOGL::PrepareForUse.
|
|
|
|
wr::RenderThread::Get()->PrepareForUse(wr::AsUint64(mExternalImageId));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:29:14 +03:00
|
|
|
gfx::SurfaceFormat WebRenderTextureHost::GetReadFormat() const {
|
|
|
|
if (!mWrappedTextureHost) {
|
|
|
|
return gfx::SurfaceFormat::UNKNOWN;
|
|
|
|
}
|
|
|
|
return mWrappedTextureHost->GetReadFormat();
|
|
|
|
}
|
|
|
|
|
2017-03-09 05:10:09 +03:00
|
|
|
int32_t WebRenderTextureHost::GetRGBStride() {
|
|
|
|
if (!mWrappedTextureHost) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
gfx::SurfaceFormat format = GetFormat();
|
2017-03-31 17:29:14 +03:00
|
|
|
if (GetFormat() == gfx::SurfaceFormat::YUV) {
|
2017-03-09 05:10:09 +03:00
|
|
|
// XXX this stride is used until yuv image rendering by webrender is used.
|
|
|
|
// Software converted RGB buffers strides are aliened to 16
|
2017-03-31 17:29:14 +03:00
|
|
|
return gfx::GetAlignedStride<16>(
|
|
|
|
GetSize().width, BytesPerPixel(gfx::SurfaceFormat::B8G8R8A8));
|
2017-03-09 05:10:09 +03:00
|
|
|
}
|
|
|
|
return ImageDataSerializer::ComputeRGBStride(format, GetSize().width);
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:58:37 +03:00
|
|
|
bool WebRenderTextureHost::HasIntermediateBuffer() const {
|
|
|
|
MOZ_ASSERT(mWrappedTextureHost);
|
|
|
|
return mWrappedTextureHost->HasIntermediateBuffer();
|
|
|
|
}
|
|
|
|
|
2019-07-24 16:33:57 +03:00
|
|
|
uint32_t WebRenderTextureHost::NumSubTextures() {
|
2017-06-15 12:07:46 +03:00
|
|
|
MOZ_ASSERT(mWrappedTextureHost);
|
2017-09-26 16:30:46 +03:00
|
|
|
return mWrappedTextureHost->NumSubTextures();
|
2017-06-07 18:44:04 +03:00
|
|
|
}
|
|
|
|
|
2018-01-29 16:33:39 +03:00
|
|
|
void WebRenderTextureHost::PushResourceUpdates(
|
|
|
|
wr::TransactionBuilder& aResources, ResourceUpdateOp aOp,
|
2017-09-26 16:30:51 +03:00
|
|
|
const Range<wr::ImageKey>& aImageKeys, const wr::ExternalImageId& aExtID) {
|
2017-05-18 17:59:07 +03:00
|
|
|
MOZ_ASSERT(mWrappedTextureHost);
|
2019-07-22 11:03:59 +03:00
|
|
|
MOZ_ASSERT(mExternalImageId == aExtID);
|
2017-05-18 17:59:07 +03:00
|
|
|
|
2017-09-26 16:30:51 +03:00
|
|
|
mWrappedTextureHost->PushResourceUpdates(aResources, aOp, aImageKeys, aExtID);
|
2017-05-18 17:59:07 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:31:00 +03:00
|
|
|
void WebRenderTextureHost::PushDisplayItems(
|
|
|
|
wr::DisplayListBuilder& aBuilder, const wr::LayoutRect& aBounds,
|
|
|
|
const wr::LayoutRect& aClip, wr::ImageRendering aFilter,
|
|
|
|
const Range<wr::ImageKey>& aImageKeys) {
|
2017-06-15 12:07:46 +03:00
|
|
|
MOZ_ASSERT(mWrappedTextureHost);
|
2017-06-07 18:44:04 +03:00
|
|
|
MOZ_ASSERT(aImageKeys.length() > 0);
|
2017-06-15 12:07:46 +03:00
|
|
|
|
2017-09-26 16:31:00 +03:00
|
|
|
mWrappedTextureHost->PushDisplayItems(aBuilder, aBounds, aClip, aFilter,
|
2017-06-07 18:44:04 +03:00
|
|
|
aImageKeys);
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:29:24 +03:00
|
|
|
bool WebRenderTextureHost::NeedsYFlip() const {
|
|
|
|
bool yFlip = TextureHost::NeedsYFlip();
|
|
|
|
if (mWrappedTextureHost->AsSurfaceTextureHost()) {
|
|
|
|
MOZ_ASSERT(yFlip);
|
|
|
|
// With WebRender, SurfaceTextureHost always requests y-flip.
|
|
|
|
// But y-flip should not be handled, since
|
|
|
|
// SurfaceTexture.getTransformMatrix() is not handled yet.
|
|
|
|
// See Bug 1507076.
|
|
|
|
yFlip = false;
|
|
|
|
}
|
|
|
|
return yFlip;
|
|
|
|
}
|
|
|
|
|
2019-04-26 10:48:54 +03:00
|
|
|
void WebRenderTextureHost::MaybeNofityForUse(wr::TransactionBuilder& aTxn) {
|
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
if (!mWrappedTextureHost->AsSurfaceTextureHost()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// SurfaceTexture of video needs NofityForUse() to detect if it is rendered
|
|
|
|
// on WebRender.
|
|
|
|
aTxn.Notify(wr::Checkpoint::FrameTexturesUpdated,
|
|
|
|
MakeUnique<ScheduleNofityForUse>(wr::AsUint64(mExternalImageId)));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-07 13:37:28 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|