GraphicBuffer.h revision c43946b931de5dafd28f49963f9af78e05390b26
13330b203039dea366d4981db1408a460134b2d2cMathias Agopian/* 23330b203039dea366d4981db1408a460134b2d2cMathias Agopian * Copyright (C) 2007 The Android Open Source Project 33330b203039dea366d4981db1408a460134b2d2cMathias Agopian * 43330b203039dea366d4981db1408a460134b2d2cMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 53330b203039dea366d4981db1408a460134b2d2cMathias Agopian * you may not use this file except in compliance with the License. 63330b203039dea366d4981db1408a460134b2d2cMathias Agopian * You may obtain a copy of the License at 73330b203039dea366d4981db1408a460134b2d2cMathias Agopian * 83330b203039dea366d4981db1408a460134b2d2cMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 93330b203039dea366d4981db1408a460134b2d2cMathias Agopian * 103330b203039dea366d4981db1408a460134b2d2cMathias Agopian * Unless required by applicable law or agreed to in writing, software 113330b203039dea366d4981db1408a460134b2d2cMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 123330b203039dea366d4981db1408a460134b2d2cMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133330b203039dea366d4981db1408a460134b2d2cMathias Agopian * See the License for the specific language governing permissions and 143330b203039dea366d4981db1408a460134b2d2cMathias Agopian * limitations under the License. 153330b203039dea366d4981db1408a460134b2d2cMathias Agopian */ 163330b203039dea366d4981db1408a460134b2d2cMathias Agopian 173330b203039dea366d4981db1408a460134b2d2cMathias Agopian#ifndef ANDROID_GRAPHIC_BUFFER_H 183330b203039dea366d4981db1408a460134b2d2cMathias Agopian#define ANDROID_GRAPHIC_BUFFER_H 193330b203039dea366d4981db1408a460134b2d2cMathias Agopian 203330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <stdint.h> 213330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <sys/types.h> 223330b203039dea366d4981db1408a460134b2d2cMathias Agopian 235f2165f9455d4893b581b73a67c5431f4344b47eMathias Agopian#include <ui/ANativeObjectBase.h> 243330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/PixelFormat.h> 253330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/Rect.h> 2698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian#include <utils/Flattenable.h> 27e041716ba17674b9af957f68750ad6b2df376c22Mathias Agopian#include <utils/RefBase.h> 285f2165f9455d4893b581b73a67c5431f4344b47eMathias Agopian 293330b203039dea366d4981db1408a460134b2d2cMathias Agopian 30697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchevstruct ANativeWindowBuffer; 313330b203039dea366d4981db1408a460134b2d2cMathias Agopian 323330b203039dea366d4981db1408a460134b2d2cMathias Agopiannamespace android { 333330b203039dea366d4981db1408a460134b2d2cMathias Agopian 343330b203039dea366d4981db1408a460134b2d2cMathias Agopianclass GraphicBufferMapper; 353330b203039dea366d4981db1408a460134b2d2cMathias Agopian 363330b203039dea366d4981db1408a460134b2d2cMathias Agopian// =========================================================================== 373330b203039dea366d4981db1408a460134b2d2cMathias Agopian// GraphicBuffer 383330b203039dea366d4981db1408a460134b2d2cMathias Agopian// =========================================================================== 393330b203039dea366d4981db1408a460134b2d2cMathias Agopian 403330b203039dea366d4981db1408a460134b2d2cMathias Agopianclass GraphicBuffer 41e041716ba17674b9af957f68750ad6b2df376c22Mathias Agopian : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >, 42e041716ba17674b9af957f68750ad6b2df376c22Mathias Agopian public Flattenable 433330b203039dea366d4981db1408a460134b2d2cMathias Agopian{ 443330b203039dea366d4981db1408a460134b2d2cMathias Agopianpublic: 453330b203039dea366d4981db1408a460134b2d2cMathias Agopian 463330b203039dea366d4981db1408a460134b2d2cMathias Agopian enum { 473330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER, 483330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY, 493330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN, 503330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK, 513330b203039dea366d4981db1408a460134b2d2cMathias Agopian 523330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER, 533330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY, 543330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN, 553330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK, 5616f0453fee84c6aad59fe0d1c7d36f061d46cffcGlenn Kasten 573330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK, 5816f0453fee84c6aad59fe0d1c7d36f061d46cffcGlenn Kasten 5916f0453fee84c6aad59fe0d1c7d36f061d46cffcGlenn Kasten USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED, 6016f0453fee84c6aad59fe0d1c7d36f061d46cffcGlenn Kasten 613330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE, 623330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER, 633330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_HW_2D = GRALLOC_USAGE_HW_2D, 643599bf2c0727bc33e8136f5163eee6f398545e05Jamie Gennis USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER, 65b7d87c40ef3f922fb10f6a4f24e6e1437ab3c7aeJamie Gennis USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER, 663330b203039dea366d4981db1408a460134b2d2cMathias Agopian USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK 673330b203039dea366d4981db1408a460134b2d2cMathias Agopian }; 683330b203039dea366d4981db1408a460134b2d2cMathias Agopian 693330b203039dea366d4981db1408a460134b2d2cMathias Agopian GraphicBuffer(); 703330b203039dea366d4981db1408a460134b2d2cMathias Agopian 713330b203039dea366d4981db1408a460134b2d2cMathias Agopian // creates w * h buffer 7254ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage); 7354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian 7454ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian // create a buffer from an existing handle 7554ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage, 7654ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian uint32_t stride, native_handle_t* handle, bool keepOwnership); 773330b203039dea366d4981db1408a460134b2d2cMathias Agopian 78697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev // create a buffer from an existing ANativeWindowBuffer 79697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership); 80309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis 813330b203039dea366d4981db1408a460134b2d2cMathias Agopian // return status 823330b203039dea366d4981db1408a460134b2d2cMathias Agopian status_t initCheck() const; 833330b203039dea366d4981db1408a460134b2d2cMathias Agopian 843330b203039dea366d4981db1408a460134b2d2cMathias Agopian uint32_t getWidth() const { return width; } 853330b203039dea366d4981db1408a460134b2d2cMathias Agopian uint32_t getHeight() const { return height; } 863330b203039dea366d4981db1408a460134b2d2cMathias Agopian uint32_t getStride() const { return stride; } 873330b203039dea366d4981db1408a460134b2d2cMathias Agopian uint32_t getUsage() const { return usage; } 883330b203039dea366d4981db1408a460134b2d2cMathias Agopian PixelFormat getPixelFormat() const { return format; } 893330b203039dea366d4981db1408a460134b2d2cMathias Agopian Rect getBounds() const { return Rect(width, height); } 903330b203039dea366d4981db1408a460134b2d2cMathias Agopian 913330b203039dea366d4981db1408a460134b2d2cMathias Agopian status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage); 923330b203039dea366d4981db1408a460134b2d2cMathias Agopian 933330b203039dea366d4981db1408a460134b2d2cMathias Agopian status_t lock(uint32_t usage, void** vaddr); 943330b203039dea366d4981db1408a460134b2d2cMathias Agopian status_t lock(uint32_t usage, const Rect& rect, void** vaddr); 95c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala // For HAL_PIXEL_FORMAT_YCbCr_420_888 96c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala status_t lockYCbCr(uint32_t usage, android_ycbcr *ycbcr); 97c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala status_t lockYCbCr(uint32_t usage, const Rect& rect, android_ycbcr *ycbcr); 983330b203039dea366d4981db1408a460134b2d2cMathias Agopian status_t unlock(); 99678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian 100697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev ANativeWindowBuffer* getNativeBuffer() const; 1013330b203039dea366d4981db1408a460134b2d2cMathias Agopian 1023330b203039dea366d4981db1408a460134b2d2cMathias Agopian void setIndex(int index); 1033330b203039dea366d4981db1408a460134b2d2cMathias Agopian int getIndex() const; 1043330b203039dea366d4981db1408a460134b2d2cMathias Agopian 105678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian // for debugging 106678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian static void dumpAllocationsToSystemLog(); 107678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian 108b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopianprivate: 1093330b203039dea366d4981db1408a460134b2d2cMathias Agopian virtual ~GraphicBuffer(); 1103330b203039dea366d4981db1408a460134b2d2cMathias Agopian 11154ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian enum { 11254ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian ownNone = 0, 11354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian ownHandle = 1, 11454ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian ownData = 2, 11554ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian }; 11654ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian 117b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian inline const GraphicBufferMapper& getBufferMapper() const { 118b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian return mBufferMapper; 119b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian } 120b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian inline GraphicBufferMapper& getBufferMapper() { 121b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian return mBufferMapper; 122b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian } 12354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian uint8_t mOwner; 1243330b203039dea366d4981db1408a460134b2d2cMathias Agopian 1253330b203039dea366d4981db1408a460134b2d2cMathias Agopianprivate: 1263330b203039dea366d4981db1408a460134b2d2cMathias Agopian friend class Surface; 1273330b203039dea366d4981db1408a460134b2d2cMathias Agopian friend class BpSurface; 1283330b203039dea366d4981db1408a460134b2d2cMathias Agopian friend class BnSurface; 1293330b203039dea366d4981db1408a460134b2d2cMathias Agopian friend class LightRefBase<GraphicBuffer>; 1303330b203039dea366d4981db1408a460134b2d2cMathias Agopian GraphicBuffer(const GraphicBuffer& rhs); 1313330b203039dea366d4981db1408a460134b2d2cMathias Agopian GraphicBuffer& operator = (const GraphicBuffer& rhs); 1323330b203039dea366d4981db1408a460134b2d2cMathias Agopian const GraphicBuffer& operator = (const GraphicBuffer& rhs) const; 1333330b203039dea366d4981db1408a460134b2d2cMathias Agopian 1343330b203039dea366d4981db1408a460134b2d2cMathias Agopian status_t initSize(uint32_t w, uint32_t h, PixelFormat format, 1353330b203039dea366d4981db1408a460134b2d2cMathias Agopian uint32_t usage); 1363330b203039dea366d4981db1408a460134b2d2cMathias Agopian 13798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian void free_handle(); 13898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian 13998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian // Flattenable interface 14098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian size_t getFlattenedSize() const; 14198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian size_t getFdCount() const; 14298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian status_t flatten(void* buffer, size_t size, 14398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian int fds[], size_t count) const; 14498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian status_t unflatten(void const* buffer, size_t size, 14598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian int fds[], size_t count); 14698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian 1473330b203039dea366d4981db1408a460134b2d2cMathias Agopian 1483330b203039dea366d4981db1408a460134b2d2cMathias Agopian GraphicBufferMapper& mBufferMapper; 1493330b203039dea366d4981db1408a460134b2d2cMathias Agopian ssize_t mInitCheck; 1503330b203039dea366d4981db1408a460134b2d2cMathias Agopian int mIndex; 151309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis 152309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis // If we're wrapping another buffer then this reference will make sure it 153309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis // doesn't get freed. 154697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev sp<ANativeWindowBuffer> mWrappedBuffer; 1553330b203039dea366d4981db1408a460134b2d2cMathias Agopian}; 1563330b203039dea366d4981db1408a460134b2d2cMathias Agopian 1573330b203039dea366d4981db1408a460134b2d2cMathias Agopian}; // namespace android 1583330b203039dea366d4981db1408a460134b2d2cMathias Agopian 1593330b203039dea366d4981db1408a460134b2d2cMathias Agopian#endif // ANDROID_GRAPHIC_BUFFER_H 160