blob: 9536bc807fbcf3ba94a3766cb1dacc26ca314d7d [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>
Chris Craik96a5c4c2015-01-27 15:46:35 -080021#include <cutils/log.h>
Romain Guycf51a412013-04-08 19:40:31 -070022
23namespace android {
24namespace 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 */
45class PixelBuffer {
46public:
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
70 virtual ~PixelBuffer() {
71 }
72
73 /**
74 * Returns the format of this render buffer.
75 */
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 /**
Romain Guycf51a412013-04-08 19:40:31 -070094 * Returns the current access mode for this buffer. If the buffer
95 * is not mapped, this method returns kAccessMode_None.
96 */
97 AccessMode getAccessMode() const {
98 return mAccessMode;
99 }
100
101 /**
Victoria Lease1e546812013-06-25 14:25:17 -0700102 * Upload the specified rectangle of this pixel buffer as a
Romain Guycf51a412013-04-08 19:40:31 -0700103 * GL_TEXTURE_2D texture. Calling this method will trigger
104 * an unmap() if necessary.
105 */
106 virtual void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) = 0;
107
108 /**
Victoria Lease1e546812013-06-25 14:25:17 -0700109 * Upload the specified rectangle of this pixel buffer as a
110 * GL_TEXTURE_2D texture. Calling this method will trigger
111 * an unmap() if necessary.
112 *
113 * This is a convenience function provided to save callers the
114 * trouble of computing the offset parameter.
115 */
116 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
117 upload(x, y, width, height, getOffset(x, y));
118 }
119
120 /**
Romain Guycf51a412013-04-08 19:40:31 -0700121 * Returns the width of the render buffer in pixels.
122 */
123 uint32_t getWidth() const {
124 return mWidth;
125 }
126
127 /**
128 * Returns the height of the render buffer in pixels.
129 */
130 uint32_t getHeight() const {
131 return mHeight;
132 }
133
134 /**
135 * Returns the size of this pixel buffer in bytes.
136 */
137 uint32_t getSize() const {
138 return mWidth * mHeight * formatSize(mFormat);
139 }
140
141 /**
Victoria Lease1e546812013-06-25 14:25:17 -0700142 * Returns the offset of a pixel in this pixel buffer, in bytes.
143 */
144 uint32_t getOffset(uint32_t x, uint32_t y) const {
145 return (y * mWidth + x) * formatSize(mFormat);
146 }
147
148 /**
Romain Guycf51a412013-04-08 19:40:31 -0700149 * Returns the number of bytes per pixel in the specified format.
150 *
151 * Supported formats:
152 * GL_ALPHA
153 * GL_RGBA
154 */
155 static uint32_t formatSize(GLenum format) {
156 switch (format) {
157 case GL_ALPHA:
158 return 1;
159 case GL_RGBA:
160 return 4;
161 }
162 return 0;
163 }
164
Digish Pandyac62c1cc2014-05-12 14:37:04 +0530165 /**
166 * Returns the alpha channel offset in the specified format.
167 *
168 * Supported formats:
169 * GL_ALPHA
170 * GL_RGBA
171 */
172 static uint32_t formatAlphaOffset(GLenum format) {
173 switch (format) {
174 case GL_ALPHA:
175 return 0;
176 case GL_RGBA:
177 return 3;
178 }
179
180 ALOGE("unsupported format: %d",format);
181 return 0;
182 }
183
Romain Guycf51a412013-04-08 19:40:31 -0700184protected:
185 /**
186 * Creates a new render buffer in the specified format and dimensions.
187 * The format must be GL_ALPHA or GL_RGBA.
188 */
189 PixelBuffer(GLenum format, uint32_t width, uint32_t height):
190 mFormat(format), mWidth(width), mHeight(height), mAccessMode(kAccessMode_None) {
191 }
192
John Reckf3ad3242016-02-24 15:36:35 -0800193 /**
194 * Unmaps this buffer, if needed. After the buffer is unmapped,
195 * the pointer previously returned by map() becomes invalid and
Teng-Hui Zhua0c0ff22016-07-22 16:11:42 -0700196 * should not be used.
John Reckf3ad3242016-02-24 15:36:35 -0800197 */
198 virtual void unmap() = 0;
199
Romain Guycf51a412013-04-08 19:40:31 -0700200 GLenum mFormat;
201
202 uint32_t mWidth;
203 uint32_t mHeight;
204
205 AccessMode mAccessMode;
206
207}; // class PixelBuffer
208
209}; // namespace uirenderer
210}; // namespace android
211
212#endif // ANDROID_HWUI_PIXEL_BUFFER_H