1 // Copyright (C) 2015 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TEXTURE_DRAW_H
16 #define TEXTURE_DRAW_H
17 
18 #include <EGL/egl.h>
19 #include <EGL/eglext.h>
20 #include <GLES2/gl2.h>
21 #include "Hwc2.h"
22 #include "aemu/base/synchronization/Lock.h"
23 
24 #include <vector>
25 
26 namespace gfxstream {
27 namespace gl {
28 
29 // Helper class used to draw a simple texture to the current framebuffer.
30 // Usage is pretty simple:
31 //
32 //   1) Create a TextureDraw instance.
33 //
34 //   2) Each time you want to draw a texture, call draw(texture, rotation),
35 //      where |texture| is the name of a GLES 2.x texture object, and
36 //      |rotation| is an angle in degrees describing the clockwise rotation
37 //      in the GL y-upwards coordinate space. This function fills the whole
38 //      framebuffer with texture content.
39 //
40 class TextureDraw {
41 public:
42     TextureDraw();
43     ~TextureDraw();
44 
45     // Fill the current framebuffer with the content of |texture|, which must
46     // be the name of a GLES 2.x texture object. |rotationDegrees| is a
47     // clockwise rotation angle in degrees (clockwise in the GL Y-upwards
48     // coordinate space; only supported values are 0, 90, 180, 270). |dx,dy| is
49     // the translation of the image towards the origin.
draw(GLuint texture,float rotationDegrees,float dx,float dy)50     bool draw(GLuint texture, float rotationDegrees, float dx, float dy) {
51         return drawImpl(texture, rotationDegrees, dx, dy, false);
52     }
53     // Same as 'draw()', but if an overlay has been provided, that overlay is
54     // drawn on top of everything else.
drawWithOverlay(GLuint texture,float rotationDegrees,float dx,float dy)55     bool drawWithOverlay(GLuint texture, float rotationDegrees, float dx, float dy) {
56         return drawImpl(texture, rotationDegrees, dx, dy, true);
57     }
58 
59     void setScreenMask(int width, int height, const unsigned char* rgbaData);
60     void drawLayer(const ComposeLayer& l, int frameWidth, int frameHeight,
61                    int cbWidth, int cbHeight, GLuint texture);
62     void prepareForDrawLayer();
63     void cleanupForDrawLayer();
64 
65 private:
66     bool drawImpl(GLuint texture, float rotationDegrees, float dx, float dy, bool wantOverlay);
67     void preDrawLayer();
68 
69     GLuint mVertexShader;
70     GLuint mFragmentShader;
71     GLuint mProgram;
72     GLint mAlpha;
73     GLint mComposeMode;
74     GLint mColor;
75     GLint mCoordTranslation;
76     GLint mCoordScale;
77     GLint mPositionSlot;
78     GLint mInCoordSlot;
79     GLint mScaleSlot;
80     GLint mTextureSlot;
81     GLint mTranslationSlot;
82     GLuint mVertexBuffer;
83     GLuint mIndexBuffer;
84 
85     android::base::Lock mMaskLock;
86     GLuint mMaskTexture;
87     int    mMaskWidth;
88     int    mMaskHeight;
89     // The size of the mMaskPixels. If the new mask is smaller than this size, we won't
90     // allocate new memory for mMaskPixels.
91     int    mMaskTextureWidth;
92     int    mMaskTextureHeight;
93     bool   mHaveNewMask;
94     bool   mMaskIsValid;
95     bool mShouldReallocateTexture;
96     // The size of mMaskPixels are always of size mMaskWidth * mMaskHeight * 4 bytes
97     std::vector<unsigned char> mMaskPixels;
98     bool   mBlendResetNeeded = false;
99 };
100 
101 }  // namespace gl
102 }  // namespace gfxstream
103 
104 #endif  // TEXTURE_DRAW_H
105