blob: 51673ffc36c43b385c5ec388d666197aa1a9f37b [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
43 : dpy(display), contentDirty(false),
44 mFlinger(flinger),
45 mTransformed(false),
Mathias Agopiana2fe0a22009-09-23 18:34:53 -070046 mUseLinearFiltering(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 mOrientation(0),
Mathias Agopianca6fab22010-02-19 17:51:58 -080048 mLeft(0), mTop(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 mTransactionFlags(0),
Mathias Agopian245e4d72010-04-21 15:24:11 -070050 mPremultipliedAlpha(true), mName("unnamed"), mDebug(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 mInvalidate(0)
52{
53 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
54 mFlags = hw.getFlags();
55}
56
57LayerBase::~LayerBase()
58{
59}
60
Mathias Agopiand1296592010-03-09 19:17:47 -080061void LayerBase::setName(const String8& name) {
62 mName = name;
63}
64
65String8 LayerBase::getName() const {
66 return mName;
67}
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069const GraphicPlane& LayerBase::graphicPlane(int dpy) const
70{
71 return mFlinger->graphicPlane(dpy);
72}
73
74GraphicPlane& LayerBase::graphicPlane(int dpy)
75{
76 return mFlinger->graphicPlane(dpy);
77}
78
79void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
80{
81 uint32_t layerFlags = 0;
82 if (flags & ISurfaceComposer::eHidden)
83 layerFlags = ISurfaceComposer::eLayerHidden;
84
85 if (flags & ISurfaceComposer::eNonPremultiplied)
86 mPremultipliedAlpha = false;
87
Mathias Agopian7e4a5872009-09-29 22:39:22 -070088 mCurrentState.z = 0;
89 mCurrentState.w = w;
90 mCurrentState.h = h;
91 mCurrentState.requested_w = w;
92 mCurrentState.requested_h = h;
93 mCurrentState.alpha = 0xFF;
94 mCurrentState.flags = layerFlags;
95 mCurrentState.sequence = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 mCurrentState.transform.set(0, 0);
97
98 // drawing state & current state are identical
99 mDrawingState = mCurrentState;
100}
101
Mathias Agopianba6be542009-09-29 22:32:36 -0700102void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104}
105void LayerBase::forceVisibilityTransaction() {
106 // this can be called without SurfaceFlinger.mStateLock, but if we
107 // can atomically increment the sequence number, it doesn't matter.
108 android_atomic_inc(&mCurrentState.sequence);
109 requestTransaction();
110}
111bool LayerBase::requestTransaction() {
112 int32_t old = setTransactionFlags(eTransactionNeeded);
113 return ((old & eTransactionNeeded) == 0);
114}
115uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
116 return android_atomic_and(~flags, &mTransactionFlags) & flags;
117}
118uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
119 return android_atomic_or(flags, &mTransactionFlags);
120}
121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122bool LayerBase::setPosition(int32_t x, int32_t y) {
123 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
124 return false;
125 mCurrentState.sequence++;
126 mCurrentState.transform.set(x, y);
127 requestTransaction();
128 return true;
129}
130bool LayerBase::setLayer(uint32_t z) {
131 if (mCurrentState.z == z)
132 return false;
133 mCurrentState.sequence++;
134 mCurrentState.z = z;
135 requestTransaction();
136 return true;
137}
138bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700139 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 return false;
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700141 mCurrentState.requested_w = w;
142 mCurrentState.requested_h = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 requestTransaction();
144 return true;
145}
146bool LayerBase::setAlpha(uint8_t alpha) {
147 if (mCurrentState.alpha == alpha)
148 return false;
149 mCurrentState.sequence++;
150 mCurrentState.alpha = alpha;
151 requestTransaction();
152 return true;
153}
154bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
155 // TODO: check the matrix has changed
156 mCurrentState.sequence++;
157 mCurrentState.transform.set(
158 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
159 requestTransaction();
160 return true;
161}
162bool LayerBase::setTransparentRegionHint(const Region& transparent) {
163 // TODO: check the region has changed
164 mCurrentState.sequence++;
165 mCurrentState.transparentRegion = transparent;
166 requestTransaction();
167 return true;
168}
169bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
170 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
171 if (mCurrentState.flags == newFlags)
172 return false;
173 mCurrentState.sequence++;
174 mCurrentState.flags = newFlags;
175 requestTransaction();
176 return true;
177}
178
179Rect LayerBase::visibleBounds() const
180{
181 return mTransformedBounds;
182}
183
184void LayerBase::setVisibleRegion(const Region& visibleRegion) {
185 // always called from main thread
186 visibleRegionScreen = visibleRegion;
187}
188
189void LayerBase::setCoveredRegion(const Region& coveredRegion) {
190 // always called from main thread
191 coveredRegionScreen = coveredRegion;
192}
193
194uint32_t LayerBase::doTransaction(uint32_t flags)
195{
196 const Layer::State& front(drawingState());
197 const Layer::State& temp(currentState());
198
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700199 if ((front.requested_w != temp.requested_w) ||
200 (front.requested_h != temp.requested_h)) {
201 // resize the layer, set the physical size to the requested size
202 Layer::State& editTemp(currentState());
203 editTemp.w = temp.requested_w;
204 editTemp.h = temp.requested_h;
205 }
206
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700207 if ((front.w != temp.w) || (front.h != temp.h)) {
208 // invalidate and recompute the visible regions if needed
209 flags |= Layer::eVisibleRegion;
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700210 }
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 if (temp.sequence != front.sequence) {
213 // invalidate and recompute the visible regions if needed
214 flags |= eVisibleRegion;
215 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700216
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700217 const bool linearFiltering = mUseLinearFiltering;
218 mUseLinearFiltering = false;
219 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
220 // we may use linear filtering, if the matrix scales us
221 const uint8_t type = temp.transform.getType();
222 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
223 mUseLinearFiltering = true;
224 }
225 }
226 }
227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700229 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 return flags;
231}
232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233void LayerBase::validateVisibility(const Transform& planeTransform)
234{
235 const Layer::State& s(drawingState());
236 const Transform tr(planeTransform * s.transform);
237 const bool transformed = tr.transformed();
238
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700239 uint32_t w = s.w;
240 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 tr.transform(mVertices[0], 0, 0);
242 tr.transform(mVertices[1], 0, h);
243 tr.transform(mVertices[2], w, h);
244 tr.transform(mVertices[3], w, 0);
245 if (UNLIKELY(transformed)) {
246 // NOTE: here we could also punt if we have too many rectangles
247 // in the transparent region
248 if (tr.preserveRects()) {
249 // transform the transparent region
250 transparentRegionScreen = tr.transform(s.transparentRegion);
251 } else {
252 // transformation too complex, can't do the transparent region
253 // optimization.
254 transparentRegionScreen.clear();
255 }
256 } else {
257 transparentRegionScreen = s.transparentRegion;
258 }
259
260 // cache a few things...
261 mOrientation = tr.getOrientation();
262 mTransformedBounds = tr.makeBounds(w, h);
263 mTransformed = transformed;
264 mLeft = tr.tx();
265 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266}
267
268void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
269{
270}
271
272void LayerBase::unlockPageFlip(
273 const Transform& planeTransform, Region& outDirtyRegion)
274{
275 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
276 outDirtyRegion.orSelf(visibleRegionScreen);
277 }
278}
279
280void LayerBase::finishPageFlip()
281{
282}
283
284void LayerBase::invalidate()
285{
286 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
287 mFlinger->signalEvent();
288 }
289}
290
291void LayerBase::drawRegion(const Region& reg) const
292{
Mathias Agopian20f68782009-05-11 00:03:41 -0700293 Region::const_iterator it = reg.begin();
294 Region::const_iterator const end = reg.end();
295 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 Rect r;
297 const DisplayHardware& hw(graphicPlane(0).displayHardware());
298 const int32_t fbWidth = hw.getWidth();
299 const int32_t fbHeight = hw.getHeight();
300 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
301 { fbWidth, fbHeight }, { 0, fbHeight } };
302 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700303 while (it != end) {
304 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 const GLint sy = fbHeight - (r.top + r.height());
306 glScissor(r.left, sy, r.width(), r.height());
307 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
308 }
309 }
310}
311
312void LayerBase::draw(const Region& inClip) const
313{
314 // invalidate the region we'll update
315 Region clip(inClip); // copy-on-write, so no-op most of the time
316
317 // Remove the transparent area from the clipping region
318 const State& s = drawingState();
319 if (LIKELY(!s.transparentRegion.isEmpty())) {
320 clip.subtract(transparentRegionScreen);
321 if (clip.isEmpty()) {
322 // usually this won't happen because this should be taken care of
323 // by SurfaceFlinger::computeVisibleRegions()
324 return;
325 }
326 }
327
328 // reset GL state
329 glEnable(GL_SCISSOR_TEST);
330
331 onDraw(clip);
332
333 /*
334 glDisable(GL_TEXTURE_2D);
335 glDisable(GL_DITHER);
336 glEnable(GL_BLEND);
337 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
338 glColor4x(0, 0x8000, 0, 0x10000);
339 drawRegion(transparentRegionScreen);
340 glDisable(GL_BLEND);
341 */
342}
343
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700344void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
345 GLclampx green, GLclampx blue,
346 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347{
348 const DisplayHardware& hw(graphicPlane(0).displayHardware());
349 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700350 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 glDisable(GL_TEXTURE_2D);
352 glDisable(GL_BLEND);
353 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700354
355 Region::const_iterator it = clip.begin();
356 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700357 glEnable(GL_SCISSOR_TEST);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700358 glVertexPointer(2, GL_FLOAT, 0, mVertices);
Mathias Agopian95a666b2009-09-24 14:57:26 -0700359 while (it != end) {
360 const Rect& r = *it++;
361 const GLint sy = fbHeight - (r.top + r.height());
362 glScissor(r.left, sy, r.width(), r.height());
363 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 }
365}
366
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700367void LayerBase::clearWithOpenGL(const Region& clip) const
368{
369 clearWithOpenGL(clip,0,0,0,0);
370}
371
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700372void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373{
374 const DisplayHardware& hw(graphicPlane(0).displayHardware());
375 const uint32_t fbHeight = hw.getHeight();
376 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700377
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700379 validateTexture(texture.name);
380 uint32_t width = texture.width;
381 uint32_t height = texture.height;
382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 glEnable(GL_TEXTURE_2D);
384
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 if (UNLIKELY(s.alpha < 0xFF)) {
386 // We have an alpha-modulation. We need to modulate all
387 // texture components by alpha because we're always using
388 // premultiplied alpha.
389
390 // If the texture doesn't have an alpha channel we can
391 // use REPLACE and switch to non premultiplied alpha
392 // blending (SRCA/ONE_MINUS_SRCA).
393
394 GLenum env, src;
395 if (needsBlending()) {
396 env = GL_MODULATE;
397 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
398 } else {
399 env = GL_REPLACE;
400 src = GL_SRC_ALPHA;
401 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700402 const GLfloat alpha = s.alpha * (1.0f/255.0f);
403 glColor4f(alpha, alpha, alpha, alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 glEnable(GL_BLEND);
405 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
406 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
407 } else {
408 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700409 glColor4f(1, 1, 1, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 if (needsBlending()) {
411 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
412 glEnable(GL_BLEND);
413 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
414 } else {
415 glDisable(GL_BLEND);
416 }
417 }
418
Mathias Agopian95a666b2009-09-24 14:57:26 -0700419 Region::const_iterator it = clip.begin();
420 Region::const_iterator const end = clip.end();
Mathias Agopian78fd5012010-04-20 14:51:04 -0700421 const GLfloat texCoords[4][2] = {
422 { 0, 0 },
423 { 0, 1 },
424 { 1, 1 },
425 { 1, 0 }
426 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427
Mathias Agopian78fd5012010-04-20 14:51:04 -0700428 glMatrixMode(GL_TEXTURE);
429 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430
Mathias Agopian78fd5012010-04-20 14:51:04 -0700431 // the texture's source is rotated
432 switch (texture.transform) {
433 case HAL_TRANSFORM_ROT_90:
434 glTranslatef(0, 1, 0);
435 glRotatef(-90, 0, 0, 1);
436 break;
437 case HAL_TRANSFORM_ROT_180:
438 glTranslatef(1, 1, 0);
439 glRotatef(-180, 0, 0, 1);
440 break;
441 case HAL_TRANSFORM_ROT_270:
442 glTranslatef(1, 0, 0);
443 glRotatef(-270, 0, 0, 1);
444 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700446
447 if (texture.NPOTAdjust) {
448 glScalef(texture.wScale, texture.hScale, 1.0f);
449 }
450
451 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
452 glVertexPointer(2, GL_FLOAT, 0, mVertices);
453 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
454
455 while (it != end) {
456 const Rect& r = *it++;
457 const GLint sy = fbHeight - (r.top + r.height());
458 glScissor(r.left, sy, r.width(), r.height());
459 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
460 }
461 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462}
463
464void LayerBase::validateTexture(GLint textureName) const
465{
466 glBindTexture(GL_TEXTURE_2D, textureName);
467 // TODO: reload the texture if needed
468 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700469 if (mUseLinearFiltering) {
470 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
471 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
472 } else {
473 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
474 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
475 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700476
477 if (needsDithering()) {
478 glEnable(GL_DITHER);
479 } else {
480 glDisable(GL_DITHER);
481 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482}
483
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700484void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
485{
486 const Layer::State& s(drawingState());
487 snprintf(buffer, SIZE,
488 "+ %s %p\n"
489 " "
490 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
491 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
492 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
493 getTypeId(), this, s.z, tx(), ty(), s.w, s.h,
494 needsBlending(), needsDithering(), contentDirty,
495 s.alpha, s.flags,
496 s.transform[0][0], s.transform[0][1],
497 s.transform[1][0], s.transform[1][1]);
498 result.append(buffer);
499}
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700500
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501// ---------------------------------------------------------------------------
502
Mathias Agopian2e123242009-06-23 20:06:46 -0700503int32_t LayerBaseClient::sIdentity = 0;
504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700506 const sp<Client>& client, int32_t i)
Mathias Agopiand606de62010-05-10 20:06:11 -0700507 : LayerBase(flinger, display), client(client), mIndex(i),
Mathias Agopian948d69f2010-03-08 19:29:09 -0800508 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700510}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700512void LayerBaseClient::onFirstRef()
513{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700514 sp<Client> client(this->client.promote());
515 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700516 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 }
518}
519
520LayerBaseClient::~LayerBaseClient()
521{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700522 sp<Client> client(this->client.promote());
523 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 client->free(mIndex);
525 }
526}
527
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700528ssize_t LayerBaseClient::serverIndex() const
Mathias Agopianf9d93272009-06-19 17:00:27 -0700529{
530 sp<Client> client(this->client.promote());
531 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 return (client->cid<<16)|mIndex;
533 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700534 return ssize_t(0xFFFF0000 | mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535}
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);
541 s = mClientSurface.promote();
542 if (s == 0) {
543 s = createSurface();
544 mClientSurface = s;
545 }
546 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 Agopian9a112062009-04-17 19:36:26 -0700551 return new Surface(mFlinger, clientIndex(), 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
559 sp<Client> client(this->client.promote());
560 snprintf(buffer, SIZE,
561 " name=%s\n"
562 " id=0x%08x, client=0x%08x, identity=%u\n",
563 getName().string(),
564 clientIndex(), client.get() ? client->cid : 0,
565 getIdentity());
566
567 result.append(buffer);
568}
569
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570// ---------------------------------------------------------------------------
571
Mathias Agopian9a112062009-04-17 19:36:26 -0700572LayerBaseClient::Surface::Surface(
573 const sp<SurfaceFlinger>& flinger,
574 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700575 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700576 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
577{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700578}
579
Mathias Agopian9a112062009-04-17 19:36:26 -0700580LayerBaseClient::Surface::~Surface()
581{
582 /*
583 * This is a good place to clean-up all client resources
584 */
585
586 // destroy client resources
587 sp<LayerBaseClient> layer = getOwner();
588 if (layer != 0) {
589 mFlinger->destroySurface(layer);
590 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700591}
592
593sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
594 sp<LayerBaseClient> owner(mOwner.promote());
595 return owner;
596}
597
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700598status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700599 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700600{
601 switch (code) {
602 case REGISTER_BUFFERS:
603 case UNREGISTER_BUFFERS:
604 case CREATE_OVERLAY:
605 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700606 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
607 IPCThreadState* ipc = IPCThreadState::self();
608 const int pid = ipc->getCallingPid();
609 const int uid = ipc->getCallingUid();
610 LOGE("Permission Denial: "
611 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
612 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700613 }
614 }
615 }
616 return BnSurface::onTransact(code, data, reply, flags);
617}
618
Mathias Agopiana138f892010-05-21 17:24:35 -0700619sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int bufferIdx,
620 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700621{
622 return NULL;
623}
624
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700625status_t LayerBaseClient::Surface::setBufferCount(int bufferCount)
626{
627 return INVALID_OPERATION;
628}
629
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700630status_t LayerBaseClient::Surface::registerBuffers(
631 const ISurface::BufferHeap& buffers)
632{
633 return INVALID_OPERATION;
634}
635
636void LayerBaseClient::Surface::postBuffer(ssize_t offset)
637{
638}
639
640void LayerBaseClient::Surface::unregisterBuffers()
641{
642}
643
644sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800645 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700646{
647 return NULL;
648};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649
650// ---------------------------------------------------------------------------
651
652}; // namespace android