1303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza/*
27460160b801b4230f20722efbafcac347312e702Mathias Agopian**
37460160b801b4230f20722efbafcac347312e702Mathias Agopian** Copyright 2009, The Android Open Source Project
47460160b801b4230f20722efbafcac347312e702Mathias Agopian**
5303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** Licensed under the Apache License, Version 2.0 (the "License");
6303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** you may not use this file except in compliance with the License.
7303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** You may obtain a copy of the License at
87460160b801b4230f20722efbafcac347312e702Mathias Agopian**
9303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza**     http://www.apache.org/licenses/LICENSE-2.0
107460160b801b4230f20722efbafcac347312e702Mathias Agopian**
11303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** Unless required by applicable law or agreed to in writing, software
12303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** distributed under the License is distributed on an "AS IS" BASIS,
13303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** See the License for the specific language governing permissions and
157460160b801b4230f20722efbafcac347312e702Mathias Agopian** limitations under the License.
167460160b801b4230f20722efbafcac347312e702Mathias Agopian*/
177460160b801b4230f20722efbafcac347312e702Mathias Agopian
187460160b801b4230f20722efbafcac347312e702Mathias Agopian#ifndef ANDROID_BUFFER_ALLOCATOR_H
197460160b801b4230f20722efbafcac347312e702Mathias Agopian#define ANDROID_BUFFER_ALLOCATOR_H
207460160b801b4230f20722efbafcac347312e702Mathias Agopian
217460160b801b4230f20722efbafcac347312e702Mathias Agopian#include <stdint.h>
227460160b801b4230f20722efbafcac347312e702Mathias Agopian
23fe2f54fc8a9810708fe05d719721af19aea10a4aMathias Agopian#include <memory>
24fe2f54fc8a9810708fe05d719721af19aea10a4aMathias Agopian#include <string>
25fe2f54fc8a9810708fe05d719721af19aea10a4aMathias Agopian
267460160b801b4230f20722efbafcac347312e702Mathias Agopian#include <cutils/native_handle.h>
277460160b801b4230f20722efbafcac347312e702Mathias Agopian
28cb8405e7e74398aeafc9ef6ddb59af2d7cc6d2a6Chia-I Wu#include <ui/PixelFormat.h>
29cb8405e7e74398aeafc9ef6ddb59af2d7cc6d2a6Chia-I Wu
307460160b801b4230f20722efbafcac347312e702Mathias Agopian#include <utils/Errors.h>
317460160b801b4230f20722efbafcac347312e702Mathias Agopian#include <utils/KeyedVector.h>
32fe2f54fc8a9810708fe05d719721af19aea10a4aMathias Agopian#include <utils/Mutex.h>
337460160b801b4230f20722efbafcac347312e702Mathias Agopian#include <utils/Singleton.h>
347460160b801b4230f20722efbafcac347312e702Mathias Agopian
357460160b801b4230f20722efbafcac347312e702Mathias Agopiannamespace android {
367460160b801b4230f20722efbafcac347312e702Mathias Agopian
379ba189dc5c03040d45dad2080a81115f48f099c3Chia-I Wunamespace Gralloc2 {
389ba189dc5c03040d45dad2080a81115f48f099c3Chia-I Wuclass Allocator;
399ba189dc5c03040d45dad2080a81115f48f099c3Chia-I Wu}
409ba189dc5c03040d45dad2080a81115f48f099c3Chia-I Wu
419ba189dc5c03040d45dad2080a81115f48f099c3Chia-I Wuclass GraphicBufferMapper;
427460160b801b4230f20722efbafcac347312e702Mathias Agopianclass String8;
437460160b801b4230f20722efbafcac347312e702Mathias Agopian
443330b203039dea366d4981db1408a460134b2d2cMathias Agopianclass GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
457460160b801b4230f20722efbafcac347312e702Mathias Agopian{
467460160b801b4230f20722efbafcac347312e702Mathias Agopianpublic:
473330b203039dea366d4981db1408a460134b2d2cMathias Agopian    static inline GraphicBufferAllocator& get() { return getInstance(); }
487460160b801b4230f20722efbafcac347312e702Mathias Agopian
498deb4da6e8fe4687991da32ae68fa0bca4cb8212Dan Stoza    status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
5082c049843870fc72ff36f48b970dfaf413db0687Chris Forbes            uint32_t layerCount, uint64_t usage,
51e96a325aff9da4e02abeb7b9178592583cf3b78cCraig Donner            buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
526ebc46a7c01b22bc19d3c00b493f8d930b15b43aCraig Donner            std::string requestorName);
537460160b801b4230f20722efbafcac347312e702Mathias Agopian
547460160b801b4230f20722efbafcac347312e702Mathias Agopian    status_t free(buffer_handle_t handle);
557460160b801b4230f20722efbafcac347312e702Mathias Agopian
567460160b801b4230f20722efbafcac347312e702Mathias Agopian    void dump(String8& res) const;
57678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    static void dumpToSystemLog();
587460160b801b4230f20722efbafcac347312e702Mathias Agopian
597460160b801b4230f20722efbafcac347312e702Mathias Agopianprivate:
607460160b801b4230f20722efbafcac347312e702Mathias Agopian    struct alloc_rec_t {
61303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        uint32_t width;
62303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        uint32_t height;
63303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        uint32_t stride;
647460160b801b4230f20722efbafcac347312e702Mathias Agopian        PixelFormat format;
656ebc46a7c01b22bc19d3c00b493f8d930b15b43aCraig Donner        uint32_t layerCount;
6682c049843870fc72ff36f48b970dfaf413db0687Chris Forbes        uint64_t usage;
677460160b801b4230f20722efbafcac347312e702Mathias Agopian        size_t size;
68024e9313ce69ced1f853d737de680ef9f50ed26dDan Stoza        std::string requestorName;
697460160b801b4230f20722efbafcac347312e702Mathias Agopian    };
70303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza
717460160b801b4230f20722efbafcac347312e702Mathias Agopian    static Mutex sLock;
727460160b801b4230f20722efbafcac347312e702Mathias Agopian    static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
73303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza
743330b203039dea366d4981db1408a460134b2d2cMathias Agopian    friend class Singleton<GraphicBufferAllocator>;
753330b203039dea366d4981db1408a460134b2d2cMathias Agopian    GraphicBufferAllocator();
763330b203039dea366d4981db1408a460134b2d2cMathias Agopian    ~GraphicBufferAllocator();
77303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza
789ba189dc5c03040d45dad2080a81115f48f099c3Chia-I Wu    GraphicBufferMapper& mMapper;
795bac7f36ee9d5ed0c2e8a0141909ca94351965a8Chia-I Wu    const std::unique_ptr<const Gralloc2::Allocator> mAllocator;
807460160b801b4230f20722efbafcac347312e702Mathias Agopian};
817460160b801b4230f20722efbafcac347312e702Mathias Agopian
827460160b801b4230f20722efbafcac347312e702Mathias Agopian// ---------------------------------------------------------------------------
837460160b801b4230f20722efbafcac347312e702Mathias Agopian}; // namespace android
847460160b801b4230f20722efbafcac347312e702Mathias Agopian
857460160b801b4230f20722efbafcac347312e702Mathias Agopian#endif // ANDROID_BUFFER_ALLOCATOR_H
86