summaryrefslogtreecommitdiffstats
path: root/gfx/layers/DcompSurfaceImage.cpp
blob: b2f3dacc2509bbcb6d1761ddc1befb36b0ea3829 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "DcompSurfaceImage.h"

#include "mozilla/ipc/FileDescriptor.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/layers/CompositorTypes.h"
#include "mozilla/layers/LayersSurfaces.h"
#include "mozilla/layers/TextureForwarder.h"
#include "mozilla/layers/KnowsCompositor.h"
#include "mozilla/webrender/RenderDcompSurfaceTextureHost.h"
#include "mozilla/webrender/WebRenderAPI.h"

extern mozilla::LazyLogModule gDcompSurface;
#define LOG(...) MOZ_LOG(gDcompSurface, LogLevel::Debug, (__VA_ARGS__))

namespace mozilla::layers {

already_AddRefed<TextureHost> CreateTextureHostDcompSurface(
    const SurfaceDescriptor& aDesc, ISurfaceAllocator* aDeallocator,
    LayersBackend aBackend, TextureFlags aFlags) {
  MOZ_ASSERT(aDesc.type() == SurfaceDescriptor::TSurfaceDescriptorDcompSurface);
  RefPtr<TextureHost> result = new DcompSurfaceHandleHost(
      aFlags, aDesc.get_SurfaceDescriptorDcompSurface());
  return result.forget();
}

/* static */
already_AddRefed<TextureClient> DcompSurfaceTexture::CreateTextureClient(
    HANDLE aHandle, gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
    KnowsCompositor* aKnowsCompositor) {
  RefPtr<TextureClient> textureClient = MakeAndAddRef<TextureClient>(
      new DcompSurfaceTexture(aHandle, aSize, aFormat), TextureFlags::NO_FLAGS,
      aKnowsCompositor->GetTextureForwarder());
  return textureClient.forget();
}

DcompSurfaceTexture::~DcompSurfaceTexture() {
  LOG("Destroy DcompSurfaceTexture %p, close handle=%p", this, mHandle);
  CloseHandle(mHandle);
}

bool DcompSurfaceTexture::Serialize(SurfaceDescriptor& aOutDescriptor) {
  aOutDescriptor = SurfaceDescriptorDcompSurface(ipc::FileDescriptor(mHandle),
                                                 mSize, mFormat);
  return true;
}

void DcompSurfaceTexture::GetSubDescriptor(
    RemoteDecoderVideoSubDescriptor* aOutDesc) {
  *aOutDesc = SurfaceDescriptorDcompSurface(ipc::FileDescriptor(mHandle), mSize,
                                            mFormat);
}

DcompSurfaceImage::DcompSurfaceImage(HANDLE aHandle, gfx::IntSize aSize,
                                     gfx::SurfaceFormat aFormat,
                                     KnowsCompositor* aKnowsCompositor)
    : Image(nullptr, ImageFormat::DCOMP_SURFACE),
      mTextureClient(DcompSurfaceTexture::CreateTextureClient(
          aHandle, aSize, aFormat, aKnowsCompositor)) {
  // Dcomp surface supports DXGI_FORMAT_B8G8R8A8_UNORM,
  // DXGI_FORMAT_R8G8B8A8_UNORM and DXGI_FORMAT_R16G16B16A16_FLOAT
  MOZ_ASSERT(aFormat == gfx::SurfaceFormat::B8G8R8A8 ||
             aFormat == gfx::SurfaceFormat::R8G8B8A8);
}

TextureClient* DcompSurfaceImage::GetTextureClient(
    KnowsCompositor* aKnowsCompositor) {
  return mTextureClient;
}

DcompSurfaceHandleHost::DcompSurfaceHandleHost(
    TextureFlags aFlags, const SurfaceDescriptorDcompSurface& aDescriptor)
    : TextureHost(TextureHostType::DcompSurface, aFlags),
      mHandle(const_cast<ipc::FileDescriptor&>(aDescriptor.handle())
                  .TakePlatformHandle()),
      mSize(aDescriptor.size()),
      mFormat(aDescriptor.format()) {
  LOG("Create DcompSurfaceHandleHost %p", this);
}

DcompSurfaceHandleHost::~DcompSurfaceHandleHost() {
  LOG("Destroy DcompSurfaceHandleHost %p", this);
}

void DcompSurfaceHandleHost::CreateRenderTexture(
    const wr::ExternalImageId& aExternalImageId) {
  MOZ_ASSERT(mExternalImageId.isSome());
  LOG("DcompSurfaceHandleHost %p CreateRenderTexture, ext-id=%" PRIu64, this,
      wr::AsUint64(aExternalImageId));
  RefPtr<wr::RenderTextureHost> texture =
      new wr::RenderDcompSurfaceTextureHost(mHandle.get(), mSize, mFormat);
  wr::RenderThread::Get()->RegisterExternalImage(aExternalImageId,
                                                 texture.forget());
}

void DcompSurfaceHandleHost::PushResourceUpdates(
    wr::TransactionBuilder& aResources, ResourceUpdateOp aOp,
    const Range<wr::ImageKey>& aImageKeys,
    const wr::ExternalImageId& aExternalImageId) {
  if (!gfx::gfxVars::UseWebRenderANGLE()) {
    MOZ_ASSERT_UNREACHABLE("Unexpected to be called without ANGLE");
    return;
  }
  MOZ_ASSERT(mHandle);
  MOZ_ASSERT(aImageKeys.length() == 1);
  auto method = aOp == TextureHost::ADD_IMAGE
                    ? &wr::TransactionBuilder::AddExternalImage
                    : &wr::TransactionBuilder::UpdateExternalImage;
  wr::ImageDescriptor descriptor(mSize, GetFormat());
  // Prefer TextureExternal unless the backend requires TextureRect.
  TextureHost::NativeTexturePolicy policy =
      TextureHost::BackendNativeTexturePolicy(aResources.GetBackendType(),
                                              mSize);
  auto imageType = policy == TextureHost::NativeTexturePolicy::REQUIRE
                       ? wr::ExternalImageType::TextureHandle(
                             wr::ImageBufferKind::TextureRect)
                       : wr::ExternalImageType::TextureHandle(
                             wr::ImageBufferKind::TextureExternal);
  LOG("DcompSurfaceHandleHost %p PushResourceUpdate, exi-id=%" PRIu64
      ", type=%s",
      this, wr::AsUint64(aExternalImageId),
      policy == TextureHost::NativeTexturePolicy::REQUIRE ? "rect" : "ext");
  (aResources.*method)(aImageKeys[0], descriptor, aExternalImageId, imageType,
                       0);
}

void DcompSurfaceHandleHost::PushDisplayItems(
    wr::DisplayListBuilder& aBuilder, const wr::LayoutRect& aBounds,
    const wr::LayoutRect& aClip, wr::ImageRendering aFilter,
    const Range<wr::ImageKey>& aImageKeys, PushDisplayItemFlagSet aFlags) {
  if (!gfx::gfxVars::UseWebRenderANGLE()) {
    MOZ_ASSERT_UNREACHABLE("Unexpected to be called without ANGLE");
    return;
  }
  LOG("DcompSurfaceHandleHost %p PushDisplayItems", this);
  MOZ_ASSERT(aImageKeys.length() == 1);
  aBuilder.PushImage(
      aBounds, aClip, true, false, aFilter, aImageKeys[0],
      !(mFlags & TextureFlags::NON_PREMULTIPLIED),
      wr::ColorF{1.0f, 1.0f, 1.0f, 1.0f},
      aFlags.contains(PushDisplayItemFlag::PREFER_COMPOSITOR_SURFACE),
      SupportsExternalCompositing(aBuilder.GetBackendType()));
}

}  // namespace mozilla::layers

#undef LOG