1 /*
2  * Copyright (C) 2018-2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #define _GDI32_ //It causes that definitions of functions are not loaded from dll in file wingdi.h because they are in this file.
9 
10 #include "opencl/test/unit_test/helpers/windows/mock_function.h"
11 #include "opencl/test/unit_test/mocks/gl/windows/mock_gl_sharing_windows.h"
12 
13 #include "GL/gl.h"
14 
15 #include <string.h>
16 
17 extern "C" {
18 const char *glString = "Intel";
19 const char *glVersion = "4.0";
20 const char *arrayStringi[2]{"GL_OES_framebuffer_object", "GL_EXT_framebuffer_object"};
21 int GLAcquireSharedBufferCalled = 0;
22 int GLAcquireSharedRenderBufferCalled = 0;
23 int GLAcquireSharedTextureCalled = 0;
24 int GLDeleteContextCalled = 0;
25 int GLGetCurrentContextCalled = 0;
26 int GLGetCurrentDisplayCalled = 0;
27 int GLGetSyncivCalled = 0;
28 int GLMakeCurrentCalled = 0;
29 int GLReleaseSharedBufferCalled = 0;
30 int GLReleaseSharedRenderBufferCalled = 0;
31 int GLReleaseSharedTextureCalled = 0;
32 int GLReleaseSyncCalled = 0;
33 int GLRetainSyncCalled = 0;
34 int WGLCreateContextCalled = 0;
35 int WGLDeleteContextCalled = 0;
36 int WGLShareListsCalled = 0;
37 CL_GL_BUFFER_INFO bufferInfoInput = {0};
38 CL_GL_BUFFER_INFO bufferInfoOutput = {0};
39 CL_GL_RESOURCE_INFO textureInfoInput = {0};
40 CL_GL_RESOURCE_INFO textureInfoOutput = {0};
41 NEO::GLMockReturnedValues glMockReturnedValues = {0};
42 GLboolean GLSetSharedOCLContextStateReturnedValue = 1u;
43 bool glGetLuidFuncAvailable = true;
44 int glGetLuidCalled = 0;
45 
glGetString(unsigned int name)46 const unsigned char *WINAPI glGetString(unsigned int name) {
47     if (name == GL_VENDOR)
48         return reinterpret_cast<const unsigned char *>(glString);
49     if (name == GL_VERSION)
50         return reinterpret_cast<const unsigned char *>(glVersion);
51     return reinterpret_cast<const unsigned char *>("");
52 };
wglSetSharedOCLContextStateINTELMock(HDC HDCHandle,HGLRC ContextHandle,unsigned char State,void * pContextInfo)53 GLboolean WINAPI wglSetSharedOCLContextStateINTELMock(HDC HDCHandle, HGLRC ContextHandle, unsigned char State,
54                                                       void *pContextInfo) {
55     ((NEO::ContextInfo *)pContextInfo)->ContextHandle = 1;
56     ((NEO::ContextInfo *)pContextInfo)->DeviceHandle = 2;
57     return GLSetSharedOCLContextStateReturnedValue;
58 };
mockGLAcquireSharedBuffer(GLDisplay,GLContext,GLContext,GLvoid * pResourceInfo)59 GLboolean WINAPI mockGLAcquireSharedBuffer(GLDisplay, GLContext, GLContext, GLvoid *pResourceInfo) {
60 
61     auto pBufferInfo = (CL_GL_BUFFER_INFO *)pResourceInfo;
62     bufferInfoInput = *pBufferInfo;
63     pBufferInfo->bufferSize = bufferInfoOutput.bufferSize;
64     pBufferInfo->globalShareHandle = bufferInfoOutput.globalShareHandle;
65     pBufferInfo->pGmmResInfo = bufferInfoOutput.pGmmResInfo;
66     pBufferInfo->bufferOffset = bufferInfoOutput.bufferOffset;
67     GLAcquireSharedBufferCalled++;
68     return (GLboolean)1;
69 };
mockGLReleaseSharedBuffer(GLDisplay,GLContext,GLContext,GLvoid * pResourceInfo)70 GLboolean WINAPI mockGLReleaseSharedBuffer(GLDisplay, GLContext, GLContext, GLvoid *pResourceInfo) {
71     bufferInfoInput = *static_cast<CL_GL_BUFFER_INFO *>(pResourceInfo);
72     GLReleaseSharedBufferCalled++;
73     return (GLboolean)1;
74 };
mockGLAcquireSharedRenderBuffer(GLDisplay,GLContext,GLContext,GLvoid * pResourceInfo)75 GLboolean WINAPI mockGLAcquireSharedRenderBuffer(GLDisplay, GLContext, GLContext, GLvoid *pResourceInfo) {
76     auto pTextureInfo = (CL_GL_RESOURCE_INFO *)pResourceInfo;
77     textureInfoInput = *pTextureInfo;
78     pTextureInfo->globalShareHandle = textureInfoOutput.globalShareHandle;
79     pTextureInfo->pGmmResInfo = textureInfoOutput.pGmmResInfo;
80     pTextureInfo->glInternalFormat = GL_RGBA8;
81     GLAcquireSharedRenderBufferCalled++;
82     return (GLboolean)1;
83 };
mockGLReleaseSharedRenderBuffer(GLDisplay,GLContext,GLContext,GLvoid * pResourceInfo)84 GLboolean WINAPI mockGLReleaseSharedRenderBuffer(GLDisplay, GLContext, GLContext, GLvoid *pResourceInfo) {
85     textureInfoInput = *static_cast<CL_GL_RESOURCE_INFO *>(pResourceInfo);
86     GLReleaseSharedRenderBufferCalled++;
87     return (GLboolean)1;
88 };
mockGLAcquireSharedTexture(GLDisplay,GLContext,GLContext,GLvoid * pResourceInfo)89 GLboolean WINAPI mockGLAcquireSharedTexture(GLDisplay, GLContext, GLContext, GLvoid *pResourceInfo) {
90     auto pTextureInfo = (CL_GL_RESOURCE_INFO *)pResourceInfo;
91     textureInfoInput = *pTextureInfo;
92     pTextureInfo->globalShareHandle = textureInfoOutput.globalShareHandle;
93     pTextureInfo->globalShareHandleMCS = textureInfoOutput.globalShareHandleMCS;
94     if (pTextureInfo->target == GL_TEXTURE_BUFFER) {
95         // size and width for texture buffer are queried from textureInfo - not from gmm
96         pTextureInfo->textureBufferSize = textureInfoOutput.textureBufferSize;
97         pTextureInfo->textureBufferWidth = textureInfoOutput.textureBufferWidth;
98     }
99     pTextureInfo->pGmmResInfo = textureInfoOutput.pGmmResInfo;
100     pTextureInfo->glInternalFormat = textureInfoOutput.glInternalFormat ? textureInfoOutput.glInternalFormat : GL_RGBA8;
101     pTextureInfo->glHWFormat = textureInfoOutput.glHWFormat;
102     pTextureInfo->textureBufferOffset = textureInfoOutput.textureBufferOffset;
103     pTextureInfo->numberOfSamples = textureInfoOutput.numberOfSamples;
104     pTextureInfo->isAuxEnabled = textureInfoOutput.isAuxEnabled;
105     GLAcquireSharedTextureCalled++;
106     return (GLboolean)1;
107 };
mockGLReleaseSharedTexture(GLDisplay,GLContext,GLContext,GLvoid * pResourceInfo)108 GLboolean WINAPI mockGLReleaseSharedTexture(GLDisplay, GLContext, GLContext, GLvoid *pResourceInfo) {
109     textureInfoInput = *static_cast<CL_GL_RESOURCE_INFO *>(pResourceInfo);
110     GLReleaseSharedTextureCalled++;
111     return (GLboolean)1;
112 };
mockGlRetainSync(GLDisplay HDCHandle,GLContext ContextHandle,GLContext BackupContextHandle,GLvoid * pSyncInfo)113 GLboolean WINAPI mockGlRetainSync(GLDisplay HDCHandle, GLContext ContextHandle, GLContext BackupContextHandle,
114                                   GLvoid *pSyncInfo) {
115     GLRetainSyncCalled++;
116     GL_CL_SYNC_INFO *syncInfo = (GL_CL_SYNC_INFO *)(pSyncInfo);
117     syncInfo->pSync = (void *)0x123;
118     return GL_TRUE;
119 };
mockGlReleaseSync(GLDisplay HDCHandle,GLContext ContextHandle,GLContext BackupContextHandle,GLvoid * pSync)120 GLboolean WINAPI mockGlReleaseSync(GLDisplay HDCHandle, GLContext ContextHandle, GLContext BackupContextHandle,
121                                    GLvoid *pSync) {
122     GLReleaseSyncCalled++;
123     return GL_TRUE;
124 };
mockGlGetSynciv(GLvoid * pSync,GLenum pname,GLint * value)125 void WINAPI mockGlGetSynciv(GLvoid *pSync, GLenum pname, GLint *value) {
126     GLGetSyncivCalled++;
127     *value = glMockReturnedValues.syncivRetVal;
128 };
glGetStringiMock(unsigned int name,unsigned int index)129 const unsigned char *_stdcall glGetStringiMock(unsigned int name, unsigned int index) { return reinterpret_cast<const unsigned char *>(arrayStringi[index]); };
mockGLGetCurrentDisplay()130 GLDisplay WINAPI mockGLGetCurrentDisplay() {
131     GLGetCurrentDisplayCalled++;
132     return glMockReturnedValues.currentDisplay;
133 };
134 
wglGetLuidMock(GLContext glContext)135 LUID WINAPI wglGetLuidMock(GLContext glContext) {
136     glGetLuidCalled++;
137     LUID luid{};
138     if (reinterpret_cast<GLContext>(1) == glContext) {
139         luid.HighPart = 0x1d2e;
140         luid.LowPart = 0x3f4a;
141     } else if (reinterpret_cast<GLContext>(2) == glContext) {
142         luid.HighPart = 0x5d2e;
143         luid.LowPart = 0x3f4a;
144     }
145     return luid;
146 };
147 
wglGetProcAddress(LPCSTR name)148 PROC WINAPI wglGetProcAddress(LPCSTR name) {
149     if (strcmp(name, "wglSetSharedOCLContextStateINTEL") == 0) {
150         return reinterpret_cast<PROC>(*wglSetSharedOCLContextStateINTELMock);
151     }
152     if (strcmp(name, "wglAcquireSharedBufferINTEL") == 0) {
153         return reinterpret_cast<PROC>(*mockGLAcquireSharedBuffer);
154     }
155     if (strcmp(name, "wglReleaseSharedBufferINTEL") == 0) {
156         return reinterpret_cast<PROC>(*mockGLReleaseSharedBuffer);
157     }
158     if (strcmp(name, "wglAcquireSharedRenderBufferINTEL") == 0) {
159         return reinterpret_cast<PROC>(*mockGLAcquireSharedRenderBuffer);
160     }
161     if (strcmp(name, "wglReleaseSharedRenderBufferINTEL") == 0) {
162         return reinterpret_cast<PROC>(*mockGLReleaseSharedRenderBuffer);
163     }
164     if (strcmp(name, "wglAcquireSharedTextureINTEL") == 0) {
165         return reinterpret_cast<PROC>(*mockGLAcquireSharedTexture);
166     }
167     if (strcmp(name, "wglReleaseSharedTextureINTEL") == 0) {
168         return reinterpret_cast<PROC>(*mockGLReleaseSharedTexture);
169     }
170     if (strcmp(name, "wglRetainSyncINTEL") == 0) {
171         return reinterpret_cast<PROC>(*mockGlRetainSync);
172     }
173     if (strcmp(name, "wglReleaseSyncINTEL") == 0) {
174         return reinterpret_cast<PROC>(*mockGlReleaseSync);
175     }
176     if (strcmp(name, "wglGetSyncivINTEL") == 0) {
177         return reinterpret_cast<PROC>(*mockGlGetSynciv);
178     }
179     if (strcmp(name, "glGetStringi") == 0) {
180         return reinterpret_cast<PROC>(*glGetStringiMock);
181     }
182     if (strcmp(name, "wglGetLuidINTEL") == 0 && glGetLuidFuncAvailable) {
183         return reinterpret_cast<PROC>(wglGetLuidMock);
184     }
185     return nullptr;
186 }
wglGetCurrentContext()187 HGLRC WINAPI wglGetCurrentContext() {
188     GLGetCurrentContextCalled++;
189     return glMockReturnedValues.currentContext;
190 };
wglGetCurrentDC()191 HDC WINAPI wglGetCurrentDC() { return mockGLGetCurrentDisplay(); };
wglCreateContext(HDC Arg1)192 HGLRC WINAPI wglCreateContext(HDC Arg1) {
193     WGLCreateContextCalled++;
194     return (GLContext)0x101;
195 };
wglDeleteContext(HGLRC Arg1)196 BOOL WINAPI wglDeleteContext(HGLRC Arg1) {
197     WGLDeleteContextCalled++;
198     GLDeleteContextCalled++;
199     return (GLboolean)1;
200 };
glGetIntegerv(GLenum pname,GLint * params)201 void WINAPI glGetIntegerv(GLenum pname, GLint *params) { return NEO::MockGLSharingFunctions::glGetIntegervTest(pname, params); };
wglShareLists(HGLRC arg1,HGLRC arg2)202 BOOL WINAPI wglShareLists(HGLRC arg1, HGLRC arg2) {
203     WGLShareListsCalled++;
204     return 1;
205 };
206 
wglMakeCurrent(HDC arg1,HGLRC arg2)207 BOOL WINAPI wglMakeCurrent(HDC arg1, HGLRC arg2) {
208     GLMakeCurrentCalled++;
209     glMockReturnedValues.madeCurrentContext = arg2;
210     if (glMockReturnedValues.forceMakeCurrentCallFail) {
211         if (glMockReturnedValues.failsCounter < glMockReturnedValues.numberOfCallFails) {
212             glMockReturnedValues.failsCounter++;
213             return GL_FALSE;
214         }
215     }
216     return (GLboolean)1;
217 };
mockLoader(const char * name)218 void *WINAPI mockLoader(const char *name) {
219     if (strcmp(name, "realFunction") == 0) {
220         return *realFunction;
221     }
222     return nullptr;
223 };
resetParam(const char * name)224 void resetParam(const char *name) {
225     if (strcmp(name, "GLAcquireSharedBufferCalled") == 0) {
226         GLAcquireSharedBufferCalled = 0;
227     }
228     if (strcmp(name, "GLAcquireSharedRenderBufferCalled") == 0) {
229         GLAcquireSharedRenderBufferCalled = 0;
230     }
231     if (strcmp(name, "GLAcquireSharedTextureCalled") == 0) {
232         GLAcquireSharedTextureCalled = 0;
233     }
234     if (strcmp(name, "GLDeleteContextCalled") == 0) {
235         GLDeleteContextCalled = 0;
236     }
237     if (strcmp(name, "GLGetCurrentContextCalled") == 0) {
238         GLGetCurrentContextCalled = 0;
239     }
240     if (strcmp(name, "GLGetCurrentDisplayCalled") == 0) {
241         GLGetCurrentDisplayCalled = 0;
242     }
243     if (strcmp(name, "GLGetSyncivCalled") == 0) {
244         GLGetSyncivCalled = 0;
245     }
246     if (strcmp(name, "GLMakeCurrentCalled") == 0) {
247         GLMakeCurrentCalled = 0;
248     }
249     if (strcmp(name, "GLReleaseSharedBufferCalled") == 0) {
250         GLReleaseSharedBufferCalled = 0;
251     }
252     if (strcmp(name, "GLReleaseSharedRenderBufferCalled") == 0) {
253         GLReleaseSharedRenderBufferCalled = 0;
254     }
255     if (strcmp(name, "GLReleaseSharedTextureCalled") == 0) {
256         GLReleaseSharedTextureCalled = 0;
257     }
258     if (strcmp(name, "GLReleaseSyncCalled") == 0) {
259         GLReleaseSyncCalled = 0;
260     }
261     if (strcmp(name, "GLRetainSyncCalled") == 0) {
262         GLRetainSyncCalled = 0;
263     }
264     if (strcmp(name, "WGLCreateContextCalled") == 0) {
265         WGLCreateContextCalled = 0;
266     }
267     if (strcmp(name, "WGLDeleteContextCalled") == 0) {
268         WGLDeleteContextCalled = 0;
269     }
270     if (strcmp(name, "WGLShareListsCalled") == 0) {
271         WGLShareListsCalled = 0;
272     }
273     if (strcmp(name, "") == 0) {
274         GLAcquireSharedBufferCalled = 0;
275         GLAcquireSharedRenderBufferCalled = 0;
276         GLAcquireSharedTextureCalled = 0;
277         GLDeleteContextCalled = 0;
278         GLGetCurrentContextCalled = 0;
279         GLGetCurrentDisplayCalled = 0;
280         GLGetSyncivCalled = 0;
281         GLMakeCurrentCalled = 0;
282         GLReleaseSharedBufferCalled = 0;
283         GLReleaseSharedRenderBufferCalled = 0;
284         GLReleaseSharedTextureCalled = 0;
285         GLReleaseSyncCalled = 0;
286         GLRetainSyncCalled = 0;
287         WGLCreateContextCalled = 0;
288         WGLDeleteContextCalled = 0;
289         WGLShareListsCalled = 0;
290         glGetLuidCalled = 0;
291         glGetLuidFuncAvailable = true;
292     }
293     if (strcmp(name, "glGetLuidCalled") == 0) {
294         glGetLuidCalled = 0;
295     }
296     if (strcmp(name, "glGetLuidFuncAvailable") == 0) {
297         glGetLuidFuncAvailable = true;
298     }
299     if (strcmp(name, "glGetLuidFuncNotAvailable") == 0) {
300         glGetLuidFuncAvailable = false;
301     }
302 };
getParam(const char * name)303 int getParam(const char *name) {
304     if (strcmp(name, "GLAcquireSharedBufferCalled") == 0) {
305         return GLAcquireSharedBufferCalled;
306     }
307     if (strcmp(name, "GLAcquireSharedRenderBufferCalled") == 0) {
308         return GLAcquireSharedRenderBufferCalled;
309     }
310     if (strcmp(name, "GLAcquireSharedTextureCalled") == 0) {
311         return GLAcquireSharedTextureCalled;
312     }
313     if (strcmp(name, "GLDeleteContextCalled") == 0) {
314         return GLDeleteContextCalled;
315     }
316     if (strcmp(name, "GLGetCurrentContextCalled") == 0) {
317         return GLGetCurrentContextCalled;
318     }
319     if (strcmp(name, "GLGetCurrentDisplayCalled") == 0) {
320         return GLGetCurrentDisplayCalled;
321     }
322     if (strcmp(name, "GLGetSyncivCalled") == 0) {
323         return GLGetSyncivCalled;
324     }
325     if (strcmp(name, "GLMakeCurrentCalled") == 0) {
326         return GLMakeCurrentCalled;
327     }
328     if (strcmp(name, "GLReleaseSharedBufferCalled") == 0) {
329         return GLReleaseSharedBufferCalled;
330     }
331     if (strcmp(name, "GLReleaseSharedRenderBufferCalled") == 0) {
332         return GLReleaseSharedRenderBufferCalled;
333     }
334     if (strcmp(name, "GLReleaseSharedTextureCalled") == 0) {
335         return GLReleaseSharedTextureCalled;
336     }
337     if (strcmp(name, "GLReleaseSyncCalled") == 0) {
338         return GLReleaseSyncCalled;
339     }
340     if (strcmp(name, "GLRetainSyncCalled") == 0) {
341         return GLRetainSyncCalled;
342     }
343     if (strcmp(name, "WGLCreateContextCalled") == 0) {
344         return WGLCreateContextCalled;
345     }
346     if (strcmp(name, "WGLDeleteContextCalled") == 0) {
347         return WGLDeleteContextCalled;
348     }
349     if (strcmp(name, "WGLShareListsCalled") == 0) {
350         return WGLShareListsCalled;
351     }
352     if (strcmp(name, "glGetLuidCalled") == 0) {
353         return glGetLuidCalled;
354     }
355     return 0;
356 };
getBufferInfo()357 CL_GL_BUFFER_INFO getBufferInfo() { return bufferInfoInput; };
getTextureInfo()358 CL_GL_RESOURCE_INFO getTextureInfo() { return textureInfoInput; };
memParam()359 void memParam() {
360     memset(&bufferInfoInput, 0, sizeof(CL_GL_BUFFER_INFO));
361     memset(&bufferInfoOutput, 0, sizeof(CL_GL_BUFFER_INFO));
362     memset(&textureInfoInput, 0, sizeof(CL_GL_RESOURCE_INFO));
363     memset(&textureInfoOutput, 0, sizeof(CL_GL_RESOURCE_INFO));
364     memset(&glMockReturnedValues, 0, sizeof(NEO::GLMockReturnedValues));
365 };
loadBuffer(CL_GL_BUFFER_INFO buff)366 void loadBuffer(CL_GL_BUFFER_INFO buff) { bufferInfoOutput = buff; };
loadTexture(CL_GL_RESOURCE_INFO texture)367 void loadTexture(CL_GL_RESOURCE_INFO texture) { textureInfoOutput = texture; };
getGlMockReturnedValues()368 NEO::GLMockReturnedValues getGlMockReturnedValues() { return glMockReturnedValues; };
setGlMockReturnedValues(NEO::GLMockReturnedValues value)369 void setGlMockReturnedValues(NEO::GLMockReturnedValues value) { glMockReturnedValues = value; };
setGetSyncivReturnValue(int val)370 void setGetSyncivReturnValue(int val) { glMockReturnedValues.syncivRetVal = val; }
glSetString(const char * name,unsigned int var)371 void glSetString(const char *name, unsigned int var) {
372     if (var == GL_VENDOR) {
373         glString = name;
374     } else if (var == GL_VERSION) {
375         glVersion = name;
376     }
377 };
glSetStringi(const char * name,unsigned int index)378 void glSetStringi(const char *name, unsigned int index) { arrayStringi[index] = name; };
setGLSetSharedOCLContextStateReturnedValue(GLboolean value)379 void setGLSetSharedOCLContextStateReturnedValue(GLboolean value) { GLSetSharedOCLContextStateReturnedValue = static_cast<GLboolean>(value); };
getGLSetSharedOCLContextStateReturnedValue()380 GLboolean getGLSetSharedOCLContextStateReturnedValue() { return GLSetSharedOCLContextStateReturnedValue; };
381 }
382