blob: b3f3771ae7ca13010b8a330568d3c06189f9b710 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -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_LAYER_BASE_H
18#define ANDROID_LAYER_BASE_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <private/ui/LayerState.h>
24
25#include <ui/Region.h>
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080026#include <ui/Overlay.h>
27
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070028#include <pixelflinger/pixelflinger.h>
29
30#include "Transform.h"
31
32namespace android {
33
34// ---------------------------------------------------------------------------
35
36class SurfaceFlinger;
37class DisplayHardware;
38class GraphicPlane;
39class Client;
40
41// ---------------------------------------------------------------------------
42
43class LayerBase
44{
45 // poor man's dynamic_cast below
46 template<typename T>
47 struct getTypeInfoOfAnyType {
48 static uint32_t get() { return T::typeInfo; }
49 };
50
51 template<typename T>
52 struct getTypeInfoOfAnyType<T*> {
53 static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); }
54 };
55
56public:
57 static const uint32_t typeInfo;
58 static const char* const typeID;
59 virtual char const* getTypeID() const { return typeID; }
60 virtual uint32_t getTypeInfo() const { return typeInfo; }
61
62 template<typename T>
63 static T dynamicCast(LayerBase* base) {
64 uint32_t mostDerivedInfo = base->getTypeInfo();
65 uint32_t castToInfo = getTypeInfoOfAnyType<T>::get();
66 if ((mostDerivedInfo & castToInfo) == castToInfo)
67 return static_cast<T>(base);
68 return 0;
69 }
70
71
72 static Vector<GLuint> deletedTextures;
73
74 LayerBase(SurfaceFlinger* flinger, DisplayID display);
75 virtual ~LayerBase();
76
77 DisplayID dpy;
78 mutable bool invalidate;
79 Region visibleRegionScreen;
80 Region transparentRegionScreen;
81 Region coveredRegionScreen;
82
83 struct State {
84 uint32_t w;
85 uint32_t h;
86 uint32_t z;
87 uint8_t alpha;
88 uint8_t flags;
89 uint8_t sequence; // changes when visible regions can change
90 uint8_t reserved;
91 uint32_t tint;
92 Transform transform;
93 Region transparentRegion;
94 };
95
96 // modify current state
97 bool setPosition(int32_t x, int32_t y);
98 bool setLayer(uint32_t z);
99 bool setSize(uint32_t w, uint32_t h);
100 bool setAlpha(uint8_t alpha);
101 bool setMatrix(const layer_state_t::matrix22_t& matrix);
102 bool setTransparentRegionHint(const Region& opaque);
103 bool setFlags(uint8_t flags, uint8_t mask);
104
105 void commitTransaction(bool skipSize);
106 bool requestTransaction();
107
108 uint32_t getTransactionFlags(uint32_t flags);
109 uint32_t setTransactionFlags(uint32_t flags);
110
111 void validateVisibility(const Transform& globalTransform);
112 Rect visibleBounds() const;
113 void drawRegion(const Region& reg) const;
114
115 virtual void draw(const Region& clip) const;
116 virtual void onDraw(const Region& clip) const = 0;
117 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
118 virtual void setSizeChanged(uint32_t w, uint32_t h);
119 virtual uint32_t doTransaction(uint32_t transactionFlags);
120 virtual void setVisibleRegion(const Region& visibleRegion);
121 virtual void setCoveredRegion(const Region& coveredRegion);
122 virtual Point getPhysicalSize() const;
123 virtual void lockPageFlip(bool& recomputeVisibleRegions);
124 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
125 virtual void finishPageFlip();
126 virtual bool needsBlending() const { return false; }
127 virtual bool isSecure() const { return false; }
128
129 enum { // flags for doTransaction()
130 eVisibleRegion = 0x00000002,
131 eRestartTransaction = 0x00000008
132 };
133
134
135 inline const State& drawingState() const { return mDrawingState; }
136 inline const State& currentState() const { return mCurrentState; }
137 inline State& currentState() { return mCurrentState; }
138
139 static int compareCurrentStateZ(LayerBase*const* layerA, LayerBase*const* layerB) {
140 return layerA[0]->currentState().z - layerB[0]->currentState().z;
141 }
142
143 int32_t getOrientation() const { return mOrientation; }
144 bool transformed() const { return mTransformed; }
145 int tx() const { return mLeft; }
146 int ty() const { return mTop; }
147
148protected:
149 const GraphicPlane& graphicPlane(int dpy) const;
150 GraphicPlane& graphicPlane(int dpy);
151
152 GLuint createTexture() const;
153
154 void drawWithOpenGL(const Region& clip,
155 GLint textureName, const GGLSurface& surface) const;
156
157 void clearWithOpenGL(const Region& clip) const;
158
159 void loadTexture(const Region& dirty,
160 GLint textureName, const GGLSurface& t,
161 GLuint& textureWidth, GLuint& textureHeight) const;
162
163 bool canUseCopybit() const;
164
165
166 SurfaceFlinger* mFlinger;
167 uint32_t mFlags;
168
169 // cached during validateVisibility()
170 bool mTransformed;
171 int32_t mOrientation;
172 GLfixed mVertices[4][2];
173 Rect mTransformedBounds;
174 bool mCanUseCopyBit;
175 int mLeft;
176 int mTop;
177
178 // these are protected by an external lock
179 State mCurrentState;
180 State mDrawingState;
181 volatile int32_t mTransactionFlags;
182
183 // don't change, don't need a lock
184 bool mPremultipliedAlpha;
185
186 // only read
187 const uint32_t mIdentity;
188
189
190private:
191 void validateTexture(GLint textureName) const;
192 static int32_t sIdentity;
193};
194
195
196// ---------------------------------------------------------------------------
197
198class LayerBaseClient : public LayerBase
199{
200public:
201 class Surface;
202 static const uint32_t typeInfo;
203 static const char* const typeID;
204 virtual char const* getTypeID() const { return typeID; }
205 virtual uint32_t getTypeInfo() const { return typeInfo; }
206
207 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
208 Client* client, int32_t i);
209 virtual ~LayerBaseClient();
210
211
212 Client* const client;
213 layer_cblk_t* const lcblk;
214
215 inline int32_t clientIndex() const { return mIndex; }
216 int32_t serverIndex() const;
217
218 virtual sp<Surface> getSurface() const;
219
220 uint32_t getIdentity() const { return mIdentity; }
221
222 class Surface : public BnSurface
223 {
224 public:
225 Surface(SurfaceID id, int identity) {
226 mParams.token = id;
227 mParams.identity = identity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 }
229 Surface(SurfaceID id,
230 const sp<IMemoryHeap>& heap0,
231 const sp<IMemoryHeap>& heap1,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800232 int identity)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 {
234 mParams.token = id;
235 mParams.identity = identity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 mParams.heap[0] = heap0;
237 mParams.heap[1] = heap1;
238 }
239 virtual ~Surface() {
240 // TODO: We now have a point here were we can clean-up the
241 // client's mess.
242 // This is also where surface id should be recycled.
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800243 //LOGD("Surface %d, heaps={%p, %p} destroyed",
244 // mId, mHeap[0].get(), mHeap[1].get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 }
246
247 virtual void getSurfaceData(
248 ISurfaceFlingerClient::surface_data_t* params) const {
249 *params = mParams;
250 }
251
252 virtual status_t registerBuffers(int w, int h, int hstride, int vstride,
253 PixelFormat format, const sp<IMemoryHeap>& heap)
254 { return INVALID_OPERATION; }
255 virtual void postBuffer(ssize_t offset) { }
256 virtual void unregisterBuffers() { };
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800257 virtual sp<Overlay> createOverlay(
258 uint32_t w, uint32_t h, int32_t format) {
259 return NULL;
260 };
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261
262 private:
263 ISurfaceFlingerClient::surface_data_t mParams;
264 };
265
266private:
267 int32_t mIndex;
268
269};
270
271// ---------------------------------------------------------------------------
272
273}; // namespace android
274
275#endif // ANDROID_LAYER_BASE_H