blob: 10b5910adf7654cfd60e52c8ffebc526b6c33813 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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_LAYER_H
18#define ANDROID_LAYER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian3330b202009-10-05 17:07:12 -070023#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <pixelflinger/pixelflinger.h>
26
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#include <EGL/egl.h>
28#include <EGL/eglext.h>
29#include <GLES/gl.h>
30#include <GLES/glext.h>
31
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include "LayerBase.h"
33#include "Transform.h"
Mathias Agopiand606de62010-05-10 20:06:11 -070034#include "TextureManager.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
36namespace android {
37
38// ---------------------------------------------------------------------------
39
40class Client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041class FreezeLock;
42
43// ---------------------------------------------------------------------------
44
45class Layer : public LayerBaseClient
46{
Mathias Agopiand606de62010-05-10 20:06:11 -070047public:
48 // lcblk is (almost) only accessed from the main SF thread, in the places
49 // where it's not, a reference to Client must be held
50 SharedBufferServer* lcblk;
51
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052 Layer(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -070053 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55 virtual ~Layer();
56
Mathias Agopiancbb288b2009-09-07 16:32:45 -070057 status_t setBuffers(uint32_t w, uint32_t h,
58 PixelFormat format, uint32_t flags=0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopiana138f892010-05-21 17:24:35 -070060 void setBufferSize(uint32_t w, uint32_t h);
61 bool isFixedSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
63 virtual void onDraw(const Region& clip) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 virtual uint32_t doTransaction(uint32_t transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 virtual void lockPageFlip(bool& recomputeVisibleRegions);
66 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
67 virtual void finishPageFlip();
68 virtual bool needsBlending() const { return mNeedsBlending; }
Mathias Agopian401c2572009-09-23 19:16:27 -070069 virtual bool needsDithering() const { return mNeedsDithering; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 virtual bool isSecure() const { return mSecure; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 virtual sp<Surface> createSurface() const;
Mathias Agopian9a112062009-04-17 19:36:26 -070072 virtual status_t ditch();
Mathias Agopiand606de62010-05-10 20:06:11 -070073 virtual void onRemoved();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070074
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 // only for debugging
Mathias Agopiand606de62010-05-10 20:06:11 -070076 inline sp<GraphicBuffer> getBuffer(int i) const { return mBufferManager.getBuffer(i); }
Mathias Agopiancbb288b2009-09-07 16:32:45 -070077 // only for debugging
78 inline const sp<FreezeLock>& getFreezeLock() const { return mFreezeLock; }
79 // only for debugging
80 inline PixelFormat pixelFormat() const { return mFormat; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
Mathias Agopian1b5e1022010-04-20 17:55:49 -070082 virtual const char* getTypeId() const { return "Layer"; }
83
84protected:
85 virtual void dump(String8& result, char* scratch, size_t size) const;
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 void reloadTexture(const Region& dirty);
89
Mathias Agopian3330b202009-10-05 17:07:12 -070090 uint32_t getEffectiveUsage(uint32_t usage) const;
91
Mathias Agopiana138f892010-05-21 17:24:35 -070092 sp<GraphicBuffer> requestBuffer(int bufferIdx,
93 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -070094 status_t setBufferCount(int bufferCount);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070095
Mathias Agopiancbb288b2009-09-07 16:32:45 -070096 class SurfaceLayer : public LayerBaseClient::Surface {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070097 public:
Mathias Agopiancbb288b2009-09-07 16:32:45 -070098 SurfaceLayer(const sp<SurfaceFlinger>& flinger,
99 SurfaceID id, const sp<Layer>& owner);
100 ~SurfaceLayer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700102 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
103 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700104 virtual status_t setBufferCount(int bufferCount);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105 sp<Layer> getOwner() const {
106 return static_cast<Layer*>(Surface::getOwner().get());
107 }
108 };
109 friend class SurfaceLayer;
110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 sp<Surface> mSurface;
112
113 bool mSecure;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 int32_t mFrontBufferIndex;
115 bool mNeedsBlending;
Mathias Agopian401c2572009-09-23 19:16:27 -0700116 bool mNeedsDithering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 Region mPostedDirtyRegion;
118 sp<FreezeLock> mFreezeLock;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700119 PixelFormat mFormat;
Mathias Agopiand606de62010-05-10 20:06:11 -0700120
121 class BufferManager {
122 static const size_t NUM_BUFFERS = 2;
123 struct BufferData {
124 sp<GraphicBuffer> buffer;
Mathias Agopianbb641242010-05-18 17:06:55 -0700125 Image texture;
Mathias Agopiand606de62010-05-10 20:06:11 -0700126 };
Mathias Agopianbb641242010-05-18 17:06:55 -0700127 // this lock protect mBufferData[].buffer but since there
128 // is very little contention, we have only one like for
129 // the whole array, we also use it to protect mNumBuffers.
Mathias Agopiand606de62010-05-10 20:06:11 -0700130 mutable Mutex mLock;
Mathias Agopianbb641242010-05-18 17:06:55 -0700131 BufferData mBufferData[SharedBufferStack::NUM_BUFFER_MAX];
132 size_t mNumBuffers;
Mathias Agopiand606de62010-05-10 20:06:11 -0700133 Texture mFailoverTexture;
134 TextureManager& mTextureManager;
135 ssize_t mActiveBuffer;
136 bool mFailover;
Mathias Agopianbb641242010-05-18 17:06:55 -0700137 static status_t destroyTexture(Image* tex, EGLDisplay dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700138
139 public:
Mathias Agopianbb641242010-05-18 17:06:55 -0700140 static size_t getDefaultBufferCount() { return NUM_BUFFERS; }
Mathias Agopiand606de62010-05-10 20:06:11 -0700141 BufferManager(TextureManager& tm);
Mathias Agopianbb641242010-05-18 17:06:55 -0700142 ~BufferManager();
Mathias Agopiand606de62010-05-10 20:06:11 -0700143
144 // detach/attach buffer from/to given index
145 sp<GraphicBuffer> detachBuffer(size_t index);
146 status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer);
147
Mathias Agopianbb641242010-05-18 17:06:55 -0700148 // resize the number of active buffers
149 status_t resize(size_t size);
150
Mathias Agopiand606de62010-05-10 20:06:11 -0700151 // ----------------------------------------------
152 // must be called from GL thread
153
154 // set/get active buffer index
155 status_t setActiveBufferIndex(size_t index);
156 size_t getActiveBufferIndex() const;
157
158 // return the active buffer
159 sp<GraphicBuffer> getActiveBuffer() const;
160
161 // return the active texture (or fail-over)
162 Texture getActiveTexture() const;
163
164 // frees resources associated with all buffers
165 status_t destroy(EGLDisplay dpy);
166
167 // load bitmap data into the active buffer
168 status_t loadTexture(const Region& dirty, const GGLSurface& t);
169
170 // make active buffer an EGLImage if needed
171 status_t initEglImage(EGLDisplay dpy,
172 const sp<GraphicBuffer>& buffer);
173
174 // ----------------------------------------------
175 // only for debugging
176 sp<GraphicBuffer> getBuffer(size_t index) const;
177 };
178
179 TextureManager mTextureManager;
180 BufferManager mBufferManager;
181
Mathias Agopianbb641242010-05-18 17:06:55 -0700182 // this lock protects mWidth and mHeight which are accessed from
183 // the main thread and requestBuffer's binder transaction thread.
Mathias Agopiand606de62010-05-10 20:06:11 -0700184 mutable Mutex mLock;
185 uint32_t mWidth;
186 uint32_t mHeight;
Mathias Agopiana138f892010-05-21 17:24:35 -0700187 uint32_t mReqWidth;
188 uint32_t mReqHeight;
189 uint32_t mReqFormat;
190 bool mFixedSize;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191};
192
193// ---------------------------------------------------------------------------
194
195}; // namespace android
196
197#endif // ANDROID_LAYER_H