1/* 2 * Copyright © 2016 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24#include <stdbool.h> 25#include "context.h" 26#include "debug_output.h" 27#include "get.h" 28#include "mtypes.h" 29#include "macros.h" 30#include "main/dispatch.h" /* for _gloffset_COUNT */ 31#include "api_exec_decl.h" 32 33static void GLAPIENTRY 34_context_lost_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, 35 GLsizei *length, GLint *values) 36{ 37 GET_CURRENT_CONTEXT(ctx); 38 if (ctx) 39 _mesa_error(ctx, GL_CONTEXT_LOST, "GetSynciv(invalid call)"); 40 41 if (pname == GL_SYNC_STATUS && bufSize >= 1) 42 *values = GL_SIGNALED; 43} 44 45static void GLAPIENTRY 46_context_lost_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params) 47{ 48 GET_CURRENT_CONTEXT(ctx); 49 if (ctx) 50 _mesa_error(ctx, GL_CONTEXT_LOST, "GetQueryObjectuiv(context lost)"); 51 52 if (pname == GL_QUERY_RESULT_AVAILABLE) 53 *params = GL_TRUE; 54} 55 56static int 57context_lost_nop_handler(void) 58{ 59 GET_CURRENT_CONTEXT(ctx); 60 if (ctx) 61 _mesa_error(ctx, GL_CONTEXT_LOST, "context lost"); 62 63 return 0; 64} 65 66void 67_mesa_set_context_lost_dispatch(struct gl_context *ctx) 68{ 69 if (ctx->ContextLost == NULL) { 70 int numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT); 71 72 ctx->ContextLost = malloc(numEntries * sizeof(_glapi_proc)); 73 if (!ctx->ContextLost) 74 return; 75 76 _glapi_proc *entry = (_glapi_proc *) ctx->ContextLost; 77 unsigned i; 78 for (i = 0; i < numEntries; i++) 79 entry[i] = (_glapi_proc) context_lost_nop_handler; 80 81 /* The ARB_robustness specification says: 82 * 83 * "* GetError and GetGraphicsResetStatus behave normally following a 84 * graphics reset, so that the application can determine a reset 85 * has occurred, and when it is safe to destroy and recreate the 86 * context. 87 * 88 * * Any commands which might cause a polling application to block 89 * indefinitely will generate a CONTEXT_LOST error, but will also 90 * return a value indicating completion to the application. Such 91 * commands include: 92 * 93 * + GetSynciv with <pname> SYNC_STATUS ignores the other 94 * parameters and returns SIGNALED in <values>. 95 * 96 * + GetQueryObjectuiv with <pname> QUERY_RESULT_AVAILABLE 97 * ignores the other parameters and returns TRUE in <params>." 98 */ 99 SET_GetError(ctx->ContextLost, _mesa_GetError); 100 SET_GetGraphicsResetStatusARB(ctx->ContextLost, _mesa_GetGraphicsResetStatusARB); 101 SET_GetSynciv(ctx->ContextLost, _context_lost_GetSynciv); 102 SET_GetQueryObjectuiv(ctx->ContextLost, _context_lost_GetQueryObjectuiv); 103 } 104 105 ctx->CurrentServerDispatch = ctx->ContextLost; 106 _glapi_set_dispatch(ctx->CurrentServerDispatch); 107} 108 109/** 110 * Returns an error code specified by GL_ARB_robustness, or GL_NO_ERROR. 111 * \return current context status 112 */ 113GLenum GLAPIENTRY 114_mesa_GetGraphicsResetStatusARB( void ) 115{ 116 GET_CURRENT_CONTEXT(ctx); 117 GLenum status = GL_NO_ERROR; 118 119 /* The ARB_robustness specification says: 120 * 121 * "If the reset notification behavior is NO_RESET_NOTIFICATION_ARB, 122 * then the implementation will never deliver notification of reset 123 * events, and GetGraphicsResetStatusARB will always return NO_ERROR." 124 */ 125 if (ctx->Const.ResetStrategy == GL_NO_RESET_NOTIFICATION_ARB) { 126 if (MESA_VERBOSE & VERBOSE_API) 127 _mesa_debug(ctx, 128 "glGetGraphicsResetStatusARB always returns GL_NO_ERROR " 129 "because reset notifictation was not requested at context " 130 "creation.\n"); 131 132 return GL_NO_ERROR; 133 } 134 135 if (ctx->Driver.GetGraphicsResetStatus) { 136 /* Query the reset status of this context from the driver core. 137 */ 138 status = ctx->Driver.GetGraphicsResetStatus(ctx); 139 140 simple_mtx_lock(&ctx->Shared->Mutex); 141 142 /* If this context has not been affected by a GPU reset, check to see if 143 * some other context in the share group has been affected by a reset. 144 * If another context saw a reset but this context did not, assume that 145 * this context was not guilty. 146 */ 147 if (status != GL_NO_ERROR) { 148 ctx->Shared->ShareGroupReset = true; 149 ctx->Shared->DisjointOperation = true; 150 } else if (ctx->Shared->ShareGroupReset && !ctx->ShareGroupReset) { 151 status = GL_INNOCENT_CONTEXT_RESET_ARB; 152 } 153 154 ctx->ShareGroupReset = ctx->Shared->ShareGroupReset; 155 simple_mtx_unlock(&ctx->Shared->Mutex); 156 } 157 158 if (status != GL_NO_ERROR) 159 _mesa_set_context_lost_dispatch(ctx); 160 161 if (!ctx->Driver.GetGraphicsResetStatus && (MESA_VERBOSE & VERBOSE_API)) 162 _mesa_debug(ctx, 163 "glGetGraphicsResetStatusARB always returns GL_NO_ERROR " 164 "because the driver doesn't track reset status.\n"); 165 166 return status; 167} 168