Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <assert.h> |
| 27 | #include <unordered_map> |
Chia-I Wu | aa4121f | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 28 | #include "xgl_dispatch_table_helper.h" |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 29 | #include "xglLayer.h" |
| 30 | |
| 31 | static std::unordered_map<XGL_VOID *, XGL_LAYER_DISPATCH_TABLE *> tableMap; |
| 32 | |
| 33 | static XGL_LAYER_DISPATCH_TABLE * initLayerTable(const XGL_BASE_LAYER_OBJECT *gpuw) |
| 34 | { |
Mark Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame^] | 35 | xglGetProcAddrType fpGPA; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 36 | 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 Wu | aa4121f | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 48 | |
| 49 | layer_initialize_dispatch_table(pTable, gpuw->pGPA, (XGL_PHYSICAL_GPU) gpuw->nextObject); |
| 50 | |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 51 | return pTable; |
| 52 | } |
| 53 | |
| 54 | XGL_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 | |
| 61 | XGL_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 Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 68 | if (!strncmp(pExtName, "xglLayerExtension1", strlen("xglLayerExtension1"))) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 69 | 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 | |
| 76 | XGL_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 | } |
| 88 | XGL_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 Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame^] | 98 | XGL_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 Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 99 | { |
| 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 Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame^] | 106 | XGL_RESULT result = pTable->EnumerateLayers((XGL_PHYSICAL_GPU)gpuw->nextObject, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 107 | 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 Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame^] | 115 | xglGetGpuInfoType fpGetGpuInfo; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 116 | XGL_PHYSICAL_GPU_PROPERTIES gpuProps; |
| 117 | XGL_SIZE dataSize = sizeof(XGL_PHYSICAL_GPU_PROPERTIES); |
Mark Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame^] | 118 | fpGetGpuInfo = (xglGetGpuInfoType) gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, "xglGetGpuInfo"); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 119 | 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 Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 132 | XGL_LAYER_EXPORT XGL_VOID * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* pName) |
| 133 | { |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 134 | if (gpu == NULL) |
| 135 | return NULL; |
Chia-I Wu | b665d94 | 2015-01-05 09:41:27 +0800 | [diff] [blame] | 136 | |
| 137 | initLayerTable((const XGL_BASE_LAYER_OBJECT *) gpu); |
| 138 | |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 139 | if (!strncmp("xglGetProcAddr", pName, sizeof("xglGetProcAddr"))) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 140 | return (XGL_VOID *) xglGetProcAddr; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 141 | else if (!strncmp("xglCreateDevice", pName, sizeof ("xglCreateDevice"))) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 142 | return (XGL_VOID *) xglCreateDevice; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 143 | else if (!strncmp("xglGetExtensionSupport", pName, sizeof ("xglGetExtensionSupport"))) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 144 | return (XGL_VOID *) xglGetExtensionSupport; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 145 | else if (!strncmp("xglEnumerateLayers", pName, sizeof ("xglEnumerateLayers"))) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 146 | return (XGL_VOID *) xglEnumerateLayers; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 147 | else if (!strncmp("xglGetFormatInfo", pName, sizeof ("xglGetFormatInfo"))) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 148 | return (XGL_VOID *) xglGetFormatInfo; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 149 | else if (!strncmp("xglLayerExtension1", pName, sizeof("xglLayerExtension1"))) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 150 | 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 | } |