1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// This file contains the ErrorState class.
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef GPU_COMMAND_BUFFER_SERVICE_ERROR_STATE_H_
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define GPU_COMMAND_BUFFER_SERVICE_ERROR_STATE_H_
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include <stdint.h>
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/compiler_specific.h"
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/macros.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "gpu/gpu_export.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace gpu {
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace gles2 {
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class Logger;
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use these macro to synthesize GL errors instead of calling the error_state
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// functions directly as they will propogate the __FILE__ and __LINE__.
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use to synthesize a GL error on the error_state.
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define ERRORSTATE_SET_GL_ERROR(error_state, error, function_name, msg) \
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    error_state->SetGLError(__FILE__, __LINE__, error, function_name, msg)
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use to synthesize an INVALID_ENUM GL error on the error_state. Will attempt
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// to expand the enum to a string.
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define ERRORSTATE_SET_GL_ERROR_INVALID_ENUM( \
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    error_state, function_name, value, label) \
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    error_state->SetGLErrorInvalidEnum( \
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        __FILE__, __LINE__, function_name, value, label)
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use to synthesize a GL error on the error_state for an invalid enum based
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// integer parameter. Will attempt to expand the parameter to a string.
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define ERRORSTATE_SET_GL_ERROR_INVALID_PARAMI( \
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    error_state, error, function_name, pname, param) \
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    error_state->SetGLErrorInvalidParami( \
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        __FILE__, __LINE__, error, function_name, pname, param)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use to synthesize a GL error on the error_state for an invalid enum based
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// float parameter. Will attempt to expand the parameter to a string.
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define ERRORSTATE_SET_GL_ERROR_INVALID_PARAMF( \
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    error_state, error, function_name, pname, param) \
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    error_state->SetGLErrorInvalidParamf( \
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        __FILE__, __LINE__, error, function_name, pname, param)
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use to move all pending error to the wrapper so on your next GL call
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// you can see if that call generates an error.
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define ERRORSTATE_COPY_REAL_GL_ERRORS_TO_WRAPPER(error_state, function_name) \
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    error_state->CopyRealGLErrorsToWrapper(__FILE__, __LINE__, function_name)
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use to look at the real GL error and still pass it on to the user.
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define ERRORSTATE_PEEK_GL_ERROR(error_state, function_name) \
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    error_state->PeekGLError(__FILE__, __LINE__, function_name)
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use to clear all current GL errors. FAILS if there are any.
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define ERRORSTATE_CLEAR_REAL_GL_ERRORS(error_state, function_name) \
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    error_state->ClearRealGLErrors(__FILE__, __LINE__, function_name)
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass GPU_EXPORT ErrorStateClient {
61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch public:
62e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // GL_OUT_OF_MEMORY can cause side effects such as losing the context.
63e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual void OnOutOfMemoryError() = 0;
64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch};
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class GPU_EXPORT ErrorState {
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual ~ErrorState();
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
70e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  static ErrorState* Create(ErrorStateClient* client, Logger* logger);
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
72010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual uint32_t GetGLError() = 0;
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void SetGLError(
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* filename,
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int line,
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      unsigned int error,
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* function_name,
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* msg) = 0;
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void SetGLErrorInvalidEnum(
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* filename,
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int line,
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* function_name,
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      unsigned int value,
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* label) = 0;
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void SetGLErrorInvalidParami(
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* filename,
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int line,
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      unsigned int error,
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* function_name,
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      unsigned int pname,
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int param) = 0;
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void SetGLErrorInvalidParamf(
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const char* filename,
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      int line,
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      unsigned int error,
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const char* function_name,
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      unsigned int pname,
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      float param) = 0;
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets the GLError and stores it in our wrapper. Effectively
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // this lets us peek at the error without losing it.
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual unsigned int PeekGLError(
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* filename, int line, const char* function_name) = 0;
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Copies the real GL errors to the wrapper. This is so we can
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // make sure there are no native GL errors before calling some GL function
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // so that on return we know any error generated was for that specific
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // command.
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void CopyRealGLErrorsToWrapper(
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* filename, int line, const char* function_name) = 0;
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Clear all real GL errors. This is to prevent the client from seeing any
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // errors caused by GL calls that it was not responsible for issuing.
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void ClearRealGLErrors(
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const char* filename, int line, const char* function_name) = 0;
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) protected:
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ErrorState();
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ErrorState);
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace gles2
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace gpu
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif  // GPU_COMMAND_BUFFER_SERVICE_ERROR_STATE_H_
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
129