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> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 28 | #include "loader_platform.h" |
Chia-I Wu | aa4121f | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 29 | #include "xgl_dispatch_table_helper.h" |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 30 | #include "xglLayer.h" |
Ian Elliott | 655cad7 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 31 | // The following is #included again to catch certain OS-specific functions |
| 32 | // being used: |
| 33 | #include "loader_platform.h" |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 34 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 35 | static std::unordered_map<void *, XGL_LAYER_DISPATCH_TABLE *> tableMap; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 36 | |
| 37 | static XGL_LAYER_DISPATCH_TABLE * initLayerTable(const XGL_BASE_LAYER_OBJECT *gpuw) |
| 38 | { |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 39 | XGL_LAYER_DISPATCH_TABLE *pTable; |
| 40 | |
| 41 | assert(gpuw); |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 42 | std::unordered_map<void *, XGL_LAYER_DISPATCH_TABLE *>::const_iterator it = tableMap.find((void *) gpuw); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 43 | if (it == tableMap.end()) |
| 44 | { |
| 45 | pTable = new XGL_LAYER_DISPATCH_TABLE; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 46 | tableMap[(void *) gpuw] = pTable; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 47 | } else |
| 48 | { |
| 49 | return it->second; |
| 50 | } |
Chia-I Wu | aa4121f | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 51 | |
| 52 | layer_initialize_dispatch_table(pTable, gpuw->pGPA, (XGL_PHYSICAL_GPU) gpuw->nextObject); |
| 53 | |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 54 | return pTable; |
| 55 | } |
| 56 | |
| 57 | XGL_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 Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 64 | XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglGetExtensionSupport(XGL_PHYSICAL_GPU gpu, const char* pExtName) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 65 | { |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 66 | XGL_RESULT result; |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame^] | 67 | XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 68 | |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame^] | 69 | /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */ |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 70 | if (!strncmp(pExtName, "xglLayerExtension1", strlen("xglLayerExtension1"))) |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame^] | 71 | { |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 72 | result = XGL_SUCCESS; |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame^] | 73 | } 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 Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 80 | result = pTable->GetExtensionSupport((XGL_PHYSICAL_GPU)gpuw->nextObject, pExtName); |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame^] | 81 | printf("Completed wrapped xglGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu); |
| 82 | } else |
| 83 | { |
| 84 | result = XGL_ERROR_INVALID_EXTENSION; |
| 85 | } |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 86 | return result; |
| 87 | } |
| 88 | |
| 89 | XGL_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 Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame^] | 92 | XGL_LAYER_DISPATCH_TABLE* pTable = tableMap[gpuw]; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 93 | |
| 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 Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 101 | XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglGetFormatInfo(XGL_DEVICE device, XGL_FORMAT format, XGL_FORMAT_INFO_TYPE infoType, size_t* pDataSize, void* pData) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 102 | { |
| 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 Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 111 | XGL_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 Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 112 | { |
| 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 Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame] | 119 | 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] | 120 | 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 Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame] | 128 | xglGetGpuInfoType fpGetGpuInfo; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 129 | XGL_PHYSICAL_GPU_PROPERTIES gpuProps; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 130 | size_t dataSize = sizeof(XGL_PHYSICAL_GPU_PROPERTIES); |
Mark Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame] | 131 | fpGetGpuInfo = (xglGetGpuInfoType) gpuw->pGPA((XGL_PHYSICAL_GPU) gpuw->nextObject, "xglGetGpuInfo"); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 132 | 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 Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 145 | XGL_LAYER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char* pName) |
Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 146 | { |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 147 | if (gpu == NULL) |
| 148 | return NULL; |
Chia-I Wu | b665d94 | 2015-01-05 09:41:27 +0800 | [diff] [blame] | 149 | |
| 150 | initLayerTable((const XGL_BASE_LAYER_OBJECT *) gpu); |
| 151 | |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 152 | if (!strncmp("xglGetProcAddr", pName, sizeof("xglGetProcAddr"))) |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 153 | return (void *) xglGetProcAddr; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 154 | else if (!strncmp("xglCreateDevice", pName, sizeof ("xglCreateDevice"))) |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 155 | return (void *) xglCreateDevice; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 156 | else if (!strncmp("xglGetExtensionSupport", pName, sizeof ("xglGetExtensionSupport"))) |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 157 | return (void *) xglGetExtensionSupport; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 158 | else if (!strncmp("xglEnumerateLayers", pName, sizeof ("xglEnumerateLayers"))) |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 159 | return (void *) xglEnumerateLayers; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 160 | else if (!strncmp("xglGetFormatInfo", pName, sizeof ("xglGetFormatInfo"))) |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 161 | return (void *) xglGetFormatInfo; |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 162 | else if (!strncmp("xglLayerExtension1", pName, sizeof("xglLayerExtension1"))) |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 163 | return (void *) xglLayerExtension1; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 164 | 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 | } |