TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / graphics / emugl / ColorBuffer.h
1 /*
2 * Copyright (C) 2011 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 #ifndef _LIBRENDER_COLORBUFFER_H
17 #define _LIBRENDER_COLORBUFFER_H
18
19 #include <EGL/egl.h>
20 #include <EGL/eglext.h>
21 #include <GLES/gl.h>
22
23 #include <memory>
24
25 class TextureDraw;
26 class TextureResize;
27
28 // A class used to model a guest color buffer, and used to implement several
29 // related things:
30 //
31 //  - Every gralloc native buffer with HW read or write requirements will
32 //    allocate a host ColorBuffer instance. When gralloc_lock() is called,
33 //    the guest will use ColorBuffer::readPixels() to read the current content
34 //    of the buffer. When gralloc_unlock() is later called, it will call
35 //    ColorBuffer::subUpdate() to send the updated pixels.
36 //
37 //  - Every guest window EGLSurface is implemented by a host PBuffer
38 //    (see WindowSurface.h) that can have a ColorBuffer instance attached to
39 //    it (through WindowSurface::attachColorBuffer()). When such an attachment
40 //    exists, WindowSurface::flushColorBuffer() will copy the PBuffer's
41 //    pixel data into the ColorBuffer. The latter can then be displayed
42 //    in the client's UI sub-window with ColorBuffer::post().
43 //
44 //  - Guest EGLImages are implemented as native gralloc buffers too.
45 //    The guest glEGLImageTargetTexture2DOES() implementations will end up
46 //    calling ColorBuffer::bindToTexture() to bind the current context's
47 //    GL_TEXTURE_2D to the buffer. Similarly, the guest versions of
48 //    glEGLImageTargetRenderbufferStorageOES() will end up calling
49 //    ColorBuffer::bindToRenderbuffer().
50 //
51 // This forces the implementation to use a host EGLImage to implement each
52 // ColorBuffer.
53 //
54 // As an additional twist.
55
56 class ColorBuffer {
57  public:
58   // Helper interface class used during ColorBuffer operations. This is
59   // introduced to remove coupling from the FrameBuffer class implementation.
60   class Helper {
61    public:
62     Helper() {}
63     virtual ~Helper() {}
64     virtual bool setupContext() = 0;
65     virtual void teardownContext() = 0;
66     virtual TextureDraw* getTextureDraw() const = 0;
67   };
68
69   // Create a new ColorBuffer instance.
70   // |p_display| is the host EGLDisplay handle.
71   // |p_width| and |p_height| are the buffer's dimensions in pixels.
72   // |p_internalFormat| is the internal pixel format to use, valid values
73   // are: GL_RGB, GL_RGB565, GL_RGBA, GL_RGB5_A1_OES and GL_RGBA4_OES.
74   // Implementation is free to use something else though.
75   // |has_eglimage_texture_2d| should be true iff the display supports
76   // the EGL_KHR_gl_texture_2D_image extension.
77   // Returns NULL on failure.
78   static ColorBuffer* create(EGLDisplay p_display, int p_width, int p_height,
79                              GLenum p_internalFormat,
80                              bool has_eglimage_texture_2d, Helper* helper);
81
82   // Destructor.
83   ~ColorBuffer();
84
85   // Return ColorBuffer width and height in pixels
86   GLuint getWidth() const { return m_width; }
87   GLuint getHeight() const { return m_height; }
88
89   // Read the ColorBuffer instance's pixel values into host memory.
90   void readPixels(int x, int y, int width, int height, GLenum p_format,
91                   GLenum p_type, void* pixels);
92
93   // Update the ColorBuffer instance's pixel values from host memory.
94   void subUpdate(int x, int y, int width, int height, GLenum p_format,
95                  GLenum p_type, void* pixels);
96
97   // Bind the current context's EGL_TEXTURE_2D texture to this ColorBuffer's
98   // EGLImage. This is intended to implement glEGLImageTargetTexture2DOES()
99   // for all GLES versions.
100   bool bindToTexture();
101
102   // Bind the current context's EGL_RENDERBUFFER_OES render buffer to this
103   // ColorBuffer's EGLImage. This is intended to implement
104   // glEGLImageTargetRenderbufferStorageOES() for all GLES versions.
105   bool bindToRenderbuffer();
106
107   // Copy the content of the current context's read surface to this
108   // ColorBuffer. This is used from WindowSurface::flushColorBuffer().
109   // Return true on success, false on failure (e.g. no current context).
110   bool blitFromCurrentReadBuffer();
111
112   // Read the content of the whole ColorBuffer as 32-bit RGBA pixels.
113   // |img| must be a buffer large enough (i.e. width * height * 4).
114   void readback(unsigned char* img);
115
116   void bind();
117
118  private:
119   ColorBuffer();  // no default constructor.
120
121   explicit ColorBuffer(EGLDisplay display, Helper* helper);
122
123  private:
124   GLuint m_tex;
125   GLuint m_blitTex;
126   EGLImageKHR m_eglImage;
127   EGLImageKHR m_blitEGLImage;
128   GLuint m_width;
129   GLuint m_height;
130   GLuint m_fbo;
131   GLenum m_internalFormat;
132   EGLDisplay m_display;
133   Helper* m_helper;
134   TextureResize* m_resizer;
135 };
136
137 typedef std::shared_ptr<ColorBuffer> ColorBufferPtr;
138
139 #endif