• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/Lock.h"
23 
24 #include <vector>
25 
26 // Helper class used to draw a simple texture to the current framebuffer.
27 // Usage is pretty simple:
28 //
29 //   1) Create a TextureDraw instance.
30 //
31 //   2) Each time you want to draw a texture, call draw(texture, rotation),
32 //      where |texture| is the name of a GLES 2.x texture object, and
33 //      |rotation| is an angle in degrees describing the clockwise rotation
34 //      in the GL y-upwards coordinate space. This function fills the whole
35 //      framebuffer with texture content.
36 //
37 class TextureDraw {
38 public:
39     TextureDraw();
40     ~TextureDraw();
41 
42     // Fill the current framebuffer with the content of |texture|, which must
43     // be the name of a GLES 2.x texture object. |rotationDegrees| is a
44     // clockwise rotation angle in degrees (clockwise in the GL Y-upwards
45     // coordinate space; only supported values are 0, 90, 180, 270). |dx,dy| is
46     // the translation of the image towards the origin.
draw(GLuint texture,float rotationDegrees,float dx,float dy)47     bool draw(GLuint texture, float rotationDegrees, float dx, float dy) {
48         return drawImpl(texture, rotationDegrees, dx, dy, false);
49     }
50     // Same as 'draw()', but if an overlay has been provided, that overlay is
51     // drawn on top of everything else.
drawWithOverlay(GLuint texture,float rotationDegrees,float dx,float dy)52     bool drawWithOverlay(GLuint texture, float rotationDegrees, float dx, float dy) {
53         return drawImpl(texture, rotationDegrees, dx, dy, true);
54     }
55 
56     void setScreenMask(int width, int height, const unsigned char* rgbaData);
57     void drawLayer(ComposeLayer* l, int frameWidth, int frameHeight,
58                    int cbWidth, int cbHeight, GLuint texture);
59     void prepareForDrawLayer();
60     void cleanupForDrawLayer();
61 
62 private:
63     bool drawImpl(GLuint texture, float rotationDegrees, float dx, float dy, bool wantOverlay);
64 
65     GLuint mVertexShader;
66     GLuint mFragmentShader;
67     GLuint mProgram;
68     GLint mAlpha;
69     GLint mComposeMode;
70     GLint mColor;
71     GLint mCoordTranslation;
72     GLint mCoordScale;
73     GLint mPositionSlot;
74     GLint mInCoordSlot;
75     GLint mScaleSlot;
76     GLint mTextureSlot;
77     GLint mTranslationSlot;
78     GLuint mVertexBuffer;
79     GLuint mIndexBuffer;
80 
81     android::base::Lock mMaskLock;
82     GLuint mMaskTexture;
83     int    mMaskWidth;
84     int    mMaskHeight;
85     // The size of the mMaskPixels. If the new mask is smaller than this size, we won't
86     // allocate new memory for mMaskPixels.
87     int    mMaskTextureWidth;
88     int    mMaskTextureHeight;
89     bool   mHaveNewMask;
90     bool   mMaskIsValid;
91     bool mShouldReallocateTexture;
92     // The size of mMaskPixels are always of size mMaskWidth * mMaskHeight * 4 bytes
93     std::vector<unsigned char> mMaskPixels;
94     bool   mBlendResetNeeded = false;
95 };
96 
97 #endif  // TEXTURE_DRAW_H
98