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