David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2011, The Android Open Source Project |
| 3 | ** |
| 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
| 7 | ** |
| 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ** |
| 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
| 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
David Li | ce30eb8 | 2011-03-28 10:39:28 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_GLES2_DBG_HEADER_H |
| 18 | #define ANDROID_GLES2_DBG_HEADER_H |
| 19 | |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <ctype.h> |
| 22 | #include <string.h> |
| 23 | #include <errno.h> |
| 24 | |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 25 | #include <GLES2/gl2.h> |
| 26 | #include <GLES2/gl2ext.h> |
| 27 | |
| 28 | #include <cutils/log.h> |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 29 | #include <utils/Timers.h> |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 30 | |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 31 | #include "hooks.h" |
| 32 | |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 33 | #include "glesv2dbg.h" |
| 34 | |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 35 | #define GL_ENTRY(_r, _api, ...) _r Debug_##_api ( __VA_ARGS__ ); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 36 | #include "glesv2dbg_functions.h" |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 37 | |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 38 | #include "debugger_message.pb.h" |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 39 | |
| 40 | using namespace android; |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 41 | using namespace com::android; |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 42 | |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 43 | #ifndef __location__ |
| 44 | #define __HIERALLOC_STRING_0__(s) #s |
| 45 | #define __HIERALLOC_STRING_1__(s) __HIERALLOC_STRING_0__(s) |
| 46 | #define __HIERALLOC_STRING_2__ __HIERALLOC_STRING_1__(__LINE__) |
| 47 | #define __location__ __FILE__ ":" __HIERALLOC_STRING_2__ |
| 48 | #endif |
| 49 | |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 50 | #undef assert |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 51 | #define assert(expr) if (!(expr)) { LOGD("\n*\n*\n* assert: %s at %s \n*\n*", #expr, __location__); int * x = 0; *x = 5; } |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 52 | //#undef LOGD |
| 53 | //#define LOGD(...) |
| 54 | |
| 55 | namespace android |
| 56 | { |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 57 | |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 58 | struct GLFunctionBitfield { |
David Li | fbfc703 | 2011-03-21 16:25:58 -0700 | [diff] [blame] | 59 | unsigned char field [24]; // 8 * 24 = 192 |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 60 | |
| 61 | void Bit(const glesv2debugger::Message_Function function, bool bit) { |
David Li | fbfc703 | 2011-03-21 16:25:58 -0700 | [diff] [blame] | 62 | const unsigned byte = function / 8, mask = 1 << (function % 8); |
| 63 | if (bit) |
| 64 | field[byte] |= mask; |
| 65 | else |
| 66 | field[byte] &= ~mask; |
| 67 | } |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 68 | |
| 69 | bool Bit(const glesv2debugger::Message_Function function) const { |
David Li | fbfc703 | 2011-03-21 16:25:58 -0700 | [diff] [blame] | 70 | const unsigned byte = function / 8, mask = 1 << (function % 8); |
| 71 | return field[byte] & mask; |
| 72 | } |
| 73 | }; |
| 74 | |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 75 | struct DbgContext { |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 76 | static const unsigned int LZF_CHUNK_SIZE = 256 * 1024; |
David Li | e9f619f | 2011-04-14 11:20:22 -0700 | [diff] [blame] | 77 | |
| 78 | private: |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame] | 79 | char * lzf_buf; // malloc / free; for lzf chunk compression and other uses |
David Li | f9bc124 | 2011-03-22 18:42:03 -0700 | [diff] [blame] | 80 | |
| 81 | // used as buffer and reference frame for ReadPixels; malloc/free |
| 82 | unsigned * lzf_ref [2]; |
| 83 | unsigned lzf_readIndex; // 0 or 1 |
| 84 | unsigned lzf_refSize, lzf_refBufSize; // bytes |
| 85 | |
David Li | 6a5ca48 | 2011-03-21 10:02:30 -0700 | [diff] [blame] | 86 | public: |
David Li | e7180e8 | 2011-04-08 18:45:45 -0700 | [diff] [blame] | 87 | const unsigned int version; // 0 is GLES1, 1 is GLES2 |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 88 | const gl_hooks_t * const hooks; |
David Li | e7180e8 | 2011-04-08 18:45:45 -0700 | [diff] [blame] | 89 | const unsigned int MAX_VERTEX_ATTRIBS; |
| 90 | const GLenum readFormat, readType; // implementation supported glReadPixels |
| 91 | const unsigned int readBytesPerPixel; |
| 92 | |
| 93 | unsigned int captureSwap; // number of eglSwapBuffers to glReadPixels |
| 94 | unsigned int captureDraw; // number of glDrawArrays/Elements to glReadPixels |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 95 | |
David Li | fbfc703 | 2011-03-21 16:25:58 -0700 | [diff] [blame] | 96 | GLFunctionBitfield expectResponse; |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 97 | |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 98 | struct VertexAttrib { |
| 99 | GLenum type; // element data type |
| 100 | unsigned size; // number of data per element |
| 101 | unsigned stride; // calculated number of bytes between elements |
| 102 | const void * ptr; |
| 103 | unsigned elemSize; // calculated number of bytes per element |
| 104 | GLuint buffer; // buffer name |
| 105 | GLboolean normalized : 1; |
| 106 | GLboolean enabled : 1; |
| 107 | VertexAttrib() : type(0), size(0), stride(0), ptr(NULL), elemSize(0), |
| 108 | buffer(0), normalized(0), enabled(0) {} |
| 109 | } * vertexAttribs; |
| 110 | bool hasNonVBOAttribs; // whether any enabled vertexAttrib is user pointer |
| 111 | |
| 112 | struct VBO { |
| 113 | const GLuint name; |
| 114 | const GLenum target; |
| 115 | VBO * next; |
| 116 | void * data; // malloc/free |
| 117 | unsigned size; // in bytes |
| 118 | VBO(const GLuint name, const GLenum target, VBO * head) : name(name), |
| 119 | target(target), next(head), data(NULL), size(0) {} |
| 120 | } * indexBuffers; // linked list of all index buffers |
| 121 | VBO * indexBuffer; // currently bound index buffer |
| 122 | |
| 123 | GLuint program; |
| 124 | unsigned maxAttrib; // number of slots used by program |
| 125 | |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 126 | DbgContext(const unsigned version, const gl_hooks_t * const hooks, |
David Li | e7180e8 | 2011-04-08 18:45:45 -0700 | [diff] [blame] | 127 | const unsigned MAX_VERTEX_ATTRIBS, const GLenum readFormat, |
| 128 | const GLenum readType); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 129 | ~DbgContext(); |
| 130 | |
| 131 | void Fetch(const unsigned index, std::string * const data) const; |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 132 | void Compress(const void * in_data, unsigned in_len, std::string * const outStr); |
David Li | e9f619f | 2011-04-14 11:20:22 -0700 | [diff] [blame] | 133 | static unsigned char * Decompress(const void * in, const unsigned int inLen, |
| 134 | unsigned int * const outLen); // malloc/free |
David Li | f9bc124 | 2011-03-22 18:42:03 -0700 | [diff] [blame] | 135 | void * GetReadPixelsBuffer(const unsigned size); |
| 136 | bool IsReadPixelBuffer(const void * const ptr) { |
| 137 | return ptr == lzf_ref[lzf_readIndex]; |
| 138 | } |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 139 | void CompressReadPixelBuffer(std::string * const outStr); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame] | 140 | char * GetBuffer(); // allocates lzf_buf if NULL |
| 141 | unsigned int GetBufferSize(); // allocates lzf_buf if NULL |
David Li | f9bc124 | 2011-03-22 18:42:03 -0700 | [diff] [blame] | 142 | |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 143 | void glUseProgram(GLuint program); |
| 144 | void glEnableVertexAttribArray(GLuint index); |
| 145 | void glDisableVertexAttribArray(GLuint index); |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 146 | void glVertexAttribPointer(GLuint indx, GLint size, GLenum type, |
| 147 | GLboolean normalized, GLsizei stride, const GLvoid* ptr); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 148 | void glBindBuffer(GLenum target, GLuint buffer); |
| 149 | void glBufferData(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage); |
| 150 | void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data); |
| 151 | void glDeleteBuffers(GLsizei n, const GLuint *buffers); |
| 152 | }; |
| 153 | |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 154 | DbgContext * getDbgContextThreadSpecific(); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 155 | |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 156 | struct FunctionCall { |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 157 | virtual const int * operator()(gl_hooks_t::gl_t const * const _c, |
| 158 | glesv2debugger::Message & msg) = 0; |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 159 | virtual ~FunctionCall() {} |
| 160 | }; |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 161 | |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 162 | // move these into DbgContext as static |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 163 | extern int timeMode; // SYSTEM_TIME_ |
| 164 | |
| 165 | extern int clientSock, serverSock; |
| 166 | |
| 167 | unsigned GetBytesPerPixel(const GLenum format, const GLenum type); |
| 168 | |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 169 | // every Debug_gl* function calls this to send message to client and possibly receive commands |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 170 | int * MessageLoop(FunctionCall & functionCall, glesv2debugger::Message & msg, |
David Li | fbfc703 | 2011-03-21 16:25:58 -0700 | [diff] [blame] | 171 | const glesv2debugger::Message_Function function); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 172 | |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 173 | void Receive(glesv2debugger::Message & cmd); |
| 174 | float Send(const glesv2debugger::Message & msg, glesv2debugger::Message & cmd); |
David Li | fbfc703 | 2011-03-21 16:25:58 -0700 | [diff] [blame] | 175 | void SetProp(DbgContext * const dbg, const glesv2debugger::Message & cmd); |
David Li | 291f5fe | 2011-03-21 17:42:50 -0700 | [diff] [blame] | 176 | const int * GenerateCall(DbgContext * const dbg, const glesv2debugger::Message & cmd, |
David Li | cbe4e5e | 2011-03-22 18:48:31 -0700 | [diff] [blame] | 177 | glesv2debugger::Message & msg, const int * const prevRet); |
David Li | 28ca2ab | 2011-03-01 16:08:10 -0800 | [diff] [blame] | 178 | }; // namespace android { |
David Li | ce30eb8 | 2011-03-28 10:39:28 -0700 | [diff] [blame] | 179 | |
| 180 | #endif // #ifndef ANDROID_GLES2_DBG_HEADER_H |