blob: 2f3deac0b3b09f73f25cb84880fa069eeb4e2142 [file] [log] [blame]
Jon Ashburn79113cc2014-12-01 14:22:40 -07001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Jon Ashburn1fec4242014-11-26 11:10:26 -070024#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <unordered_map>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070028#include "loader_platform.h"
Chia-I Wuaa4121f2015-01-04 23:11:43 +080029#include "xgl_dispatch_table_helper.h"
Jon Ashburn1fec4242014-11-26 11:10:26 -070030#include "xglLayer.h"
Ian Elliott655cad72015-02-12 17:08:34 -070031// The following is #included again to catch certain OS-specific functions
32// being used:
33#include "loader_platform.h"
Jon Ashburn1fec4242014-11-26 11:10:26 -070034
Mark Lobodzinski17caf572015-01-29 08:55:56 -060035static std::unordered_map<void *, XGL_LAYER_DISPATCH_TABLE *> tableMap;
Jon Ashburn1fec4242014-11-26 11:10:26 -070036
37static XGL_LAYER_DISPATCH_TABLE * initLayerTable(const XGL_BASE_LAYER_OBJECT *gpuw)
38{
Jon Ashburn1fec4242014-11-26 11:10:26 -070039 XGL_LAYER_DISPATCH_TABLE *pTable;
40
41 assert(gpuw);
Mark Lobodzinski17caf572015-01-29 08:55:56 -060042 std::unordered_map<void *, XGL_LAYER_DISPATCH_TABLE *>::const_iterator it = tableMap.find((void *) gpuw);
Jon Ashburn1fec4242014-11-26 11:10:26 -070043 if (it == tableMap.end())
44 {
45 pTable = new XGL_LAYER_DISPATCH_TABLE;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060046 tableMap[(void *) gpuw] = pTable;
Jon Ashburn1fec4242014-11-26 11:10:26 -070047 } else
48 {
49 return it->second;
50 }
Chia-I Wuaa4121f2015-01-04 23:11:43 +080051
52 layer_initialize_dispatch_table(pTable, gpuw->pGPA, (XGL_PHYSICAL_GPU) gpuw->nextObject);
53
Jon Ashburn1fec4242014-11-26 11:10:26 -070054 return pTable;
55}
56
57XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglLayerExtension1(XGL_DEVICE device)
58{
59 printf("In xglLayerExtension1() call w/ device: %p\n", (void*)device);
60 printf("xglLayerExtension1 returning SUCCESS\n");
61 return XGL_SUCCESS;
62}
63
Mark Lobodzinski17caf572015-01-29 08:55:56 -060064XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglGetExtensionSupport(XGL_PHYSICAL_GPU gpu, const char* pExtName)
Jon Ashburn1fec4242014-11-26 11:10:26 -070065{
66 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
67 XGL_RESULT result;
68 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
69
70 printf("At start of wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
Chia-I Wuf1a5a742014-12-27 15:16:07 +080071 if (!strncmp(pExtName, "xglLayerExtension1", strlen("xglLayerExtension1")))
Jon Ashburn1fec4242014-11-26 11:10:26 -070072 result = XGL_SUCCESS;
73 else
74 result = pTable->GetExtensionSupport((XGL_PHYSICAL_GPU)gpuw->nextObject, pExtName);
75 printf("Completed wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
76 return result;
77}
78
79XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglCreateDevice(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo, XGL_DEVICE* pDevice)
80{
81 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
82 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
83
84 printf("At start of wrapped xglCreateDevice() call w/ gpu: %p\n", (void*)gpu);
85 XGL_RESULT result = pTable->CreateDevice((XGL_PHYSICAL_GPU)gpuw->nextObject, pCreateInfo, pDevice);
86 // create a mapping for the device object into the dispatch table
87 tableMap.emplace(*pDevice, pTable);
88 printf("Completed wrapped xglCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
89 return result;
90}
Mark Lobodzinski17caf572015-01-29 08:55:56 -060091XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglGetFormatInfo(XGL_DEVICE device, XGL_FORMAT format, XGL_FORMAT_INFO_TYPE infoType, size_t* pDataSize, void* pData)
Jon Ashburn1fec4242014-11-26 11:10:26 -070092{
93 XGL_LAYER_DISPATCH_TABLE* pTable = tableMap[device];
94
95 printf("At start of wrapped xglGetFormatInfo() call w/ device: %p\n", (void*)device);
96 XGL_RESULT result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
97 printf("Completed wrapped xglGetFormatInfo() call w/ device: %p\n", (void*)device);
98 return result;
99}
100
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600101XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(XGL_PHYSICAL_GPU gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburn1fec4242014-11-26 11:10:26 -0700102{
103 if (gpu != NULL)
104 {
105 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
106 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
107
108 printf("At start of wrapped xglEnumerateLayers() call w/ gpu: %p\n", gpu);
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600109 XGL_RESULT result = pTable->EnumerateLayers((XGL_PHYSICAL_GPU)gpuw->nextObject, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved);
Jon Ashburn1fec4242014-11-26 11:10:26 -0700110 return result;
111 } else
112 {
113 if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL)
114 return XGL_ERROR_INVALID_POINTER;
115
116 // Example of a layer that is only compatible with Intel's GPUs
117 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT*) pReserved;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600118 xglGetGpuInfoType fpGetGpuInfo;
Jon Ashburn1fec4242014-11-26 11:10:26 -0700119 XGL_PHYSICAL_GPU_PROPERTIES gpuProps;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600120 size_t dataSize = sizeof(XGL_PHYSICAL_GPU_PROPERTIES);
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600121 fpGetGpuInfo = (xglGetGpuInfoType) gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, "xglGetGpuInfo");
Jon Ashburn1fec4242014-11-26 11:10:26 -0700122 fpGetGpuInfo((XGL_PHYSICAL_GPU) gpuw->nextObject, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, &dataSize, &gpuProps);
123 if (gpuProps.vendorId == 0x8086)
124 {
125 *pOutLayerCount = 1;
126 strncpy((char *) pOutLayers[0], "Basic", maxStringSize);
127 } else
128 {
129 *pOutLayerCount = 0;
130 }
131 return XGL_SUCCESS;
132 }
133}
134
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600135XGL_LAYER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char* pName)
Jon Ashburn79113cc2014-12-01 14:22:40 -0700136{
Jon Ashburn1fec4242014-11-26 11:10:26 -0700137 if (gpu == NULL)
138 return NULL;
Chia-I Wub665d942015-01-05 09:41:27 +0800139
140 initLayerTable((const XGL_BASE_LAYER_OBJECT *) gpu);
141
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800142 if (!strncmp("xglGetProcAddr", pName, sizeof("xglGetProcAddr")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600143 return (void *) xglGetProcAddr;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800144 else if (!strncmp("xglCreateDevice", pName, sizeof ("xglCreateDevice")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600145 return (void *) xglCreateDevice;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800146 else if (!strncmp("xglGetExtensionSupport", pName, sizeof ("xglGetExtensionSupport")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600147 return (void *) xglGetExtensionSupport;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800148 else if (!strncmp("xglEnumerateLayers", pName, sizeof ("xglEnumerateLayers")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600149 return (void *) xglEnumerateLayers;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800150 else if (!strncmp("xglGetFormatInfo", pName, sizeof ("xglGetFormatInfo")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600151 return (void *) xglGetFormatInfo;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800152 else if (!strncmp("xglLayerExtension1", pName, sizeof("xglLayerExtension1")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600153 return (void *) xglLayerExtension1;
Jon Ashburn1fec4242014-11-26 11:10:26 -0700154 else {
155 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
156 if (gpuw->pGPA == NULL)
157 return NULL;
158 return gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, pName);
159 }
160}