blob: 6bd8faab0692c549bd6a6400a2a72136c2ace971 [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
7#include "base/trace_event/trace_event.h"
8#include "third_party/angle/include/EGL/egl.h"
9#include "third_party/angle/include/EGL/eglext.h"
10#include "ui/gl/gl_surface_egl.h"
11
12namespace gl {
13
14void* QueryDeviceObjectFromANGLE(int object_type) {
15 EGLDisplay egl_display = nullptr;
16 intptr_t egl_device = 0;
17 intptr_t device = 0;
18
19 {
20 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. GetHardwareDisplay");
21 egl_display = gl::GLSurfaceEGL::GetHardwareDisplay();
22 }
23
24 if (!gl::GLSurfaceEGL::HasEGLExtension("EGL_EXT_device_query"))
25 return nullptr;
26
27 PFNEGLQUERYDISPLAYATTRIBEXTPROC QueryDisplayAttribEXT = nullptr;
28
29 {
30 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. eglGetProcAddress");
31
32 QueryDisplayAttribEXT = reinterpret_cast<PFNEGLQUERYDISPLAYATTRIBEXTPROC>(
33 eglGetProcAddress("eglQueryDisplayAttribEXT"));
34
35 if (!QueryDisplayAttribEXT)
36 return nullptr;
37 }
38
39 PFNEGLQUERYDEVICEATTRIBEXTPROC QueryDeviceAttribEXT = nullptr;
40
41 {
42 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. eglGetProcAddress");
43
44 QueryDeviceAttribEXT = reinterpret_cast<PFNEGLQUERYDEVICEATTRIBEXTPROC>(
45 eglGetProcAddress("eglQueryDeviceAttribEXT"));
46 if (!QueryDeviceAttribEXT)
47 return nullptr;
48 }
49
50 {
51 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. QueryDisplayAttribEXT");
52
53 if (!QueryDisplayAttribEXT(egl_display, EGL_DEVICE_EXT, &egl_device))
54 return nullptr;
55 }
56 if (!egl_device)
57 return nullptr;
58
59 {
60 TRACE_EVENT0("gpu", "QueryDeviceObjectFromANGLE. QueryDisplayAttribEXT");
61
62 if (!QueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(egl_device),
63 object_type, &device)) {
64 return nullptr;
65 }
66 }
67
68 return reinterpret_cast<void*>(device);
69}
70
71base::win::ScopedComPtr<ID3D11Device> QueryD3D11DeviceObjectFromANGLE() {
72 base::win::ScopedComPtr<ID3D11Device> d3d11_device;
73 d3d11_device = reinterpret_cast<ID3D11Device*>(
74 QueryDeviceObjectFromANGLE(EGL_D3D11_DEVICE_ANGLE));
75 return d3d11_device;
76}
77
78base::win::ScopedComPtr<IDirect3DDevice9> QueryD3D9DeviceObjectFromANGLE() {
79 base::win::ScopedComPtr<IDirect3DDevice9> d3d9_device;
80 d3d9_device = reinterpret_cast<IDirect3DDevice9*>(
81 QueryDeviceObjectFromANGLE(EGL_D3D9_DEVICE_ANGLE));
82 return d3d9_device;
83}
84
jbauman2a2ad222017-03-02 00:35:1385base::win::ScopedComPtr<IDCompositionDevice2> QueryDirectCompositionDevice(
86 base::win::ScopedComPtr<ID3D11Device> d3d11_device) {
87 // Each D3D11 device will have a DirectComposition device stored in its
88 // private data under this GUID.
89 // {CF81D85A-8D30-4769-8509-B9D73898D870}
90 static const GUID kDirectCompositionGUID = {
91 0xcf81d85a,
92 0x8d30,
93 0x4769,
94 {0x85, 0x9, 0xb9, 0xd7, 0x38, 0x98, 0xd8, 0x70}};
95
96 base::win::ScopedComPtr<IDCompositionDevice2> dcomp_device;
97 if (!d3d11_device)
98 return dcomp_device;
99
100 UINT data_size = sizeof(dcomp_device.get());
101 HRESULT hr = d3d11_device->GetPrivateData(kDirectCompositionGUID, &data_size,
102 dcomp_device.ReceiveVoid());
103 if (SUCCEEDED(hr) && dcomp_device)
104 return dcomp_device;
105
106 // Allocate a new DirectComposition device if none currently exists.
107 HMODULE dcomp_module = ::GetModuleHandle(L"dcomp.dll");
108 if (!dcomp_module)
109 return dcomp_device;
110
111 using PFN_DCOMPOSITION_CREATE_DEVICE2 = HRESULT(WINAPI*)(
112 IUnknown * renderingDevice, REFIID iid, void** dcompositionDevice);
113 PFN_DCOMPOSITION_CREATE_DEVICE2 create_device_function =
114 reinterpret_cast<PFN_DCOMPOSITION_CREATE_DEVICE2>(
115 ::GetProcAddress(dcomp_module, "DCompositionCreateDevice2"));
116 if (!create_device_function)
117 return dcomp_device;
118
119 base::win::ScopedComPtr<IDXGIDevice> dxgi_device;
120 d3d11_device.QueryInterface(dxgi_device.Receive());
121 base::win::ScopedComPtr<IDCompositionDesktopDevice> desktop_device;
122 hr = create_device_function(dxgi_device.get(),
123 IID_PPV_ARGS(desktop_device.Receive()));
124 if (FAILED(hr))
125 return dcomp_device;
126
127 hr = desktop_device.QueryInterface(dcomp_device.Receive());
128 CHECK(SUCCEEDED(hr));
129 d3d11_device->SetPrivateDataInterface(kDirectCompositionGUID,
130 dcomp_device.get());
131
132 return dcomp_device;
133}
134
jbauman575788b2016-12-15 21:06:22135} // namespace gl