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 /** 1031e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease * Upload the specified rectangle of this pixel buffer as a 104cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * GL_TEXTURE_2D texture. Calling this method will trigger 105cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * an unmap() if necessary. 106cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy */ 107cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy virtual void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) = 0; 108cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 109cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy /** 1101e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease * Upload the specified rectangle of this pixel buffer as a 1111e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease * GL_TEXTURE_2D texture. Calling this method will trigger 1121e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease * an unmap() if necessary. 1131e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease * 1141e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease * This is a convenience function provided to save callers the 1151e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease * trouble of computing the offset parameter. 1161e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease */ 1171e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height) { 1181e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease upload(x, y, width, height, getOffset(x, y)); 1191e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease } 1201e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease 1211e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease /** 122cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Returns the width of the render buffer in pixels. 123cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy */ 124cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy uint32_t getWidth() const { 125cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy return mWidth; 126cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy } 127cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 128cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy /** 129cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Returns the height of the render buffer in pixels. 130cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy */ 131cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy uint32_t getHeight() const { 132cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy return mHeight; 133cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy } 134cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 135cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy /** 136cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Returns the size of this pixel buffer in bytes. 137cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy */ 138cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy uint32_t getSize() const { 139cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy return mWidth * mHeight * formatSize(mFormat); 140cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy } 141cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 142cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy /** 1431e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease * Returns the offset of a pixel in this pixel buffer, in bytes. 1441e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease */ 1451e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease uint32_t getOffset(uint32_t x, uint32_t y) const { 1461e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease return (y * mWidth + x) * formatSize(mFormat); 1471e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease } 1481e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease 1491e546815bbb736c50679a8aefc25f48561026fc5Victoria Lease /** 150cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Returns the number of bytes per pixel in the specified format. 151cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * 152cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Supported formats: 153cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * GL_ALPHA 154cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * GL_RGBA 155cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy */ 156cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy static uint32_t formatSize(GLenum format) { 157cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy switch (format) { 158cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy case GL_ALPHA: 159cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy return 1; 160cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy case GL_RGBA: 161cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy return 4; 162cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy } 163cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy return 0; 164cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy } 165cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 166c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya /** 167c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya * Returns the alpha channel offset in the specified format. 168c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya * 169c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya * Supported formats: 170c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya * GL_ALPHA 171c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya * GL_RGBA 172c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya */ 173c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya static uint32_t formatAlphaOffset(GLenum format) { 174c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya switch (format) { 175c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya case GL_ALPHA: 176c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya return 0; 177c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya case GL_RGBA: 178c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya return 3; 179c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya } 180c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya 181c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya ALOGE("unsupported format: %d",format); 182c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya return 0; 183c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya } 184c62c1cc17ded117433d23ba5dd6c364b05fbcdbdDigish Pandya 185cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guyprotected: 186cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy /** 187cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * Creates a new render buffer in the specified format and dimensions. 188cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy * The format must be GL_ALPHA or GL_RGBA. 189cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy */ 190cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy PixelBuffer(GLenum format, uint32_t width, uint32_t height): 191cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy mFormat(format), mWidth(width), mHeight(height), mAccessMode(kAccessMode_None) { 192cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy } 193cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 194f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck /** 195f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck * Unmaps this buffer, if needed. After the buffer is unmapped, 196f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck * the pointer previously returned by map() becomes invalid and 197a0c0ff2f9a45fd1be86a7ad428296381e7138ad9Teng-Hui Zhu * should not be used. 198f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck */ 199f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck virtual void unmap() = 0; 200f3ad324a8d3f5b5530bd1945f461faf4b0adec8cJohn Reck 201cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy GLenum mFormat; 202cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 203cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy uint32_t mWidth; 204cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy uint32_t mHeight; 205cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 206cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy AccessMode mAccessMode; 207cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 208cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy}; // class PixelBuffer 209cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 210cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy}; // namespace uirenderer 211cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy}; // namespace android 212cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy 213cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy#endif // ANDROID_HWUI_PIXEL_BUFFER_H 214