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