1 //
2 // Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // Device.cpp: Implements the egl::Device class, representing the abstract
8 // device. Implements EGLDevice.
9 
10 #include "libANGLE/Device.h"
11 
12 #include <iterator>
13 
14 #include <platform/Platform.h>
15 #include <EGL/eglext.h>
16 
17 #include "common/debug.h"
18 #include "common/platform.h"
19 #include "libANGLE/renderer/DeviceImpl.h"
20 
21 #if defined(ANGLE_ENABLE_D3D11)
22 #include "libANGLE/renderer/d3d/DeviceD3D.h"
23 #endif
24 
25 namespace egl
26 {
27 
28 template <typename T>
GenerateExtensionsString(const T & extensions)29 static std::string GenerateExtensionsString(const T &extensions)
30 {
31     std::vector<std::string> extensionsVector = extensions.getStrings();
32 
33     std::ostringstream stream;
34     std::copy(extensionsVector.begin(), extensionsVector.end(), std::ostream_iterator<std::string>(stream, " "));
35     return stream.str();
36 }
37 
38 typedef std::set<egl::Device *> DeviceSet;
GetDeviceSet()39 static DeviceSet *GetDeviceSet()
40 {
41     static DeviceSet devices;
42     return &devices;
43 }
44 
45 // Static factory methods
CreateDevice(void * devicePointer,EGLint deviceType,Device ** outDevice)46 egl::Error Device::CreateDevice(void *devicePointer, EGLint deviceType, Device **outDevice)
47 {
48     *outDevice = nullptr;
49 
50 #if defined(ANGLE_ENABLE_D3D11)
51     if (deviceType == EGL_D3D11_DEVICE_ANGLE)
52     {
53         std::unique_ptr<rx::DeviceD3D> deviceD3D(new rx::DeviceD3D());
54         ANGLE_TRY(deviceD3D->initialize(devicePointer, deviceType, EGL_TRUE));
55         *outDevice = new Device(nullptr, deviceD3D.release());
56         GetDeviceSet()->insert(*outDevice);
57         return NoError();
58     }
59 #endif
60 
61     // Note that creating an EGL device from inputted D3D9 parameters isn't currently supported
62     return EglBadAttribute();
63 }
64 
CreateDevice(Display * owningDisplay,rx::DeviceImpl * impl,Device ** outDevice)65 egl::Error Device::CreateDevice(Display *owningDisplay, rx::DeviceImpl *impl, Device **outDevice)
66 {
67     *outDevice = new Device(owningDisplay, impl);
68     GetDeviceSet()->insert(*outDevice);
69     return NoError();
70 }
71 
IsValidDevice(Device * device)72 bool Device::IsValidDevice(Device *device)
73 {
74     const DeviceSet *deviceSet = GetDeviceSet();
75     return deviceSet->find(device) != deviceSet->end();
76 }
77 
Device(Display * owningDisplay,rx::DeviceImpl * impl)78 Device::Device(Display *owningDisplay, rx::DeviceImpl *impl)
79     : mOwningDisplay(owningDisplay), mImplementation(impl)
80 {
81     initDeviceExtensions();
82 }
83 
~Device()84 Device::~Device()
85 {
86     ASSERT(GetDeviceSet()->find(this) != GetDeviceSet()->end());
87     GetDeviceSet()->erase(this);
88 
89     if (mImplementation->deviceExternallySourced())
90     {
91         // If the device isn't externally sourced then it is up to the renderer to delete the impl
92         SafeDelete(mImplementation);
93     }
94 }
95 
getDevice(EGLAttrib * value)96 Error Device::getDevice(EGLAttrib *value)
97 {
98     void *nativeDevice = nullptr;
99     egl::Error error = getImplementation()->getDevice(&nativeDevice);
100     *value = reinterpret_cast<EGLAttrib>(nativeDevice);
101     return error;
102 }
103 
getType()104 EGLint Device::getType()
105 {
106     return getImplementation()->getType();
107 }
108 
initDeviceExtensions()109 void Device::initDeviceExtensions()
110 {
111     mImplementation->generateExtensions(&mDeviceExtensions);
112     mDeviceExtensionString = GenerateExtensionsString(mDeviceExtensions);
113 }
114 
getExtensions() const115 const DeviceExtensions &Device::getExtensions() const
116 {
117     return mDeviceExtensions;
118 }
119 
getExtensionString() const120 const std::string &Device::getExtensionString() const
121 {
122     return mDeviceExtensionString;
123 }
124 
125 }
126