2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef _LIBRENDER_COLORBUFFER_H
17 #define _LIBRENDER_COLORBUFFER_H
20 #include <EGL/eglext.h>
28 // A class used to model a guest color buffer, and used to implement several
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.
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().
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().
51 // This forces the implementation to use a host EGLImage to implement each
54 // As an additional twist.
58 // Helper interface class used during ColorBuffer operations. This is
59 // introduced to remove coupling from the FrameBuffer class implementation.
64 virtual bool setupContext() = 0;
65 virtual void teardownContext() = 0;
66 virtual TextureDraw* getTextureDraw() const = 0;
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);
85 // Return ColorBuffer width and height in pixels
86 GLuint getWidth() const { return m_width; }
87 GLuint getHeight() const { return m_height; }
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);
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);
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();
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();
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();
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);
119 ColorBuffer(); // no default constructor.
121 explicit ColorBuffer(EGLDisplay display, Helper* helper);
126 EGLImageKHR m_eglImage;
127 EGLImageKHR m_blitEGLImage;
131 GLenum m_internalFormat;
132 EGLDisplay m_display;
134 TextureResize* m_resizer;
137 typedef std::shared_ptr<ColorBuffer> ColorBufferPtr;