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 = getRenderThreadInfo();
     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 void rcOpenColorBuffer(uint32_t colorbuffer)
    219 {
    220     FrameBuffer *fb = FrameBuffer::getFB();
    221     if (!fb) {
    222         return;
    223     }
    224     fb->openColorBuffer( colorbuffer );
    225 }
    226 
    227 static void rcCloseColorBuffer(uint32_t colorbuffer)
    228 {
    229     FrameBuffer *fb = FrameBuffer::getFB();
    230     if (!fb) {
    231         return;
    232     }
    233     fb->closeColorBuffer( colorbuffer );
    234 }
    235 
    236 static int rcFlushWindowColorBuffer(uint32_t windowSurface)
    237 {
    238     FrameBuffer *fb = FrameBuffer::getFB();
    239     if (!fb) {
    240         return -1;
    241     }
    242     fb->flushWindowSurfaceColorBuffer(windowSurface);
    243     return 0;
    244 }
    245 
    246 static void rcSetWindowColorBuffer(uint32_t windowSurface,
    247                                    uint32_t colorBuffer)
    248 {
    249     FrameBuffer *fb = FrameBuffer::getFB();
    250     if (!fb) {
    251         return;
    252     }
    253     fb->setWindowSurfaceColorBuffer(windowSurface, colorBuffer);
    254 }
    255 
    256 static EGLint rcMakeCurrent(uint32_t context,
    257                             uint32_t drawSurf, uint32_t readSurf)
    258 {
    259     FrameBuffer *fb = FrameBuffer::getFB();
    260     if (!fb) {
    261         return EGL_FALSE;
    262     }
    263 
    264     bool ret = fb->bindContext(context, drawSurf, readSurf);
    265 
    266     return (ret ? EGL_TRUE : EGL_FALSE);
    267 }
    268 
    269 static void rcFBPost(uint32_t colorBuffer)
    270 {
    271     FrameBuffer *fb = FrameBuffer::getFB();
    272     if (!fb) {
    273         return;
    274     }
    275 
    276     fb->post(colorBuffer);
    277 }
    278 
    279 static void rcFBSetSwapInterval(EGLint interval)
    280 {
    281    // XXX: TBD - should be implemented
    282 }
    283 
    284 static void rcBindTexture(uint32_t colorBuffer)
    285 {
    286     FrameBuffer *fb = FrameBuffer::getFB();
    287     if (!fb) {
    288         return;
    289     }
    290 
    291     fb->bindColorBufferToTexture(colorBuffer);
    292 }
    293 
    294 static void rcBindRenderbuffer(uint32_t colorBuffer)
    295 {
    296     FrameBuffer *fb = FrameBuffer::getFB();
    297     if (!fb) {
    298         return;
    299     }
    300 
    301     fb->bindColorBufferToRenderbuffer(colorBuffer);
    302 }
    303 
    304 static EGLint rcColorBufferCacheFlush(uint32_t colorBuffer,
    305                                       EGLint postCount, int forRead)
    306 {
    307    // XXX: TBD - should be implemented
    308    return 0;
    309 }
    310 
    311 static void rcReadColorBuffer(uint32_t colorBuffer,
    312                               GLint x, GLint y,
    313                               GLint width, GLint height,
    314                               GLenum format, GLenum type, void* pixels)
    315 {
    316    // XXX: TBD - should be implemented
    317 }
    318 
    319 static int rcUpdateColorBuffer(uint32_t colorBuffer,
    320                                 GLint x, GLint y,
    321                                 GLint width, GLint height,
    322                                 GLenum format, GLenum type, void* pixels)
    323 {
    324     FrameBuffer *fb = FrameBuffer::getFB();
    325     if (!fb) {
    326         return -1;
    327     }
    328 
    329     fb->updateColorBuffer(colorBuffer, x, y, width, height, format, type, pixels);
    330     return 0;
    331 }
    332 
    333 void initRenderControlContext(renderControl_decoder_context_t *dec)
    334 {
    335     dec->set_rcGetRendererVersion(rcGetRendererVersion);
    336     dec->set_rcGetEGLVersion(rcGetEGLVersion);
    337     dec->set_rcQueryEGLString(rcQueryEGLString);
    338     dec->set_rcGetGLString(rcGetGLString);
    339     dec->set_rcGetNumConfigs(rcGetNumConfigs);
    340     dec->set_rcGetConfigs(rcGetConfigs);
    341     dec->set_rcChooseConfig(rcChooseConfig);
    342     dec->set_rcGetFBParam(rcGetFBParam);
    343     dec->set_rcCreateContext(rcCreateContext);
    344     dec->set_rcDestroyContext(rcDestroyContext);
    345     dec->set_rcCreateWindowSurface(rcCreateWindowSurface);
    346     dec->set_rcDestroyWindowSurface(rcDestroyWindowSurface);
    347     dec->set_rcCreateColorBuffer(rcCreateColorBuffer);
    348     dec->set_rcOpenColorBuffer(rcOpenColorBuffer);
    349     dec->set_rcCloseColorBuffer(rcCloseColorBuffer);
    350     dec->set_rcSetWindowColorBuffer(rcSetWindowColorBuffer);
    351     dec->set_rcFlushWindowColorBuffer(rcFlushWindowColorBuffer);
    352     dec->set_rcMakeCurrent(rcMakeCurrent);
    353     dec->set_rcFBPost(rcFBPost);
    354     dec->set_rcFBSetSwapInterval(rcFBSetSwapInterval);
    355     dec->set_rcBindTexture(rcBindTexture);
    356     dec->set_rcBindRenderbuffer(rcBindRenderbuffer);
    357     dec->set_rcColorBufferCacheFlush(rcColorBufferCacheFlush);
    358     dec->set_rcReadColorBuffer(rcReadColorBuffer);
    359     dec->set_rcUpdateColorBuffer(rcUpdateColorBuffer);
    360 }
    361