16950e428feaccc8164b989ef64e771a99948797aMathias Agopian/*
26950e428feaccc8164b989ef64e771a99948797aMathias Agopian * Copyright (C) 2007 The Android Open Source Project
36950e428feaccc8164b989ef64e771a99948797aMathias Agopian *
46950e428feaccc8164b989ef64e771a99948797aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
56950e428feaccc8164b989ef64e771a99948797aMathias Agopian * you may not use this file except in compliance with the License.
66950e428feaccc8164b989ef64e771a99948797aMathias Agopian * You may obtain a copy of the License at
76950e428feaccc8164b989ef64e771a99948797aMathias Agopian *
86950e428feaccc8164b989ef64e771a99948797aMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
96950e428feaccc8164b989ef64e771a99948797aMathias Agopian *
106950e428feaccc8164b989ef64e771a99948797aMathias Agopian * Unless required by applicable law or agreed to in writing, software
116950e428feaccc8164b989ef64e771a99948797aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
126950e428feaccc8164b989ef64e771a99948797aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136950e428feaccc8164b989ef64e771a99948797aMathias Agopian * See the License for the specific language governing permissions and
146950e428feaccc8164b989ef64e771a99948797aMathias Agopian * limitations under the License.
156950e428feaccc8164b989ef64e771a99948797aMathias Agopian */
166950e428feaccc8164b989ef64e771a99948797aMathias Agopian
176950e428feaccc8164b989ef64e771a99948797aMathias Agopian#ifndef ANDROID_GRAPHIC_BUFFER_H
186950e428feaccc8164b989ef64e771a99948797aMathias Agopian#define ANDROID_GRAPHIC_BUFFER_H
196950e428feaccc8164b989ef64e771a99948797aMathias Agopian
206950e428feaccc8164b989ef64e771a99948797aMathias Agopian#include <stdint.h>
216950e428feaccc8164b989ef64e771a99948797aMathias Agopian#include <sys/types.h>
226950e428feaccc8164b989ef64e771a99948797aMathias Agopian
236950e428feaccc8164b989ef64e771a99948797aMathias Agopian#include <ui/android_native_buffer.h>
246950e428feaccc8164b989ef64e771a99948797aMathias Agopian#include <ui/PixelFormat.h>
256950e428feaccc8164b989ef64e771a99948797aMathias Agopian#include <ui/Rect.h>
26c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian#include <utils/Flattenable.h>
276950e428feaccc8164b989ef64e771a99948797aMathias Agopian#include <pixelflinger/pixelflinger.h>
286950e428feaccc8164b989ef64e771a99948797aMathias Agopian
2948e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian#include <hardware/hardware.h>
3048e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian
316950e428feaccc8164b989ef64e771a99948797aMathias Agopianstruct android_native_buffer_t;
326950e428feaccc8164b989ef64e771a99948797aMathias Agopian
336950e428feaccc8164b989ef64e771a99948797aMathias Agopiannamespace android {
346950e428feaccc8164b989ef64e771a99948797aMathias Agopian
356950e428feaccc8164b989ef64e771a99948797aMathias Agopianclass GraphicBufferMapper;
366950e428feaccc8164b989ef64e771a99948797aMathias Agopian
376950e428feaccc8164b989ef64e771a99948797aMathias Agopian// ===========================================================================
386950e428feaccc8164b989ef64e771a99948797aMathias Agopian// GraphicBuffer
396950e428feaccc8164b989ef64e771a99948797aMathias Agopian// ===========================================================================
406950e428feaccc8164b989ef64e771a99948797aMathias Agopian
416950e428feaccc8164b989ef64e771a99948797aMathias Agopianclass GraphicBuffer
426950e428feaccc8164b989ef64e771a99948797aMathias Agopian    : public EGLNativeBase<
436950e428feaccc8164b989ef64e771a99948797aMathias Agopian        android_native_buffer_t,
446950e428feaccc8164b989ef64e771a99948797aMathias Agopian        GraphicBuffer,
45c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian        LightRefBase<GraphicBuffer> >, public Flattenable
466950e428feaccc8164b989ef64e771a99948797aMathias Agopian{
476950e428feaccc8164b989ef64e771a99948797aMathias Agopianpublic:
486950e428feaccc8164b989ef64e771a99948797aMathias Agopian
496950e428feaccc8164b989ef64e771a99948797aMathias Agopian    enum {
506950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SW_READ_NEVER     = GRALLOC_USAGE_SW_READ_NEVER,
516950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SW_READ_RARELY    = GRALLOC_USAGE_SW_READ_RARELY,
526950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SW_READ_OFTEN     = GRALLOC_USAGE_SW_READ_OFTEN,
536950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SW_READ_MASK      = GRALLOC_USAGE_SW_READ_MASK,
546950e428feaccc8164b989ef64e771a99948797aMathias Agopian
556950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SW_WRITE_NEVER    = GRALLOC_USAGE_SW_WRITE_NEVER,
566950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SW_WRITE_RARELY   = GRALLOC_USAGE_SW_WRITE_RARELY,
576950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SW_WRITE_OFTEN    = GRALLOC_USAGE_SW_WRITE_OFTEN,
586950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SW_WRITE_MASK     = GRALLOC_USAGE_SW_WRITE_MASK,
596950e428feaccc8164b989ef64e771a99948797aMathias Agopian
606950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
616950e428feaccc8164b989ef64e771a99948797aMathias Agopian
626950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_HW_TEXTURE        = GRALLOC_USAGE_HW_TEXTURE,
636950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_HW_RENDER         = GRALLOC_USAGE_HW_RENDER,
646950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_HW_2D             = GRALLOC_USAGE_HW_2D,
656950e428feaccc8164b989ef64e771a99948797aMathias Agopian        USAGE_HW_MASK           = GRALLOC_USAGE_HW_MASK
666950e428feaccc8164b989ef64e771a99948797aMathias Agopian    };
676950e428feaccc8164b989ef64e771a99948797aMathias Agopian
6848e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian    enum {
6948e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian        TRANSFORM_IDENTITY      = 0,
7048e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian        TRANSFORM_ROT_90        = HAL_TRANSFORM_ROT_90,
7148e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian        TRANSFORM_ROT_180       = HAL_TRANSFORM_ROT_180,
7248e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian        TRANSFORM_ROT_270       = HAL_TRANSFORM_ROT_270
7348e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian    };
7448e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian
756950e428feaccc8164b989ef64e771a99948797aMathias Agopian    GraphicBuffer();
766950e428feaccc8164b989ef64e771a99948797aMathias Agopian
776950e428feaccc8164b989ef64e771a99948797aMathias Agopian    // creates w * h buffer
789042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian    GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
799042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian
809042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian    // create a buffer from an existing handle
819042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian    GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
829042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian            uint32_t stride, native_handle_t* handle, bool keepOwnership);
836950e428feaccc8164b989ef64e771a99948797aMathias Agopian
846950e428feaccc8164b989ef64e771a99948797aMathias Agopian    // return status
856950e428feaccc8164b989ef64e771a99948797aMathias Agopian    status_t initCheck() const;
866950e428feaccc8164b989ef64e771a99948797aMathias Agopian
876950e428feaccc8164b989ef64e771a99948797aMathias Agopian    uint32_t getWidth() const           { return width; }
886950e428feaccc8164b989ef64e771a99948797aMathias Agopian    uint32_t getHeight() const          { return height; }
896950e428feaccc8164b989ef64e771a99948797aMathias Agopian    uint32_t getStride() const          { return stride; }
906950e428feaccc8164b989ef64e771a99948797aMathias Agopian    uint32_t getUsage() const           { return usage; }
9148e723a0ee9b625825d23642f843b91fa276ab7cMathias Agopian    uint32_t getTransform() const       { return transform; }
926950e428feaccc8164b989ef64e771a99948797aMathias Agopian    PixelFormat getPixelFormat() const  { return format; }
936950e428feaccc8164b989ef64e771a99948797aMathias Agopian    Rect getBounds() const              { return Rect(width, height); }
946950e428feaccc8164b989ef64e771a99948797aMathias Agopian
956950e428feaccc8164b989ef64e771a99948797aMathias Agopian    status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage);
966950e428feaccc8164b989ef64e771a99948797aMathias Agopian
976950e428feaccc8164b989ef64e771a99948797aMathias Agopian    status_t lock(uint32_t usage, void** vaddr);
986950e428feaccc8164b989ef64e771a99948797aMathias Agopian    status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
996950e428feaccc8164b989ef64e771a99948797aMathias Agopian    status_t lock(GGLSurface* surface, uint32_t usage);
1006950e428feaccc8164b989ef64e771a99948797aMathias Agopian    status_t unlock();
101e869aee382cfe01d42dffba4912458a16bb791fbMathias Agopian
1026950e428feaccc8164b989ef64e771a99948797aMathias Agopian    android_native_buffer_t* getNativeBuffer() const;
1036950e428feaccc8164b989ef64e771a99948797aMathias Agopian
1046950e428feaccc8164b989ef64e771a99948797aMathias Agopian    void setIndex(int index);
1056950e428feaccc8164b989ef64e771a99948797aMathias Agopian    int getIndex() const;
1066950e428feaccc8164b989ef64e771a99948797aMathias Agopian
107e869aee382cfe01d42dffba4912458a16bb791fbMathias Agopian    // for debugging
108e869aee382cfe01d42dffba4912458a16bb791fbMathias Agopian    static void dumpAllocationsToSystemLog();
109e869aee382cfe01d42dffba4912458a16bb791fbMathias Agopian
1107623da435e45c7c03ef6a00a43675deb6645f070Mathias Agopianprivate:
1116950e428feaccc8164b989ef64e771a99948797aMathias Agopian    virtual ~GraphicBuffer();
1126950e428feaccc8164b989ef64e771a99948797aMathias Agopian
1139042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian    enum {
1149042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian        ownNone   = 0,
1159042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian        ownHandle = 1,
1169042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian        ownData   = 2,
1179042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian    };
1189042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian
1197623da435e45c7c03ef6a00a43675deb6645f070Mathias Agopian    inline const GraphicBufferMapper& getBufferMapper() const {
1207623da435e45c7c03ef6a00a43675deb6645f070Mathias Agopian        return mBufferMapper;
1217623da435e45c7c03ef6a00a43675deb6645f070Mathias Agopian    }
1227623da435e45c7c03ef6a00a43675deb6645f070Mathias Agopian    inline GraphicBufferMapper& getBufferMapper() {
1237623da435e45c7c03ef6a00a43675deb6645f070Mathias Agopian        return mBufferMapper;
1247623da435e45c7c03ef6a00a43675deb6645f070Mathias Agopian    }
1259042b4564de5477b18e680c7dce13b587a681dd9Mathias Agopian    uint8_t mOwner;
1266950e428feaccc8164b989ef64e771a99948797aMathias Agopian
1276950e428feaccc8164b989ef64e771a99948797aMathias Agopianprivate:
1286950e428feaccc8164b989ef64e771a99948797aMathias Agopian    friend class Surface;
1296950e428feaccc8164b989ef64e771a99948797aMathias Agopian    friend class BpSurface;
1306950e428feaccc8164b989ef64e771a99948797aMathias Agopian    friend class BnSurface;
1316950e428feaccc8164b989ef64e771a99948797aMathias Agopian    friend class LightRefBase<GraphicBuffer>;
1326950e428feaccc8164b989ef64e771a99948797aMathias Agopian    GraphicBuffer(const GraphicBuffer& rhs);
1336950e428feaccc8164b989ef64e771a99948797aMathias Agopian    GraphicBuffer& operator = (const GraphicBuffer& rhs);
1346950e428feaccc8164b989ef64e771a99948797aMathias Agopian    const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
1356950e428feaccc8164b989ef64e771a99948797aMathias Agopian
1366950e428feaccc8164b989ef64e771a99948797aMathias Agopian    status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
1376950e428feaccc8164b989ef64e771a99948797aMathias Agopian            uint32_t usage);
1386950e428feaccc8164b989ef64e771a99948797aMathias Agopian
139c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian    void free_handle();
140c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian
141c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian    // Flattenable interface
142c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian    size_t getFlattenedSize() const;
143c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian    size_t getFdCount() const;
144c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian    status_t flatten(void* buffer, size_t size,
145c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian            int fds[], size_t count) const;
146c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian    status_t unflatten(void const* buffer, size_t size,
147c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian            int fds[], size_t count);
148c86727f5805f28fbd25da141c50fb6843f364f3aMathias Agopian
1496950e428feaccc8164b989ef64e771a99948797aMathias Agopian
1506950e428feaccc8164b989ef64e771a99948797aMathias Agopian    GraphicBufferMapper& mBufferMapper;
1516950e428feaccc8164b989ef64e771a99948797aMathias Agopian    ssize_t mInitCheck;
1526950e428feaccc8164b989ef64e771a99948797aMathias Agopian    int mIndex;
1536950e428feaccc8164b989ef64e771a99948797aMathias Agopian};
1546950e428feaccc8164b989ef64e771a99948797aMathias Agopian
1556950e428feaccc8164b989ef64e771a99948797aMathias Agopian}; // namespace android
1566950e428feaccc8164b989ef64e771a99948797aMathias Agopian
1576950e428feaccc8164b989ef64e771a99948797aMathias Agopian#endif // ANDROID_GRAPHIC_BUFFER_H
158