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