blob: 79aaa771bc6b3fa139ad5f5319fbdc23729b85ab [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 Agopiana7f66922010-05-26 22:08:52 -070046 mNeedsFiltering(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 Agopiana7f66922010-05-26 22:08:52 -0700217 mNeedsFiltering = false;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700218 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
219 // we may use linear filtering, if the matrix scales us
220 const uint8_t type = temp.transform.getType();
221 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
Mathias Agopiana7f66922010-05-26 22:08:52 -0700222 mNeedsFiltering = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700223 }
224 }
225 }
226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700228 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 return flags;
230}
231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232void LayerBase::validateVisibility(const Transform& planeTransform)
233{
234 const Layer::State& s(drawingState());
235 const Transform tr(planeTransform * s.transform);
236 const bool transformed = tr.transformed();
237
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700238 uint32_t w = s.w;
239 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 tr.transform(mVertices[0], 0, 0);
241 tr.transform(mVertices[1], 0, h);
242 tr.transform(mVertices[2], w, h);
243 tr.transform(mVertices[3], w, 0);
244 if (UNLIKELY(transformed)) {
245 // NOTE: here we could also punt if we have too many rectangles
246 // in the transparent region
247 if (tr.preserveRects()) {
248 // transform the transparent region
249 transparentRegionScreen = tr.transform(s.transparentRegion);
250 } else {
251 // transformation too complex, can't do the transparent region
252 // optimization.
253 transparentRegionScreen.clear();
254 }
255 } else {
256 transparentRegionScreen = s.transparentRegion;
257 }
258
259 // cache a few things...
260 mOrientation = tr.getOrientation();
261 mTransformedBounds = tr.makeBounds(w, h);
262 mTransformed = transformed;
263 mLeft = tr.tx();
264 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265}
266
267void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
268{
269}
270
271void LayerBase::unlockPageFlip(
272 const Transform& planeTransform, Region& outDirtyRegion)
273{
274 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
275 outDirtyRegion.orSelf(visibleRegionScreen);
276 }
277}
278
279void LayerBase::finishPageFlip()
280{
281}
282
283void LayerBase::invalidate()
284{
285 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
286 mFlinger->signalEvent();
287 }
288}
289
290void LayerBase::drawRegion(const Region& reg) const
291{
Mathias Agopian20f68782009-05-11 00:03:41 -0700292 Region::const_iterator it = reg.begin();
293 Region::const_iterator const end = reg.end();
294 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 Rect r;
296 const DisplayHardware& hw(graphicPlane(0).displayHardware());
297 const int32_t fbWidth = hw.getWidth();
298 const int32_t fbHeight = hw.getHeight();
299 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
300 { fbWidth, fbHeight }, { 0, fbHeight } };
301 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700302 while (it != end) {
303 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 const GLint sy = fbHeight - (r.top + r.height());
305 glScissor(r.left, sy, r.width(), r.height());
306 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
307 }
308 }
309}
310
311void LayerBase::draw(const Region& inClip) const
312{
313 // invalidate the region we'll update
314 Region clip(inClip); // copy-on-write, so no-op most of the time
315
316 // Remove the transparent area from the clipping region
317 const State& s = drawingState();
318 if (LIKELY(!s.transparentRegion.isEmpty())) {
319 clip.subtract(transparentRegionScreen);
320 if (clip.isEmpty()) {
321 // usually this won't happen because this should be taken care of
322 // by SurfaceFlinger::computeVisibleRegions()
323 return;
324 }
325 }
326
327 // reset GL state
328 glEnable(GL_SCISSOR_TEST);
329
330 onDraw(clip);
331
332 /*
333 glDisable(GL_TEXTURE_2D);
334 glDisable(GL_DITHER);
335 glEnable(GL_BLEND);
336 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
337 glColor4x(0, 0x8000, 0, 0x10000);
338 drawRegion(transparentRegionScreen);
339 glDisable(GL_BLEND);
340 */
341}
342
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700343void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
344 GLclampx green, GLclampx blue,
345 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346{
347 const DisplayHardware& hw(graphicPlane(0).displayHardware());
348 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700349 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 glDisable(GL_TEXTURE_2D);
351 glDisable(GL_BLEND);
352 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700353
354 Region::const_iterator it = clip.begin();
355 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700356 glEnable(GL_SCISSOR_TEST);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700357 glVertexPointer(2, GL_FLOAT, 0, mVertices);
Mathias Agopian95a666b2009-09-24 14:57:26 -0700358 while (it != end) {
359 const Rect& r = *it++;
360 const GLint sy = fbHeight - (r.top + r.height());
361 glScissor(r.left, sy, r.width(), r.height());
362 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363 }
364}
365
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700366void LayerBase::clearWithOpenGL(const Region& clip) const
367{
368 clearWithOpenGL(clip,0,0,0,0);
369}
370
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700371void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372{
373 const DisplayHardware& hw(graphicPlane(0).displayHardware());
374 const uint32_t fbHeight = hw.getHeight();
375 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700376
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700378 validateTexture(texture.name);
379 uint32_t width = texture.width;
380 uint32_t height = texture.height;
381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 glEnable(GL_TEXTURE_2D);
383
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 if (UNLIKELY(s.alpha < 0xFF)) {
385 // We have an alpha-modulation. We need to modulate all
386 // texture components by alpha because we're always using
387 // premultiplied alpha.
388
389 // If the texture doesn't have an alpha channel we can
390 // use REPLACE and switch to non premultiplied alpha
391 // blending (SRCA/ONE_MINUS_SRCA).
392
393 GLenum env, src;
394 if (needsBlending()) {
395 env = GL_MODULATE;
396 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
397 } else {
398 env = GL_REPLACE;
399 src = GL_SRC_ALPHA;
400 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700401 const GLfloat alpha = s.alpha * (1.0f/255.0f);
402 glColor4f(alpha, alpha, alpha, alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 glEnable(GL_BLEND);
404 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
405 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
406 } else {
407 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700408 glColor4f(1, 1, 1, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 if (needsBlending()) {
410 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
411 glEnable(GL_BLEND);
412 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
413 } else {
414 glDisable(GL_BLEND);
415 }
416 }
417
Mathias Agopian95a666b2009-09-24 14:57:26 -0700418 Region::const_iterator it = clip.begin();
419 Region::const_iterator const end = clip.end();
Mathias Agopian78fd5012010-04-20 14:51:04 -0700420 const GLfloat texCoords[4][2] = {
421 { 0, 0 },
422 { 0, 1 },
423 { 1, 1 },
424 { 1, 0 }
425 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426
Mathias Agopian78fd5012010-04-20 14:51:04 -0700427 glMatrixMode(GL_TEXTURE);
428 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429
Mathias Agopian78fd5012010-04-20 14:51:04 -0700430 // the texture's source is rotated
431 switch (texture.transform) {
432 case HAL_TRANSFORM_ROT_90:
433 glTranslatef(0, 1, 0);
434 glRotatef(-90, 0, 0, 1);
435 break;
436 case HAL_TRANSFORM_ROT_180:
437 glTranslatef(1, 1, 0);
438 glRotatef(-180, 0, 0, 1);
439 break;
440 case HAL_TRANSFORM_ROT_270:
441 glTranslatef(1, 0, 0);
442 glRotatef(-270, 0, 0, 1);
443 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700445
446 if (texture.NPOTAdjust) {
447 glScalef(texture.wScale, texture.hScale, 1.0f);
448 }
449
450 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
451 glVertexPointer(2, GL_FLOAT, 0, mVertices);
452 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
453
454 while (it != end) {
455 const Rect& r = *it++;
456 const GLint sy = fbHeight - (r.top + r.height());
457 glScissor(r.left, sy, r.width(), r.height());
458 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
459 }
460 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461}
462
463void LayerBase::validateTexture(GLint textureName) const
464{
465 glBindTexture(GL_TEXTURE_2D, textureName);
466 // TODO: reload the texture if needed
467 // this is currently done in loadTexture() below
Mathias Agopiana7f66922010-05-26 22:08:52 -0700468 if (needsFiltering()) {
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700469 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
470 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
471 } else {
472 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
473 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
474 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700475
476 if (needsDithering()) {
477 glEnable(GL_DITHER);
478 } else {
479 glDisable(GL_DITHER);
480 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481}
482
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700483void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
484{
485 const Layer::State& s(drawingState());
486 snprintf(buffer, SIZE,
487 "+ %s %p\n"
488 " "
489 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
490 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
491 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
492 getTypeId(), this, s.z, tx(), ty(), s.w, s.h,
493 needsBlending(), needsDithering(), contentDirty,
494 s.alpha, s.flags,
495 s.transform[0][0], s.transform[0][1],
496 s.transform[1][0], s.transform[1][1]);
497 result.append(buffer);
498}
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500// ---------------------------------------------------------------------------
501
Mathias Agopian631f3582010-05-25 17:51:34 -0700502int32_t LayerBaseClient::sIdentity = 1;
Mathias Agopian2e123242009-06-23 20:06:46 -0700503
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700505 const sp<Client>& client, int32_t i)
Mathias Agopiand606de62010-05-10 20:06:11 -0700506 : LayerBase(flinger, display), client(client), mIndex(i),
Mathias Agopian948d69f2010-03-08 19:29:09 -0800507 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700509}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511void LayerBaseClient::onFirstRef()
512{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700513 sp<Client> client(this->client.promote());
514 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700515 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 }
517}
518
519LayerBaseClient::~LayerBaseClient()
520{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700521 sp<Client> client(this->client.promote());
522 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 client->free(mIndex);
524 }
525}
526
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700527ssize_t LayerBaseClient::serverIndex() const
Mathias Agopianf9d93272009-06-19 17:00:27 -0700528{
529 sp<Client> client(this->client.promote());
530 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 return (client->cid<<16)|mIndex;
532 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700533 return ssize_t(0xFFFF0000 | mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534}
535
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700538 sp<Surface> s;
539 Mutex::Autolock _l(mLock);
540 s = mClientSurface.promote();
541 if (s == 0) {
542 s = createSurface();
543 mClientSurface = s;
544 }
545 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546}
547
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
549{
Mathias Agopian9a112062009-04-17 19:36:26 -0700550 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700551 const_cast<LayerBaseClient *>(this));
552}
553
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700554void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
555{
556 LayerBase::dump(result, buffer, SIZE);
557
558 sp<Client> client(this->client.promote());
559 snprintf(buffer, SIZE,
560 " name=%s\n"
561 " id=0x%08x, client=0x%08x, identity=%u\n",
562 getName().string(),
563 clientIndex(), client.get() ? client->cid : 0,
564 getIdentity());
565
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,
573 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700575 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
576{
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