blob: 291ba107eeebd5fe144d8e4abeb46f8cff4c07a5 [file] [log] [blame]
jbauman575788b2016-12-15 21:06:221// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/gl/gl_angle_util_win.h"
6
robliao6fc41c62017-04-28 22:41:177#include <objbase.h>
8
jbauman575788b2016-12-15 21:06:229#include "base/trace_event/trace_event.h"
10#include "third_party/angle/include/EGL/egl.h"
11#include "third_party/angle/include/EGL/eglext.h"
12#include "ui/gl/gl_surface_egl.h"
13
14namespace gl {
15
16void* QueryDeviceObjectFromANGLE(int object_type) {
17 EGLDisplay egl_display = nullptr;
18 intptr_t egl_device = 0;
19 intptr_t device = 0;
20
21 {
22 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. GetHardwareDisplay");
23 egl_display = gl::GLSurfaceEGL::GetHardwareDisplay();
24 }
25
26 if (!gl::GLSurfaceEGL::HasEGLExtension("EGL_EXT_device_query"))
27 return nullptr;
28
29 PFNEGLQUERYDISPLAYATTRIBEXTPROC QueryDisplayAttribEXT = nullptr;
30
31 {
32 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. eglGetProcAddress");
33
34 QueryDisplayAttribEXT = reinterpret_cast<PFNEGLQUERYDISPLAYATTRIBEXTPROC>(
35 eglGetProcAddress("eglQueryDisplayAttribEXT"));
36
37 if (!QueryDisplayAttribEXT)
38 return nullptr;
39 }
40
41 PFNEGLQUERYDEVICEATTRIBEXTPROC QueryDeviceAttribEXT = nullptr;
42
43 {
44 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. eglGetProcAddress");
45
46 QueryDeviceAttribEXT = reinterpret_cast<PFNEGLQUERYDEVICEATTRIBEXTPROC>(
47 eglGetProcAddress("eglQueryDeviceAttribEXT"));
48 if (!QueryDeviceAttribEXT)
49 return nullptr;
50 }
51
52 {
53 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. QueryDisplayAttribEXT");
54
55 if (!QueryDisplayAttribEXT(egl_display, EGL_DEVICE_EXT, &egl_device))
56 return nullptr;
57 }
58 if (!egl_device)
59 return nullptr;
60
61 {
62 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. QueryDisplayAttribEXT");
63
64 if (!QueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(egl_device),
65 object_type, &device)) {
66 return nullptr;
67 }
68 }
69
70 return reinterpret_cast<void*>(device);
71}
72
73base::win::ScopedComPtr<ID3D11Device> QueryD3D11DeviceObjectFromANGLE() {
74 base::win::ScopedComPtr<ID3D11Device> d3d11_device;
75 d3d11_device = reinterpret_cast<ID3D11Device*>(
76 QueryDeviceObjectFromANGLE(EGL_D3D11_DEVICE_ANGLE));
77 return d3d11_device;
78}
79
80base::win::ScopedComPtr<IDirect3DDevice9> QueryD3D9DeviceObjectFromANGLE() {
81 base::win::ScopedComPtr<IDirect3DDevice9> d3d9_device;
82 d3d9_device = reinterpret_cast<IDirect3DDevice9*>(
83 QueryDeviceObjectFromANGLE(EGL_D3D9_DEVICE_ANGLE));
84 return d3d9_device;
85}
86
jbauman2a2ad222017-03-02 00:35:1387base::win::ScopedComPtr<IDCompositionDevice2> QueryDirectCompositionDevice(
88 base::win::ScopedComPtr<ID3D11Device> d3d11_device) {
89 // Each D3D11 device will have a DirectComposition device stored in its
90 // private data under this GUID.
91 // {CF81D85A-8D30-4769-8509-B9D73898D870}
92 static const GUID kDirectCompositionGUID = {
93 0xcf81d85a,
94 0x8d30,
95 0x4769,
96 {0x85, 0x9, 0xb9, 0xd7, 0x38, 0x98, 0xd8, 0x70}};
97
98 base::win::ScopedComPtr<IDCompositionDevice2> dcomp_device;
99 if (!d3d11_device)
100 return dcomp_device;
101
robliaob44b9072017-04-19 00:28:09102 UINT data_size = sizeof(dcomp_device.Get());
jbauman2a2ad222017-03-02 00:35:13103 HRESULT hr = d3d11_device->GetPrivateData(kDirectCompositionGUID, &data_size,
Robert Liao733f2852017-07-25 17:49:24104 dcomp_device.GetAddressOf());
jbauman2a2ad222017-03-02 00:35:13105 if (SUCCEEDED(hr) && dcomp_device)
106 return dcomp_device;
107
108 // Allocate a new DirectComposition device if none currently exists.
109 HMODULE dcomp_module = ::GetModuleHandle(L"dcomp.dll");
110 if (!dcomp_module)
111 return dcomp_device;
112
113 using PFN_DCOMPOSITION_CREATE_DEVICE2 = HRESULT(WINAPI*)(
114 IUnknown * renderingDevice, REFIID iid, void** dcompositionDevice);
115 PFN_DCOMPOSITION_CREATE_DEVICE2 create_device_function =
116 reinterpret_cast<PFN_DCOMPOSITION_CREATE_DEVICE2>(
117 ::GetProcAddress(dcomp_module, "DCompositionCreateDevice2"));
118 if (!create_device_function)
119 return dcomp_device;
120
121 base::win::ScopedComPtr<IDXGIDevice> dxgi_device;
robliao310fa98b2017-05-11 17:14:00122 d3d11_device.CopyTo(dxgi_device.GetAddressOf());
jbauman2a2ad222017-03-02 00:35:13123 base::win::ScopedComPtr<IDCompositionDesktopDevice> desktop_device;
robliaob44b9072017-04-19 00:28:09124 hr = create_device_function(dxgi_device.Get(),
robliao310fa98b2017-05-11 17:14:00125 IID_PPV_ARGS(desktop_device.GetAddressOf()));
jbauman2a2ad222017-03-02 00:35:13126 if (FAILED(hr))
127 return dcomp_device;
128
robliao310fa98b2017-05-11 17:14:00129 hr = desktop_device.CopyTo(dcomp_device.GetAddressOf());
jbauman2a2ad222017-03-02 00:35:13130 CHECK(SUCCEEDED(hr));
131 d3d11_device->SetPrivateDataInterface(kDirectCompositionGUID,
robliaob44b9072017-04-19 00:28:09132 dcomp_device.Get());
jbauman2a2ad222017-03-02 00:35:13133
134 return dcomp_device;
135}
136
jbauman575788b2016-12-15 21:06:22137} // namespace gl