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 
17 #include <gui/GLConsumer.h>
18 #include <gui/Surface.h>
19 #include <gui/SurfaceControl.h>
20 
21 #include <EGL/egl.h>
22 #include <GLES2/gl2.h>
23 
24 namespace android {
25 
26 class SurfaceComposerClient;
27 class SurfaceControl;
28 
29 enum { MAX_SHADER_LINES = 128 };
30 
31 struct ShaderDesc {
32     const char* name;
33     const char* vertexShader[MAX_SHADER_LINES];
34     const char* fragmentShader[MAX_SHADER_LINES];
35 };
36 
37 class GLHelper {
38 
39 public:
40 
41     enum { DITHER_KERNEL_SIZE = 4 };
42 
43     GLHelper();
44 
45     ~GLHelper();
46 
47     bool setUp(const ShaderDesc* shaderDescs, size_t numShaders);
48 
49     void tearDown();
50 
51     bool makeCurrent(EGLSurface surface);
52 
53     bool createSurfaceTexture(uint32_t w, uint32_t h,
54             sp<GLConsumer>* surfaceTexture, EGLSurface* surface,
55             GLuint* name);
56 
57     bool createWindowSurface(uint32_t w, uint32_t h,
58             sp<SurfaceControl>* surfaceControl, EGLSurface* surface);
59 
60     void destroySurface(EGLSurface* surface);
61 
62     bool swapBuffers(EGLSurface surface);
63 
64     bool getShaderProgram(const char* name, GLuint* outPgm);
65 
66     bool getDitherTexture(GLuint* outTexName);
67 
68 private:
69 
70     bool createNamedSurfaceTexture(GLuint name, uint32_t w, uint32_t h,
71             sp<GLConsumer>* surfaceTexture, EGLSurface* surface);
72 
73     bool computeWindowScale(uint32_t w, uint32_t h, float* scale);
74 
75     bool setUpShaders(const ShaderDesc* shaderDescs, size_t numShaders);
76 
77     EGLDisplay mDisplay;
78     EGLContext mContext;
79     EGLSurface mDummySurface;
80     sp<GLConsumer> mDummyGLConsumer;
81     EGLConfig mConfig;
82 
83     sp<SurfaceComposerClient> mSurfaceComposerClient;
84 
85     GLuint* mShaderPrograms;
86     const ShaderDesc* mShaderDescs;
87     size_t mNumShaders;
88 
89     GLuint mDitherTexture;
90 };
91 
92 } // namespace android
93