115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian/*
215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian**
315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** Copyright 2009, The Android Open Source Project
415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian**
515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** Licensed under the Apache License, Version 2.0 (the "License");
615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** you may not use this file except in compliance with the License.
715b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** You may obtain a copy of the License at
815b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian**
915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian**     http://www.apache.org/licenses/LICENSE-2.0
1015b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian**
1115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** Unless required by applicable law or agreed to in writing, software
1215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** distributed under the License is distributed on an "AS IS" BASIS,
1315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** See the License for the specific language governing permissions and
1515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian** limitations under the License.
1615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian*/
1715b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
1815b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#ifndef ANDROID_BUFFER_ALLOCATOR_H
1915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#define ANDROID_BUFFER_ALLOCATOR_H
2015b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
2115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#include <stdint.h>
2215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
2315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#include <cutils/native_handle.h>
2415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
2515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#include <utils/Errors.h>
2615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#include <utils/KeyedVector.h>
2715b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#include <utils/threads.h>
2815b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#include <utils/Singleton.h>
2915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
3015b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#include <ui/PixelFormat.h>
3115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
3215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#include <hardware/gralloc.h>
3315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
3415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
3515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopiannamespace android {
3615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian// ---------------------------------------------------------------------------
3715b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
3815b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopianclass String8;
3915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
406950e428feaccc8164b989ef64e771a99948797aMathias Agopianclass GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
4115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian{
4215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopianpublic:
4315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    enum {
4415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SW_READ_NEVER     = GRALLOC_USAGE_SW_READ_NEVER,
4515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SW_READ_RARELY    = GRALLOC_USAGE_SW_READ_RARELY,
4615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SW_READ_OFTEN     = GRALLOC_USAGE_SW_READ_OFTEN,
4715b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SW_READ_MASK      = GRALLOC_USAGE_SW_READ_MASK,
4815b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
4915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SW_WRITE_NEVER    = GRALLOC_USAGE_SW_WRITE_NEVER,
5015b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SW_WRITE_RARELY   = GRALLOC_USAGE_SW_WRITE_RARELY,
5115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SW_WRITE_OFTEN    = GRALLOC_USAGE_SW_WRITE_OFTEN,
5215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SW_WRITE_MASK     = GRALLOC_USAGE_SW_WRITE_MASK,
5315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
5415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
5515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
5615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_HW_TEXTURE        = GRALLOC_USAGE_HW_TEXTURE,
5715b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_HW_RENDER         = GRALLOC_USAGE_HW_RENDER,
5815b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_HW_2D             = GRALLOC_USAGE_HW_2D,
5915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        USAGE_HW_MASK           = GRALLOC_USAGE_HW_MASK
6015b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    };
6115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
626950e428feaccc8164b989ef64e771a99948797aMathias Agopian    static inline GraphicBufferAllocator& get() { return getInstance(); }
6315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
6415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
6515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    status_t alloc(uint32_t w, uint32_t h, PixelFormat format, int usage,
6615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian            buffer_handle_t* handle, int32_t* stride);
6715b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
6815b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    status_t free(buffer_handle_t handle);
6915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
7015b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    void dump(String8& res) const;
71e869aee382cfe01d42dffba4912458a16bb791fbMathias Agopian    static void dumpToSystemLog();
7215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
7315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopianprivate:
7415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    struct alloc_rec_t {
7515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        uint32_t w;
7615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        uint32_t h;
7767bbac844d3fda5c693464deddf349b80c5190a0Mathias Agopian        uint32_t s;
7815b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        PixelFormat format;
7915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        uint32_t usage;
8015b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian        size_t size;
8115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    };
8215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
8315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    static Mutex sLock;
8415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
8515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
866950e428feaccc8164b989ef64e771a99948797aMathias Agopian    friend class Singleton<GraphicBufferAllocator>;
876950e428feaccc8164b989ef64e771a99948797aMathias Agopian    GraphicBufferAllocator();
886950e428feaccc8164b989ef64e771a99948797aMathias Agopian    ~GraphicBufferAllocator();
8915b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
9015b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian    alloc_device_t  *mAllocDev;
9115b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian};
9215b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
9315b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian// ---------------------------------------------------------------------------
9415b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian}; // namespace android
9515b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian
9615b0ebb925d2d676052a0ef571b435c072b11280Mathias Agopian#endif // ANDROID_BUFFER_ALLOCATOR_H
97