summaryrefslogtreecommitdiffstats
path: root/gfx/webrender_bindings/RenderD3D11TextureHost.h
blob: 83a7d3a27919b38b9c27aa49b34365e766fca8f8 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* -*- 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
 * 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/. */

#ifndef MOZILLA_GFX_RENDERD3D11TEXTUREHOST_H
#define MOZILLA_GFX_RENDERD3D11TEXTUREHOST_H

#include "GLTypes.h"
#include "RenderTextureHostSWGL.h"

#include <d3d11.h>

struct ID3D11Texture2D;
struct IDXGIKeyedMutex;

namespace mozilla {

namespace wr {

class RenderDXGITextureHost final : public RenderTextureHostSWGL {
 public:
  RenderDXGITextureHost(
      WindowsHandle aHandle,
      Maybe<layers::GpuProcessTextureId>& aGpuProcessTextureId,
      uint32_t aArrayIndex, gfx::SurfaceFormat aFormat, gfx::ColorSpace2,
      gfx::ColorRange aColorRange, gfx::IntSize aSize);

  wr::WrExternalImage Lock(uint8_t aChannelIndex, gl::GLContext* aGL) override;
  void Unlock() override;
  void ClearCachedResources() override;

  gfx::IntSize GetSize(uint8_t aChannelIndex) const;
  GLuint GetGLHandle(uint8_t aChannelIndex) const;

  bool SyncObjectNeeded() override { return true; }

  RenderDXGITextureHost* AsRenderDXGITextureHost() override { return this; }

  gfx::ColorRange GetColorRange() const { return mColorRange; }

  ID3D11Texture2D* GetD3D11Texture2DWithGL();
  ID3D11Texture2D* GetD3D11Texture2D() { return mTexture; }

  // RenderTextureHostSWGL
  gfx::SurfaceFormat GetFormat() const override { return mFormat; }
  gfx::ColorDepth GetColorDepth() const override {
    if (mFormat == gfx::SurfaceFormat::P010) {
      return gfx::ColorDepth::COLOR_10;
    }
    if (mFormat == gfx::SurfaceFormat::P016) {
      return gfx::ColorDepth::COLOR_16;
    }
    return gfx::ColorDepth::COLOR_8;
  }
  size_t GetPlaneCount() const override;
  bool MapPlane(RenderCompositor* aCompositor, uint8_t aChannelIndex,
                PlaneInfo& aPlaneInfo) override;
  void UnmapPlanes() override;
  gfx::YUVRangedColorSpace GetYUVColorSpace() const override {
    return ToYUVRangedColorSpace(ToYUVColorSpace(mColorSpace), mColorRange);
  }

  bool EnsureD3D11Texture2D(ID3D11Device* aDevice);
  bool LockInternal();

  size_t Bytes() override {
    size_t bytes = 0;

    size_t bpp = GetPlaneCount() > 1
                     ? (GetColorDepth() == gfx::ColorDepth::COLOR_8 ? 1 : 2)
                     : 4;

    for (size_t i = 0; i < GetPlaneCount(); i++) {
      gfx::IntSize size = GetSize(i);
      bytes += size.width * size.height * bpp;
    }
    return bytes;
  }

  uint32_t ArrayIndex() const { return mArrayIndex; }

 private:
  virtual ~RenderDXGITextureHost();

  bool EnsureD3D11Texture2DWithGL();
  bool EnsureLockable();

  void DeleteTextureHandle();

  RefPtr<gl::GLContext> mGL;

  WindowsHandle mHandle;
  Maybe<layers::GpuProcessTextureId> mGpuProcessTextureId;
  RefPtr<ID3D11Texture2D> mTexture;
  uint32_t mArrayIndex = 0;
  RefPtr<IDXGIKeyedMutex> mKeyedMutex;

  // Temporary state between MapPlane and UnmapPlanes.
  RefPtr<ID3D11DeviceContext> mDeviceContext;
  RefPtr<ID3D11Texture2D> mCpuTexture;
  D3D11_MAPPED_SUBRESOURCE mMappedSubresource;

