blob: ef5a9598d69e455f345d94247550561d4c8376e7 [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 Agopian076b1cc2009-04-10 14:24:30 -070023#include <utils/IPCThreadState.h>
24#include <utils/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"
33#include "LayerBlur.h"
34#include "SurfaceFlinger.h"
35#include "DisplayHardware/DisplayHardware.h"
36
37
38// We don't honor the premultiplied alpha flags, which means that
39// premultiplied surface may be composed using a non-premultiplied
40// equation. We do this because it may be a lot faster on some hardware
41// The correct value is HONOR_PREMULTIPLIED_ALPHA = 1
42#define HONOR_PREMULTIPLIED_ALPHA 0
43
44namespace android {
45
46// ---------------------------------------------------------------------------
47
48const uint32_t LayerBase::typeInfo = 1;
49const char* const LayerBase::typeID = "LayerBase";
50
51const uint32_t LayerBaseClient::typeInfo = LayerBase::typeInfo | 2;
52const char* const LayerBaseClient::typeID = "LayerBaseClient";
53
54// ---------------------------------------------------------------------------
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056int32_t LayerBase::sIdentity = 0;
57
58LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
59 : dpy(display), contentDirty(false),
60 mFlinger(flinger),
61 mTransformed(false),
62 mOrientation(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 mTransactionFlags(0),
64 mPremultipliedAlpha(true),
65 mIdentity(uint32_t(android_atomic_inc(&sIdentity))),
66 mInvalidate(0)
67{
68 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
69 mFlags = hw.getFlags();
70}
71
72LayerBase::~LayerBase()
73{
74}
75
76const GraphicPlane& LayerBase::graphicPlane(int dpy) const
77{
78 return mFlinger->graphicPlane(dpy);
79}
80
81GraphicPlane& LayerBase::graphicPlane(int dpy)
82{
83 return mFlinger->graphicPlane(dpy);
84}
85
86void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
87{
88 uint32_t layerFlags = 0;
89 if (flags & ISurfaceComposer::eHidden)
90 layerFlags = ISurfaceComposer::eLayerHidden;
91
92 if (flags & ISurfaceComposer::eNonPremultiplied)
93 mPremultipliedAlpha = false;
94
95 mCurrentState.z = 0;
96 mCurrentState.w = w;
97 mCurrentState.h = h;
98 mCurrentState.alpha = 0xFF;
99 mCurrentState.flags = layerFlags;
100 mCurrentState.sequence = 0;
101 mCurrentState.transform.set(0, 0);
102
103 // drawing state & current state are identical
104 mDrawingState = mCurrentState;
105}
106
107void LayerBase::commitTransaction(bool skipSize) {
108 const uint32_t w = mDrawingState.w;
109 const uint32_t h = mDrawingState.h;
110 mDrawingState = mCurrentState;
111 if (skipSize) {
112 mDrawingState.w = w;
113 mDrawingState.h = h;
114 }
115}
116void LayerBase::forceVisibilityTransaction() {
117 // this can be called without SurfaceFlinger.mStateLock, but if we
118 // can atomically increment the sequence number, it doesn't matter.
119 android_atomic_inc(&mCurrentState.sequence);
120 requestTransaction();
121}
122bool LayerBase::requestTransaction() {
123 int32_t old = setTransactionFlags(eTransactionNeeded);
124 return ((old & eTransactionNeeded) == 0);
125}
126uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
127 return android_atomic_and(~flags, &mTransactionFlags) & flags;
128}
129uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
130 return android_atomic_or(flags, &mTransactionFlags);
131}
132
133void LayerBase::setSizeChanged(uint32_t w, uint32_t h) {
134}
135
136bool LayerBase::setPosition(int32_t x, int32_t y) {
137 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
138 return false;
139 mCurrentState.sequence++;
140 mCurrentState.transform.set(x, y);
141 requestTransaction();
142 return true;
143}
144bool LayerBase::setLayer(uint32_t z) {
145 if (mCurrentState.z == z)
146 return false;
147 mCurrentState.sequence++;
148 mCurrentState.z = z;
149 requestTransaction();
150 return true;
151}
152bool LayerBase::setSize(uint32_t w, uint32_t h) {
153 if (mCurrentState.w == w && mCurrentState.h == h)
154 return false;
155 setSizeChanged(w, h);
156 mCurrentState.w = w;
157 mCurrentState.h = h;
158 requestTransaction();
159 return true;
160}
161bool LayerBase::setAlpha(uint8_t alpha) {
162 if (mCurrentState.alpha == alpha)
163 return false;
164 mCurrentState.sequence++;
165 mCurrentState.alpha = alpha;
166 requestTransaction();
167 return true;
168}
169bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
170 // TODO: check the matrix has changed
171 mCurrentState.sequence++;
172 mCurrentState.transform.set(
173 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
174 requestTransaction();
175 return true;
176}
177bool LayerBase::setTransparentRegionHint(const Region& transparent) {
178 // TODO: check the region has changed
179 mCurrentState.sequence++;
180 mCurrentState.transparentRegion = transparent;
181 requestTransaction();
182 return true;
183}
184bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
185 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
186 if (mCurrentState.flags == newFlags)
187 return false;
188 mCurrentState.sequence++;
189 mCurrentState.flags = newFlags;
190 requestTransaction();
191 return true;
192}
193
194Rect LayerBase::visibleBounds() const
195{
196 return mTransformedBounds;
197}
198
199void LayerBase::setVisibleRegion(const Region& visibleRegion) {
200 // always called from main thread
201 visibleRegionScreen = visibleRegion;
202}
203
204void LayerBase::setCoveredRegion(const Region& coveredRegion) {
205 // always called from main thread
206 coveredRegionScreen = coveredRegion;
207}
208
209uint32_t LayerBase::doTransaction(uint32_t flags)
210{
211 const Layer::State& front(drawingState());
212 const Layer::State& temp(currentState());
213
214 if (temp.sequence != front.sequence) {
215 // invalidate and recompute the visible regions if needed
216 flags |= eVisibleRegion;
217 this->contentDirty = true;
218 }
219
220 // Commit the transaction
221 commitTransaction(flags & eRestartTransaction);
222 return flags;
223}
224
225Point LayerBase::getPhysicalSize() const
226{
227 const Layer::State& front(drawingState());
228 return Point(front.w, front.h);
229}
230
231void 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
237 const Point size(getPhysicalSize());
238 uint32_t w = size.x;
239 uint32_t h = size.y;
240 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{
292 Region::iterator iterator(reg);
293 if (iterator) {
294 Rect r;
295 const DisplayHardware& hw(graphicPlane(0).displayHardware());
296 const int32_t fbWidth = hw.getWidth();
297 const int32_t fbHeight = hw.getHeight();
298 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
299 { fbWidth, fbHeight }, { 0, fbHeight } };
300 glVertexPointer(2, GL_SHORT, 0, vertices);
301 while (iterator.iterate(&r)) {
302 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
341GLuint LayerBase::createTexture() const
342{
343 GLuint textureName = -1;
344 glGenTextures(1, &textureName);
345 glBindTexture(GL_TEXTURE_2D, textureName);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
347 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
348 if (mFlags & DisplayHardware::SLOW_CONFIG) {
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
350 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
351 } else {
352 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
353 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
354 }
355 return textureName;
356}
357
358void LayerBase::clearWithOpenGL(const Region& clip) const
359{
360 const DisplayHardware& hw(graphicPlane(0).displayHardware());
361 const uint32_t fbHeight = hw.getHeight();
362 glColor4x(0,0,0,0);
363 glDisable(GL_TEXTURE_2D);
364 glDisable(GL_BLEND);
365 glDisable(GL_DITHER);
366 Rect r;
367 Region::iterator iterator(clip);
368 if (iterator) {
369 glEnable(GL_SCISSOR_TEST);
370 glVertexPointer(2, GL_FIXED, 0, mVertices);
371 while (iterator.iterate(&r)) {
372 const GLint sy = fbHeight - (r.top + r.height());
373 glScissor(r.left, sy, r.width(), r.height());
374 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
375 }
376 }
377}
378
379void LayerBase::drawWithOpenGL(const Region& clip,
380 GLint textureName, const GGLSurface& t, int transform) const
381{
382 const DisplayHardware& hw(graphicPlane(0).displayHardware());
383 const uint32_t fbHeight = hw.getHeight();
384 const State& s(drawingState());
385
386 // bind our texture
387 validateTexture(textureName);
388 glEnable(GL_TEXTURE_2D);
389
390 // Dithering...
391 if (s.flags & ISurfaceComposer::eLayerDither) {
392 glEnable(GL_DITHER);
393 } else {
394 glDisable(GL_DITHER);
395 }
396
397 if (UNLIKELY(s.alpha < 0xFF)) {
398 // We have an alpha-modulation. We need to modulate all
399 // texture components by alpha because we're always using
400 // premultiplied alpha.
401
402 // If the texture doesn't have an alpha channel we can
403 // use REPLACE and switch to non premultiplied alpha
404 // blending (SRCA/ONE_MINUS_SRCA).
405
406 GLenum env, src;
407 if (needsBlending()) {
408 env = GL_MODULATE;
409 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
410 } else {
411 env = GL_REPLACE;
412 src = GL_SRC_ALPHA;
413 }
414 const GGLfixed alpha = (s.alpha << 16)/255;
415 glColor4x(alpha, alpha, alpha, alpha);
416 glEnable(GL_BLEND);
417 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
418 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
419 } else {
420 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
421 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
422 if (needsBlending()) {
423 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
424 glEnable(GL_BLEND);
425 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
426 } else {
427 glDisable(GL_BLEND);
428 }
429 }
430
431 if (UNLIKELY(transformed()
432 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
433 {
434 //StopWatch watch("GL transformed");
435 Region::iterator iterator(clip);
436 if (iterator) {
437 // always use high-quality filtering with fast configurations
438 bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG);
439 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
440 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
441 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
442 }
443 const GLfixed texCoords[4][2] = {
444 { 0, 0 },
445 { 0, 0x10000 },
446 { 0x10000, 0x10000 },
447 { 0x10000, 0 }
448 };
449
450 glMatrixMode(GL_TEXTURE);
451 glLoadIdentity();
452
453 if (transform == HAL_TRANSFORM_ROT_90) {
454 glTranslatef(0, 1, 0);
455 glRotatef(-90, 0, 0, 1);
456 }
457
458 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
459 // find the smallest power-of-two that will accommodate our surface
460 GLuint tw = 1 << (31 - clz(t.width));
461 GLuint th = 1 << (31 - clz(t.height));
462 if (tw < t.width) tw <<= 1;
463 if (th < t.height) th <<= 1;
464 // this divide should be relatively fast because it's
465 // a power-of-two (optimized path in libgcc)
466 GLfloat ws = GLfloat(t.width) /tw;
467 GLfloat hs = GLfloat(t.height)/th;
468 glScalef(ws, hs, 1.0f);
469 }
470
471 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
472 glVertexPointer(2, GL_FIXED, 0, mVertices);
473 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
474
475 Rect r;
476 while (iterator.iterate(&r)) {
477 const GLint sy = fbHeight - (r.top + r.height());
478 glScissor(r.left, sy, r.width(), r.height());
479 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
480 }
481
482 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
483 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
484 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
485 }
486 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
487 }
488 } else {
489 Region::iterator iterator(clip);
490 if (iterator) {
491 Rect r;
492 GLint crop[4] = { 0, t.height, t.width, -t.height };
493 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
494 int x = tx();
495 int y = ty();
496 y = fbHeight - (y + t.height);
497 while (iterator.iterate(&r)) {
498 const GLint sy = fbHeight - (r.top + r.height());
499 glScissor(r.left, sy, r.width(), r.height());
500 glDrawTexiOES(x, y, 0, t.width, t.height);
501 }
502 }
503 }
504}
505
506void LayerBase::validateTexture(GLint textureName) const
507{
508 glBindTexture(GL_TEXTURE_2D, textureName);
509 // TODO: reload the texture if needed
510 // this is currently done in loadTexture() below
511}
512
513void LayerBase::loadTexture(const Region& dirty,
514 GLint textureName, const GGLSurface& t,
515 GLuint& textureWidth, GLuint& textureHeight) const
516{
517 // TODO: defer the actual texture reload until LayerBase::validateTexture
518 // is called.
519
520 uint32_t flags = mFlags;
521 glBindTexture(GL_TEXTURE_2D, textureName);
522
523 GLuint tw = t.width;
524 GLuint th = t.height;
525
526 /*
527 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700528 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
530 * need to do something reasonable (here creating a bigger texture).
531 *
532 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
533 *
534 * This situation doesn't happen often, but some h/w have a limitation
535 * for their framebuffer (eg: must be multiple of 8 pixels), and
536 * we need to take that into account when using these buffers as
537 * textures.
538 *
539 * This should never be a problem with POT textures
540 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700541
542 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
543 unpack = 1 << ((unpack > 3) ? 3 : unpack);
544 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
545
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 /*
547 * round to POT if needed
548 */
549
550 GLuint texture_w = tw;
551 GLuint texture_h = th;
552 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
553 // find the smallest power-of-two that will accommodate our surface
554 texture_w = 1 << (31 - clz(t.width));
555 texture_h = 1 << (31 - clz(t.height));
556 if (texture_w < t.width) texture_w <<= 1;
557 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700561 Rect bounds(dirty.bounds());
562 GLvoid* data = 0;
563 if (texture_w!=textureWidth || texture_h!=textureHeight) {
564 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700566 if (!textureWidth || !textureHeight) {
567 // this is the first time, load the whole texture
568 if (texture_w==tw && texture_h==th) {
569 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572 // we have to create the texture first because it
573 // doesn't match the size of the buffer
574 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577
578 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
579 glTexImage2D(GL_TEXTURE_2D, 0,
580 GL_RGB, texture_w, texture_h, 0,
581 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
582 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
583 glTexImage2D(GL_TEXTURE_2D, 0,
584 GL_RGBA, texture_w, texture_h, 0,
585 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
586 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
587 glTexImage2D(GL_TEXTURE_2D, 0,
588 GL_RGBA, texture_w, texture_h, 0,
589 GL_RGBA, GL_UNSIGNED_BYTE, data);
590 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
591 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
592 // just show the Y plane of YUV buffers
593 glTexImage2D(GL_TEXTURE_2D, 0,
594 GL_LUMINANCE, texture_w, texture_h, 0,
595 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
596 } else {
597 // oops, we don't handle this format!
598 LOGE("layer %p, texture=%d, using format %d, which is not "
599 "supported by the GL", this, textureName, t.format);
600 textureName = -1;
601 }
602 textureWidth = texture_w;
603 textureHeight = texture_h;
604 }
605 if (!data && textureName>=0) {
606 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
607 glTexSubImage2D(GL_TEXTURE_2D, 0,
608 0, bounds.top, t.width, bounds.height(),
609 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
610 t.data + bounds.top*t.stride*2);
611 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
612 glTexSubImage2D(GL_TEXTURE_2D, 0,
613 0, bounds.top, t.width, bounds.height(),
614 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
615 t.data + bounds.top*t.stride*2);
616 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
617 glTexSubImage2D(GL_TEXTURE_2D, 0,
618 0, bounds.top, t.width, bounds.height(),
619 GL_RGBA, GL_UNSIGNED_BYTE,
620 t.data + bounds.top*t.stride*4);
621 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
622 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
623 // just show the Y plane of YUV buffers
624 glTexSubImage2D(GL_TEXTURE_2D, 0,
625 0, bounds.top, t.width, bounds.height(),
626 GL_LUMINANCE, GL_UNSIGNED_BYTE,
627 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 }
629 }
630}
631
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632// ---------------------------------------------------------------------------
633
634LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
635 Client* c, int32_t i)
636 : LayerBase(flinger, display), client(c),
637 lcblk( c ? &(c->ctrlblk->layers[i]) : 0 ),
638 mIndex(i)
639{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700640}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700642void LayerBaseClient::onFirstRef()
643{
644 if (client) {
645 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 // Initialize this layer's control block
647 memset(this->lcblk, 0, sizeof(layer_cblk_t));
648 this->lcblk->identity = mIdentity;
649 Region::writeEmpty(&(this->lcblk->region[0]), sizeof(flat_region_t));
650 Region::writeEmpty(&(this->lcblk->region[1]), sizeof(flat_region_t));
651 }
652}
653
654LayerBaseClient::~LayerBaseClient()
655{
656 if (client) {
657 client->free(mIndex);
658 }
659}
660
661int32_t LayerBaseClient::serverIndex() const {
662 if (client) {
663 return (client->cid<<16)|mIndex;
664 }
665 return 0xFFFF0000 | mIndex;
666}
667
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700668sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700670 sp<Surface> s;
671 Mutex::Autolock _l(mLock);
672 s = mClientSurface.promote();
673 if (s == 0) {
674 s = createSurface();
675 mClientSurface = s;
676 }
677 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678}
679
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700680sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
681{
Mathias Agopian9a112062009-04-17 19:36:26 -0700682 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700683 const_cast<LayerBaseClient *>(this));
684}
685
686// ---------------------------------------------------------------------------
687
Mathias Agopian9a112062009-04-17 19:36:26 -0700688LayerBaseClient::Surface::Surface(
689 const sp<SurfaceFlinger>& flinger,
690 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700691 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700692 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
693{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700694}
695
696
Mathias Agopian9a112062009-04-17 19:36:26 -0700697LayerBaseClient::Surface::~Surface()
698{
699 /*
700 * This is a good place to clean-up all client resources
701 */
702
703 // destroy client resources
704 sp<LayerBaseClient> layer = getOwner();
705 if (layer != 0) {
706 mFlinger->destroySurface(layer);
707 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708}
709
710sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
711 sp<LayerBaseClient> owner(mOwner.promote());
712 return owner;
713}
714
715
716void LayerBaseClient::Surface::getSurfaceData(
717 ISurfaceFlingerClient::surface_data_t* params) const
718{
719 params->token = mToken;
720 params->identity = mIdentity;
721}
722
723status_t LayerBaseClient::Surface::onTransact(
724 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
725{
726 switch (code) {
727 case REGISTER_BUFFERS:
728 case UNREGISTER_BUFFERS:
729 case CREATE_OVERLAY:
730 {
731 // codes that require permission check
732 IPCThreadState* ipc = IPCThreadState::self();
733 const int pid = ipc->getCallingPid();
734 const int self_pid = getpid();
735 if (LIKELY(pid != self_pid)) {
736 // we're called from a different process, do the real check
737 if (!checkCallingPermission(
738 String16("android.permission.ACCESS_SURFACE_FLINGER")))
739 {
740 const int uid = ipc->getCallingUid();
741 LOGE("Permission Denial: "
742 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
743 return PERMISSION_DENIED;
744 }
745 }
746 }
747 }
748 return BnSurface::onTransact(code, data, reply, flags);
749}
750
751sp<SurfaceBuffer> LayerBaseClient::Surface::getBuffer()
752{
753 return NULL;
754}
755
756status_t LayerBaseClient::Surface::registerBuffers(
757 const ISurface::BufferHeap& buffers)
758{
759 return INVALID_OPERATION;
760}
761
762void LayerBaseClient::Surface::postBuffer(ssize_t offset)
763{
764}
765
766void LayerBaseClient::Surface::unregisterBuffers()
767{
768}
769
770sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
771 uint32_t w, uint32_t h, int32_t format)
772{
773 return NULL;
774};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775
776// ---------------------------------------------------------------------------
777
778}; // namespace android