Home | History | Annotate | Download | only in src
      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 
     17 #ifndef ANDROID_GLES2_DBG_HEADER_H
     18 #define ANDROID_GLES2_DBG_HEADER_H
     19 
     20 #include <stdlib.h>
     21 #include <ctype.h>
     22 #include <string.h>
     23 #include <errno.h>
     24 
     25 #include <GLES2/gl2.h>
     26 #include <GLES2/gl2ext.h>
     27 
     28 #include <cutils/log.h>
     29 #include <utils/Timers.h>
     30 
     31 #include "hooks.h"
     32 
     33 #include "glesv2dbg.h"
     34 
     35 #define GL_ENTRY(_r, _api, ...) _r Debug_##_api ( __VA_ARGS__ );
     36 #include "glesv2dbg_functions.h"
     37 
     38 #include "debugger_message.pb.h"
     39 
     40 using namespace android;
     41 using namespace com::android;
     42 
     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 
     50 #undef assert
     51 #define assert(expr) if (!(expr)) { LOGD("\n*\n*\n* assert: %s at %s \n*\n*", #expr, __location__); int * x = 0; *x = 5; }
     52 //#undef LOGD
     53 //#define LOGD(...)
     54 
     55 namespace android
     56 {
     57 
     58 struct GLFunctionBitfield {
     59     unsigned char field [24]; // 8 * 24 = 192
     60 
     61     void Bit(const glesv2debugger::Message_Function function, bool bit) {
     62         const unsigned byte = function / 8, mask = 1 << (function % 8);
     63         if (bit)
     64             field[byte] |= mask;
     65         else
     66             field[byte] &= ~mask;
     67     }
     68 
     69     bool Bit(const glesv2debugger::Message_Function function) const {
     70         const unsigned byte = function / 8, mask = 1 << (function % 8);
     71         return field[byte] & mask;
     72     }
     73 };
     74 
     75 struct DbgContext {
     76     static const unsigned int LZF_CHUNK_SIZE = 256 * 1024;
     77 
     78 private:
     79     char * lzf_buf; // malloc / free; for lzf chunk compression and other uses
     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 
     86 public:
     87     const unsigned int version; // 0 is GLES1, 1 is GLES2
     88     const gl_hooks_t * const hooks;
     89     const unsigned int MAX_VERTEX_ATTRIBS;
     90     const unsigned int readBytesPerPixel;
     91 
     92     unsigned int captureSwap; // number of eglSwapBuffers to glReadPixels
     93     unsigned int captureDraw; // number of glDrawArrays/Elements to glReadPixels
     94 
     95     GLFunctionBitfield expectResponse;
     96 
     97     struct VertexAttrib {
     98         GLenum type; // element data type
     99         unsigned size; // number of data per element
    100         unsigned stride; // calculated number of bytes between elements
    101         const void * ptr;
    102         unsigned elemSize; // calculated number of bytes per element
    103         GLuint buffer; // buffer name
    104         GLboolean normalized : 1;
    105         GLboolean enabled : 1;
    106         VertexAttrib() : type(0), size(0), stride(0), ptr(NULL), elemSize(0),
    107                 buffer(0), normalized(0), enabled(0) {}
    108     } * vertexAttribs;
    109     bool hasNonVBOAttribs; // whether any enabled vertexAttrib is user pointer
    110 
    111     struct VBO {
    112         const GLuint name;
    113         const GLenum target;
    114         VBO * next;
    115         void * data; // malloc/free
    116         unsigned size; // in bytes
    117         VBO(const GLuint name, const GLenum target, VBO * head) : name(name),
    118                 target(target), next(head), data(NULL), size(0) {}
    119     } * indexBuffers; // linked list of all index buffers
    120     VBO * indexBuffer; // currently bound index buffer
    121 
    122     GLuint program;
    123     unsigned maxAttrib; // number of slots used by program
    124 
    125     DbgContext(const unsigned version, const gl_hooks_t * const hooks,
    126                const unsigned MAX_VERTEX_ATTRIBS);
    127     ~DbgContext();
    128 
    129     void Fetch(const unsigned index, std::string * const data) const;
    130     void Compress(const void * in_data, unsigned in_len, std::string * const outStr);
    131     static unsigned char * Decompress(const void * in, const unsigned int inLen,
    132                                       unsigned int * const outLen); // malloc/free
    133     void * GetReadPixelsBuffer(const unsigned size);
    134     bool IsReadPixelBuffer(const void * const ptr)  {
    135         return ptr == lzf_ref[lzf_readIndex];
    136     }
    137     void CompressReadPixelBuffer(std::string * const outStr);
    138     char * GetBuffer(); // allocates lzf_buf if NULL
    139     unsigned int GetBufferSize(); // allocates lzf_buf if NULL
    140 
    141     void glUseProgram(GLuint program);
    142     void glEnableVertexAttribArray(GLuint index);
    143     void glDisableVertexAttribArray(GLuint index);
    144     void glVertexAttribPointer(GLuint indx, GLint size, GLenum type,
    145                                GLboolean normalized, GLsizei stride, const GLvoid* ptr);
    146     void glBindBuffer(GLenum target, GLuint buffer);
    147     void glBufferData(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage);
    148     void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data);
    149     void glDeleteBuffers(GLsizei n, const GLuint *buffers);
    150 };
    151 
    152 DbgContext * getDbgContextThreadSpecific();
    153 
    154 struct FunctionCall {
    155     virtual const int * operator()(gl_hooks_t::gl_t const * const _c,
    156                                    glesv2debugger::Message & msg) = 0;
    157     virtual ~FunctionCall() {}
    158 };
    159 
    160 // move these into DbgContext as static
    161 extern int timeMode; // SYSTEM_TIME_
    162 
    163 extern int clientSock, serverSock;
    164 
    165 unsigned GetBytesPerPixel(const GLenum format, const GLenum type);
    166 
    167 // every Debug_gl* function calls this to send message to client and possibly receive commands
    168 int * MessageLoop(FunctionCall & functionCall, glesv2debugger::Message & msg,
    169                   const glesv2debugger::Message_Function function);
    170 
    171 void Receive(glesv2debugger::Message & cmd);
    172 float Send(const glesv2debugger::Message & msg, glesv2debugger::Message & cmd);
    173 void SetProp(DbgContext * const dbg, const glesv2debugger::Message & cmd);
    174 const int * GenerateCall(DbgContext * const dbg, const glesv2debugger::Message & cmd,
    175                          glesv2debugger::Message & msg, const int * const prevRet);
    176 }; // namespace android {
    177 
    178 #endif // #ifndef ANDROID_GLES2_DBG_HEADER_H
    179