  EGLSurface mSurface;
  EGLStreamKHR mStream;

  // We could use NV12 format for this texture. So, we might have 2 gl texture
  // handles for Y and CbCr data.
  GLuint mTextureHandle[2];

 public:
  const gfx::SurfaceFormat mFormat;
  const gfx::ColorSpace2 mColorSpace;
  const gfx::ColorRange mColorRange;
  const gfx::IntSize mSize;

 private:
  bool mLocked;
};

class RenderDXGIYCbCrTextureHost final : public RenderTextureHostSWGL {
 public:
  explicit RenderDXGIYCbCrTextureHost(WindowsHandle (&aHandles)[3],
                                      gfx::YUVColorSpace aYUVColorSpace,
                                      gfx::ColorDepth aColorDepth,
                                      gfx::ColorRange aColorRange,
                                      gfx::IntSize aSizeY,
                                      gfx::IntSize aSizeCbCr);

  RenderDXGIYCbCrTextureHost* AsRenderDXGIYCbCrTextureHost() override {
    return this;
  }

  wr::WrExternalImage Lock(uint8_t aChannelIndex, gl::GLContext* aGL) override;
  void Unlock() override;
  void ClearCachedResources() override;

  gfx::IntSize GetSize(uint8_t aChannelIndex) const;
  GLuint GetGLHandle(uint8_t aChannelIndex) const;

  bool SyncObjectNeeded() override { return true; }

  gfx::ColorRange GetColorRange() const { return mColorRange; }

  // RenderTextureHostSWGL
  gfx::SurfaceFormat GetFormat() const override {
    return gfx::SurfaceFormat::YUV;
  }
  gfx::ColorDepth GetColorDepth() const override { return mColorDepth; }
  size_t GetPlaneCount() const override { return 3; }
  bool MapPlane(RenderCompositor* aCompositor, uint8_t aChannelIndex,
                PlaneInfo& aPlaneInfo) override;
  void UnmapPlanes() override;
  gfx::YUVRangedColorSpace GetYUVColorSpace() const override {
    return ToYUVRangedColorSpace(mYUVColorSpace, GetColorRange());
  }

  bool EnsureD3D11Texture2D(ID3D11Device* aDevice);
  bool LockInternal();

  ID3D11Texture2D* GetD3D11Texture2D(uint8_t aChannelIndex) {
    return mTextures[aChannelIndex];
  }

  size_t Bytes() override {
    size_t bytes = 0;

    size_t bpp = mColorDepth == gfx::ColorDepth::COLOR_8 ? 1 : 2;

    for (size_t i = 0; i < GetPlaneCount(); i++) {
      gfx::IntSize size = GetSize(i);
      bytes += size.width * size.height * bpp;
    }
    return bytes;
  }

 private:
  virtual ~RenderDXGIYCbCrTextureHost();

  bool EnsureLockable();

  void DeleteTextureHandle();

  RefPtr<gl::GLContext> mGL;

  WindowsHandle mHandles[3];
  RefPtr<ID3D11Texture2D> mTextures[3];
  RefPtr<IDXGIKeyedMutex> mKeyedMutexs[3];

  EGLSurface mSurfaces[3];
  EGLStreamKHR mStreams[3];

  // The gl handles for Y, Cb and Cr data.
  GLuint mTextureHandles[3];

  // Temporary state between MapPlane and UnmapPlanes.
  RefPtr<ID3D11DeviceContext> mDeviceContext;
  RefPtr<ID3D11Texture2D> mCpuTexture[3];

  gfx::YUVColorSpace mYUVColorSpace;
  gfx::ColorDepth mColorDepth;
  gfx::ColorRange mColorRange;
  gfx::IntSize mSizeY;
  gfx::IntSize mSizeCbCr;

  bool mLocked;
};

}  // namespace wr
}  // namespace mozilla

#endif  // MOZILLA_GFX_RENDERD3D11TEXTUREHOST_H