Home | History | Annotate | Download | only in pixelflinger2
      1 /**
      2  **
      3  ** Copyright 2010, The Android Open Source Project
      4  **
      5  ** Licensed under the Apache License, Version 2.0 (the "License");
      6  ** you may not use this file except in compliance with the License.
      7  ** You may obtain a copy of the License at
      8  **
      9  **     http://www.apache.org/licenses/LICENSE-2.0
     10  **
     11  ** Unless required by applicable law or agreed to in writing, software
     12  ** distributed under the License is distributed on an "AS IS" BASIS,
     13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  ** See the License for the specific language governing permissions and
     15  ** limitations under the License.
     16  */
     17 
     18 #ifndef _PIXELFLINGER2_H_
     19 #define _PIXELFLINGER2_H_
     20 
     21 #define USE_LLVM_TEXTURE_SAMPLER 1
     22 #define USE_LLVM_SCANLINE 1
     23 #ifndef USE_LLVM_EXECUTIONENGINE
     24 #define USE_LLVM_EXECUTIONENGINE 0 // 1 to use llvm::Execution, 0 to use libBCC, requires modifying makefile
     25 #endif
     26 #define USE_DUAL_THREAD 1
     27 
     28 #define debug_printf printf
     29 
     30 #include <stdlib.h>
     31 #include <assert.h>
     32 #include <stdio.h>
     33 
     34 #ifdef __arm__
     35 #include <cutils/log.h>
     36 
     37 #ifndef __location__
     38 #define __HIERALLOC_STRING_0__(s)   #s
     39 #define __HIERALLOC_STRING_1__(s)   __HIERALLOC_STRING_0__(s)
     40 #define __HIERALLOC_STRING_2__      __HIERALLOC_STRING_1__(__LINE__)
     41 #define __location__                __FILE__ ":" __HIERALLOC_STRING_2__
     42 #endif
     43 #undef assert
     44 #define assert(EXPR) { do { if (!(EXPR)) {ALOGD("\n*\n*\n*\n* assert fail: '"#EXPR"' at "__location__"\n*\n*\n*"); exit(EXIT_FAILURE); } } while (false); }
     45 
     46 #else // #ifdef __arm__
     47 
     48 #ifndef ALOGD
     49 #define ALOGD printf
     50 #endif //#include <stdio.h>
     51 
     52 #endif // #ifdef __arm__
     53 
     54 #include "pixelflinger2/pixelflinger2_interface.h"
     55 
     56 #include <string.h>
     57 
     58 #ifndef MIN2
     59 #  define MIN2(a, b) ((a) < (b) ? (a) : (b))
     60 #endif
     61 #ifndef MAX2
     62 #  define MAX2(a, b) ((a) > (b) ? (a) : (b))
     63 #endif
     64 
     65 namespace bcc
     66 {
     67 class BCCContext;
     68 };
     69 
     70 #if !USE_LLVM_SCANLINE
     71 typedef int BlendComp_t;
     72 #endif
     73 
     74 #if USE_DUAL_THREAD
     75 #include <pthread.h>
     76 #endif
     77 
     78 typedef void (*ShaderFunction_t)(const void*,void*,const void*);
     79 
     80 #define GGL_GET_CONTEXT(context, interface) GGLContext * context = (GGLContext *)interface;
     81 #define GGL_GET_CONST_CONTEXT(context, interface) const GGLContext * context = \
     82     (const GGLContext *)interface; (void)context;
     83 
     84 struct GGLContext {
     85    GGLInterface interface; // must be first member so that GGLContext * == GGLInterface *
     86 
     87    GGLSurface frameSurface;
     88    GGLSurface depthSurface;
     89    GGLSurface stencilSurface;
     90 
     91    bcc::BCCContext * bccCtx;
     92 
     93    struct {
     94       int depth; // assuming ieee 754 32 bit float and 32 bit 2's complement int; z_32
     95       unsigned color; // clear value; rgba_8888
     96       unsigned stencil; // s_8; repeated to clear 4 pixels at a time
     97    } clearState;
     98 
     99    gl_shader_program * CurrentProgram;
    100 
    101    mutable GGLActiveStencil activeStencil; // after primitive assembly, call StencilSelect
    102 
    103    GGLState state; // states affecting jit
    104 
    105 #if USE_DUAL_THREAD
    106    mutable struct Worker {
    107       const GGLInterface * iface;
    108       unsigned startY, endY, varyingCount;
    109       VertexOutput bV, cV, bDx, cDx;
    110       int width, height;
    111       bool assignedWork; // only used by main; worker uses assignCond & quit
    112       bool quit;
    113 
    114       pthread_cond_t assignCond;
    115       pthread_mutex_t assignLock; // held by worker execpt for during cond_wait assign
    116       pthread_cond_t finishCond;
    117       pthread_mutex_t finishLock; // held by main except for during cond_wait finish
    118       pthread_t thread;
    119 
    120       Worker() : assignedWork(false), quit(false), thread(0)
    121       {
    122          pthread_cond_init(&assignCond, NULL);
    123          pthread_cond_init(&finishCond, NULL);
    124          pthread_mutex_init(&assignLock, NULL);
    125          pthread_mutex_init(&finishLock, NULL);
    126          pthread_mutex_lock(&finishLock);
    127          // actual thread is created later in raster.cpp
    128       }
    129       ~Worker()
    130       {
    131          if (0 != thread)
    132          {
    133             pthread_mutex_lock(&assignLock);
    134             quit = true;
    135             pthread_cond_signal(&assignCond); // signal thread to quit
    136             pthread_mutex_unlock(&assignLock);
    137             pthread_join(thread, NULL);
    138          }
    139          pthread_mutex_unlock(&finishLock);
    140 
    141          pthread_cond_destroy(&assignCond);
    142          pthread_cond_destroy(&finishCond);
    143          pthread_mutex_destroy(&assignLock);
    144          pthread_mutex_destroy(&finishLock);
    145       }
    146    } worker;
    147 #endif
    148 
    149    // called by ShaderUse to set to proper rendering functions
    150    void (* PickScanLine)(GGLInterface * iface);
    151    void (* PickRaster)(GGLInterface * iface);
    152 
    153    // viewport params are transformed so that Zw = Zd * f + n
    154    // and Xw/Yw = x/y + Xd/Yd * w/h
    155    struct {
    156       VectorComp_t x, y, w, h, n, f;
    157    } viewport; // should be moved into libAgl2
    158 
    159    struct { // should be moved into libAgl2
    160 unsigned enable :
    161       1;
    162 unsigned frontFace :
    163       1; // GL_CW = 0, GL_CCW, actual value is GLenum - GL_CW
    164 unsigned cullFace :
    165       2; // GL_FRONT = 0, GL_BACK, GL_FRONT_AND_BACK, value = GLenum - GL_FRONT
    166    } cullState;
    167 };
    168 
    169 #define _PF2_TEXTURE_DATA_NAME_ "gl_PF2TEXTURE_DATA" /* sampler data pointers used by LLVM */
    170 #define _PF2_TEXTURE_DIMENSIONS_NAME_ "gl_PF2TEXTURE_DIMENSIONS" /* sampler dimensions used by LLVM */
    171 
    172 void gglError(unsigned error); // not implmented, just an assert
    173 
    174 void InitializeGGLState(GGLInterface * iface); // should be private
    175 void UninitializeGGLState(GGLInterface * iface); // should be private
    176 
    177 // they just set the function pointers
    178 void InitializeBufferFunctions(GGLInterface * iface);
    179 void InitializeRasterFunctions(GGLInterface * iface);
    180 void InitializeScanLineFunctions(GGLInterface * iface);
    181 void InitializeTextureFunctions(GGLInterface * iface);
    182 
    183 void InitializeShaderFunctions(GGLInterface * iface); // set function pointers and create needed objects
    184 void SetShaderVerifyFunctions(GGLInterface * iface); // called by state change functions
    185 void DestroyShaderFunctions(GGLInterface * iface); // destroy needed objects
    186 // actual gl_shader and gl_shader_program is created and destroyed by Shader(Program)Create/Delete,
    187 
    188 #endif // #ifndef _PIXELFLINGER2_H_
    189