PixelBuffer.h revision 52eb4e01a49fe2e94555c000de38bbcbbb13401b
1cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy/*
2cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Copyright (C) 2013 The Android Open Source Project
3cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy *
4cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * you may not use this file except in compliance with the License.
6cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * You may obtain a copy of the License at
7cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy *
8cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy *
10cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Unless required by applicable law or agreed to in writing, software
11cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * See the License for the specific language governing permissions and
14cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * limitations under the License.
15cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy */
16cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
17cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy#ifndef ANDROID_HWUI_PIXEL_BUFFER_H
18cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy#define ANDROID_HWUI_PIXEL_BUFFER_H
19cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
20cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy#include <GLES3/gl3.h>
2152eb4e01a49fe2e94555c000de38bbcbbb13401bMark Salyzyn
2252eb4e01a49fe2e94555c000de38bbcbbb13401bMark Salyzyn#include <log/log.h>
23cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
24cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guynamespace android {
25cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guynamespace uirenderer {
26cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
27cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy/**
28cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Represents a pixel buffer. A pixel buffer will be backed either by a
29cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * PBO on OpenGL ES 3.0 and higher or by an array of uint8_t on other
30cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * versions. If the buffer is backed by a PBO it will of type
31cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * GL_PIXEL_UNPACK_BUFFER.
32cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy *
33cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * To read from or write into a PixelBuffer you must first map the
34cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * buffer using the map(AccessMode) method. This method returns a
35cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * pointer to the beginning of the buffer.
36cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy *
37cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Before the buffer can be used by the GPU, for instance to upload
38cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * a texture, you must first unmap the buffer. To do so, call the
39cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * unmap() method.
40cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy *
41cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Mapping and unmapping a PixelBuffer can have the side effect of
42cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * changing the currently active GL_PIXEL_UNPACK_BUFFER. It is
43cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * therefore recommended to call Caches::unbindPixelbuffer() after
44cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * using a PixelBuffer to upload to a texture.
45cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy */
46cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guyclass PixelBuffer {
47cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guypublic:
48cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    enum BufferType {
49cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        kBufferType_Auto,
50cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        kBufferType_CPU
51cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    };
52cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
53cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    enum AccessMode {
54cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        kAccessMode_None = 0,
55cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        kAccessMode_Read = GL_MAP_READ_BIT,
56cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        kAccessMode_Write = GL_MAP_WRITE_BIT,
57cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        kAccessMode_ReadWrite = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT
58cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    };
59cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
60cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
61cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Creates a new PixelBuffer object with the specified format and
62cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * dimensions. The buffer is immediately allocated.
63cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     *
64cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * The buffer type specifies how the buffer should be allocated.
65cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * By default this method will automatically choose whether to allocate
66cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * a CPU or GPU buffer.
67cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
68cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    static PixelBuffer* create(GLenum format, uint32_t width, uint32_t height,
69cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy            BufferType type = kBufferType_Auto);
70cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
71cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    virtual ~PixelBuffer() {
72cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    }
73cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
74cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
75cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Returns the format of this render buffer.
76cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
77cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    GLenum getFormat() const {
78cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        return mFormat;
79cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    }
80cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
81cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
82cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Maps this before with the specified access mode. This method
83cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * returns a pointer to the region of memory where the buffer was
84cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * mapped.
85cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     *
86cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * If the buffer is already mapped when this method is invoked,
87cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * this method will return the previously mapped pointer. The
88cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * access mode can only be changed by calling unmap() first.
89cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     *
90cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * The specified access mode cannot be kAccessMode_None.
91cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
92cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    virtual uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) = 0;
93cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
94cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
95cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Returns the current access mode for this buffer. If the buffer
96cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * is not mapped, this method returns kAccessMode_None.
97cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
98cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    AccessMode getAccessMode() const {
99cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        return mAccessMode;
100cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    }
101cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
102cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
103cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Returns the currently mapped pointer. Returns NULL if the buffer
104cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * is not mapped.
105cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
106cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    virtual uint8_t* getMappedPointer() const = 0;
107cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
108cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
1091e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     * Upload the specified rectangle of this pixel buffer as a
110cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * GL_TEXTURE_2D texture. Calling this method will trigger
111cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * an unmap() if necessary.
112cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
113cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    virtual void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) = 0;
114cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
115cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
1161e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     * Upload the specified rectangle of this pixel buffer as a
1171e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     * GL_TEXTURE_2D texture. Calling this method will trigger
1181e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     * an unmap() if necessary.
1191e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     *
1201e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     * This is a convenience function provided to save callers the
1211e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     * trouble of computing the offset parameter.
1221e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     */
1231e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease    void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
1241e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease        upload(x, y, width, height, getOffset(x, y));
1251e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease    }
1261e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease
1271e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease    /**
128cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Returns the width of the render buffer in pixels.
129cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
130cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    uint32_t getWidth() const {
131cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        return mWidth;
132cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    }
133cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
134cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
135cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Returns the height of the render buffer in pixels.
136cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
137cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    uint32_t getHeight() const {
138cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        return mHeight;
139cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    }
140cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
141cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
142cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Returns the size of this pixel buffer in bytes.
143cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
144cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    uint32_t getSize() const {
145cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        return mWidth * mHeight * formatSize(mFormat);
146cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    }
147cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
148cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
1491e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     * Returns the offset of a pixel in this pixel buffer, in bytes.
1501e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease     */
1511e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease    uint32_t getOffset(uint32_t x, uint32_t y) const {
1521e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease        return (y * mWidth + x) * formatSize(mFormat);
1531e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease    }
1541e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease
1551e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease    /**
156cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Returns the number of bytes per pixel in the specified format.
157cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     *
158cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Supported formats:
159cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     *      GL_ALPHA
160cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     *      GL_RGBA
161cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
162cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    static uint32_t formatSize(GLenum format) {
163cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        switch (format) {
164cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy            case GL_ALPHA:
165cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy                return 1;
166cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy            case GL_RGBA:
167cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy                return 4;
168cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        }
169cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy        return 0;
170cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    }
171cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
172c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya    /**
173c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya     * Returns the alpha channel offset in the specified format.
174c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya     *
175c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya     * Supported formats:
176c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya     *      GL_ALPHA
177c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya     *      GL_RGBA
178c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya     */
179c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya    static uint32_t formatAlphaOffset(GLenum format) {
180c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya        switch (format) {
181c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya            case GL_ALPHA:
182c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya                return 0;
183c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya            case GL_RGBA:
184c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya                return 3;
185c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya        }
186c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya
187c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya        ALOGE("unsupported format: %d",format);
188c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya        return 0;
189c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya    }
190c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya
191cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guyprotected:
192cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    /**
193cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * Creates a new render buffer in the specified format and dimensions.
194cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     * The format must be GL_ALPHA or GL_RGBA.
195cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy     */
196cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    PixelBuffer(GLenum format, uint32_t width, uint32_t height):
197cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy            mFormat(format), mWidth(width), mHeight(height), mAccessMode(kAccessMode_None) {
198cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    }
199cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
200f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck    /**
201f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck     * Unmaps this buffer, if needed. After the buffer is unmapped,
202f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck     * the pointer previously returned by map() becomes invalid and
203f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck     * should not be used. After calling this method, getMappedPointer()
204f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck     * will always return NULL.
205f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck     */
206f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck    virtual void unmap() = 0;
207f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck
208cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    GLenum mFormat;
209cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
210cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    uint32_t mWidth;
211cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    uint32_t mHeight;
212cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
213cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    AccessMode mAccessMode;
214cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
215cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy}; // class PixelBuffer
216cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
217cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy}; // namespace uirenderer
218cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy}; // namespace android
219cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy
220cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy#endif // ANDROID_HWUI_PIXEL_BUFFER_H
221