summaryrefslogtreecommitdiffstats
path: root/gfx/layers/d3d11/GpuProcessD3D11TextureMap.h
blob: 6c348aa4d29df50139fbe6e495389bba883123de (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
/* -*- 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_GpuProcessD3D11TextureMap_H
#define MOZILLA_GFX_GpuProcessD3D11TextureMap_H

#include <d3d11.h>
#include <unordered_map>
#include <unordered_set>

#include "mozilla/gfx/2D.h"
#include "mozilla/layers/LayersTypes.h"
#include "mozilla/layers/TextureHost.h"
#include "mozilla/Maybe.h"
#include "mozilla/StaticPtr.h"

namespace mozilla {
namespace layers {

class IMFSampleUsageInfo;
class TextureWrapperD3D11Allocator;

/**
 * A class to manage ID3D11Texture2Ds that is shared without using shared handle
 * in GPU process. On some GPUs, ID3D11Texture2Ds of hardware decoded video
 * frames with zero video frame copy could not use shared handle.
 */
class GpuProcessD3D11TextureMap {
  struct UpdatingTextureHolder;

 public:
  static void Init();
  static void Shutdown();
  static GpuProcessD3D11TextureMap* Get() { return sInstance; }
  static GpuProcessTextureId GetNextTextureId();

  GpuProcessD3D11TextureMap();
  ~GpuProcessD3D11TextureMap();

  void Register(GpuProcessTextureId aTextureId, ID3D11Texture2D* aTexture,
                uint32_t aArrayIndex, const gfx::IntSize& aSize,
                RefPtr<IMFSampleUsageInfo> aUsageInfo);
  void Register(const MonitorAutoLock& aProofOfLock,
                GpuProcessTextureId aTextureId, ID3D11Texture2D* aTexture,
                uint32_t aArrayIndex, const gfx::IntSize& aSize,
                RefPtr<IMFSampleUsageInfo> aUsageInfo);
  void Unregister(GpuProcessTextureId aTextureId);

  RefPtr<ID3D11Texture2D> GetTexture(GpuProcessTextureId aTextureId);
  Maybe<HANDLE> GetSharedHandleOfCopiedTexture(GpuProcessTextureId aTextureId);

  size_t GetWaitingTextureCount() const;

  bool WaitTextureReady(const GpuProcessTextureId aTextureId);

  void PostUpdateTextureDataTask(const GpuProcessTextureId aTextureId,
                                 TextureHost* aTextureHost,
                                 TextureHost* aWrappedTextureHost,
                                 TextureWrapperD3D11Allocator* aAllocator);

  void HandleInTextureUpdateThread();

 private:
  struct TextureHolder {
    TextureHolder(ID3D11Texture2D* aTexture, uint32_t aArrayIndex,
                  const gfx::IntSize& aSize,
                  RefPtr<IMFSampleUsageInfo> aUsageInfo);
    TextureHolder() = default;

    RefPtr<ID3D11Texture2D> mTexture;
    uint32_t mArrayIndex = 0;
    gfx::IntSize mSize;
    RefPtr<IMFSampleUsageInfo> mIMFSampleUsageInfo;
    RefPtr<ID3D11Texture2D> mCopiedTexture;
    RefPtr<gfx::FileHandleWrapper> mCopiedTextureSharedHandle;
  };

  struct UpdatingTextureHolder {
    UpdatingTextureHolder(const GpuProcessTextureId aTextureId,
                          TextureHost* aTextureHost,
                          TextureHost* aWrappedTextureHost,
                          TextureWrapperD3D11Allocator* aAllocator);

    ~UpdatingTextureHolder();

    const GpuProcessTextureId mTextureId;
    RefPtr<TextureHost> mTextureHost;
    CompositableTextureHostRef mWrappedTextureHost;
    RefPtr<TextureWrapperD3D11Allocator> mAllocator;
  };

  enum class UpdatingStatus { Waiting, Updating, Error };

  RefPtr<ID3D11Texture2D> UpdateTextureData(UpdatingTextureHolder* aHolder);

  mutable Monitor mMonitor MOZ_UNANNOTATED;

  std::unordered_map<GpuProcessTextureId, TextureHolder,
                     GpuProcessTextureId::HashFn>
      mD3D11TexturesById;

  std::deque<UniquePtr<UpdatingTextureHolder>> mWaitingTextureQueue;

  std::unordered_set<GpuProcessTextureId, GpuProcessTextureId::HashFn>
      mWaitingTextures;

  static StaticAutoPtr<GpuProcessD3D11TextureMap> sInstance;
};

}  // namespace layers
}  // namespace mozilla

#endif /* MOZILLA_GFX_GpuProcessD3D11TextureMap_H */