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