blob: ac8cf803523eeedc131d688a3d0dcbfd5603b9bd [file] [log] [blame]
Jon Ashburn8d8dad02014-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 Ashburn227a9422014-11-26 11:10:26 -070024#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <unordered_map>
Ian Elliott81ac44c2015-01-13 17:52:38 -070028#include "loader_platform.h"
Chia-I Wu0f65b1e2015-01-04 23:11:43 +080029#include "xgl_dispatch_table_helper.h"
Jon Ashburn227a9422014-11-26 11:10:26 -070030#include "xglLayer.h"
31
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060032static std::unordered_map<void *, XGL_LAYER_DISPATCH_TABLE *> tableMap;
Jon Ashburn227a9422014-11-26 11:10:26 -070033
34static XGL_LAYER_DISPATCH_TABLE * initLayerTable(const XGL_BASE_LAYER_OBJECT *gpuw)
35{
Mark Lobodzinski953a1692015-01-09 15:12:03 -060036 xglGetProcAddrType fpGPA;
Jon Ashburn227a9422014-11-26 11:10:26 -070037 XGL_LAYER_DISPATCH_TABLE *pTable;
38
39 assert(gpuw);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060040 std::unordered_map<void *, XGL_LAYER_DISPATCH_TABLE *>::const_iterator it = tableMap.find((void *) gpuw);
Jon Ashburn227a9422014-11-26 11:10:26 -070041 if (it == tableMap.end())
42 {
43 pTable = new XGL_LAYER_DISPATCH_TABLE;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060044 tableMap[(void *) gpuw] = pTable;
Jon Ashburn227a9422014-11-26 11:10:26 -070045 } else
46 {
47 return it->second;
48 }
Chia-I Wu0f65b1e2015-01-04 23:11:43 +080049
50 layer_initialize_dispatch_table(pTable, gpuw->pGPA, (XGL_PHYSICAL_GPU) gpuw->nextObject);
51
Jon Ashburn227a9422014-11-26 11:10:26 -070052 return pTable;
53}
54
55XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglLayerExtension1(XGL_DEVICE device)
56{
57 printf("In xglLayerExtension1() call w/ device: %p\n", (void*)device);
58 printf("xglLayerExtension1 returning SUCCESS\n");
59 return XGL_SUCCESS;
60}
61
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060062XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglGetExtensionSupport(XGL_PHYSICAL_GPU gpu, const char* pExtName)
Jon Ashburn227a9422014-11-26 11:10:26 -070063{
64 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
65 XGL_RESULT result;
66 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
67
68 printf("At start of wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
Chia-I Wu7461fcf2014-12-27 15:16:07 +080069 if (!strncmp(pExtName, "xglLayerExtension1", strlen("xglLayerExtension1")))
Jon Ashburn227a9422014-11-26 11:10:26 -070070 result = XGL_SUCCESS;
71 else
72 result = pTable->GetExtensionSupport((XGL_PHYSICAL_GPU)gpuw->nextObject, pExtName);
73 printf("Completed wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
74 return result;
75}
76
77XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglCreateDevice(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo, XGL_DEVICE* pDevice)
78{
79 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
80 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
81
82 printf("At start of wrapped xglCreateDevice() call w/ gpu: %p\n", (void*)gpu);
83 XGL_RESULT result = pTable->CreateDevice((XGL_PHYSICAL_GPU)gpuw->nextObject, pCreateInfo, pDevice);
84 // create a mapping for the device object into the dispatch table
85 tableMap.emplace(*pDevice, pTable);
86 printf("Completed wrapped xglCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
87 return result;
88}
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060089XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglGetFormatInfo(XGL_DEVICE device, XGL_FORMAT format, XGL_FORMAT_INFO_TYPE infoType, size_t* pDataSize, void* pData)
Jon Ashburn227a9422014-11-26 11:10:26 -070090{
91 XGL_LAYER_DISPATCH_TABLE* pTable = tableMap[device];
92
93 printf("At start of wrapped xglGetFormatInfo() call w/ device: %p\n", (void*)device);
94 XGL_RESULT result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
95 printf("Completed wrapped xglGetFormatInfo() call w/ device: %p\n", (void*)device);
96 return result;
97}
98
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060099XGL_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 Ashburn227a9422014-11-26 11:10:26 -0700100{
101 if (gpu != NULL)
102 {
103 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
104 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
105
106 printf("At start of wrapped xglEnumerateLayers() call w/ gpu: %p\n", gpu);
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600107 XGL_RESULT result = pTable->EnumerateLayers((XGL_PHYSICAL_GPU)gpuw->nextObject, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved);
Jon Ashburn227a9422014-11-26 11:10:26 -0700108 return result;
109 } else
110 {
111 if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL)
112 return XGL_ERROR_INVALID_POINTER;
113
114 // Example of a layer that is only compatible with Intel's GPUs
115 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT*) pReserved;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600116 xglGetGpuInfoType fpGetGpuInfo;
Jon Ashburn227a9422014-11-26 11:10:26 -0700117 XGL_PHYSICAL_GPU_PROPERTIES gpuProps;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600118 size_t dataSize = sizeof(XGL_PHYSICAL_GPU_PROPERTIES);
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600119 fpGetGpuInfo = (xglGetGpuInfoType) gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, "xglGetGpuInfo");
Jon Ashburn227a9422014-11-26 11:10:26 -0700120 fpGetGpuInfo((XGL_PHYSICAL_GPU) gpuw->nextObject, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, &dataSize, &gpuProps);
121 if (gpuProps.vendorId == 0x8086)
122 {
123 *pOutLayerCount = 1;
124 strncpy((char *) pOutLayers[0], "Basic", maxStringSize);
125 } else
126 {
127 *pOutLayerCount = 0;
128 }
129 return XGL_SUCCESS;
130 }
131}
132
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600133XGL_LAYER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700134{
Jon Ashburn227a9422014-11-26 11:10:26 -0700135 if (gpu == NULL)
136 return NULL;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800137
138 initLayerTable((const XGL_BASE_LAYER_OBJECT *) gpu);
139
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800140 if (!strncmp("xglGetProcAddr", pName, sizeof("xglGetProcAddr")))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600141 return (void *) xglGetProcAddr;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800142 else if (!strncmp("xglCreateDevice", pName, sizeof ("xglCreateDevice")))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600143 return (void *) xglCreateDevice;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800144 else if (!strncmp("xglGetExtensionSupport", pName, sizeof ("xglGetExtensionSupport")))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600145 return (void *) xglGetExtensionSupport;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800146 else if (!strncmp("xglEnumerateLayers", pName, sizeof ("xglEnumerateLayers")))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600147 return (void *) xglEnumerateLayers;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800148 else if (!strncmp("xglGetFormatInfo", pName, sizeof ("xglGetFormatInfo")))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600149 return (void *) xglGetFormatInfo;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800150 else if (!strncmp("xglLayerExtension1", pName, sizeof("xglLayerExtension1")))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600151 return (void *) xglLayerExtension1;
Jon Ashburn227a9422014-11-26 11:10:26 -0700152 else {
153 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
154 if (gpuw->pGPA == NULL)
155 return NULL;
156 return gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, pName);
157 }
158}