blob: 5b2bb58002df99494be6448f101d4d4bbd7b0d21 [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>
Chia-I Wuaa4121f2015-01-04 23:11:43 +080028#include "xgl_dispatch_table_helper.h"
Jon Ashburn1fec4242014-11-26 11:10:26 -070029#include "xglLayer.h"
30
31static std::unordered_map<XGL_VOID *, XGL_LAYER_DISPATCH_TABLE *> tableMap;
32
33static XGL_LAYER_DISPATCH_TABLE * initLayerTable(const XGL_BASE_LAYER_OBJECT *gpuw)
34{
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -060035 xglGetProcAddrType fpGPA;
Jon Ashburn1fec4242014-11-26 11:10:26 -070036 XGL_LAYER_DISPATCH_TABLE *pTable;
37
38 assert(gpuw);
39 std::unordered_map<XGL_VOID *, XGL_LAYER_DISPATCH_TABLE *>::const_iterator it = tableMap.find((XGL_VOID *) gpuw);
40 if (it == tableMap.end())
41 {
42 pTable = new XGL_LAYER_DISPATCH_TABLE;
43 tableMap[(XGL_VOID *) gpuw] = pTable;
44 } else
45 {
46 return it->second;
47 }
Chia-I Wuaa4121f2015-01-04 23:11:43 +080048
49 layer_initialize_dispatch_table(pTable, gpuw->pGPA, (XGL_PHYSICAL_GPU) gpuw->nextObject);
50
Jon Ashburn1fec4242014-11-26 11:10:26 -070051 return pTable;
52}
53
54XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglLayerExtension1(XGL_DEVICE device)
55{
56 printf("In xglLayerExtension1() call w/ device: %p\n", (void*)device);
57 printf("xglLayerExtension1 returning SUCCESS\n");
58 return XGL_SUCCESS;
59}
60
61XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglGetExtensionSupport(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* pExtName)
62{
63 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
64 XGL_RESULT result;
65 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
66
67 printf("At start of wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
Chia-I Wuf1a5a742014-12-27 15:16:07 +080068 if (!strncmp(pExtName, "xglLayerExtension1", strlen("xglLayerExtension1")))
Jon Ashburn1fec4242014-11-26 11:10:26 -070069 result = XGL_SUCCESS;
70 else
71 result = pTable->GetExtensionSupport((XGL_PHYSICAL_GPU)gpuw->nextObject, pExtName);
72 printf("Completed wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
73 return result;
74}
75
76XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglCreateDevice(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo, XGL_DEVICE* pDevice)
77{
78 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
79 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
80
81 printf("At start of wrapped xglCreateDevice() call w/ gpu: %p\n", (void*)gpu);
82 XGL_RESULT result = pTable->CreateDevice((XGL_PHYSICAL_GPU)gpuw->nextObject, pCreateInfo, pDevice);
83 // create a mapping for the device object into the dispatch table
84 tableMap.emplace(*pDevice, pTable);
85 printf("Completed wrapped xglCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
86 return result;
87}
88XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglGetFormatInfo(XGL_DEVICE device, XGL_FORMAT format, XGL_FORMAT_INFO_TYPE infoType, XGL_SIZE* pDataSize, XGL_VOID* pData)
89{
90 XGL_LAYER_DISPATCH_TABLE* pTable = tableMap[device];
91
92 printf("At start of wrapped xglGetFormatInfo() call w/ device: %p\n", (void*)device);
93 XGL_RESULT result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
94 printf("Completed wrapped xglGetFormatInfo() call w/ device: %p\n", (void*)device);
95 return result;
96}
97
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -060098XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(XGL_PHYSICAL_GPU gpu, XGL_SIZE maxLayerCount, XGL_SIZE maxStringSize, XGL_SIZE* pOutLayerCount, XGL_CHAR* const* pOutLayers, XGL_VOID* pReserved)
Jon Ashburn1fec4242014-11-26 11:10:26 -070099{
100 if (gpu != NULL)
101 {
102 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
103 XGL_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw);
104
105 printf("At start of wrapped xglEnumerateLayers() call w/ gpu: %p\n", gpu);
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600106 XGL_RESULT result = pTable->EnumerateLayers((XGL_PHYSICAL_GPU)gpuw->nextObject, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved);
Jon Ashburn1fec4242014-11-26 11:10:26 -0700107 return result;
108 } else
109 {
110 if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL)
111 return XGL_ERROR_INVALID_POINTER;
112
113 // Example of a layer that is only compatible with Intel's GPUs
114 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT*) pReserved;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600115 xglGetGpuInfoType fpGetGpuInfo;
Jon Ashburn1fec4242014-11-26 11:10:26 -0700116 XGL_PHYSICAL_GPU_PROPERTIES gpuProps;
117 XGL_SIZE dataSize = sizeof(XGL_PHYSICAL_GPU_PROPERTIES);
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600118 fpGetGpuInfo = (xglGetGpuInfoType) gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, "xglGetGpuInfo");
Jon Ashburn1fec4242014-11-26 11:10:26 -0700119 fpGetGpuInfo((XGL_PHYSICAL_GPU) gpuw->nextObject, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, &dataSize, &gpuProps);
120 if (gpuProps.vendorId == 0x8086)
121 {
122 *pOutLayerCount = 1;
123 strncpy((char *) pOutLayers[0], "Basic", maxStringSize);
124 } else
125 {
126 *pOutLayerCount = 0;
127 }
128 return XGL_SUCCESS;
129 }
130}
131
Jon Ashburn79113cc2014-12-01 14:22:40 -0700132XGL_LAYER_EXPORT XGL_VOID * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* pName)
133{
Jon Ashburn1fec4242014-11-26 11:10:26 -0700134 if (gpu == NULL)
135 return NULL;
Chia-I Wub665d942015-01-05 09:41:27 +0800136
137 initLayerTable((const XGL_BASE_LAYER_OBJECT *) gpu);
138
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800139 if (!strncmp("xglGetProcAddr", pName, sizeof("xglGetProcAddr")))
Jon Ashburn1fec4242014-11-26 11:10:26 -0700140 return (XGL_VOID *) xglGetProcAddr;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800141 else if (!strncmp("xglCreateDevice", pName, sizeof ("xglCreateDevice")))
Jon Ashburn1fec4242014-11-26 11:10:26 -0700142 return (XGL_VOID *) xglCreateDevice;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800143 else if (!strncmp("xglGetExtensionSupport", pName, sizeof ("xglGetExtensionSupport")))
Jon Ashburn1fec4242014-11-26 11:10:26 -0700144 return (XGL_VOID *) xglGetExtensionSupport;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800145 else if (!strncmp("xglEnumerateLayers", pName, sizeof ("xglEnumerateLayers")))
Jon Ashburn1fec4242014-11-26 11:10:26 -0700146 return (XGL_VOID *) xglEnumerateLayers;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800147 else if (!strncmp("xglGetFormatInfo", pName, sizeof ("xglGetFormatInfo")))
Jon Ashburn1fec4242014-11-26 11:10:26 -0700148 return (XGL_VOID *) xglGetFormatInfo;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800149 else if (!strncmp("xglLayerExtension1", pName, sizeof("xglLayerExtension1")))
Jon Ashburn1fec4242014-11-26 11:10:26 -0700150 return (XGL_VOID *) xglLayerExtension1;
151 else {
152 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
153 if (gpuw->pGPA == NULL)
154 return NULL;
155 return gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, pName);
156 }
157}