GLHelper.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2012 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 <gui/GraphicBufferAlloc.h>
  17. #include <gui/GLConsumer.h>
  18. #include <gui/Surface.h>
  19. #include <gui/SurfaceControl.h>
  20. #include <EGL/egl.h>
  21. #include <GLES2/gl2.h>
  22. namespace android {
  23. class SurfaceComposerClient;
  24. class SurfaceControl;
  25. enum { MAX_SHADER_LINES = 128 };
  26. struct ShaderDesc {
  27. const char* name;
  28. const char* vertexShader[MAX_SHADER_LINES];
  29. const char* fragmentShader[MAX_SHADER_LINES];
  30. };
  31. class GLHelper {
  32. public:
  33. enum { DITHER_KERNEL_SIZE = 4 };
  34. GLHelper();
  35. ~GLHelper();
  36. bool setUp(const ShaderDesc* shaderDescs, size_t numShaders);
  37. void tearDown();
  38. bool makeCurrent(EGLSurface surface);
  39. bool createSurfaceTexture(uint32_t w, uint32_t h,
  40. sp<GLConsumer>* surfaceTexture, EGLSurface* surface,
  41. GLuint* name);
  42. bool createWindowSurface(uint32_t w, uint32_t h,
  43. sp<SurfaceControl>* surfaceControl, EGLSurface* surface);
  44. void destroySurface(EGLSurface* surface);
  45. bool swapBuffers(EGLSurface surface);
  46. bool getShaderProgram(const char* name, GLuint* outPgm);
  47. bool getDitherTexture(GLuint* outTexName);
  48. private:
  49. bool createNamedSurfaceTexture(GLuint name, uint32_t w, uint32_t h,
  50. sp<GLConsumer>* surfaceTexture, EGLSurface* surface);
  51. bool computeWindowScale(uint32_t w, uint32_t h, float* scale);
  52. bool setUpShaders(const ShaderDesc* shaderDescs, size_t numShaders);
  53. sp<GraphicBufferAlloc> mGraphicBufferAlloc;
  54. EGLDisplay mDisplay;
  55. EGLContext mContext;
  56. EGLSurface mDummySurface;
  57. sp<GLConsumer> mDummyGLConsumer;
  58. EGLConfig mConfig;
  59. sp<SurfaceComposerClient> mSurfaceComposerClient;
  60. GLuint* mShaderPrograms;
  61. const ShaderDesc* mShaderDescs;
  62. size_t mNumShaders;
  63. GLuint mDitherTexture;
  64. };
  65. } // namespace android