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