blob: a93fcb13b8aeb5dd9671d18dcbd008952b1cb578 [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
Andreas Hubere049a952010-06-25 09:25:19 -070040class FreezeLock;
Mathias Agopian1f7bec62010-06-25 18:02:21 -070041class Client;
42class GLExtensions;
43class UserClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45// ---------------------------------------------------------------------------
46
47class Layer : public LayerBaseClient
48{
Mathias Agopiand606de62010-05-10 20:06:11 -070049public:
Mathias Agopianb7e930d2010-06-01 15:12:58 -070050 Layer(SurfaceFlinger* flinger, DisplayID display,
51 const sp<Client>& client);
Mathias Agopiand606de62010-05-10 20:06:11 -070052
Mathias Agopianb7e930d2010-06-01 15:12:58 -070053 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopianb7e930d2010-06-01 15:12:58 -070055 virtual const char* getTypeId() const { return "Layer"; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopianb7e930d2010-06-01 15:12:58 -070057 // the this layer's size and format
Mathias Agopiancbb288b2009-09-07 16:32:45 -070058 status_t setBuffers(uint32_t w, uint32_t h,
59 PixelFormat format, uint32_t flags=0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopianb7e930d2010-06-01 15:12:58 -070061 // associate a UserClient to this Layer
62 status_t setToken(const sp<UserClient>& uc, SharedClient* sc, int32_t idx);
63 int32_t getToken() const;
Mathias Agopian579b3f82010-06-08 19:54:15 -070064 sp<UserClient> getClient() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070065
66 // Set this Layer's buffers size
Mathias Agopiana138f892010-05-21 17:24:35 -070067 void setBufferSize(uint32_t w, uint32_t h);
68 bool isFixedSize() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Mathias Agopianb7e930d2010-06-01 15:12:58 -070070 // LayerBase interface
Mathias Agopiandf85c452010-09-29 13:02:36 -070071 virtual void drawForSreenShot() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 virtual void onDraw(const Region& clip) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 virtual uint32_t doTransaction(uint32_t transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 virtual void lockPageFlip(bool& recomputeVisibleRegions);
75 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
76 virtual void finishPageFlip();
77 virtual bool needsBlending() const { return mNeedsBlending; }
Mathias Agopian401c2572009-09-23 19:16:27 -070078 virtual bool needsDithering() const { return mNeedsDithering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -070079 virtual bool needsFiltering() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 virtual bool isSecure() const { return mSecure; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 virtual sp<Surface> createSurface() const;
Mathias Agopian9a112062009-04-17 19:36:26 -070082 virtual status_t ditch();
Mathias Agopiand606de62010-05-10 20:06:11 -070083 virtual void onRemoved();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084
Mathias Agopianb7e930d2010-06-01 15:12:58 -070085 // only for debugging
86 inline sp<GraphicBuffer> getBuffer(int i) const {
87 return mBufferManager.getBuffer(i); }
88 // only for debugging
89 inline const sp<FreezeLock>& getFreezeLock() const {
90 return mFreezeLock; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -070091
92protected:
93 virtual void dump(String8& result, char* scratch, size_t size) const;
94
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 void reloadTexture(const Region& dirty);
Mathias Agopian3330b202009-10-05 17:07:12 -070097 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopiana138f892010-05-21 17:24:35 -070098 sp<GraphicBuffer> requestBuffer(int bufferIdx,
99 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700100 status_t setBufferCount(int bufferCount);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700101
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700102 // -----------------------------------------------------------------------
103
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700104 class SurfaceLayer : public LayerBaseClient::Surface {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105 public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700106 SurfaceLayer(const sp<SurfaceFlinger>& flinger, const sp<Layer>& owner);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700107 ~SurfaceLayer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700108 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700109 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
110 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700111 virtual status_t setBufferCount(int bufferCount);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112 sp<Layer> getOwner() const {
113 return static_cast<Layer*>(Surface::getOwner().get());
114 }
115 };
116 friend class SurfaceLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700118 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700119
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700120 class ClientRef {
121 ClientRef(const ClientRef& rhs);
122 ClientRef& operator = (const ClientRef& rhs);
123 mutable Mutex mLock;
124 // binder thread, page-flip thread
Mathias Agopian579b3f82010-06-08 19:54:15 -0700125 sp<SharedBufferServer> mControlBlock;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700126 wp<UserClient> mUserClient;
127 int32_t mToken;
128 public:
129 ClientRef();
130 ~ClientRef();
131 int32_t getToken() const;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700132 sp<UserClient> getClient() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700133 status_t setToken(const sp<UserClient>& uc,
Mathias Agopian579b3f82010-06-08 19:54:15 -0700134 const sp<SharedBufferServer>& sharedClient, int32_t token);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700135 sp<UserClient> getUserClientUnsafe() const;
136 class Access {
137 Access(const Access& rhs);
138 Access& operator = (const Access& rhs);
139 sp<UserClient> mUserClientStrongRef;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700140 sp<SharedBufferServer> mControlBlock;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700141 public:
142 Access(const ClientRef& ref);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700143 ~Access();
144 inline SharedBufferServer* get() const { return mControlBlock.get(); }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700145 };
146 friend class Access;
147 };
Mathias Agopiand606de62010-05-10 20:06:11 -0700148
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700149 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700150
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700151 class BufferManager {
152 static const size_t NUM_BUFFERS = 2;
153 struct BufferData {
154 sp<GraphicBuffer> buffer;
155 Image texture;
156 };
157 // this lock protect mBufferData[].buffer but since there
158 // is very little contention, we have only one like for
159 // the whole array, we also use it to protect mNumBuffers.
160 mutable Mutex mLock;
161 BufferData mBufferData[SharedBufferStack::NUM_BUFFER_MAX];
162 size_t mNumBuffers;
163 Texture mFailoverTexture;
164 TextureManager& mTextureManager;
165 ssize_t mActiveBuffer;
166 bool mFailover;
167 static status_t destroyTexture(Image* tex, EGLDisplay dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700168
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169 public:
170 static size_t getDefaultBufferCount() { return NUM_BUFFERS; }
171 BufferManager(TextureManager& tm);
172 ~BufferManager();
Mathias Agopianbb641242010-05-18 17:06:55 -0700173
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700174 // detach/attach buffer from/to given index
175 sp<GraphicBuffer> detachBuffer(size_t index);
176 status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer);
177 // resize the number of active buffers
178 status_t resize(size_t size);
Mathias Agopiand606de62010-05-10 20:06:11 -0700179
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700180 // ----------------------------------------------
181 // must be called from GL thread
Mathias Agopiand606de62010-05-10 20:06:11 -0700182
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700183 // set/get active buffer index
184 status_t setActiveBufferIndex(size_t index);
185 size_t getActiveBufferIndex() const;
186 // return the active buffer
187 sp<GraphicBuffer> getActiveBuffer() const;
188 // return the active texture (or fail-over)
189 Texture getActiveTexture() const;
190 // frees resources associated with all buffers
191 status_t destroy(EGLDisplay dpy);
192 // load bitmap data into the active buffer
193 status_t loadTexture(const Region& dirty, const GGLSurface& t);
194 // make active buffer an EGLImage if needed
195 status_t initEglImage(EGLDisplay dpy,
196 const sp<GraphicBuffer>& buffer);
Mathias Agopiand606de62010-05-10 20:06:11 -0700197
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700198 // ----------------------------------------------
199 // only for debugging
200 sp<GraphicBuffer> getBuffer(size_t index) const;
201 };
Mathias Agopiand606de62010-05-10 20:06:11 -0700202
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700203 // -----------------------------------------------------------------------
Mathias Agopiand606de62010-05-10 20:06:11 -0700204
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700205 // thread-safe
206 ClientRef mUserClientRef;
Mathias Agopiand606de62010-05-10 20:06:11 -0700207
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700208 // constants
209 sp<Surface> mSurface;
210 PixelFormat mFormat;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700211 const GLExtensions& mGLExtensions;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700212 bool mNeedsBlending;
213 bool mNeedsDithering;
Mathias Agopiand606de62010-05-10 20:06:11 -0700214
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700215 // page-flip thread (currently main thread)
216 bool mSecure;
217 Region mPostedDirtyRegion;
Mathias Agopiand606de62010-05-10 20:06:11 -0700218
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700219 // page-flip thread and transaction thread (currently main thread)
220 sp<FreezeLock> mFreezeLock;
Mathias Agopiand606de62010-05-10 20:06:11 -0700221
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700222 // see threading usage in declaration
223 TextureManager mTextureManager;
224 BufferManager mBufferManager;
Mathias Agopian96f08192010-06-02 23:28:45 -0700225
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700226 // binder thread, transaction thread
227 mutable Mutex mLock;
228 uint32_t mWidth;
229 uint32_t mHeight;
230 uint32_t mReqWidth;
231 uint32_t mReqHeight;
232 uint32_t mReqFormat;
Mathias Agopian733189d2010-12-02 21:32:29 -0800233 bool mNeedsScaling;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700234 bool mFixedSize;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235};
236
237// ---------------------------------------------------------------------------
238
239}; // namespace android
240
241#endif // ANDROID_LAYER_H