summaryrefslogtreecommitdiff
path: root/chromium/ui/gl/gl_image_dxgi.h
blob: 74bdf59a32afe7910114ca7091aceb61d8c5be4a (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GL_GL_IMAGE_DXGI_H_
#define UI_GL_GL_IMAGE_DXGI_H_

#include <DXGI1_2.h>
#include <d3d11.h>
#include <wrl/client.h>

#include "base/win/scoped_handle.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gl/gl_export.h"
#include "ui/gl/gl_image.h"

typedef void* EGLStreamKHR;
typedef void* EGLConfig;
typedef void* EGLSurface;

namespace gl {

// TODO(776010): Reconcile the different GLImageDXGI types.  Remove
// GLImageDXGIHandle, and move its implementation into GLImageDXGI.
class GL_EXPORT GLImageDXGIBase : public GLImage {
 public:
  GLImageDXGIBase(const gfx::Size& size);

  // Safe downcast. Returns nullptr on failure.
  static GLImageDXGIBase* FromGLImage(GLImage* image);

  // GLImage implementation.
  gfx::Size GetSize() override;
  unsigned GetInternalFormat() override;
  bool BindTexImage(unsigned target) override;
  void ReleaseTexImage(unsigned target) override;
  bool CopyTexImage(unsigned target) override;
  bool CopyTexSubImage(unsigned target,
                       const gfx::Point& offset,
                       const gfx::Rect& rect) override;
  bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
                            int z_order,
                            gfx::OverlayTransform transform,
                            const gfx::Rect& bounds_rect,
                            const gfx::RectF& crop_rect,
                            bool enable_blend) override;
  void SetColorSpace(const gfx::ColorSpace& color_space) override;
  void Flush() override;
  void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
                    uint64_t process_tracing_id,
                    const std::string& dump_name) override;
  Type GetType() const override;

  Microsoft::WRL::ComPtr<ID3D11Texture2D> texture() { return texture_; }
  const gfx::ColorSpace& color_space() const { return color_space_; }

  size_t level() const { return level_; }
  Microsoft::WRL::ComPtr<IDXGIKeyedMutex> keyed_mutex() { return keyed_mutex_; }

 protected:
  ~GLImageDXGIBase() override;

  gfx::Size size_;
  gfx::ColorSpace color_space_;

  Microsoft::WRL::ComPtr<ID3D11Texture2D> texture_;
  Microsoft::WRL::ComPtr<IDXGIKeyedMutex> keyed_mutex_;
  size_t level_ = 0;
};

class GL_EXPORT GLImageDXGI : public GLImageDXGIBase {
 public:
  GLImageDXGI(const gfx::Size& size, EGLStreamKHR stream);

  // GLImage implementation.
  bool BindTexImage(unsigned target) override;

  void SetTexture(const Microsoft::WRL::ComPtr<ID3D11Texture2D>& texture,
                  size_t level);

 protected:
  ~GLImageDXGI() override;

  EGLStreamKHR stream_;
};

// This copies to a new texture on bind.
class GL_EXPORT CopyingGLImageDXGI : public GLImageDXGI {
 public:
  CopyingGLImageDXGI(const Microsoft::WRL::ComPtr<ID3D11Device>& d3d11_device,
                     const gfx::Size& size,
                     EGLStreamKHR stream);

  bool Initialize();
  bool InitializeVideoProcessor(
      const Microsoft::WRL::ComPtr<ID3D11VideoProcessor>& video_processor,
      const Microsoft::WRL::ComPtr<ID3D11VideoProcessorEnumerator>& enumerator);
  void UnbindFromTexture();

  // GLImage implementation.
  bool BindTexImage(unsigned target) override;

 private:
  ~CopyingGLImageDXGI() override;

  bool copied_ = false;

  Microsoft::WRL::ComPtr<ID3D11VideoDevice> video_device_;
  Microsoft::WRL::ComPtr<ID3D11VideoContext> video_context_;
  Microsoft::WRL::ComPtr<ID3D11VideoProcessor> d3d11_processor_;
  Microsoft::WRL::ComPtr<ID3D11VideoProcessorEnumerator> enumerator_;
  Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
  Microsoft::WRL::ComPtr<ID3D11Texture2D> decoder_copy_texture_;
  Microsoft::WRL::ComPtr<ID3D11VideoProcessorOutputView> output_view_;
};

class GL_EXPORT GLImageDXGIHandle : public GLImageDXGIBase {
 public:
  GLImageDXGIHandle(const gfx::Size& size,
                    uint32_t level,
                    gfx::BufferFormat format);

  bool Initialize(base::win::ScopedHandle handle);

  // GLImage implementation.
  bool BindTexImage(unsigned target) override;
  unsigned GetInternalFormat() override;
  void ReleaseTexImage(unsigned target) override;

 protected:
  ~GLImageDXGIHandle() override;

  EGLSurface surface_ = nullptr;
  base::win::ScopedHandle handle_;
  gfx::BufferFormat format_;
};
}

#endif  // UI_GL_GL_IMAGE_DXGI_H_