jbauman | 575788b | 2016-12-15 21:06:22 | [diff] [blame] | 1 | // 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 | |
robliao | 6fc41c6 | 2017-04-28 22:41:17 | [diff] [blame] | 7 | #include <objbase.h> |
| 8 | |
jbauman | 575788b | 2016-12-15 21:06:22 | [diff] [blame] | 9 | #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 | |
| 14 | namespace gl { |
| 15 | |
| 16 | void* 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 | |
| 73 | base::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 | |
| 80 | base::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 | |
jbauman | 2a2ad22 | 2017-03-02 00:35:13 | [diff] [blame] | 87 | base::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 | |
robliao | b44b907 | 2017-04-19 00:28:09 | [diff] [blame] | 102 | UINT data_size = sizeof(dcomp_device.Get()); |
jbauman | 2a2ad22 | 2017-03-02 00:35:13 | [diff] [blame] | 103 | HRESULT hr = d3d11_device->GetPrivateData(kDirectCompositionGUID, &data_size, |
Robert Liao | 733f285 | 2017-07-25 17:49:24 | [diff] [blame] | 104 | dcomp_device.GetAddressOf()); |
jbauman | 2a2ad22 | 2017-03-02 00:35:13 | [diff] [blame] | 105 | 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; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 122 | d3d11_device.CopyTo(dxgi_device.GetAddressOf()); |
jbauman | 2a2ad22 | 2017-03-02 00:35:13 | [diff] [blame] | 123 | base::win::ScopedComPtr<IDCompositionDesktopDevice> desktop_device; |
robliao | b44b907 | 2017-04-19 00:28:09 | [diff] [blame] | 124 | hr = create_device_function(dxgi_device.Get(), |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 125 | IID_PPV_ARGS(desktop_device.GetAddressOf())); |
jbauman | 2a2ad22 | 2017-03-02 00:35:13 | [diff] [blame] | 126 | if (FAILED(hr)) |
| 127 | return dcomp_device; |
| 128 | |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 129 | hr = desktop_device.CopyTo(dcomp_device.GetAddressOf()); |
jbauman | 2a2ad22 | 2017-03-02 00:35:13 | [diff] [blame] | 130 | CHECK(SUCCEEDED(hr)); |
| 131 | d3d11_device->SetPrivateDataInterface(kDirectCompositionGUID, |
robliao | b44b907 | 2017-04-19 00:28:09 | [diff] [blame] | 132 | dcomp_device.Get()); |
jbauman | 2a2ad22 | 2017-03-02 00:35:13 | [diff] [blame] | 133 | |
| 134 | return dcomp_device; |
| 135 | } |
| 136 | |
jbauman | 575788b | 2016-12-15 21:06:22 | [diff] [blame] | 137 | } // namespace gl |