blob: 7ab74b42765d2ad1972d37f06f4fa4a515dc2d5c [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopian947f4f42009-05-22 01:27:01 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080033#include "SurfaceFlinger.h"
34#include "DisplayHardware/DisplayHardware.h"
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070035#include "TextureManager.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038namespace android {
39
40// ---------------------------------------------------------------------------
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
43 : dpy(display), contentDirty(false),
44 mFlinger(flinger),
Mathias Agopian92377032010-05-26 22:08:52 -070045 mNeedsFiltering(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 mOrientation(0),
Mathias Agopianed2ab7f2010-02-19 17:51:58 -080047 mLeft(0), mTop(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 mTransactionFlags(0),
Mathias Agopiana8a0aa82010-04-21 15:24:11 -070049 mPremultipliedAlpha(true), mName("unnamed"), mDebug(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 mInvalidate(0)
51{
52 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
53 mFlags = hw.getFlags();
54}
55
56LayerBase::~LayerBase()
57{
58}
59
Mathias Agopian015b5972010-03-09 19:17:47 -080060void LayerBase::setName(const String8& name) {
61 mName = name;
62}
63
64String8 LayerBase::getName() const {
65 return mName;
66}
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068const GraphicPlane& LayerBase::graphicPlane(int dpy) const
69{
70 return mFlinger->graphicPlane(dpy);
71}
72
73GraphicPlane& LayerBase::graphicPlane(int dpy)
74{
75 return mFlinger->graphicPlane(dpy);
76}
77
78void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
79{
80 uint32_t layerFlags = 0;
81 if (flags & ISurfaceComposer::eHidden)
82 layerFlags = ISurfaceComposer::eLayerHidden;
83
84 if (flags & ISurfaceComposer::eNonPremultiplied)
85 mPremultipliedAlpha = false;
86
Mathias Agopiane1b6f242009-09-29 22:39:22 -070087 mCurrentState.z = 0;
88 mCurrentState.w = w;
89 mCurrentState.h = h;
90 mCurrentState.requested_w = w;
91 mCurrentState.requested_h = h;
92 mCurrentState.alpha = 0xFF;
93 mCurrentState.flags = layerFlags;
94 mCurrentState.sequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 mCurrentState.transform.set(0, 0);
96
97 // drawing state & current state are identical
98 mDrawingState = mCurrentState;
99}
100
Mathias Agopian88516172009-09-29 22:32:36 -0700101void LayerBase::commitTransaction() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 mDrawingState = mCurrentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103}
104void LayerBase::forceVisibilityTransaction() {
105 // this can be called without SurfaceFlinger.mStateLock, but if we
106 // can atomically increment the sequence number, it doesn't matter.
107 android_atomic_inc(&mCurrentState.sequence);
108 requestTransaction();
109}
110bool LayerBase::requestTransaction() {
111 int32_t old = setTransactionFlags(eTransactionNeeded);
112 return ((old & eTransactionNeeded) == 0);
113}
114uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
115 return android_atomic_and(~flags, &mTransactionFlags) & flags;
116}
117uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
118 return android_atomic_or(flags, &mTransactionFlags);
119}
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121bool LayerBase::setPosition(int32_t x, int32_t y) {
122 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
123 return false;
124 mCurrentState.sequence++;
125 mCurrentState.transform.set(x, y);
126 requestTransaction();
127 return true;
128}
129bool LayerBase::setLayer(uint32_t z) {
130 if (mCurrentState.z == z)
131 return false;
132 mCurrentState.sequence++;
133 mCurrentState.z = z;
134 requestTransaction();
135 return true;
136}
137bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700138 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 return false;
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700140 mCurrentState.requested_w = w;
141 mCurrentState.requested_h = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 requestTransaction();
143 return true;
144}
145bool LayerBase::setAlpha(uint8_t alpha) {
146 if (mCurrentState.alpha == alpha)
147 return false;
148 mCurrentState.sequence++;
149 mCurrentState.alpha = alpha;
150 requestTransaction();
151 return true;
152}
153bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
154 // TODO: check the matrix has changed
155 mCurrentState.sequence++;
156 mCurrentState.transform.set(
157 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
158 requestTransaction();
159 return true;
160}
161bool LayerBase::setTransparentRegionHint(const Region& transparent) {
162 // TODO: check the region has changed
163 mCurrentState.sequence++;
164 mCurrentState.transparentRegion = transparent;
165 requestTransaction();
166 return true;
167}
168bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
169 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
170 if (mCurrentState.flags == newFlags)
171 return false;
172 mCurrentState.sequence++;
173 mCurrentState.flags = newFlags;
174 requestTransaction();
175 return true;
176}
177
178Rect LayerBase::visibleBounds() const
179{
180 return mTransformedBounds;
181}
182
183void LayerBase::setVisibleRegion(const Region& visibleRegion) {
184 // always called from main thread
185 visibleRegionScreen = visibleRegion;
186}
187
188void LayerBase::setCoveredRegion(const Region& coveredRegion) {
189 // always called from main thread
190 coveredRegionScreen = coveredRegion;
191}
192
193uint32_t LayerBase::doTransaction(uint32_t flags)
194{
195 const Layer::State& front(drawingState());
196 const Layer::State& temp(currentState());
197
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700198 if ((front.requested_w != temp.requested_w) ||
199 (front.requested_h != temp.requested_h)) {
200 // resize the layer, set the physical size to the requested size
201 Layer::State& editTemp(currentState());
202 editTemp.w = temp.requested_w;
203 editTemp.h = temp.requested_h;
204 }
205
Mathias Agopian70cab912009-09-30 12:48:47 -0700206 if ((front.w != temp.w) || (front.h != temp.h)) {
207 // invalidate and recompute the visible regions if needed
208 flags |= Layer::eVisibleRegion;
Mathias Agopian70cab912009-09-30 12:48:47 -0700209 }
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 if (temp.sequence != front.sequence) {
212 // invalidate and recompute the visible regions if needed
213 flags |= eVisibleRegion;
214 this->contentDirty = true;
Mathias Agopian44cac132009-09-23 18:34:53 -0700215
Mathias Agopian92377032010-05-26 22:08:52 -0700216 mNeedsFiltering = false;
Mathias Agopian44cac132009-09-23 18:34:53 -0700217 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
218 // we may use linear filtering, if the matrix scales us
219 const uint8_t type = temp.transform.getType();
220 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
Mathias Agopian92377032010-05-26 22:08:52 -0700221 mNeedsFiltering = true;
Mathias Agopian44cac132009-09-23 18:34:53 -0700222 }
223 }
224 }
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 // Commit the transaction
Mathias Agopian88516172009-09-29 22:32:36 -0700227 commitTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 return flags;
229}
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231void LayerBase::validateVisibility(const Transform& planeTransform)
232{
233 const Layer::State& s(drawingState());
234 const Transform tr(planeTransform * s.transform);
235 const bool transformed = tr.transformed();
236
Mathias Agopian9779b222009-09-07 16:32:45 -0700237 uint32_t w = s.w;
238 uint32_t h = s.h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 tr.transform(mVertices[0], 0, 0);
240 tr.transform(mVertices[1], 0, h);
241 tr.transform(mVertices[2], w, h);
242 tr.transform(mVertices[3], w, 0);
243 if (UNLIKELY(transformed)) {
244 // NOTE: here we could also punt if we have too many rectangles
245 // in the transparent region
246 if (tr.preserveRects()) {
247 // transform the transparent region
248 transparentRegionScreen = tr.transform(s.transparentRegion);
249 } else {
250 // transformation too complex, can't do the transparent region
251 // optimization.
252 transparentRegionScreen.clear();
253 }
254 } else {
255 transparentRegionScreen = s.transparentRegion;
256 }
257
258 // cache a few things...
259 mOrientation = tr.getOrientation();
260 mTransformedBounds = tr.makeBounds(w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 mLeft = tr.tx();
262 mTop = tr.ty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263}
264
265void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
266{
267}
268
269void LayerBase::unlockPageFlip(
270 const Transform& planeTransform, Region& outDirtyRegion)
271{
272 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
273 outDirtyRegion.orSelf(visibleRegionScreen);
274 }
275}
276
277void LayerBase::finishPageFlip()
278{
279}
280
281void LayerBase::invalidate()
282{
283 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
284 mFlinger->signalEvent();
285 }
286}
287
288void LayerBase::drawRegion(const Region& reg) const
289{
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700290 Region::const_iterator it = reg.begin();
291 Region::const_iterator const end = reg.end();
292 if (it != end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 Rect r;
294 const DisplayHardware& hw(graphicPlane(0).displayHardware());
295 const int32_t fbWidth = hw.getWidth();
296 const int32_t fbHeight = hw.getHeight();
297 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
298 { fbWidth, fbHeight }, { 0, fbHeight } };
299 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700300 while (it != end) {
301 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 const GLint sy = fbHeight - (r.top + r.height());
303 glScissor(r.left, sy, r.width(), r.height());
304 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
305 }
306 }
307}
308
309void LayerBase::draw(const Region& inClip) const
310{
311 // invalidate the region we'll update
312 Region clip(inClip); // copy-on-write, so no-op most of the time
313
314 // Remove the transparent area from the clipping region
315 const State& s = drawingState();
316 if (LIKELY(!s.transparentRegion.isEmpty())) {
317 clip.subtract(transparentRegionScreen);
318 if (clip.isEmpty()) {
319 // usually this won't happen because this should be taken care of
320 // by SurfaceFlinger::computeVisibleRegions()
321 return;
322 }
323 }
324
325 // reset GL state
326 glEnable(GL_SCISSOR_TEST);
327
328 onDraw(clip);
329
330 /*
331 glDisable(GL_TEXTURE_2D);
332 glDisable(GL_DITHER);
333 glEnable(GL_BLEND);
334 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
335 glColor4x(0, 0x8000, 0, 0x10000);
336 drawRegion(transparentRegionScreen);
337 glDisable(GL_BLEND);
338 */
339}
340
Mathias Agopian0d3c0062010-05-26 22:26:12 -0700341void LayerBase::clearWithOpenGL(const Region& clip, GLclampf red,
342 GLclampf green, GLclampf blue,
343 GLclampf alpha) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344{
345 const DisplayHardware& hw(graphicPlane(0).displayHardware());
346 const uint32_t fbHeight = hw.getHeight();
Mathias Agopian0d3c0062010-05-26 22:26:12 -0700347 glColor4f(red,green,blue,alpha);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 glDisable(GL_TEXTURE_2D);
349 glDisable(GL_BLEND);
350 glDisable(GL_DITHER);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700351
352 Region::const_iterator it = clip.begin();
353 Region::const_iterator const end = clip.end();
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700354 glEnable(GL_SCISSOR_TEST);
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700355 glVertexPointer(2, GL_FLOAT, 0, mVertices);
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700356 while (it != end) {
357 const Rect& r = *it++;
358 const GLint sy = fbHeight - (r.top + r.height());
359 glScissor(r.left, sy, r.width(), r.height());
360 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362}
363
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700364void LayerBase::clearWithOpenGL(const Region& clip) const
365{
366 clearWithOpenGL(clip,0,0,0,0);
367}
368
Mathias Agopian999543b2009-06-23 18:08:22 -0700369void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370{
371 const DisplayHardware& hw(graphicPlane(0).displayHardware());
372 const uint32_t fbHeight = hw.getHeight();
373 const State& s(drawingState());
Mathias Agopiandff8e582009-05-04 14:17:04 -0700374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 // bind our texture
Mathias Agopian999543b2009-06-23 18:08:22 -0700376 validateTexture(texture.name);
377 uint32_t width = texture.width;
378 uint32_t height = texture.height;
379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 glEnable(GL_TEXTURE_2D);
381
Mathias Agopiana0612e42010-04-12 15:34:55 -0700382 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 if (UNLIKELY(s.alpha < 0xFF)) {
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700384 const GLfloat alpha = s.alpha * (1.0f/255.0f);
Mathias Agopiana0612e42010-04-12 15:34:55 -0700385 if (mPremultipliedAlpha) {
386 glColor4f(alpha, alpha, alpha, alpha);
387 } else {
388 glColor4f(1, 1, 1, alpha);
389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 glEnable(GL_BLEND);
391 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana0612e42010-04-12 15:34:55 -0700392 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 } else {
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700394 glColor4f(1, 1, 1, 1);
Mathias Agopiana0612e42010-04-12 15:34:55 -0700395 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 if (needsBlending()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 glEnable(GL_BLEND);
398 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
399 } else {
400 glDisable(GL_BLEND);
401 }
402 }
403
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700404 Region::const_iterator it = clip.begin();
405 Region::const_iterator const end = clip.end();
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700406 const GLfloat texCoords[4][2] = {
407 { 0, 0 },
408 { 0, 1 },
409 { 1, 1 },
410 { 1, 0 }
411 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700413 glMatrixMode(GL_TEXTURE);
414 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700416 // the texture's source is rotated
417 switch (texture.transform) {
418 case HAL_TRANSFORM_ROT_90:
419 glTranslatef(0, 1, 0);
420 glRotatef(-90, 0, 0, 1);
421 break;
422 case HAL_TRANSFORM_ROT_180:
423 glTranslatef(1, 1, 0);
424 glRotatef(-180, 0, 0, 1);
425 break;
426 case HAL_TRANSFORM_ROT_270:
427 glTranslatef(1, 0, 0);
428 glRotatef(-270, 0, 0, 1);
429 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700431
432 if (texture.NPOTAdjust) {
433 glScalef(texture.wScale, texture.hScale, 1.0f);
434 }
435
436 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
437 glVertexPointer(2, GL_FLOAT, 0, mVertices);
438 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
439
440 while (it != end) {
441 const Rect& r = *it++;
442 const GLint sy = fbHeight - (r.top + r.height());
443 glScissor(r.left, sy, r.width(), r.height());
444 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
445 }
446 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447}
448
449void LayerBase::validateTexture(GLint textureName) const
450{
451 glBindTexture(GL_TEXTURE_2D, textureName);
452 // TODO: reload the texture if needed
453 // this is currently done in loadTexture() below
Mathias Agopian92377032010-05-26 22:08:52 -0700454 if (needsFiltering()) {
Mathias Agopian44cac132009-09-23 18:34:53 -0700455 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
456 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
457 } else {
458 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
459 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
460 }
Mathias Agopiancc934762009-09-23 19:16:27 -0700461
462 if (needsDithering()) {
463 glEnable(GL_DITHER);
464 } else {
465 glDisable(GL_DITHER);
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467}
468
Mathias Agopian9bce8732010-04-20 17:55:49 -0700469void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
470{
471 const Layer::State& s(drawingState());
472 snprintf(buffer, SIZE,
473 "+ %s %p\n"
474 " "
475 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
476 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
477 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
478 getTypeId(), this, s.z, tx(), ty(), s.w, s.h,
479 needsBlending(), needsDithering(), contentDirty,
480 s.alpha, s.flags,
481 s.transform[0][0], s.transform[0][1],
482 s.transform[1][0], s.transform[1][1]);
483 result.append(buffer);
484}
Mathias Agopian9042b452009-10-26 20:12:37 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486// ---------------------------------------------------------------------------
487
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700488int32_t LayerBaseClient::sIdentity = 1;
Mathias Agopianc6603952009-06-23 20:06:46 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700491 const sp<Client>& client, int32_t i)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700492 : LayerBase(flinger, display), client(client), mIndex(i),
Mathias Agopian85b8d122010-03-08 19:29:09 -0800493 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494{
Mathias Agopian1473f462009-04-10 14:24:30 -0700495}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
Mathias Agopian1473f462009-04-10 14:24:30 -0700497void LayerBaseClient::onFirstRef()
498{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700499 sp<Client> client(this->client.promote());
500 if (client != 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700501 client->bindLayer(this, mIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503}
504
505LayerBaseClient::~LayerBaseClient()
506{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700507 sp<Client> client(this->client.promote());
508 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 client->free(mIndex);
510 }
511}
512
Mathias Agopian9bce8732010-04-20 17:55:49 -0700513ssize_t LayerBaseClient::serverIndex() const
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700514{
515 sp<Client> client(this->client.promote());
516 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 return (client->cid<<16)|mIndex;
518 }
Mathias Agopian9bce8732010-04-20 17:55:49 -0700519 return ssize_t(0xFFFF0000 | mIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520}
521
Mathias Agopian1473f462009-04-10 14:24:30 -0700522sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523{
Mathias Agopian1473f462009-04-10 14:24:30 -0700524 sp<Surface> s;
525 Mutex::Autolock _l(mLock);
526 s = mClientSurface.promote();
527 if (s == 0) {
528 s = createSurface();
529 mClientSurface = s;
530 }
531 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532}
533
Mathias Agopian1473f462009-04-10 14:24:30 -0700534sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
535{
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700536 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700537 const_cast<LayerBaseClient *>(this));
538}
539
Mathias Agopian9bce8732010-04-20 17:55:49 -0700540void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
541{
542 LayerBase::dump(result, buffer, SIZE);
543
544 sp<Client> client(this->client.promote());
545 snprintf(buffer, SIZE,
546 " name=%s\n"
547 " id=0x%08x, client=0x%08x, identity=%u\n",
548 getName().string(),
549 clientIndex(), client.get() ? client->cid : 0,
550 getIdentity());
551
552 result.append(buffer);
553}
554
Mathias Agopian1473f462009-04-10 14:24:30 -0700555// ---------------------------------------------------------------------------
556
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700557LayerBaseClient::Surface::Surface(
558 const sp<SurfaceFlinger>& flinger,
559 SurfaceID id, int identity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700560 const sp<LayerBaseClient>& owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700561 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
562{
Mathias Agopian1473f462009-04-10 14:24:30 -0700563}
564
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700565LayerBaseClient::Surface::~Surface()
566{
567 /*
568 * This is a good place to clean-up all client resources
569 */
570
571 // destroy client resources
572 sp<LayerBaseClient> layer = getOwner();
573 if (layer != 0) {
574 mFlinger->destroySurface(layer);
575 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700576}
577
578sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
579 sp<LayerBaseClient> owner(mOwner.promote());
580 return owner;
581}
582
Mathias Agopian1473f462009-04-10 14:24:30 -0700583status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian151e8592009-06-15 18:24:59 -0700584 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian1473f462009-04-10 14:24:30 -0700585{
586 switch (code) {
587 case REGISTER_BUFFERS:
588 case UNREGISTER_BUFFERS:
589 case CREATE_OVERLAY:
590 {
Mathias Agopian151e8592009-06-15 18:24:59 -0700591 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
592 IPCThreadState* ipc = IPCThreadState::self();
593 const int pid = ipc->getCallingPid();
594 const int uid = ipc->getCallingUid();
595 LOGE("Permission Denial: "
596 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
597 return PERMISSION_DENIED;
Mathias Agopian1473f462009-04-10 14:24:30 -0700598 }
599 }
600 }
601 return BnSurface::onTransact(code, data, reply, flags);
602}
603
Mathias Agopian2be352a2010-05-21 17:24:35 -0700604sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int bufferIdx,
605 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700606{
607 return NULL;
608}
609
Mathias Agopian59751db2010-05-07 15:58:44 -0700610status_t LayerBaseClient::Surface::setBufferCount(int bufferCount)
611{
612 return INVALID_OPERATION;
613}
614
Mathias Agopian1473f462009-04-10 14:24:30 -0700615status_t LayerBaseClient::Surface::registerBuffers(
616 const ISurface::BufferHeap& buffers)
617{
618 return INVALID_OPERATION;
619}
620
621void LayerBaseClient::Surface::postBuffer(ssize_t offset)
622{
623}
624
625void LayerBaseClient::Surface::unregisterBuffers()
626{
627}
628
629sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800630 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
Mathias Agopian1473f462009-04-10 14:24:30 -0700631{
632 return NULL;
633};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
635// ---------------------------------------------------------------------------
636
637}; // namespace android