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 validNativeDisplay(EGLNativeInternalDisplayType dpy) { 107 return true; 108 } 109 110 bool validNativeWin(EGLNativeDisplayType dpy, EGLNativeWindowType win) { 111 unsigned int width,height; 112 return nsGetWinDims(win,&width,&height); 113 } 114 115 bool validNativeWin(EGLNativeDisplayType dpy, EGLNativeSurfaceType win) { 116 return validNativeWin(dpy,(EGLNativeWindowType)win); 117 } 118 119 //no support for pixmap in mac 120 bool validNativePixmap(EGLNativeDisplayType dpy, EGLNativeSurfaceType pix) { 121 122 return true; 123 } 124 125 bool checkWindowPixelFormatMatch(EGLNativeDisplayType dpy,EGLNativeWindowType win,EglConfig* cfg,unsigned int* width,unsigned int* height) { 126 int r,g,b; 127 bool ret = nsGetWinDims(win,width,height); 128 129 cfg->getConfAttrib(EGL_RED_SIZE,&r); 130 cfg->getConfAttrib(EGL_GREEN_SIZE,&g); 131 cfg->getConfAttrib(EGL_BLUE_SIZE,&b); 132 bool match = nsCheckColor(win,r + g + b); 133 134 return ret && match; 135 } 136 137 //no support for pixmap in mac 138 bool checkPixmapPixelFormatMatch(EGLNativeDisplayType dpy,EGLNativePixmapType pix,EglConfig* cfg,unsigned int* width,unsigned int* height) { 139 return false; 140 } 141 142 EGLNativeSurfaceType createPbufferSurface(EGLNativeDisplayType dpy,EglConfig* cfg,EglPbufferSurface* srfc){ 143 EGLint width,height,hasMipmap,tmp; 144 EGLint target,format; 145 srfc->getDim(&width,&height,&tmp); 146 srfc->getTexInfo(&format,&target); 147 srfc->getAttrib(EGL_MIPMAP_TEXTURE,&hasMipmap); 148 EGLint maxMipmap = hasMipmap ? MAX_PBUFFER_MIPMAP_LEVEL:0; 149 return (EGLNativeSurfaceType)nsCreatePBuffer(target,format,maxMipmap,width,height); 150 } 151 152 bool releasePbuffer(EGLNativeDisplayType dis,EGLNativeSurfaceType pb) { 153 nsDestroyPBuffer(pb); 154 return true; 155 } 156 157 EGLNativeContextType createContext(EGLNativeDisplayType dpy,EglConfig* cfg,EGLNativeContextType sharedContext) { 158 return nsCreateContext(cfg->nativeConfig(),sharedContext); 159 } 160 161 bool destroyContext(EGLNativeDisplayType dpy,EGLNativeContextType ctx) { 162 nsDestroyContext(ctx); 163 return true; 164 } 165 166 bool makeCurrent(EGLNativeDisplayType dpy,EglSurface* read,EglSurface* draw,EGLNativeContextType ctx){ 167 168 // check for unbind 169 if (ctx == NULL && read == NULL && draw == NULL) { 170 nsWindowMakeCurrent(NULL, NULL); 171 return true; 172 } 173 else if (ctx == NULL || read == NULL || draw == NULL) { 174 // error ! 175 return false; 176 } 177 178 //dont supporting diffrent read & draw surfaces on Mac 179 if(read->native() != draw->native()) return false; 180 switch(draw->type()){ 181 case EglSurface::WINDOW: 182 nsWindowMakeCurrent(ctx,draw->native()); 183 break; 184 case EglSurface::PBUFFER: 185 { 186 EGLint hasMipmap; 187 draw->getAttrib(EGL_MIPMAP_TEXTURE,&hasMipmap); 188 int mipmapLevel = hasMipmap ? MAX_PBUFFER_MIPMAP_LEVEL:0; 189 nsPBufferMakeCurrent(ctx,draw->native(),mipmapLevel); 190 break; 191 } 192 case EglSurface::PIXMAP: // not supported on Mac 193 default: 194 return false; 195 } 196 return true; 197 } 198 199 void swapBuffers(EGLNativeDisplayType dpy,EGLNativeSurfaceType srfc){ 200 nsSwapBuffers(); 201 } 202 203 void waitNative(){} 204 205 void swapInterval(EGLNativeDisplayType dpy,EGLNativeSurfaceType win,int interval){ 206 nsSwapInterval(&interval); 207 } 208 209 EGLNativeSurfaceType createWindowSurface(EGLNativeWindowType wnd){ 210 return (EGLNativeSurfaceType)wnd; 211 } 212 213 EGLNativeSurfaceType createPixmapSurface(EGLNativePixmapType pix){ 214 return (EGLNativeSurfaceType)pix; 215 } 216 217 void destroySurface(EGLNativeSurfaceType srfc){ 218 } 219 220 EGLNativeInternalDisplayType getInternalDisplay(EGLNativeDisplayType dpy){ 221 return (EGLNativeInternalDisplayType)dpy; 222 } 223 224 void deleteDisplay(EGLNativeInternalDisplayType idpy){ 225 } 226 227 }; 228