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