• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 #ifndef ANDROID_HWUI_PIXEL_BUFFER_H
18 #define ANDROID_HWUI_PIXEL_BUFFER_H
19 
20 #include <GLES3/gl3.h>
21 #include <cutils/log.h>
22 
23 namespace android {
24 namespace uirenderer {
25 
26 /**
27  * Represents a pixel buffer. A pixel buffer will be backed either by a
28  * PBO on OpenGL ES 3.0 and higher or by an array of uint8_t on other
29  * versions. If the buffer is backed by a PBO it will of type
30  * GL_PIXEL_UNPACK_BUFFER.
31  *
32  * To read from or write into a PixelBuffer you must first map the
33  * buffer using the map(AccessMode) method. This method returns a
34  * pointer to the beginning of the buffer.
35  *
36  * Before the buffer can be used by the GPU, for instance to upload
37  * a texture, you must first unmap the buffer. To do so, call the
38  * unmap() method.
39  *
40  * Mapping and unmapping a PixelBuffer can have the side effect of
41  * changing the currently active GL_PIXEL_UNPACK_BUFFER. It is
42  * therefore recommended to call Caches::unbindPixelbuffer() after
43  * using a PixelBuffer to upload to a texture.
44  */
45 class PixelBuffer {
46 public:
47     enum BufferType {
48         kBufferType_Auto,
49         kBufferType_CPU
50     };
51 
52     enum AccessMode {
53         kAccessMode_None = 0,
54         kAccessMode_Read = GL_MAP_READ_BIT,
55         kAccessMode_Write = GL_MAP_WRITE_BIT,
56         kAccessMode_ReadWrite = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT
57     };
58 
59     /**
60      * Creates a new PixelBuffer object with the specified format and
61      * dimensions. The buffer is immediately allocated.
62      *
63      * The buffer type specifies how the buffer should be allocated.
64      * By default this method will automatically choose whether to allocate
65      * a CPU or GPU buffer.
66      */
67     static PixelBuffer* create(GLenum format, uint32_t width, uint32_t height,
68             BufferType type = kBufferType_Auto);
69 
~PixelBuffer()70     virtual ~PixelBuffer() {
71     }
72 
73     /**
74      * Returns the format of this render buffer.
75      */
getFormat()76     GLenum getFormat() const {
77         return mFormat;
78     }
79 
80     /**
81      * Maps this before with the specified access mode. This method
82      * returns a pointer to the region of memory where the buffer was
83      * mapped.
84      *
85      * If the buffer is already mapped when this method is invoked,
86      * this method will return the previously mapped pointer. The
87      * access mode can only be changed by calling unmap() first.
88      *
89      * The specified access mode cannot be kAccessMode_None.
90      */
91     virtual uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) = 0;
92 
93     /**
94      * Returns the current access mode for this buffer. If the buffer
95      * is not mapped, this method returns kAccessMode_None.
96      */
getAccessMode()97     AccessMode getAccessMode() const {
98         return mAccessMode;
99     }
100 
101     /**
102      * Returns the currently mapped pointer. Returns NULL if the buffer
103      * is not mapped.
104      */
105     virtual uint8_t* getMappedPointer() const = 0;
106 
107     /**
108      * Upload the specified rectangle of this pixel buffer as a
109      * GL_TEXTURE_2D texture. Calling this method will trigger
110      * an unmap() if necessary.
111      */
112     virtual void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) = 0;
113 
114     /**
115      * Upload the specified rectangle of this pixel buffer as a
116      * GL_TEXTURE_2D texture. Calling this method will trigger
117      * an unmap() if necessary.
118      *
119      * This is a convenience function provided to save callers the
120      * trouble of computing the offset parameter.
121      */
upload(uint32_t x,uint32_t y,uint32_t width,uint32_t height)122     void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
123         upload(x, y, width, height, getOffset(x, y));
124     }
125 
126     /**
127      * Returns the width of the render buffer in pixels.
128      */
getWidth()129     uint32_t getWidth() const {
130         return mWidth;
131     }
132 
133     /**
134      * Returns the height of the render buffer in pixels.
135      */
getHeight()136     uint32_t getHeight() const {
137         return mHeight;
138     }
139 
140     /**
141      * Returns the size of this pixel buffer in bytes.
142      */
getSize()143     uint32_t getSize() const {
144         return mWidth * mHeight * formatSize(mFormat);
145     }
146 
147     /**
148      * Returns the offset of a pixel in this pixel buffer, in bytes.
149      */
getOffset(uint32_t x,uint32_t y)150     uint32_t getOffset(uint32_t x, uint32_t y) const {
151         return (y * mWidth + x) * formatSize(mFormat);
152     }
153 
154     /**
155      * Returns the number of bytes per pixel in the specified format.
156      *
157      * Supported formats:
158      *      GL_ALPHA
159      *      GL_RGBA
160      */
formatSize(GLenum format)161     static uint32_t formatSize(GLenum format) {
162         switch (format) {
163             case GL_ALPHA:
164                 return 1;
165             case GL_RGBA:
166                 return 4;
167         }
168         return 0;
169     }
170 
171     /**
172      * Returns the alpha channel offset in the specified format.
173      *
174      * Supported formats:
175      *      GL_ALPHA
176      *      GL_RGBA
177      */
formatAlphaOffset(GLenum format)178     static uint32_t formatAlphaOffset(GLenum format) {
179         switch (format) {
180             case GL_ALPHA:
181                 return 0;
182             case GL_RGBA:
183                 return 3;
184         }
185 
186         ALOGE("unsupported format: %d",format);
187         return 0;
188     }
189 
190 protected:
191     /**
192      * Creates a new render buffer in the specified format and dimensions.
193      * The format must be GL_ALPHA or GL_RGBA.
194      */
PixelBuffer(GLenum format,uint32_t width,uint32_t height)195     PixelBuffer(GLenum format, uint32_t width, uint32_t height):
196             mFormat(format), mWidth(width), mHeight(height), mAccessMode(kAccessMode_None) {
197     }
198 
199     /**
200      * Unmaps this buffer, if needed. After the buffer is unmapped,
201      * the pointer previously returned by map() becomes invalid and
202      * should not be used. After calling this method, getMappedPointer()
203      * will always return NULL.
204      */
205     virtual void unmap() = 0;
206 
207     GLenum mFormat;
208 
209     uint32_t mWidth;
210     uint32_t mHeight;
211 
212     AccessMode mAccessMode;
213 
214 }; // class PixelBuffer
215 
216 }; // namespace uirenderer
217 }; // namespace android
218 
219 #endif // ANDROID_HWUI_PIXEL_BUFFER_H
220