blob: 627dc879d7842f16ac411e97dc249a6e79ca3473 [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{
Jon Ashburn1fec4242014-11-26 11:10:26 -070066 XGL_RESULT result;
Jon Ashburn5f3960e2015-04-02 12:06:28 -060067 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburn1fec4242014-11-26 11:10:26 -070068
Jon Ashburn5f3960e2015-04-02 12:06:28 -060069 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
Chia-I Wuf1a5a742014-12-27 15:16:07 +080070 if (!strncmp(pExtName, "xglLayerExtension1", strlen("xglLayerExtension1")))
Jon Ashburn5f3960e2015-04-02 12:06:28 -060071 {
Jon Ashburn1fec4242014-11-26 11:10:26 -070072 result = XGL_SUCCESS;
Jon Ashburn5f3960e2015-04-02 12:06:28 -060073 } else if (!strncmp(pExtName, "Basic", strlen("Basic")))
74 {
75 result = XGL_SUCCESS;
76 } else if (!tableMap.empty() && (tableMap.find(gpuw) != tableMap.end()))
77 {
78 printf("At start of wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
79 XGL_LAYER_DISPATCH_TABLE* pTable = tableMap[gpuw];
Jon Ashburn1fec4242014-11-26 11:10:26 -070080 result = pTable->GetExtensionSupport((XGL_PHYSICAL_GPU)gpuw->nextObject, pExtName);
Jon Ashburn5f3960e2015-04-02 12:06:28 -060081 printf("Completed wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
82 } else
83 {
84 result = XGL_ERROR_INVALID_EXTENSION;
85 }
Jon Ashburn1fec4242014-11-26 11:10:26 -070086 return result;
87}
88
89XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglCreateDevice(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo, XGL_DEVICE* pDevice)
90{
91 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburn5f3960e2015-04-02 12:06:28 -060092 XGL_LAYER_DISPATCH_TABLE* pTable = tableMap[gpuw];
Jon Ashburn1fec4242014-11-26 11:10:26 -070093
94 printf("At start of wrapped xglCreateDevice() call w/ gpu: %p\n", (void*)gpu);
95 XGL_RESULT result = pTable->CreateDevice((XGL_PHYSICAL_GPU)gpuw->nextObject, pCreateInfo, pDevice);
96 // create a mapping for the device object into the dispatch table
97 tableMap.emplace(*pDevice, pTable);
98 printf("Completed wrapped xglCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
99 return result;
100}
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600101XGL_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 -0700102{
103 XGL_LAYER_DISPATCH_TABLE* pTable = tableMap[device];
104
105 printf("At start of wrapped xglGetFormatInfo() call w/ device: %p\n", (void*)device);
106 XGL_RESULT result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
107 printf("Completed wrapped xglGetFormatInfo() call w/ device: %p\n", (void*)device);
108 return result;
109}
110
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600111XGL_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 -0700112{
113 if (gpu != NULL)
114 {
115 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
116 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
117
118 printf("At start of wrapped xglEnumerateLayers() call w/ gpu: %p\n", gpu);
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600119 XGL_RESULT result = pTable->EnumerateLayers((XGL_PHYSICAL_GPU)gpuw->nextObject, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved);
Jon Ashburn1fec4242014-11-26 11:10:26 -0700120 return result;
121 } else
122 {
123 if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL)
124 return XGL_ERROR_INVALID_POINTER;
125
126 // Example of a layer that is only compatible with Intel's GPUs
127 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT*) pReserved;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600128 xglGetGpuInfoType fpGetGpuInfo;
Jon Ashburn1fec4242014-11-26 11:10:26 -0700129 XGL_PHYSICAL_GPU_PROPERTIES gpuProps;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600130 size_t dataSize = sizeof(XGL_PHYSICAL_GPU_PROPERTIES);
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600131 fpGetGpuInfo = (xglGetGpuInfoType) gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, "xglGetGpuInfo");
Jon Ashburn1fec4242014-11-26 11:10:26 -0700132 fpGetGpuInfo((XGL_PHYSICAL_GPU) gpuw->nextObject, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, &dataSize, &gpuProps);
133 if (gpuProps.vendorId == 0x8086)
134 {
135 *pOutLayerCount = 1;
136 strncpy((char *) pOutLayers[0], "Basic", maxStringSize);
137 } else
138 {
139 *pOutLayerCount = 0;
140 }
141 return XGL_SUCCESS;
142 }
143}
144
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600145XGL_LAYER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char* pName)
Jon Ashburn79113cc2014-12-01 14:22:40 -0700146{
Jon Ashburn1fec4242014-11-26 11:10:26 -0700147 if (gpu == NULL)
148 return NULL;
Chia-I Wub665d942015-01-05 09:41:27 +0800149
150 initLayerTable((const XGL_BASE_LAYER_OBJECT *) gpu);
151
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800152 if (!strncmp("xglGetProcAddr", pName, sizeof("xglGetProcAddr")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600153 return (void *) xglGetProcAddr;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800154 else if (!strncmp("xglCreateDevice", pName, sizeof ("xglCreateDevice")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600155 return (void *) xglCreateDevice;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800156 else if (!strncmp("xglGetExtensionSupport", pName, sizeof ("xglGetExtensionSupport")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600157 return (void *) xglGetExtensionSupport;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800158 else if (!strncmp("xglEnumerateLayers", pName, sizeof ("xglEnumerateLayers")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600159 return (void *) xglEnumerateLayers;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800160 else if (!strncmp("xglGetFormatInfo", pName, sizeof ("xglGetFormatInfo")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600161 return (void *) xglGetFormatInfo;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800162 else if (!strncmp("xglLayerExtension1", pName, sizeof("xglLayerExtension1")))
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600163 return (void *) xglLayerExtension1;
Jon Ashburn1fec4242014-11-26 11:10:26 -0700164 else {
165 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
166 if (gpuw->pGPA == NULL)
167 return NULL;
168 return gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, pName);
169 }
170}