Home | History | Annotate | Download | only in drm_hwcomposer
      1 /*
      2  * Copyright (C) 2015 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 
     17 #ifndef ANDROID_GL_WORKER_H_
     18 #define ANDROID_GL_WORKER_H_
     19 
     20 #include <pthread.h>
     21 
     22 #include <memory>
     23 #include <vector>
     24 
     25 #define EGL_EGLEXT_PROTOTYPES
     26 #define GL_GLEXT_PROTOTYPES
     27 
     28 #include <EGL/egl.h>
     29 #include <EGL/eglext.h>
     30 #include <GLES2/gl2.h>
     31 #include <GLES2/gl2ext.h>
     32 
     33 struct hwc_layer_1;
     34 
     35 namespace android {
     36 
     37 #define AUTO_GL_TYPE(name, type, zero, deleter) \
     38   struct name##Deleter {                        \
     39     typedef type pointer;                       \
     40                                                 \
     41     void operator()(pointer p) const {          \
     42       if (p != zero) {                          \
     43         deleter;                                \
     44       }                                         \
     45     }                                           \
     46   };                                            \
     47   typedef std::unique_ptr<type, name##Deleter> name;
     48 
     49 AUTO_GL_TYPE(AutoGLFramebuffer, GLuint, 0, glDeleteFramebuffers(1, &p))
     50 AUTO_GL_TYPE(AutoGLBuffer, GLuint, 0, glDeleteBuffers(1, &p))
     51 AUTO_GL_TYPE(AutoGLTexture, GLuint, 0, glDeleteTextures(1, &p))
     52 AUTO_GL_TYPE(AutoGLShader, GLint, 0, glDeleteShader(p))
     53 AUTO_GL_TYPE(AutoGLProgram, GLint, 0, glDeleteProgram(p))
     54 
     55 struct EGLImageDeleter {
     56   typedef EGLImageKHR pointer;
     57 
     58   EGLDisplay egl_display_;
     59 
     60   EGLImageDeleter(EGLDisplay egl_display) : egl_display_(egl_display) {
     61   }
     62 
     63   void operator()(EGLImageKHR p) const {
     64     if (p != EGL_NO_IMAGE_KHR) {
     65       eglDestroyImageKHR(egl_display_, p);
     66     }
     67   }
     68 };
     69 typedef std::unique_ptr<EGLImageKHR, EGLImageDeleter> AutoEGLImageKHR;
     70 
     71 struct AutoEGLImageAndGLTexture {
     72   AutoEGLImageKHR image;
     73   AutoGLTexture texture;
     74 
     75   AutoEGLImageAndGLTexture(EGLDisplay egl_display)
     76       : image(EGL_NO_IMAGE_KHR, EGLImageDeleter(egl_display)) {
     77   }
     78 };
     79 
     80 class GLWorker {
     81  public:
     82   struct Work {
     83     hwc_layer_1 *layers;
     84     size_t num_layers;
     85     int timeline_fd;
     86     sp<GraphicBuffer> framebuffer;
     87 
     88     Work() = default;
     89     Work(const Work &rhs) = delete;
     90   };
     91 
     92   class Compositor {
     93    public:
     94     Compositor();
     95     ~Compositor();
     96 
     97     int Init();
     98 
     99     int Composite(hwc_layer_1 *layers, size_t num_layers,
    100                   sp<GraphicBuffer> framebuffer);
    101 
    102    private:
    103     EGLDisplay egl_display_;
    104     EGLContext egl_ctx_;
    105 
    106     std::vector<AutoGLProgram> blend_programs_;
    107     AutoGLBuffer vertex_buffer_;
    108   };
    109 
    110   GLWorker();
    111   ~GLWorker();
    112 
    113   int Init();
    114 
    115   int DoWork(Work *work);
    116 
    117  private:
    118   bool initialized_;
    119   pthread_t thread_;
    120   pthread_mutex_t lock_;
    121   pthread_cond_t work_ready_cond_;
    122   pthread_cond_t work_done_cond_;
    123   Work *worker_work_;
    124   bool work_ready_;
    125   bool worker_exit_;
    126   int worker_ret_;
    127 
    128   void WorkerRoutine();
    129   int DoComposition(Compositor &compositor, Work *work);
    130 
    131   int SignalWorker(Work *work, bool worker_exit);
    132 
    133   static void *StartRoutine(void *arg);
    134 };
    135 }
    136 
    137 #endif
    138