blob: 21c36e18485e9306cd0e9ab5ee89b978d2312041 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22#include <utils/Log.h>
Mathias Agopian310f8da2009-05-22 01:27:01 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28
29#include <hardware/hardware.h>
30
31#include "clz.h"
32#include "LayerBase.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include "SurfaceFlinger.h"
34#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiand606de62010-05-10 20:06:11 -070035#include "TextureManager.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038namespace android {
39
40// ---------------------------------------------------------------------------
41
Mathias Agopianf6679fc2010-08-10 18:09:09 -070042int32_t LayerBase::sSequence = 1;
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
45 : dpy(display), contentDirty(false),
Mathias Agopianf6679fc2010-08-10 18:09:09 -070046 sequence(uint32_t(android_atomic_inc(&sSequence))),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 mFlinger(flinger),
Mathias Agopiana7f66922010-05-26 22:08:52 -070048 mNeedsFiltering(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 mOrientation(0),
Mathias Agopianca6fab22010-02-19 17:51:58 -080050 mLeft(0), mTop(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 mTransactionFlags(0),
Mathias Agopian245e4d72010-04-21 15:24:11 -070052 mPremultipliedAlpha(true), mName("unnamed"), mDebug(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053 mInvalidate(0)
54{
55 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
56 mFlags = hw.getFlags();
Mathias Agopianb661d662010-08-19 17:01:19 -070057 mBufferCrop.makeInvalid();
58 mBufferTransform = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059}
60
61LayerBase::~LayerBase()
62{
63}
64
Mathias Agopiand1296592010-03-09 19:17:47 -080065void LayerBase::setName(const String8& name) {
66 mName = name;
67}
68
69String8 LayerBase::getName() const {
70 return mName;
71}
72
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073const GraphicPlane& LayerBase::graphicPlane(int dpy) const
74{
75 return mFlinger->graphicPlane(dpy);
76}
77
78GraphicPlane& LayerBase::graphicPlane(int dpy)
79{
80 return mFlinger->graphicPlane(dpy);
81}
82
83void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
84{
85 uint32_t layerFlags = 0;
86 if (flags & ISurfaceComposer::eHidden)
87 layerFlags = ISurfaceComposer::eLayerHidden;
88
89 if (flags & ISurfaceComposer::eNonPremultiplied)
90 mPremultipliedAlpha = false;
91
Mathias Agopian7e4a5872009-09-29 22:39:22 -070092 mCurrentState.z = 0;
93 mCurrentState.w = w;
94 mCurrentState.h = h;
95 mCurrentState.requested_w = w;
96 mCurrentState.requested_h = h;
97 mCurrentState.alpha = 0xFF;
98 mCurrentState.flags = layerFlags;
99 mCurrentState.sequence = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 mCurrentState.transform.set(0, 0);
101
102 // drawing state & current state are identical
103 mDrawingState = mCurrentState;
104}
105
Mathias Agopianba6be542009-09-29 22:32:36 -0700106void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108}
109void LayerBase::forceVisibilityTransaction() {
110 // this can be called without SurfaceFlinger.mStateLock, but if we
111 // can atomically increment the sequence number, it doesn't matter.
112 android_atomic_inc(&mCurrentState.sequence);
113 requestTransaction();
114}
115bool LayerBase::requestTransaction() {
116 int32_t old = setTransactionFlags(eTransactionNeeded);
117 return ((old & eTransactionNeeded) == 0);
118}
119uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
120 return android_atomic_and(~flags, &mTransactionFlags) & flags;
121}
122uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
123 return android_atomic_or(flags, &mTransactionFlags);
124}
125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126bool LayerBase::setPosition(int32_t x, int32_t y) {
127 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
128 return false;
129 mCurrentState.sequence++;
130 mCurrentState.transform.set(x, y);
131 requestTransaction();
132 return true;
133}
134bool LayerBase::setLayer(uint32_t z) {
135 if (mCurrentState.z == z)
136 return false;
137 mCurrentState.sequence++;
138 mCurrentState.z = z;
139 requestTransaction();
140 return true;
141}
142bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700143 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 return false;
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700145 mCurrentState.requested_w = w;
146 mCurrentState.requested_h = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 requestTransaction();
148 return true;
149}
150bool LayerBase::setAlpha(uint8_t alpha) {
151 if (mCurrentState.alpha == alpha)
152 return false;
153 mCurrentState.sequence++;
154 mCurrentState.alpha = alpha;
155 requestTransaction();
156 return true;
157}
158bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 mCurrentState.sequence++;
160 mCurrentState.transform.set(
161 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
162 requestTransaction();
163 return true;
164}
165bool LayerBase::setTransparentRegionHint(const Region& transparent) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 mCurrentState.sequence++;
167 mCurrentState.transparentRegion = transparent;
168 requestTransaction();
169 return true;
170}
171bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
172 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
173 if (mCurrentState.flags == newFlags)
174 return false;
175 mCurrentState.sequence++;
176 mCurrentState.flags = newFlags;
177 requestTransaction();
178 return true;
179}
180
181Rect LayerBase::visibleBounds() const
182{
183 return mTransformedBounds;
184}
185
186void LayerBase::setVisibleRegion(const Region& visibleRegion) {
187 // always called from main thread
188 visibleRegionScreen = visibleRegion;
189}
190
191void LayerBase::setCoveredRegion(const Region& coveredRegion) {
192 // always called from main thread
193 coveredRegionScreen = coveredRegion;
194}
195
196uint32_t LayerBase::doTransaction(uint32_t flags)
197{
198 const Layer::State& front(drawingState());
199 const Layer::State& temp(currentState());
200
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700201 if ((front.requested_w != temp.requested_w) ||
202 (front.requested_h != temp.requested_h)) {
203 // resize the layer, set the physical size to the requested size
204 Layer::State& editTemp(currentState());
205 editTemp.w = temp.requested_w;
206 editTemp.h = temp.requested_h;
207 }
208
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700209 if ((front.w != temp.w) || (front.h != temp.h)) {
210 // invalidate and recompute the visible regions if needed
211 flags |= Layer::eVisibleRegion;
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700212 }
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 if (temp.sequence != front.sequence) {
215 // invalidate and recompute the visible regions if needed
216 flags |= eVisibleRegion;
217 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700218
Mathias Agopian733189d2010-12-02 21:32:29 -0800219 // we may use linear filtering, if the matrix scales us
220 const uint8_t type = temp.transform.getType();
221 mNeedsFiltering = (!temp.transform.preserveRects() ||
222 (type >= Transform::SCALE));
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700223 }
224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700226 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 return flags;
228}
229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230void LayerBase::validateVisibility(const Transform& planeTransform)
231{
232 const Layer::State& s(drawingState());
233 const Transform tr(planeTransform * s.transform);
234 const bool transformed = tr.transformed();
235
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700236 uint32_t w = s.w;
237 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 tr.transform(mVertices[0], 0, 0);
239 tr.transform(mVertices[1], 0, h);
240 tr.transform(mVertices[2], w, h);
241 tr.transform(mVertices[3], w, 0);
242 if (UNLIKELY(transformed)) {
243 // NOTE: here we could also punt if we have too many rectangles
244 // in the transparent region
245 if (tr.preserveRects()) {
246 // transform the transparent region
247 transparentRegionScreen = tr.transform(s.transparentRegion);
248 } else {
249 // transformation too complex, can't do the transparent region
250 // optimization.
251 transparentRegionScreen.clear();
252 }
253 } else {
254 transparentRegionScreen = s.transparentRegion;
255 }
256
257 // cache a few things...
258 mOrientation = tr.getOrientation();
259 mTransformedBounds = tr.makeBounds(w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 mLeft = tr.tx();
261 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262}
263
264void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
265{
266}
267
268void LayerBase::unlockPageFlip(
269 const Transform& planeTransform, Region& outDirtyRegion)
270{
271 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
272 outDirtyRegion.orSelf(visibleRegionScreen);
273 }
274}
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276void LayerBase::invalidate()
277{
278 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
279 mFlinger->signalEvent();
280 }
281}
282
283void LayerBase::drawRegion(const Region& reg) const
284{
Mathias Agopian20f68782009-05-11 00:03:41 -0700285 Region::const_iterator it = reg.begin();
286 Region::const_iterator const end = reg.end();
287 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 Rect r;
289 const DisplayHardware& hw(graphicPlane(0).displayHardware());
290 const int32_t fbWidth = hw.getWidth();
291 const int32_t fbHeight = hw.getHeight();
292 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
293 { fbWidth, fbHeight }, { 0, fbHeight } };
294 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700295 while (it != end) {
296 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 const GLint sy = fbHeight - (r.top + r.height());
298 glScissor(r.left, sy, r.width(), r.height());
299 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
300 }
301 }
302}
303
Mathias Agopianbc7e31a2010-08-10 20:42:20 -0700304void LayerBase::draw(const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 // reset GL state
307 glEnable(GL_SCISSOR_TEST);
308
309 onDraw(clip);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310}
311
Mathias Agopiandf85c452010-09-29 13:02:36 -0700312void LayerBase::drawForSreenShot() const
313{
314 const DisplayHardware& hw(graphicPlane(0).displayHardware());
315 onDraw( Region(hw.bounds()) );
316}
317
Mathias Agopian010fccb2010-05-26 22:26:12 -0700318void LayerBase::clearWithOpenGL(const Region& clip, GLclampf red,
319 GLclampf green, GLclampf blue,
320 GLclampf alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321{
322 const DisplayHardware& hw(graphicPlane(0).displayHardware());
323 const uint32_t fbHeight = hw.getHeight();
Mathias Agopian010fccb2010-05-26 22:26:12 -0700324 glColor4f(red,green,blue,alpha);
Mathias Agopian0a917752010-06-14 21:20:00 -0700325
326 TextureManager::deactivateTextures();
327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 glDisable(GL_BLEND);
329 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700330
331 Region::const_iterator it = clip.begin();
332 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700333 glEnable(GL_SCISSOR_TEST);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700334 glVertexPointer(2, GL_FLOAT, 0, mVertices);
Mathias Agopian95a666b2009-09-24 14:57:26 -0700335 while (it != end) {
336 const Rect& r = *it++;
337 const GLint sy = fbHeight - (r.top + r.height());
338 glScissor(r.left, sy, r.width(), r.height());
339 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 }
341}
342
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700343void LayerBase::clearWithOpenGL(const Region& clip) const
344{
345 clearWithOpenGL(clip,0,0,0,0);
346}
347
Mathias Agopianb661d662010-08-19 17:01:19 -0700348template <typename T>
349static inline
350void swap(T& a, T& b) {
351 T t(a);
352 a = b;
353 b = t;
354}
355
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700356void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357{
358 const DisplayHardware& hw(graphicPlane(0).displayHardware());
359 const uint32_t fbHeight = hw.getHeight();
360 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700361
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 // bind our texture
Mathias Agopian0a917752010-06-14 21:20:00 -0700363 TextureManager::activateTexture(texture, needsFiltering());
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700364 uint32_t width = texture.width;
365 uint32_t height = texture.height;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366
Mathias Agopian49753262010-04-12 15:34:55 -0700367 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368 if (UNLIKELY(s.alpha < 0xFF)) {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700369 const GLfloat alpha = s.alpha * (1.0f/255.0f);
Mathias Agopian49753262010-04-12 15:34:55 -0700370 if (mPremultipliedAlpha) {
371 glColor4f(alpha, alpha, alpha, alpha);
372 } else {
373 glColor4f(1, 1, 1, alpha);
374 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 glEnable(GL_BLEND);
376 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopian49753262010-04-12 15:34:55 -0700377 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 } else {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700379 glColor4f(1, 1, 1, 1);
Mathias Agopian49753262010-04-12 15:34:55 -0700380 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 if (needsBlending()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 glEnable(GL_BLEND);
383 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
384 } else {
385 glDisable(GL_BLEND);
386 }
387 }
388
Mathias Agopianb661d662010-08-19 17:01:19 -0700389 /*
390 * compute texture coordinates
391 * here, we handle NPOT, cropping and buffer transformations
392 */
393
394 GLfloat cl, ct, cr, cb;
395 if (!mBufferCrop.isEmpty()) {
396 // source is cropped
397 const GLfloat us = (texture.NPOTAdjust ? texture.wScale : 1.0f) / width;
398 const GLfloat vs = (texture.NPOTAdjust ? texture.hScale : 1.0f) / height;
399 cl = mBufferCrop.left * us;
400 ct = mBufferCrop.top * vs;
401 cr = mBufferCrop.right * us;
402 cb = mBufferCrop.bottom * vs;
403 } else {
404 cl = 0;
405 ct = 0;
406 cr = (texture.NPOTAdjust ? texture.wScale : 1.0f);
407 cb = (texture.NPOTAdjust ? texture.hScale : 1.0f);
408 }
409
Mathias Agopian883dffa2010-10-25 18:29:35 -0700410 /*
411 * For the buffer transformation, we apply the rotation last.
412 * Since we're transforming the texture-coordinates, we need
413 * to apply the inverse of the buffer transformation:
414 * inverse( FLIP_V -> FLIP_H -> ROT_90 )
415 * <=> inverse( ROT_90 * FLIP_H * FLIP_V )
416 * = inverse(FLIP_V) * inverse(FLIP_H) * inverse(ROT_90)
417 * = FLIP_V * FLIP_H * ROT_270
418 * <=> ROT_270 -> FLIP_H -> FLIP_V
419 *
420 * The rotation is performed first, in the texture coordinate space.
421 *
422 */
423
Mathias Agopianb661d662010-08-19 17:01:19 -0700424 struct TexCoords {
425 GLfloat u;
426 GLfloat v;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700427 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428
Mathias Agopianb661d662010-08-19 17:01:19 -0700429 enum {
430 // name of the corners in the texture map
431 LB = 0, // left-bottom
432 LT = 1, // left-top
433 RT = 2, // right-top
434 RB = 3 // right-bottom
435 };
436
437 // vertices in screen space
438 int vLT = LB;
439 int vLB = LT;
440 int vRB = RT;
441 int vRT = RB;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442
Mathias Agopian78fd5012010-04-20 14:51:04 -0700443 // the texture's source is rotated
Mathias Agopianb661d662010-08-19 17:01:19 -0700444 uint32_t transform = mBufferTransform;
445 if (transform & HAL_TRANSFORM_ROT_90) {
446 vLT = RB;
447 vLB = LB;
448 vRB = LT;
449 vRT = RT;
450 }
451 if (transform & HAL_TRANSFORM_FLIP_V) {
452 swap(vLT, vLB);
Mathias Agopian3c2c54c2010-10-11 14:19:24 -0700453 swap(vRT, vRB);
Mathias Agopianb661d662010-08-19 17:01:19 -0700454 }
455 if (transform & HAL_TRANSFORM_FLIP_H) {
Mathias Agopian3c2c54c2010-10-11 14:19:24 -0700456 swap(vLT, vRT);
457 swap(vLB, vRB);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700459
Mathias Agopianb661d662010-08-19 17:01:19 -0700460 TexCoords texCoords[4];
461 texCoords[vLT].u = cl;
462 texCoords[vLT].v = ct;
463 texCoords[vLB].u = cl;
464 texCoords[vLB].v = cb;
465 texCoords[vRB].u = cr;
466 texCoords[vRB].v = cb;
467 texCoords[vRT].u = cr;
468 texCoords[vRT].v = ct;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700469
Mathias Agopian0a917752010-06-14 21:20:00 -0700470 if (needsDithering()) {
471 glEnable(GL_DITHER);
472 } else {
473 glDisable(GL_DITHER);
474 }
475
Mathias Agopian78fd5012010-04-20 14:51:04 -0700476 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
477 glVertexPointer(2, GL_FLOAT, 0, mVertices);
478 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
479
Mathias Agopianb661d662010-08-19 17:01:19 -0700480 Region::const_iterator it = clip.begin();
481 Region::const_iterator const end = clip.end();
Mathias Agopian78fd5012010-04-20 14:51:04 -0700482 while (it != end) {
483 const Rect& r = *it++;
484 const GLint sy = fbHeight - (r.top + r.height());
485 glScissor(r.left, sy, r.width(), r.height());
486 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
487 }
488 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489}
490
Mathias Agopianb661d662010-08-19 17:01:19 -0700491void LayerBase::setBufferCrop(const Rect& crop) {
492 if (!crop.isEmpty()) {
493 mBufferCrop = crop;
494 }
495}
496
497void LayerBase::setBufferTransform(uint32_t transform) {
498 mBufferTransform = transform;
499}
500
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700501void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
502{
503 const Layer::State& s(drawingState());
504 snprintf(buffer, SIZE,
505 "+ %s %p\n"
506 " "
507 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
508 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
509 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
510 getTypeId(), this, s.z, tx(), ty(), s.w, s.h,
511 needsBlending(), needsDithering(), contentDirty,
512 s.alpha, s.flags,
513 s.transform[0][0], s.transform[0][1],
514 s.transform[1][0], s.transform[1][1]);
515 result.append(buffer);
516}
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700517
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518// ---------------------------------------------------------------------------
519
Mathias Agopian631f3582010-05-25 17:51:34 -0700520int32_t LayerBaseClient::sIdentity = 1;
Mathias Agopian2e123242009-06-23 20:06:46 -0700521
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -0700523 const sp<Client>& client)
Mathias Agopianf7662af2011-03-09 16:13:31 -0800524 : LayerBase(flinger, display), mClientRef(client),
Mathias Agopian948d69f2010-03-08 19:29:09 -0800525 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700527}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529LayerBaseClient::~LayerBaseClient()
530{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700531 sp<Client> c(mClientRef.promote());
Mathias Agopian96f08192010-06-02 23:28:45 -0700532 if (c != 0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700533 c->detachLayer(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534 }
535}
536
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539 sp<Surface> s;
540 Mutex::Autolock _l(mLock);
Mathias Agopianf7662af2011-03-09 16:13:31 -0800541 s = mClientSurface.promote();
542 if (s == 0) {
543 s = createSurface();
544 mClientSurface = s;
545 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547}
548
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700549sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
550{
Mathias Agopian96f08192010-06-02 23:28:45 -0700551 return new Surface(mFlinger, mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700552 const_cast<LayerBaseClient *>(this));
553}
554
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700555void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
556{
557 LayerBase::dump(result, buffer, SIZE);
558
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700559 sp<Client> client(mClientRef.promote());
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700560 snprintf(buffer, SIZE,
561 " name=%s\n"
Mathias Agopian96f08192010-06-02 23:28:45 -0700562 " client=%p, identity=%u\n",
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700563 getName().string(),
Mathias Agopian96f08192010-06-02 23:28:45 -0700564 client.get(), getIdentity());
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700565
566 result.append(buffer);
567}
568
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700569// ---------------------------------------------------------------------------
570
Mathias Agopian9a112062009-04-17 19:36:26 -0700571LayerBaseClient::Surface::Surface(
572 const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700573 int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 const sp<LayerBaseClient>& owner)
Mathias Agopian96f08192010-06-02 23:28:45 -0700575 : mFlinger(flinger), mIdentity(identity), mOwner(owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700576{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577}
578
Mathias Agopian9a112062009-04-17 19:36:26 -0700579LayerBaseClient::Surface::~Surface()
580{
581 /*
582 * This is a good place to clean-up all client resources
583 */
584
585 // destroy client resources
586 sp<LayerBaseClient> layer = getOwner();
587 if (layer != 0) {
588 mFlinger->destroySurface(layer);
589 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590}
591
592sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
593 sp<LayerBaseClient> owner(mOwner.promote());
594 return owner;
595}
596
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700597status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700598 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599{
600 switch (code) {
601 case REGISTER_BUFFERS:
602 case UNREGISTER_BUFFERS:
603 case CREATE_OVERLAY:
604 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700605 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
606 IPCThreadState* ipc = IPCThreadState::self();
607 const int pid = ipc->getCallingPid();
608 const int uid = ipc->getCallingUid();
609 LOGE("Permission Denial: "
610 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
611 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612 }
613 }
614 }
615 return BnSurface::onTransact(code, data, reply, flags);
616}
617
Mathias Agopiana138f892010-05-21 17:24:35 -0700618sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int bufferIdx,
619 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620{
621 return NULL;
622}
623
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700624status_t LayerBaseClient::Surface::setBufferCount(int bufferCount)
625{
626 return INVALID_OPERATION;
627}
628
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700629status_t LayerBaseClient::Surface::registerBuffers(
630 const ISurface::BufferHeap& buffers)
631{
632 return INVALID_OPERATION;
633}
634
635void LayerBaseClient::Surface::postBuffer(ssize_t offset)
636{
637}
638
639void LayerBaseClient::Surface::unregisterBuffers()
640{
641}
642
643sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800644 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645{
646 return NULL;
647};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648
649// ---------------------------------------------------------------------------
650
651}; // namespace android