GraphicBuffer.h revision 678bdd6349344df254cc0c3377a40fd99e216635
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
233330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/android_native_buffer.h>
243330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/PixelFormat.h>
253330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/Rect.h>
2698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian#include <utils/Flattenable.h>
273330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <pixelflinger/pixelflinger.h>
283330b203039dea366d4981db1408a460134b2d2cMathias Agopian
293330b203039dea366d4981db1408a460134b2d2cMathias Agopianstruct android_native_buffer_t;
303330b203039dea366d4981db1408a460134b2d2cMathias Agopian
313330b203039dea366d4981db1408a460134b2d2cMathias Agopiannamespace android {
323330b203039dea366d4981db1408a460134b2d2cMathias Agopian
333330b203039dea366d4981db1408a460134b2d2cMathias Agopianclass GraphicBufferMapper;
343330b203039dea366d4981db1408a460134b2d2cMathias Agopian
353330b203039dea366d4981db1408a460134b2d2cMathias Agopian// ===========================================================================
363330b203039dea366d4981db1408a460134b2d2cMathias Agopian// GraphicBuffer
373330b203039dea366d4981db1408a460134b2d2cMathias Agopian// ===========================================================================
383330b203039dea366d4981db1408a460134b2d2cMathias Agopian
393330b203039dea366d4981db1408a460134b2d2cMathias Agopianclass GraphicBuffer
403330b203039dea366d4981db1408a460134b2d2cMathias Agopian    : public EGLNativeBase<
413330b203039dea366d4981db1408a460134b2d2cMathias Agopian        android_native_buffer_t,
423330b203039dea366d4981db1408a460134b2d2cMathias Agopian        GraphicBuffer,
4398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        LightRefBase<GraphicBuffer> >, public Flattenable
443330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
453330b203039dea366d4981db1408a460134b2d2cMathias Agopianpublic:
463330b203039dea366d4981db1408a460134b2d2cMathias Agopian
473330b203039dea366d4981db1408a460134b2d2cMathias Agopian    enum {
483330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SW_READ_NEVER     = GRALLOC_USAGE_SW_READ_NEVER,
493330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SW_READ_RARELY    = GRALLOC_USAGE_SW_READ_RARELY,
503330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SW_READ_OFTEN     = GRALLOC_USAGE_SW_READ_OFTEN,
513330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SW_READ_MASK      = GRALLOC_USAGE_SW_READ_MASK,
523330b203039dea366d4981db1408a460134b2d2cMathias Agopian
533330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SW_WRITE_NEVER    = GRALLOC_USAGE_SW_WRITE_NEVER,
543330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SW_WRITE_RARELY   = GRALLOC_USAGE_SW_WRITE_RARELY,
553330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SW_WRITE_OFTEN    = GRALLOC_USAGE_SW_WRITE_OFTEN,
563330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SW_WRITE_MASK     = GRALLOC_USAGE_SW_WRITE_MASK,
573330b203039dea366d4981db1408a460134b2d2cMathias Agopian
583330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
593330b203039dea366d4981db1408a460134b2d2cMathias Agopian
603330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_HW_TEXTURE        = GRALLOC_USAGE_HW_TEXTURE,
613330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_HW_RENDER         = GRALLOC_USAGE_HW_RENDER,
623330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_HW_2D             = GRALLOC_USAGE_HW_2D,
633330b203039dea366d4981db1408a460134b2d2cMathias Agopian        USAGE_HW_MASK           = GRALLOC_USAGE_HW_MASK
643330b203039dea366d4981db1408a460134b2d2cMathias Agopian    };
653330b203039dea366d4981db1408a460134b2d2cMathias Agopian
663330b203039dea366d4981db1408a460134b2d2cMathias Agopian    GraphicBuffer();
673330b203039dea366d4981db1408a460134b2d2cMathias Agopian
683330b203039dea366d4981db1408a460134b2d2cMathias Agopian    // creates w * h buffer
6954ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
7054ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian
7154ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    // create a buffer from an existing handle
7254ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
7354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian            uint32_t stride, native_handle_t* handle, bool keepOwnership);
743330b203039dea366d4981db1408a460134b2d2cMathias Agopian
753330b203039dea366d4981db1408a460134b2d2cMathias Agopian    // return status
763330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t initCheck() const;
773330b203039dea366d4981db1408a460134b2d2cMathias Agopian
783330b203039dea366d4981db1408a460134b2d2cMathias Agopian    uint32_t getWidth() const           { return width; }
793330b203039dea366d4981db1408a460134b2d2cMathias Agopian    uint32_t getHeight() const          { return height; }
803330b203039dea366d4981db1408a460134b2d2cMathias Agopian    uint32_t getStride() const          { return stride; }
813330b203039dea366d4981db1408a460134b2d2cMathias Agopian    uint32_t getUsage() const           { return usage; }
823330b203039dea366d4981db1408a460134b2d2cMathias Agopian    PixelFormat getPixelFormat() const  { return format; }
833330b203039dea366d4981db1408a460134b2d2cMathias Agopian    Rect getBounds() const              { return Rect(width, height); }
843330b203039dea366d4981db1408a460134b2d2cMathias Agopian
853330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage);
863330b203039dea366d4981db1408a460134b2d2cMathias Agopian
873330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t lock(uint32_t usage, void** vaddr);
883330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
893330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t lock(GGLSurface* surface, uint32_t usage);
903330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t unlock();
91678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian
923330b203039dea366d4981db1408a460134b2d2cMathias Agopian    android_native_buffer_t* getNativeBuffer() const;
933330b203039dea366d4981db1408a460134b2d2cMathias Agopian
943330b203039dea366d4981db1408a460134b2d2cMathias Agopian    void setIndex(int index);
953330b203039dea366d4981db1408a460134b2d2cMathias Agopian    int getIndex() const;
963330b203039dea366d4981db1408a460134b2d2cMathias Agopian
97678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    // for debugging
98678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    static void dumpAllocationsToSystemLog();
99678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian
100b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopianprivate:
1013330b203039dea366d4981db1408a460134b2d2cMathias Agopian    virtual ~GraphicBuffer();
1023330b203039dea366d4981db1408a460134b2d2cMathias Agopian
10354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    enum {
10454ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        ownNone   = 0,
10554ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        ownHandle = 1,
10654ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        ownData   = 2,
10754ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    };
10854ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian
109b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian    inline const GraphicBufferMapper& getBufferMapper() const {
110b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian        return mBufferMapper;
111b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian    }
112b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian    inline GraphicBufferMapper& getBufferMapper() {
113b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian        return mBufferMapper;
114b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian    }
11554ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    uint8_t mOwner;
1163330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1173330b203039dea366d4981db1408a460134b2d2cMathias Agopianprivate:
1183330b203039dea366d4981db1408a460134b2d2cMathias Agopian    friend class Surface;
1193330b203039dea366d4981db1408a460134b2d2cMathias Agopian    friend class BpSurface;
1203330b203039dea366d4981db1408a460134b2d2cMathias Agopian    friend class BnSurface;
1213330b203039dea366d4981db1408a460134b2d2cMathias Agopian    friend class LightRefBase<GraphicBuffer>;
1223330b203039dea366d4981db1408a460134b2d2cMathias Agopian    GraphicBuffer(const GraphicBuffer& rhs);
1233330b203039dea366d4981db1408a460134b2d2cMathias Agopian    GraphicBuffer& operator = (const GraphicBuffer& rhs);
1243330b203039dea366d4981db1408a460134b2d2cMathias Agopian    const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
1253330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1263330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
1273330b203039dea366d4981db1408a460134b2d2cMathias Agopian            uint32_t usage);
1283330b203039dea366d4981db1408a460134b2d2cMathias Agopian
12998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    void free_handle();
13098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
13198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    // Flattenable interface
13298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    size_t getFlattenedSize() const;
13398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    size_t getFdCount() const;
13498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    status_t flatten(void* buffer, size_t size,
13598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian            int fds[], size_t count) const;
13698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    status_t unflatten(void const* buffer, size_t size,
13798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian            int fds[], size_t count);
13898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
1393330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1403330b203039dea366d4981db1408a460134b2d2cMathias Agopian    GraphicBufferMapper& mBufferMapper;
1413330b203039dea366d4981db1408a460134b2d2cMathias Agopian    ssize_t mInitCheck;
1423330b203039dea366d4981db1408a460134b2d2cMathias Agopian    int mIndex;
1433330b203039dea366d4981db1408a460134b2d2cMathias Agopian};
1443330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1453330b203039dea366d4981db1408a460134b2d2cMathias Agopian}; // namespace android
1463330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1473330b203039dea366d4981db1408a460134b2d2cMathias Agopian#endif // ANDROID_GRAPHIC_BUFFER_H
148