blob: 0be26a79a71d1b7df86d1dfdc0f03bbc67182829 [file] [log] [blame]
Mathias Agopian3330b202009-10-05 17:07:12 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_GRAPHIC_BUFFER_H
18#define ANDROID_GRAPHIC_BUFFER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <ui/android_native_buffer.h>
24#include <ui/PixelFormat.h>
25#include <ui/Rect.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080026#include <utils/Flattenable.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070027#include <pixelflinger/pixelflinger.h>
28
Mathias Agopian30eb1b12010-11-02 20:57:14 -070029#include <hardware/hardware.h>
30
Mathias Agopian3330b202009-10-05 17:07:12 -070031struct android_native_buffer_t;
32
33namespace android {
34
35class GraphicBufferMapper;
Mathias Agopian3330b202009-10-05 17:07:12 -070036
37// ===========================================================================
38// GraphicBuffer
39// ===========================================================================
40
41class GraphicBuffer
42 : public EGLNativeBase<
43 android_native_buffer_t,
44 GraphicBuffer,
Mathias Agopian98e71dd2010-02-11 17:30:52 -080045 LightRefBase<GraphicBuffer> >, public Flattenable
Mathias Agopian3330b202009-10-05 17:07:12 -070046{
47public:
48
49 enum {
50 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
51 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
52 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
53 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
54
55 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
56 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
57 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
58 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
59
60 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
61
62 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
63 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
64 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
65 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK
66 };
67
Mathias Agopian30eb1b12010-11-02 20:57:14 -070068 enum {
69 TRANSFORM_IDENTITY = 0,
70 TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
71 TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
72 TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270
73 };
74
Mathias Agopian3330b202009-10-05 17:07:12 -070075 GraphicBuffer();
76
77 // creates w * h buffer
Mathias Agopian54ba51d2009-10-26 20:12:37 -070078 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
79
80 // create a buffer from an existing handle
81 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
82 uint32_t stride, native_handle_t* handle, bool keepOwnership);
Mathias Agopian3330b202009-10-05 17:07:12 -070083
84 // return status
85 status_t initCheck() const;
86
87 uint32_t getWidth() const { return width; }
88 uint32_t getHeight() const { return height; }
89 uint32_t getStride() const { return stride; }
90 uint32_t getUsage() const { return usage; }
Mathias Agopian30eb1b12010-11-02 20:57:14 -070091 uint32_t getTransform() const { return transform; }
Mathias Agopian3330b202009-10-05 17:07:12 -070092 PixelFormat getPixelFormat() const { return format; }
93 Rect getBounds() const { return Rect(width, height); }
94
95 status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage);
96
97 status_t lock(uint32_t usage, void** vaddr);
98 status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
99 status_t lock(GGLSurface* surface, uint32_t usage);
100 status_t unlock();
Mathias Agopian678bdd62010-12-03 17:33:09 -0800101
Mathias Agopian3330b202009-10-05 17:07:12 -0700102 android_native_buffer_t* getNativeBuffer() const;
103
104 void setIndex(int index);
105 int getIndex() const;
106
Mathias Agopian678bdd62010-12-03 17:33:09 -0800107 // for debugging
108 static void dumpAllocationsToSystemLog();
109
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700110private:
Mathias Agopian3330b202009-10-05 17:07:12 -0700111 virtual ~GraphicBuffer();
112
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700113 enum {
114 ownNone = 0,
115 ownHandle = 1,
116 ownData = 2,
117 };
118
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119 inline const GraphicBufferMapper& getBufferMapper() const {
120 return mBufferMapper;
121 }
122 inline GraphicBufferMapper& getBufferMapper() {
123 return mBufferMapper;
124 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700125 uint8_t mOwner;
Mathias Agopian3330b202009-10-05 17:07:12 -0700126
127private:
128 friend class Surface;
129 friend class BpSurface;
130 friend class BnSurface;
131 friend class LightRefBase<GraphicBuffer>;
132 GraphicBuffer(const GraphicBuffer& rhs);
133 GraphicBuffer& operator = (const GraphicBuffer& rhs);
134 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
135
136 status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
137 uint32_t usage);
138
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800139 void free_handle();
140
141 // Flattenable interface
142 size_t getFlattenedSize() const;
143 size_t getFdCount() const;
144 status_t flatten(void* buffer, size_t size,
145 int fds[], size_t count) const;
146 status_t unflatten(void const* buffer, size_t size,
147 int fds[], size_t count);
148
Mathias Agopian3330b202009-10-05 17:07:12 -0700149
150 GraphicBufferMapper& mBufferMapper;
151 ssize_t mInitCheck;
Mathias Agopian3330b202009-10-05 17:07:12 -0700152 int mIndex;
153};
154
155}; // namespace android
156
157#endif // ANDROID_GRAPHIC_BUFFER_H