1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "DcompSurfaceImage.h"
8 #include "mozilla/ipc/FileDescriptor.h"
9 #include "mozilla/gfx/gfxVars.h"
10 #include "mozilla/layers/CompositorTypes.h"
11 #include "mozilla/layers/LayersSurfaces.h"
12 #include "mozilla/layers/TextureForwarder.h"
13 #include "mozilla/layers/KnowsCompositor.h"
14 #include "mozilla/webrender/RenderDcompSurfaceTextureHost.h"
15 #include "mozilla/webrender/WebRenderAPI.h"
17 extern mozilla::LazyLogModule gDcompSurface
;
18 #define LOG(...) MOZ_LOG(gDcompSurface, LogLevel::Debug, (__VA_ARGS__))
20 namespace mozilla::layers
{
22 already_AddRefed
<TextureHost
> CreateTextureHostDcompSurface(
23 const SurfaceDescriptor
& aDesc
, ISurfaceAllocator
* aDeallocator
,
24 LayersBackend aBackend
, TextureFlags aFlags
) {
25 MOZ_ASSERT(aDesc
.type() == SurfaceDescriptor::TSurfaceDescriptorDcompSurface
);
26 RefPtr
<TextureHost
> result
= new DcompSurfaceHandleHost(
27 aFlags
, aDesc
.get_SurfaceDescriptorDcompSurface());
28 return result
.forget();
32 already_AddRefed
<TextureClient
> DcompSurfaceTexture::CreateTextureClient(
33 HANDLE aHandle
, gfx::IntSize aSize
, gfx::SurfaceFormat aFormat
,
34 KnowsCompositor
* aKnowsCompositor
) {
35 RefPtr
<TextureClient
> textureClient
= MakeAndAddRef
<TextureClient
>(
36 new DcompSurfaceTexture(aHandle
, aSize
, aFormat
), TextureFlags::NO_FLAGS
,
37 aKnowsCompositor
->GetTextureForwarder());
38 return textureClient
.forget();
41 DcompSurfaceTexture::~DcompSurfaceTexture() {
42 LOG("Destroy DcompSurfaceTexture %p, close handle=%p", this, mHandle
);
46 bool DcompSurfaceTexture::Serialize(SurfaceDescriptor
& aOutDescriptor
) {
47 aOutDescriptor
= SurfaceDescriptorDcompSurface(ipc::FileDescriptor(mHandle
),
52 void DcompSurfaceTexture::GetSubDescriptor(
53 RemoteDecoderVideoSubDescriptor
* aOutDesc
) {
54 *aOutDesc
= SurfaceDescriptorDcompSurface(ipc::FileDescriptor(mHandle
), mSize
,
58 DcompSurfaceImage::DcompSurfaceImage(HANDLE aHandle
, gfx::IntSize aSize
,
59 gfx::SurfaceFormat aFormat
,
60 KnowsCompositor
* aKnowsCompositor
)
61 : Image(nullptr, ImageFormat::DCOMP_SURFACE
),
62 mTextureClient(DcompSurfaceTexture::CreateTextureClient(
63 aHandle
, aSize
, aFormat
, aKnowsCompositor
)) {
64 // Dcomp surface supports DXGI_FORMAT_B8G8R8A8_UNORM,
65 // DXGI_FORMAT_R8G8B8A8_UNORM and DXGI_FORMAT_R16G16B16A16_FLOAT
66 MOZ_ASSERT(aFormat
== gfx::SurfaceFormat::B8G8R8A8
||
67 aFormat
== gfx::SurfaceFormat::R8G8B8A8
);
70 TextureClient
* DcompSurfaceImage::GetTextureClient(
71 KnowsCompositor
* aKnowsCompositor
) {
72 return mTextureClient
;
75 DcompSurfaceHandleHost::DcompSurfaceHandleHost(
76 TextureFlags aFlags
, const SurfaceDescriptorDcompSurface
& aDescriptor
)
77 : TextureHost(TextureHostType::DcompSurface
, aFlags
),
78 mHandle(const_cast<ipc::FileDescriptor
&>(aDescriptor
.handle())
79 .TakePlatformHandle()),
80 mSize(aDescriptor
.size()),
81 mFormat(aDescriptor
.format()) {
82 LOG("Create DcompSurfaceHandleHost %p", this);
85 DcompSurfaceHandleHost::~DcompSurfaceHandleHost() {
86 LOG("Destroy DcompSurfaceHandleHost %p", this);
89 void DcompSurfaceHandleHost::CreateRenderTexture(
90 const wr::ExternalImageId
& aExternalImageId
) {
91 MOZ_ASSERT(mExternalImageId
.isSome());
92 LOG("DcompSurfaceHandleHost %p CreateRenderTexture, ext-id=%" PRIu64
, this,
93 wr::AsUint64(aExternalImageId
));
94 RefPtr
<wr::RenderTextureHost
> texture
=
95 new wr::RenderDcompSurfaceTextureHost(mHandle
.get(), mSize
, mFormat
);
96 wr::RenderThread::Get()->RegisterExternalImage(aExternalImageId
,
100 void DcompSurfaceHandleHost::PushResourceUpdates(
101 wr::TransactionBuilder
& aResources
, ResourceUpdateOp aOp
,
102 const Range
<wr::ImageKey
>& aImageKeys
,
103 const wr::ExternalImageId
& aExternalImageId
) {
104 if (!gfx::gfxVars::UseWebRenderANGLE()) {
105 MOZ_ASSERT_UNREACHABLE("Unexpected to be called without ANGLE");
109 MOZ_ASSERT(aImageKeys
.length() == 1);
110 auto method
= aOp
== TextureHost::ADD_IMAGE
111 ? &wr::TransactionBuilder::AddExternalImage
112 : &wr::TransactionBuilder::UpdateExternalImage
;
113 wr::ImageDescriptor
descriptor(mSize
, GetFormat());
114 // Prefer TextureExternal unless the backend requires TextureRect.
115 TextureHost::NativeTexturePolicy policy
=
116 TextureHost::BackendNativeTexturePolicy(aResources
.GetBackendType(),
118 auto imageType
= policy
== TextureHost::NativeTexturePolicy::REQUIRE
119 ? wr::ExternalImageType::TextureHandle(
120 wr::ImageBufferKind::TextureRect
)
121 : wr::ExternalImageType::TextureHandle(
122 wr::ImageBufferKind::TextureExternal
);
123 LOG("DcompSurfaceHandleHost %p PushResourceUpdate, exi-id=%" PRIu64
125 this, wr::AsUint64(aExternalImageId
),
126 policy
== TextureHost::NativeTexturePolicy::REQUIRE
? "rect" : "ext");
127 (aResources
.*method
)(aImageKeys
[0], descriptor
, aExternalImageId
, imageType
,
131 void DcompSurfaceHandleHost::PushDisplayItems(
132 wr::DisplayListBuilder
& aBuilder
, const wr::LayoutRect
& aBounds
,
133 const wr::LayoutRect
& aClip
, wr::ImageRendering aFilter
,
134 const Range
<wr::ImageKey
>& aImageKeys
, PushDisplayItemFlagSet aFlags
) {
135 if (!gfx::gfxVars::UseWebRenderANGLE()) {
136 MOZ_ASSERT_UNREACHABLE("Unexpected to be called without ANGLE");
139 LOG("DcompSurfaceHandleHost %p PushDisplayItems", this);
140 MOZ_ASSERT(aImageKeys
.length() == 1);
142 aBounds
, aClip
, true, false, aFilter
, aImageKeys
[0],
143 !(mFlags
& TextureFlags::NON_PREMULTIPLIED
),
144 wr::ColorF
{1.0f
, 1.0f
, 1.0f
, 1.0f
},
145 aFlags
.contains(PushDisplayItemFlag::PREFER_COMPOSITOR_SURFACE
),
146 SupportsExternalCompositing(aBuilder
.GetBackendType()));
149 } // namespace mozilla::layers