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