blob: dcb27a002bb3431647f39d38cb9475d3d5b223e1 [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
Mathias Agopian01e44832010-06-24 21:22:43 -070040class Client;
Mathias Agopian01e44832010-06-24 21:22:43 -070041class UserClient;
Andreas Hubere049a952010-06-25 09:25:19 -070042class FreezeLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44// ---------------------------------------------------------------------------
45
46class Layer : public LayerBaseClient
47{
Mathias Agopiand606de62010-05-10 20:06:11 -070048public:
Mathias Agopianb7e930d2010-06-01 15:12:58 -070049 Layer(SurfaceFlinger* flinger, DisplayID display,
50 const sp<Client>& client);
Mathias Agopiand606de62010-05-10 20:06:11 -070051
Mathias Agopianb7e930d2010-06-01 15:12:58 -070052 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopianb7e930d2010-06-01 15:12:58 -070054 virtual const char* getTypeId() const { return "Layer"; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopianb7e930d2010-06-01 15:12:58 -070056 // the this layer's size and format
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 Agopianb7e930d2010-06-01 15:12:58 -070060 // associate a UserClient to this Layer
61 status_t setToken(const sp<UserClient>& uc, SharedClient* sc, int32_t idx);
62 int32_t getToken() const;
Mathias Agopian579b3f82010-06-08 19:54:15 -070063 sp<UserClient> getClient() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070064
65 // Set this Layer's buffers size
Mathias Agopiana138f892010-05-21 17:24:35 -070066 void setBufferSize(uint32_t w, uint32_t h);
67 bool isFixedSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopianb7e930d2010-06-01 15:12:58 -070069 // LayerBase interface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 virtual void onDraw(const Region& clip) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 virtual uint32_t doTransaction(uint32_t transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 virtual void lockPageFlip(bool& recomputeVisibleRegions);
73 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
74 virtual void finishPageFlip();
75 virtual bool needsBlending() const { return mNeedsBlending; }
Mathias Agopian401c2572009-09-23 19:16:27 -070076 virtual bool needsDithering() const { return mNeedsDithering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -070077 virtual bool needsFiltering() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 virtual bool isSecure() const { return mSecure; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079 virtual sp<Surface> createSurface() const;
Mathias Agopian9a112062009-04-17 19:36:26 -070080 virtual status_t ditch();
Mathias Agopiand606de62010-05-10 20:06:11 -070081 virtual void onRemoved();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082
Mathias Agopianb7e930d2010-06-01 15:12:58 -070083 // only for debugging
84 inline sp<GraphicBuffer> getBuffer(int i) const {
85 return mBufferManager.getBuffer(i); }
86 // only for debugging
87 inline const sp<FreezeLock>& getFreezeLock() const {
88 return mFreezeLock; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -070089
90protected:
91 virtual void dump(String8& result, char* scratch, size_t size) const;
92
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 void reloadTexture(const Region& dirty);
Mathias Agopian3330b202009-10-05 17:07:12 -070095 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopiana138f892010-05-21 17:24:35 -070096 sp<GraphicBuffer> requestBuffer(int bufferIdx,
97 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -070098 status_t setBufferCount(int bufferCount);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070099
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700100 // -----------------------------------------------------------------------
101
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700102 class SurfaceLayer : public LayerBaseClient::Surface {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700104 SurfaceLayer(const sp<SurfaceFlinger>& flinger, const sp<Layer>& owner);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700105 ~SurfaceLayer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700106 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700107 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
108 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700109 virtual status_t setBufferCount(int bufferCount);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110 sp<Layer> getOwner() const {
111 return static_cast<Layer*>(Surface::getOwner().get());
112 }
113 };
114 friend class SurfaceLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700116 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700117
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700118 class ClientRef {
119 ClientRef(const ClientRef& rhs);
120 ClientRef& operator = (const ClientRef& rhs);
121 mutable Mutex mLock;
122 // binder thread, page-flip thread
Mathias Agopian579b3f82010-06-08 19:54:15 -0700123 sp<SharedBufferServer> mControlBlock;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700124 wp<UserClient> mUserClient;
125 int32_t mToken;
126 public:
127 ClientRef();
128 ~ClientRef();
129 int32_t getToken() const;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700130 sp<UserClient> getClient() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700131 status_t setToken(const sp<UserClient>& uc,
Mathias Agopian579b3f82010-06-08 19:54:15 -0700132 const sp<SharedBufferServer>& sharedClient, int32_t token);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700133 sp<UserClient> getUserClientUnsafe() const;
134 class Access {
135 Access(const Access& rhs);
136 Access& operator = (const Access& rhs);
137 sp<UserClient> mUserClientStrongRef;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700138 sp<SharedBufferServer> mControlBlock;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700139 public:
140 Access(const ClientRef& ref);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700141 ~Access();
142 inline SharedBufferServer* get() const { return mControlBlock.get(); }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700143 };
144 friend class Access;
145 };
Mathias Agopiand606de62010-05-10 20:06:11 -0700146
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700147 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700148
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700149 class BufferManager {
150 static const size_t NUM_BUFFERS = 2;
151 struct BufferData {
152 sp<GraphicBuffer> buffer;
153 Image texture;
154 };
155 // this lock protect mBufferData[].buffer but since there
156 // is very little contention, we have only one like for
157 // the whole array, we also use it to protect mNumBuffers.
158 mutable Mutex mLock;
159 BufferData mBufferData[SharedBufferStack::NUM_BUFFER_MAX];
160 size_t mNumBuffers;
161 Texture mFailoverTexture;
162 TextureManager& mTextureManager;
163 ssize_t mActiveBuffer;
164 bool mFailover;
165 static status_t destroyTexture(Image* tex, EGLDisplay dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700166
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700167 public:
168 static size_t getDefaultBufferCount() { return NUM_BUFFERS; }
169 BufferManager(TextureManager& tm);
170 ~BufferManager();
Mathias Agopianbb641242010-05-18 17:06:55 -0700171
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700172 // detach/attach buffer from/to given index
173 sp<GraphicBuffer> detachBuffer(size_t index);
174 status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer);
175 // resize the number of active buffers
176 status_t resize(size_t size);
Mathias Agopiand606de62010-05-10 20:06:11 -0700177
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700178 // ----------------------------------------------
179 // must be called from GL thread
Mathias Agopiand606de62010-05-10 20:06:11 -0700180
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700181 // set/get active buffer index
182 status_t setActiveBufferIndex(size_t index);
183 size_t getActiveBufferIndex() const;
184 // return the active buffer
185 sp<GraphicBuffer> getActiveBuffer() const;
186 // return the active texture (or fail-over)
187 Texture getActiveTexture() const;
188 // frees resources associated with all buffers
189 status_t destroy(EGLDisplay dpy);
190 // load bitmap data into the active buffer
191 status_t loadTexture(const Region& dirty, const GGLSurface& t);
192 // make active buffer an EGLImage if needed
193 status_t initEglImage(EGLDisplay dpy,
194 const sp<GraphicBuffer>& buffer);
Mathias Agopiand606de62010-05-10 20:06:11 -0700195
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700196 // ----------------------------------------------
197 // only for debugging
198 sp<GraphicBuffer> getBuffer(size_t index) const;
199 };
Mathias Agopiand606de62010-05-10 20:06:11 -0700200
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700201 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700202
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700203 // thread-safe
204 ClientRef mUserClientRef;
Mathias Agopiand606de62010-05-10 20:06:11 -0700205
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700206 // constants
207 sp<Surface> mSurface;
208 PixelFormat mFormat;
209 bool mNeedsBlending;
210 bool mNeedsDithering;
Mathias Agopiand606de62010-05-10 20:06:11 -0700211
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700212 // page-flip thread (currently main thread)
213 bool mSecure;
214 Region mPostedDirtyRegion;
Mathias Agopiand606de62010-05-10 20:06:11 -0700215
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700216 // page-flip thread and transaction thread (currently main thread)
217 sp<FreezeLock> mFreezeLock;
Mathias Agopiand606de62010-05-10 20:06:11 -0700218
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700219 // see threading usage in declaration
220 TextureManager mTextureManager;
221 BufferManager mBufferManager;
Mathias Agopian96f08192010-06-02 23:28:45 -0700222
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700223 // binder thread, transaction thread
224 mutable Mutex mLock;
225 uint32_t mWidth;
226 uint32_t mHeight;
227 uint32_t mReqWidth;
228 uint32_t mReqHeight;
229 uint32_t mReqFormat;
230 bool mFixedSize;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231};
232
233// ---------------------------------------------------------------------------
234
235}; // namespace android
236
237#endif // ANDROID_LAYER_H