Home | History | Annotate | Download | only in EGL
      1 /*
      2 * Copyright (C) 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 #include "EglOsApi.h"
     17 #include "MacNative.h"
     18 #define MAX_PBUFFER_MIPMAP_LEVEL 1
     19 
     20 namespace EglOS {
     21 
     22 static std::list<EGLNativePixelFormatType> s_nativeConfigs;
     23 
     24 EGLNativeDisplayType getDefaultDisplay() {return 0;}
     25 
     26 bool releaseDisplay(EGLNativeDisplayType dpy) {
     27     return true;
     28 }
     29 
     30 static EglConfig* pixelFormatToConfig(int index,int renderableType,EGLNativePixelFormatType* frmt){
     31     if(!frmt) return NULL;
     32 
     33     EGLint  red,green,blue,alpha,depth,stencil;
     34     EGLint  supportedSurfaces,visualType,visualId;
     35     EGLint  transparentType,samples;
     36     EGLint  tRed,tGreen,tBlue;
     37     EGLint  pMaxWidth,pMaxHeight,pMaxPixels;
     38     EGLint  configId,level;
     39     EGLint  window,pbuffer;
     40     EGLint  doubleBuffer,colorSize;
     41 
     42     getPixelFormatAttrib(*frmt,MAC_HAS_DOUBLE_BUFFER,&doubleBuffer);
     43     if(!doubleBuffer) return NULL; //pixel double buffer
     44 
     45     supportedSurfaces = 0;
     46 
     47     getPixelFormatAttrib(*frmt,MAC_DRAW_TO_WINDOW,&window);
     48     getPixelFormatAttrib(*frmt,MAC_DRAW_TO_PBUFFER,&pbuffer);
     49 
     50     if(window)  supportedSurfaces |= EGL_WINDOW_BIT;
     51     if(pbuffer) supportedSurfaces |= EGL_PBUFFER_BIT;
     52 
     53     if(!supportedSurfaces) return NULL;
     54 
     55     //default values
     56     visualId                  = 0;
     57     visualType                = EGL_NONE;
     58     EGLenum caveat            = EGL_NONE;
     59     EGLBoolean renderable     = EGL_FALSE;
     60     pMaxWidth                 = PBUFFER_MAX_WIDTH;
     61     pMaxHeight                = PBUFFER_MAX_HEIGHT;
     62     pMaxPixels                = PBUFFER_MAX_PIXELS;
     63     samples                   = 0;
     64     level                     = 0;
     65     tRed = tGreen = tBlue     = 0;
     66 
     67     transparentType = EGL_NONE;
     68 
     69     getPixelFormatAttrib(*frmt,MAC_SAMPLES_PER_PIXEL,&samples);
     70     getPixelFormatAttrib(*frmt,MAC_COLOR_SIZE,&colorSize);
     71     getPixelFormatAttrib(*frmt,MAC_ALPHA_SIZE,&alpha);
     72     getPixelFormatAttrib(*frmt,MAC_DEPTH_SIZE,&depth);
     73     getPixelFormatAttrib(*frmt,MAC_STENCIL_SIZE,&stencil);
     74 
     75     red = green = blue = (colorSize / 4); //TODO: ask guy if it is OK
     76 
     77     return new EglConfig(red,green,blue,alpha,caveat,(EGLint)index,depth,level,pMaxWidth,pMaxHeight,pMaxPixels,renderable,renderableType,
     78                          visualId,visualType,samples,stencil,supportedSurfaces,transparentType,tRed,tGreen,tBlue,*frmt);
     79 }
     80 
     81 
     82 static void initNativeConfigs(){
     83     int nConfigs = getNumPixelFormats();
     84     if(s_nativeConfigs.empty()){
     85         for(int i=0; i < nConfigs ;i++){
     86              EGLNativePixelFormatType frmt = getPixelFormat(i);
     87              if(frmt){
     88                  s_nativeConfigs.push_back(frmt);
     89              }
     90         }
     91     }
     92 }
     93 
     94 void queryConfigs(EGLNativeDisplayType dpy,int renderableType,ConfigsList& listOut) {
     95     int i = 0 ;
     96     initNativeConfigs();
     97     for(std::list<EGLNativePixelFormatType>::iterator it = s_nativeConfigs.begin(); it != s_nativeConfigs.end();it++){
     98          EGLNativePixelFormatType frmt = *it;
     99          EglConfig* conf = pixelFormatToConfig(i++,renderableType,&frmt);
    100          if(conf){
    101              listOut.push_front(conf);
    102          };
    103     }
    104 }
    105 
    106 bool validNativeWin(EGLNativeDisplayType dpy, EGLNativeWindowType win) {
    107     unsigned int width,height;
    108     return nsGetWinDims(win,&width,&height);
    109 }
    110 
    111 bool validNativeWin(EGLNativeDisplayType dpy, EGLNativeSurfaceType win) {
    112     return validNativeWin(dpy,(EGLNativeWindowType)win);
    113 }
    114 
    115 //no support for pixmap in mac
    116 bool validNativePixmap(EGLNativeDisplayType dpy, EGLNativeSurfaceType pix) {
    117 
    118    return true;
    119 }
    120 
    121 bool checkWindowPixelFormatMatch(EGLNativeDisplayType dpy,EGLNativeWindowType win,EglConfig* cfg,unsigned int* width,unsigned int* height) {
    122     int r,g,b;
    123     bool ret = nsGetWinDims(win,width,height);
    124 
    125     cfg->getConfAttrib(EGL_RED_SIZE,&r);
    126     cfg->getConfAttrib(EGL_GREEN_SIZE,&g);
    127     cfg->getConfAttrib(EGL_BLUE_SIZE,&b);
    128     bool match = nsCheckColor(win,r + g + b);
    129 
    130     return ret && match;
    131 }
    132 
    133 //no support for pixmap in mac
    134 bool checkPixmapPixelFormatMatch(EGLNativeDisplayType dpy,EGLNativePixmapType pix,EglConfig* cfg,unsigned int* width,unsigned int* height) {
    135     return false;
    136 }
    137 
    138 EGLNativeSurfaceType createPbufferSurface(EGLNativeDisplayType dpy,EglConfig* cfg,EglPbufferSurface* srfc){
    139     EGLint width,height,hasMipmap,tmp;
    140     EGLint target,format;
    141     srfc->getDim(&width,&height,&tmp);
    142     srfc->getTexInfo(&format,&target);
    143     srfc->getAttrib(EGL_MIPMAP_TEXTURE,&hasMipmap);
    144     EGLint maxMipmap = hasMipmap ? MAX_PBUFFER_MIPMAP_LEVEL:0;
    145     return (EGLNativeSurfaceType)nsCreatePBuffer(target,format,maxMipmap,width,height);
    146 }
    147 
    148 bool releasePbuffer(EGLNativeDisplayType dis,EGLNativeSurfaceType pb) {
    149     nsDestroyPBuffer(pb);
    150     return true;
    151 }
    152 
    153 EGLNativeContextType createContext(EGLNativeDisplayType dpy,EglConfig* cfg,EGLNativeContextType sharedContext) {
    154  return nsCreateContext(cfg->nativeConfig(),sharedContext);
    155 }
    156 
    157 bool destroyContext(EGLNativeDisplayType dpy,EGLNativeContextType ctx) {
    158     nsDestroyContext(ctx);
    159     return true;
    160 }
    161 
    162 bool makeCurrent(EGLNativeDisplayType dpy,EglSurface* read,EglSurface* draw,EGLNativeContextType ctx){
    163 
    164     // check for unbind
    165     if (ctx == NULL && read == NULL && draw == NULL) {
    166         nsWindowMakeCurrent(NULL, NULL);
    167         return true;
    168     }
    169     else if (ctx == NULL || read == NULL || draw == NULL) {
    170         // error !
    171         return false;
    172     }
    173 
    174     //dont supporting diffrent read & draw surfaces on Mac
    175     if(read->native() != draw->native()) return false;
    176     switch(draw->type()){
    177     case EglSurface::WINDOW:
    178         nsWindowMakeCurrent(ctx,draw->native());
    179         break;
    180     case EglSurface::PBUFFER:
    181     {
    182         EGLint hasMipmap;
    183         draw->getAttrib(EGL_MIPMAP_TEXTURE,&hasMipmap);
    184         int mipmapLevel = hasMipmap ? MAX_PBUFFER_MIPMAP_LEVEL:0;
    185         nsPBufferMakeCurrent(ctx,draw->native(),mipmapLevel);
    186         break;
    187     }
    188     case EglSurface::PIXMAP: // not supported on Mac
    189     default:
    190         return false;
    191     }
    192     return true;
    193 }
    194 
    195 void swapBuffers(EGLNativeDisplayType dpy,EGLNativeSurfaceType srfc){
    196     nsSwapBuffers();
    197 }
    198 
    199 void waitNative(){}
    200 
    201 void swapInterval(EGLNativeDisplayType dpy,EGLNativeSurfaceType win,int interval){
    202     nsSwapInterval(&interval);
    203 }
    204 
    205 EGLNativeSurfaceType createWindowSurface(EGLNativeWindowType wnd){
    206     return (EGLNativeSurfaceType)wnd;
    207 }
    208 
    209 EGLNativeSurfaceType createPixmapSurface(EGLNativePixmapType pix){
    210     return (EGLNativeSurfaceType)pix;
    211 }
    212 
    213 void destroySurface(EGLNativeSurfaceType srfc){
    214 }
    215 
    216 EGLNativeInternalDisplayType getInternalDisplay(EGLNativeDisplayType dpy){
    217     return (EGLNativeInternalDisplayType)dpy;
    218 }
    219 
    220 void deleteDisplay(EGLNativeInternalDisplayType idpy){
    221 }
    222 
    223 };
    224