Home | History | Annotate | Download | only in libOpenglRender
      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 "renderControl_dec.h"
     17 #include "FrameBuffer.h"
     18 #include "FBConfig.h"
     19 #include "EGLDispatch.h"
     20 #include "GLDispatch.h"
     21 #include "GL2Dispatch.h"
     22 #include "ThreadInfo.h"
     23 
     24 static const GLint rendererVersion = 1;
     25 
     26 static GLint rcGetRendererVersion()
     27 {
     28     return rendererVersion;
     29 }
     30 
     31 static EGLint rcGetEGLVersion(EGLint* major, EGLint* minor)
     32 {
     33     FrameBuffer *fb = FrameBuffer::getFB();
     34     if (!fb) {
     35         return EGL_FALSE;
     36     }
     37     *major = (EGLint)fb->getCaps().eglMajor;
     38     *minor = (EGLint)fb->getCaps().eglMinor;
     39 
     40     return EGL_TRUE;
     41 }
     42 
     43 static EGLint rcQueryEGLString(EGLenum name, void* buffer, EGLint bufferSize)
     44 {
     45     FrameBuffer *fb = FrameBuffer::getFB();
     46     if (!fb) {
     47         return 0;
     48     }
     49 
     50     const char *str = s_egl.eglQueryString(fb->getDisplay(), name);
     51     if (!str) {
     52         return 0;
     53     }
     54 
     55     int len = strlen(str) + 1;
     56     if (!buffer || len > bufferSize) {
     57         return -len;
     58     }
     59 
     60     strcpy((char *)buffer, str);
     61     return len;
     62 }
     63 
     64 static EGLint rcGetGLString(EGLenum name, void* buffer, EGLint bufferSize)
     65 {
     66     RenderThreadInfo *tInfo = RenderThreadInfo::get();
     67     if (!tInfo || !tInfo->currContext.Ptr()) {
     68         return 0;
     69     }
     70 
     71     const char *str = NULL;
     72 #ifdef WITH_GLES2
     73     if (tInfo->currContext->isGL2()) {
     74         str = (const char *)s_gl2.glGetString(name);
     75     }
     76     else {
     77 #endif
     78         str = (const char *)s_gl.glGetString(name);
     79 #ifdef WITH_GLES2
     80     }
     81 #endif
     82 
     83     if (!str) {
     84         return 0;
     85     }
     86 
     87     int len = strlen(str) + 1;
     88     if (!buffer || len > bufferSize) {
     89         return -len;
     90     }
     91 
     92     strcpy((char *)buffer, str);
     93     return len;
     94 }
     95 
     96 static EGLint rcGetNumConfigs(uint32_t* numAttribs)
     97 {
     98     if (numAttribs) {
     99         *numAttribs = FBConfig::getNumAttribs();
    100     }
    101     return FBConfig::getNumConfigs();
    102 }
    103 
    104 static EGLint rcGetConfigs(uint32_t bufSize, GLuint* buffer)
    105 {
    106     int configSize = FBConfig::getNumAttribs();
    107     int nConfigs = FBConfig::getNumConfigs();
    108     uint32_t neededSize = (nConfigs + 1) * configSize * sizeof(GLuint);
    109     if (!buffer || bufSize < neededSize) {
    110         return -neededSize;
    111     }
    112     FBConfig::packConfigsInfo(buffer);
    113     return nConfigs;
    114 }
    115 
    116 static EGLint rcChooseConfig(EGLint *attribs, uint32_t attribs_size, uint32_t *configs, uint32_t configs_size)
    117 {
    118     FrameBuffer *fb = FrameBuffer::getFB();
    119     if (!fb) {
    120         return 0;
    121     }
    122 
    123     return FBConfig::chooseConfig(fb, attribs, configs, configs_size);
    124 }
    125 
    126 static EGLint rcGetFBParam(EGLint param)
    127 {
    128     FrameBuffer *fb = FrameBuffer::getFB();
    129     if (!fb) {
    130         return 0;
    131     }
    132 
    133     EGLint ret = 0;
    134 
    135     switch(param) {
    136         case FB_WIDTH:
    137             ret = fb->getWidth();
    138             break;
    139         case FB_HEIGHT:
    140             ret = fb->getHeight();
    141             break;
    142         case FB_XDPI:
    143             ret = 72; // XXX: should be implemented
    144             break;
    145         case FB_YDPI:
    146             ret = 72; // XXX: should be implemented
    147             break;
    148         case FB_FPS:
    149             ret = 60;
    150             break;
    151         case FB_MIN_SWAP_INTERVAL:
    152             ret = 1; // XXX: should be implemented
    153             break;
    154         case FB_MAX_SWAP_INTERVAL:
    155             ret = 1; // XXX: should be implemented
    156             break;
    157         default:
    158             break;
    159     }
    160 
    161     return ret;
    162 }
    163 
    164 static uint32_t rcCreateContext(uint32_t config,
    165                                 uint32_t share, uint32_t glVersion)
    166 {
    167     FrameBuffer *fb = FrameBuffer::getFB();
    168     if (!fb) {
    169         return 0;
    170     }
    171 
    172     HandleType ret = fb->createRenderContext(config, share, glVersion == 2);
    173     return ret;
    174 }
    175 
    176 static void rcDestroyContext(uint32_t context)
    177 {
    178     FrameBuffer *fb = FrameBuffer::getFB();
    179     if (!fb) {
    180         return;
    181     }
    182 
    183     fb->DestroyRenderContext(context);
    184 }
    185 
    186 static uint32_t rcCreateWindowSurface(uint32_t config,
    187                                       uint32_t width, uint32_t height)
    188 {
    189     FrameBuffer *fb = FrameBuffer::getFB();
    190     if (!fb) {
    191         return 0;
    192     }
    193 
    194     return fb->createWindowSurface(config, width, height);
    195 }
    196 
    197 static void rcDestroyWindowSurface(uint32_t windowSurface)
    198 {
    199     FrameBuffer *fb = FrameBuffer::getFB();
    200     if (!fb) {
    201         return;
    202     }
    203 
    204     fb->DestroyWindowSurface( windowSurface );
    205 }
    206 
    207 static uint32_t rcCreateColorBuffer(uint32_t width,
    208                                     uint32_t height, GLenum internalFormat)
    209 {
    210     FrameBuffer *fb = FrameBuffer::getFB();
    211     if (!fb) {
    212         return 0;
    213     }
    214 
    215     return fb->createColorBuffer(width, height, internalFormat);
    216 }
    217 
    218 static int rcOpenColorBuffer2(uint32_t colorbuffer)
    219 {
    220     FrameBuffer *fb = FrameBuffer::getFB();
    221     if (!fb) {
    222         return -1;
    223     }
    224     return fb->openColorBuffer( colorbuffer );
    225 }
    226 
    227 // Deprecated, kept for compatibility with old system images only.
    228 // Use rcOpenColorBuffer2 instead.
    229 static void rcOpenColorBuffer(uint32_t colorbuffer)
    230 {
    231     (void) rcOpenColorBuffer2(colorbuffer);
    232 }
    233 
    234 static void rcCloseColorBuffer(uint32_t colorbuffer)
    235 {
    236     FrameBuffer *fb = FrameBuffer::getFB();
    237     if (!fb) {
    238         return;
    239     }
    240     fb->closeColorBuffer( colorbuffer );
    241 }
    242 
    243 static int rcFlushWindowColorBuffer(uint32_t windowSurface)
    244 {
    245     FrameBuffer *fb = FrameBuffer::getFB();
    246     if (!fb) {
    247         return -1;
    248     }
    249     if (!fb->flushWindowSurfaceColorBuffer(windowSurface)) {
    250         return -1;
    251     }
    252     return 0;
    253 }
    254 
    255 static void rcSetWindowColorBuffer(uint32_t windowSurface,
    256                                    uint32_t colorBuffer)
    257 {
    258     FrameBuffer *fb = FrameBuffer::getFB();
    259     if (!fb) {
    260         return;
    261     }
    262     fb->setWindowSurfaceColorBuffer(windowSurface, colorBuffer);
    263 }
    264 
    265 static EGLint rcMakeCurrent(uint32_t context,
    266                             uint32_t drawSurf, uint32_t readSurf)
    267 {
    268     FrameBuffer *fb = FrameBuffer::getFB();
    269     if (!fb) {
    270         return EGL_FALSE;
    271     }
    272 
    273     bool ret = fb->bindContext(context, drawSurf, readSurf);
    274 
    275     return (ret ? EGL_TRUE : EGL_FALSE);
    276 }
    277 
    278 static void rcFBPost(uint32_t colorBuffer)
    279 {
    280     FrameBuffer *fb = FrameBuffer::getFB();
    281     if (!fb) {
    282         return;
    283     }
    284 
    285     fb->post(colorBuffer);
    286 }
    287 
    288 static void rcFBSetSwapInterval(EGLint interval)
    289 {
    290    // XXX: TBD - should be implemented
    291 }
    292 
    293 static void rcBindTexture(uint32_t colorBuffer)
    294 {
    295     FrameBuffer *fb = FrameBuffer::getFB();
    296     if (!fb) {
    297         return;
    298     }
    299 
    300     fb->bindColorBufferToTexture(colorBuffer);
    301 }
    302 
    303 static void rcBindRenderbuffer(uint32_t colorBuffer)
    304 {
    305     FrameBuffer *fb = FrameBuffer::getFB();
    306     if (!fb) {
    307         return;
    308     }
    309 
    310     fb->bindColorBufferToRenderbuffer(colorBuffer);
    311 }
    312 
    313 static EGLint rcColorBufferCacheFlush(uint32_t colorBuffer,
    314                                       EGLint postCount, int forRead)
    315 {
    316    // XXX: TBD - should be implemented
    317    return 0;
    318 }
    319 
    320 static void rcReadColorBuffer(uint32_t colorBuffer,
    321                               GLint x, GLint y,
    322                               GLint width, GLint height,
    323                               GLenum format, GLenum type, void* pixels)
    324 {
    325    // XXX: TBD - should be implemented
    326 }
    327 
    328 static int rcUpdateColorBuffer(uint32_t colorBuffer,
    329                                 GLint x, GLint y,
    330                                 GLint width, GLint height,
    331                                 GLenum format, GLenum type, void* pixels)
    332 {
    333     FrameBuffer *fb = FrameBuffer::getFB();
    334     if (!fb) {
    335         return -1;
    336     }
    337 
    338     fb->updateColorBuffer(colorBuffer, x, y, width, height, format, type, pixels);
    339     return 0;
    340 }
    341 
    342 void initRenderControlContext(renderControl_decoder_context_t *dec)
    343 {
    344     dec->set_rcGetRendererVersion(rcGetRendererVersion);
    345     dec->set_rcGetEGLVersion(rcGetEGLVersion);
    346     dec->set_rcQueryEGLString(rcQueryEGLString);
    347     dec->set_rcGetGLString(rcGetGLString);
    348     dec->set_rcGetNumConfigs(rcGetNumConfigs);
    349     dec->set_rcGetConfigs(rcGetConfigs);
    350     dec->set_rcChooseConfig(rcChooseConfig);
    351     dec->set_rcGetFBParam(rcGetFBParam);
    352     dec->set_rcCreateContext(rcCreateContext);
    353     dec->set_rcDestroyContext(rcDestroyContext);
    354     dec->set_rcCreateWindowSurface(rcCreateWindowSurface);
    355     dec->set_rcDestroyWindowSurface(rcDestroyWindowSurface);
    356     dec->set_rcCreateColorBuffer(rcCreateColorBuffer);
    357     dec->set_rcOpenColorBuffer(rcOpenColorBuffer);
    358     dec->set_rcCloseColorBuffer(rcCloseColorBuffer);
    359     dec->set_rcSetWindowColorBuffer(rcSetWindowColorBuffer);
    360     dec->set_rcFlushWindowColorBuffer(rcFlushWindowColorBuffer);
    361     dec->set_rcMakeCurrent(rcMakeCurrent);
    362     dec->set_rcFBPost(rcFBPost);
    363     dec->set_rcFBSetSwapInterval(rcFBSetSwapInterval);
    364     dec->set_rcBindTexture(rcBindTexture);
    365     dec->set_rcBindRenderbuffer(rcBindRenderbuffer);
    366     dec->set_rcColorBufferCacheFlush(rcColorBufferCacheFlush);
    367     dec->set_rcReadColorBuffer(rcReadColorBuffer);
    368     dec->set_rcUpdateColorBuffer(rcUpdateColorBuffer);
    369     dec->set_rcOpenColorBuffer2(rcOpenColorBuffer2);
    370 }
    371