xref: /aosp_15_r20/external/mesa3d/src/mesa/main/robustness.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 #include "glthread_marshal.h"
33 
34 static void GLAPIENTRY
_context_lost_GetSynciv(GLsync sync,GLenum pname,GLsizei bufSize,GLsizei * length,GLint * values)35 _context_lost_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize,
36                         GLsizei *length, GLint *values)
37 {
38    GET_CURRENT_CONTEXT(ctx);
39    if (ctx)
40       _mesa_error(ctx, GL_CONTEXT_LOST, "GetSynciv(invalid call)");
41 
42    if (pname == GL_SYNC_STATUS && bufSize >= 1)
43       *values = GL_SIGNALED;
44 }
45 
46 static void GLAPIENTRY
_context_lost_GetQueryObjectuiv(GLuint id,GLenum pname,GLuint * params)47 _context_lost_GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
48 {
49    GET_CURRENT_CONTEXT(ctx);
50    if (ctx)
51       _mesa_error(ctx, GL_CONTEXT_LOST, "GetQueryObjectuiv(context lost)");
52 
53    if (pname == GL_QUERY_RESULT_AVAILABLE)
54       *params = GL_TRUE;
55 }
56 
57 static int
context_lost_nop_handler(void)58 context_lost_nop_handler(void)
59 {
60    GET_CURRENT_CONTEXT(ctx);
61    if (ctx)
62       _mesa_error(ctx, GL_CONTEXT_LOST, "context lost");
63 
64    return 0;
65 }
66 
67 void
_mesa_set_context_lost_dispatch(struct gl_context * ctx)68 _mesa_set_context_lost_dispatch(struct gl_context *ctx)
69 {
70    if (ctx->Dispatch.ContextLost == NULL) {
71       int numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
72 
73       ctx->Dispatch.ContextLost = malloc(numEntries * sizeof(_glapi_proc));
74       if (!ctx->Dispatch.ContextLost)
75          return;
76 
77       _glapi_proc *entry = (_glapi_proc *) ctx->Dispatch.ContextLost;
78       unsigned i;
79       for (i = 0; i < numEntries; i++)
80          entry[i] = (_glapi_proc) context_lost_nop_handler;
81 
82       /* The ARB_robustness specification says:
83        *
84        *    "* GetError and GetGraphicsResetStatus behave normally following a
85        *       graphics reset, so that the application can determine a reset
86        *       has occurred, and when it is safe to destroy and recreate the
87        *       context.
88        *
89        *     * Any commands which might cause a polling application to block
90        *       indefinitely will generate a CONTEXT_LOST error, but will also
91        *       return a value indicating completion to the application. Such
92        *       commands include:
93        *
94        *        + GetSynciv with <pname> SYNC_STATUS ignores the other
95        *          parameters and returns SIGNALED in <values>.
96        *
97        *        + GetQueryObjectuiv with <pname> QUERY_RESULT_AVAILABLE
98        *          ignores the other parameters and returns TRUE in <params>."
99        */
100       SET_GetError(ctx->Dispatch.ContextLost, _mesa_GetError);
101       SET_GetGraphicsResetStatusARB(ctx->Dispatch.ContextLost, _mesa_GetGraphicsResetStatusARB);
102       SET_GetSynciv(ctx->Dispatch.ContextLost, _context_lost_GetSynciv);
103       SET_GetQueryObjectuiv(ctx->Dispatch.ContextLost, _context_lost_GetQueryObjectuiv);
104    }
105 
106    ctx->Dispatch.Current = ctx->Dispatch.ContextLost;
107    _glapi_set_dispatch(ctx->Dispatch.Current);
108 }
109 
110 /**
111  * Returns an error code specified by GL_ARB_robustness, or GL_NO_ERROR.
112  * \return current context status
113  */
114 GLenum GLAPIENTRY
_mesa_GetGraphicsResetStatusARB(void)115 _mesa_GetGraphicsResetStatusARB( void )
116 {
117    GET_CURRENT_CONTEXT(ctx);
118    GLenum status = GL_NO_ERROR;
119 
120    /* The ARB_robustness specification says:
121     *
122     *     "If the reset notification behavior is NO_RESET_NOTIFICATION_ARB,
123     *     then the implementation will never deliver notification of reset
124     *     events, and GetGraphicsResetStatusARB will always return NO_ERROR."
125     */
126    if (ctx->Const.ResetStrategy == GL_NO_RESET_NOTIFICATION_ARB) {
127       if (MESA_VERBOSE & VERBOSE_API)
128          _mesa_debug(ctx,
129                      "glGetGraphicsResetStatusARB always returns GL_NO_ERROR "
130                      "because reset notifictation was not requested at context "
131                      "creation.\n");
132 
133       return GL_NO_ERROR;
134    }
135 
136    /* Query the reset status of this context from the driver core. */
137    if (ctx->Driver.GetGraphicsResetStatus)
138       status = ctx->Driver.GetGraphicsResetStatus(ctx);
139 
140    if (status != GL_NO_ERROR)
141       _mesa_set_context_lost_dispatch(ctx);
142 
143    if (!ctx->Driver.GetGraphicsResetStatus && (MESA_VERBOSE & VERBOSE_API))
144       _mesa_debug(ctx,
145                   "glGetGraphicsResetStatusARB always returns GL_NO_ERROR "
146                   "because the driver doesn't track reset status.\n");
147 
148    return status;
149 }
150