blob: 6da0bf77e10b80732c5ef48770fef7a73360c29b [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
56Vector<GLuint> LayerBase::deletedTextures;
57
58int32_t LayerBase::sIdentity = 0;
59
60LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
61 : dpy(display), contentDirty(false),
62 mFlinger(flinger),
63 mTransformed(false),
64 mOrientation(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 mTransactionFlags(0),
66 mPremultipliedAlpha(true),
67 mIdentity(uint32_t(android_atomic_inc(&sIdentity))),
68 mInvalidate(0)
69{
70 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
71 mFlags = hw.getFlags();
72}
73
74LayerBase::~LayerBase()
75{
76}
77
78const GraphicPlane& LayerBase::graphicPlane(int dpy) const
79{
80 return mFlinger->graphicPlane(dpy);
81}
82
83GraphicPlane& LayerBase::graphicPlane(int dpy)
84{
85 return mFlinger->graphicPlane(dpy);
86}
87
88void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
89{
90 uint32_t layerFlags = 0;
91 if (flags & ISurfaceComposer::eHidden)
92 layerFlags = ISurfaceComposer::eLayerHidden;
93
94 if (flags & ISurfaceComposer::eNonPremultiplied)
95 mPremultipliedAlpha = false;
96
97 mCurrentState.z = 0;
98 mCurrentState.w = w;
99 mCurrentState.h = h;
100 mCurrentState.alpha = 0xFF;
101 mCurrentState.flags = layerFlags;
102 mCurrentState.sequence = 0;
103 mCurrentState.transform.set(0, 0);
104
105 // drawing state & current state are identical
106 mDrawingState = mCurrentState;
107}
108
109void LayerBase::commitTransaction(bool skipSize) {
110 const uint32_t w = mDrawingState.w;
111 const uint32_t h = mDrawingState.h;
112 mDrawingState = mCurrentState;
113 if (skipSize) {
114 mDrawingState.w = w;
115 mDrawingState.h = h;
116 }
117}
118void LayerBase::forceVisibilityTransaction() {
119 // this can be called without SurfaceFlinger.mStateLock, but if we
120 // can atomically increment the sequence number, it doesn't matter.
121 android_atomic_inc(&mCurrentState.sequence);
122 requestTransaction();
123}
124bool LayerBase::requestTransaction() {
125 int32_t old = setTransactionFlags(eTransactionNeeded);
126 return ((old & eTransactionNeeded) == 0);
127}
128uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
129 return android_atomic_and(~flags, &mTransactionFlags) & flags;
130}
131uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
132 return android_atomic_or(flags, &mTransactionFlags);
133}
134
135void LayerBase::setSizeChanged(uint32_t w, uint32_t h) {
136}
137
138bool LayerBase::setPosition(int32_t x, int32_t y) {
139 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
140 return false;
141 mCurrentState.sequence++;
142 mCurrentState.transform.set(x, y);
143 requestTransaction();
144 return true;
145}
146bool LayerBase::setLayer(uint32_t z) {
147 if (mCurrentState.z == z)
148 return false;
149 mCurrentState.sequence++;
150 mCurrentState.z = z;
151 requestTransaction();
152 return true;
153}
154bool LayerBase::setSize(uint32_t w, uint32_t h) {
155 if (mCurrentState.w == w && mCurrentState.h == h)
156 return false;
157 setSizeChanged(w, h);
158 mCurrentState.w = w;
159 mCurrentState.h = h;
160 requestTransaction();
161 return true;
162}
163bool LayerBase::setAlpha(uint8_t alpha) {
164 if (mCurrentState.alpha == alpha)
165 return false;
166 mCurrentState.sequence++;
167 mCurrentState.alpha = alpha;
168 requestTransaction();
169 return true;
170}
171bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
172 // TODO: check the matrix has changed
173 mCurrentState.sequence++;
174 mCurrentState.transform.set(
175 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
176 requestTransaction();
177 return true;
178}
179bool LayerBase::setTransparentRegionHint(const Region& transparent) {
180 // TODO: check the region has changed
181 mCurrentState.sequence++;
182 mCurrentState.transparentRegion = transparent;
183 requestTransaction();
184 return true;
185}
186bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
187 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
188 if (mCurrentState.flags == newFlags)
189 return false;
190 mCurrentState.sequence++;
191 mCurrentState.flags = newFlags;
192 requestTransaction();
193 return true;
194}
195
196Rect LayerBase::visibleBounds() const
197{
198 return mTransformedBounds;
199}
200
201void LayerBase::setVisibleRegion(const Region& visibleRegion) {
202 // always called from main thread
203 visibleRegionScreen = visibleRegion;
204}
205
206void LayerBase::setCoveredRegion(const Region& coveredRegion) {
207 // always called from main thread
208 coveredRegionScreen = coveredRegion;
209}
210
211uint32_t LayerBase::doTransaction(uint32_t flags)
212{
213 const Layer::State& front(drawingState());
214 const Layer::State& temp(currentState());
215
216 if (temp.sequence != front.sequence) {
217 // invalidate and recompute the visible regions if needed
218 flags |= eVisibleRegion;
219 this->contentDirty = true;
220 }
221
222 // Commit the transaction
223 commitTransaction(flags & eRestartTransaction);
224 return flags;
225}
226
227Point LayerBase::getPhysicalSize() const
228{
229 const Layer::State& front(drawingState());
230 return Point(front.w, front.h);
231}
232
233void 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
239 const Point size(getPhysicalSize());
240 uint32_t w = size.x;
241 uint32_t h = size.y;
242 tr.transform(mVertices[0], 0, 0);
243 tr.transform(mVertices[1], 0, h);
244 tr.transform(mVertices[2], w, h);
245 tr.transform(mVertices[3], w, 0);
246 if (UNLIKELY(transformed)) {
247 // NOTE: here we could also punt if we have too many rectangles
248 // in the transparent region
249 if (tr.preserveRects()) {
250 // transform the transparent region
251 transparentRegionScreen = tr.transform(s.transparentRegion);
252 } else {
253 // transformation too complex, can't do the transparent region
254 // optimization.
255 transparentRegionScreen.clear();
256 }
257 } else {
258 transparentRegionScreen = s.transparentRegion;
259 }
260
261 // cache a few things...
262 mOrientation = tr.getOrientation();
263 mTransformedBounds = tr.makeBounds(w, h);
264 mTransformed = transformed;
265 mLeft = tr.tx();
266 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267}
268
269void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
270{
271}
272
273void LayerBase::unlockPageFlip(
274 const Transform& planeTransform, Region& outDirtyRegion)
275{
276 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
277 outDirtyRegion.orSelf(visibleRegionScreen);
278 }
279}
280
281void LayerBase::finishPageFlip()
282{
283}
284
285void LayerBase::invalidate()
286{
287 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
288 mFlinger->signalEvent();
289 }
290}
291
292void LayerBase::drawRegion(const Region& reg) const
293{
294 Region::iterator iterator(reg);
295 if (iterator) {
296 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);
303 while (iterator.iterate(&r)) {
304 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
343GLuint LayerBase::createTexture() const
344{
345 GLuint textureName = -1;
346 glGenTextures(1, &textureName);
347 glBindTexture(GL_TEXTURE_2D, textureName);
348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
350 if (mFlags & DisplayHardware::SLOW_CONFIG) {
351 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
352 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
353 } else {
354 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
355 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
356 }
357 return textureName;
358}
359
360void LayerBase::clearWithOpenGL(const Region& clip) const
361{
362 const DisplayHardware& hw(graphicPlane(0).displayHardware());
363 const uint32_t fbHeight = hw.getHeight();
364 glColor4x(0,0,0,0);
365 glDisable(GL_TEXTURE_2D);
366 glDisable(GL_BLEND);
367 glDisable(GL_DITHER);
368 Rect r;
369 Region::iterator iterator(clip);
370 if (iterator) {
371 glEnable(GL_SCISSOR_TEST);
372 glVertexPointer(2, GL_FIXED, 0, mVertices);
373 while (iterator.iterate(&r)) {
374 const GLint sy = fbHeight - (r.top + r.height());
375 glScissor(r.left, sy, r.width(), r.height());
376 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
377 }
378 }
379}
380
381void LayerBase::drawWithOpenGL(const Region& clip,
382 GLint textureName, const GGLSurface& t, int transform) const
383{
384 const DisplayHardware& hw(graphicPlane(0).displayHardware());
385 const uint32_t fbHeight = hw.getHeight();
386 const State& s(drawingState());
387
388 // bind our texture
389 validateTexture(textureName);
390 glEnable(GL_TEXTURE_2D);
391
392 // Dithering...
393 if (s.flags & ISurfaceComposer::eLayerDither) {
394 glEnable(GL_DITHER);
395 } else {
396 glDisable(GL_DITHER);
397 }
398
399 if (UNLIKELY(s.alpha < 0xFF)) {
400 // We have an alpha-modulation. We need to modulate all
401 // texture components by alpha because we're always using
402 // premultiplied alpha.
403
404 // If the texture doesn't have an alpha channel we can
405 // use REPLACE and switch to non premultiplied alpha
406 // blending (SRCA/ONE_MINUS_SRCA).
407
408 GLenum env, src;
409 if (needsBlending()) {
410 env = GL_MODULATE;
411 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
412 } else {
413 env = GL_REPLACE;
414 src = GL_SRC_ALPHA;
415 }
416 const GGLfixed alpha = (s.alpha << 16)/255;
417 glColor4x(alpha, alpha, alpha, alpha);
418 glEnable(GL_BLEND);
419 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
420 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
421 } else {
422 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
423 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
424 if (needsBlending()) {
425 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
426 glEnable(GL_BLEND);
427 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
428 } else {
429 glDisable(GL_BLEND);
430 }
431 }
432
433 if (UNLIKELY(transformed()
434 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
435 {
436 //StopWatch watch("GL transformed");
437 Region::iterator iterator(clip);
438 if (iterator) {
439 // always use high-quality filtering with fast configurations
440 bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG);
441 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
442 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
443 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
444 }
445 const GLfixed texCoords[4][2] = {
446 { 0, 0 },
447 { 0, 0x10000 },
448 { 0x10000, 0x10000 },
449 { 0x10000, 0 }
450 };
451
452 glMatrixMode(GL_TEXTURE);
453 glLoadIdentity();
454
455 if (transform == HAL_TRANSFORM_ROT_90) {
456 glTranslatef(0, 1, 0);
457 glRotatef(-90, 0, 0, 1);
458 }
459
460 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
461 // find the smallest power-of-two that will accommodate our surface
462 GLuint tw = 1 << (31 - clz(t.width));
463 GLuint th = 1 << (31 - clz(t.height));
464 if (tw < t.width) tw <<= 1;
465 if (th < t.height) th <<= 1;
466 // this divide should be relatively fast because it's
467 // a power-of-two (optimized path in libgcc)
468 GLfloat ws = GLfloat(t.width) /tw;
469 GLfloat hs = GLfloat(t.height)/th;
470 glScalef(ws, hs, 1.0f);
471 }
472
473 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
474 glVertexPointer(2, GL_FIXED, 0, mVertices);
475 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
476
477 Rect r;
478 while (iterator.iterate(&r)) {
479 const GLint sy = fbHeight - (r.top + r.height());
480 glScissor(r.left, sy, r.width(), r.height());
481 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
482 }
483
484 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
485 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
486 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
487 }
488 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
489 }
490 } else {
491 Region::iterator iterator(clip);
492 if (iterator) {
493 Rect r;
494 GLint crop[4] = { 0, t.height, t.width, -t.height };
495 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
496 int x = tx();
497 int y = ty();
498 y = fbHeight - (y + t.height);
499 while (iterator.iterate(&r)) {
500 const GLint sy = fbHeight - (r.top + r.height());
501 glScissor(r.left, sy, r.width(), r.height());
502 glDrawTexiOES(x, y, 0, t.width, t.height);
503 }
504 }
505 }
506}
507
508void LayerBase::validateTexture(GLint textureName) const
509{
510 glBindTexture(GL_TEXTURE_2D, textureName);
511 // TODO: reload the texture if needed
512 // this is currently done in loadTexture() below
513}
514
515void LayerBase::loadTexture(const Region& dirty,
516 GLint textureName, const GGLSurface& t,
517 GLuint& textureWidth, GLuint& textureHeight) const
518{
519 // TODO: defer the actual texture reload until LayerBase::validateTexture
520 // is called.
521
522 uint32_t flags = mFlags;
523 glBindTexture(GL_TEXTURE_2D, textureName);
524
525 GLuint tw = t.width;
526 GLuint th = t.height;
527
528 /*
529 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700530 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
532 * need to do something reasonable (here creating a bigger texture).
533 *
534 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
535 *
536 * This situation doesn't happen often, but some h/w have a limitation
537 * for their framebuffer (eg: must be multiple of 8 pixels), and
538 * we need to take that into account when using these buffers as
539 * textures.
540 *
541 * This should never be a problem with POT textures
542 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700543
544 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
545 unpack = 1 << ((unpack > 3) ? 3 : unpack);
546 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
547
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 /*
549 * round to POT if needed
550 */
551
552 GLuint texture_w = tw;
553 GLuint texture_h = th;
554 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
555 // find the smallest power-of-two that will accommodate our surface
556 texture_w = 1 << (31 - clz(t.width));
557 texture_h = 1 << (31 - clz(t.height));
558 if (texture_w < t.width) texture_w <<= 1;
559 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700561
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700563 Rect bounds(dirty.bounds());
564 GLvoid* data = 0;
565 if (texture_w!=textureWidth || texture_h!=textureHeight) {
566 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700568 if (!textureWidth || !textureHeight) {
569 // this is the first time, load the whole texture
570 if (texture_w==tw && texture_h==th) {
571 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 // we have to create the texture first because it
575 // doesn't match the size of the buffer
576 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579
580 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
581 glTexImage2D(GL_TEXTURE_2D, 0,
582 GL_RGB, texture_w, texture_h, 0,
583 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
584 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
585 glTexImage2D(GL_TEXTURE_2D, 0,
586 GL_RGBA, texture_w, texture_h, 0,
587 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
588 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
589 glTexImage2D(GL_TEXTURE_2D, 0,
590 GL_RGBA, texture_w, texture_h, 0,
591 GL_RGBA, GL_UNSIGNED_BYTE, data);
592 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
593 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
594 // just show the Y plane of YUV buffers
595 glTexImage2D(GL_TEXTURE_2D, 0,
596 GL_LUMINANCE, texture_w, texture_h, 0,
597 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
598 } else {
599 // oops, we don't handle this format!
600 LOGE("layer %p, texture=%d, using format %d, which is not "
601 "supported by the GL", this, textureName, t.format);
602 textureName = -1;
603 }
604 textureWidth = texture_w;
605 textureHeight = texture_h;
606 }
607 if (!data && textureName>=0) {
608 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
609 glTexSubImage2D(GL_TEXTURE_2D, 0,
610 0, bounds.top, t.width, bounds.height(),
611 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
612 t.data + bounds.top*t.stride*2);
613 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
614 glTexSubImage2D(GL_TEXTURE_2D, 0,
615 0, bounds.top, t.width, bounds.height(),
616 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
617 t.data + bounds.top*t.stride*2);
618 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
619 glTexSubImage2D(GL_TEXTURE_2D, 0,
620 0, bounds.top, t.width, bounds.height(),
621 GL_RGBA, GL_UNSIGNED_BYTE,
622 t.data + bounds.top*t.stride*4);
623 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
624 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
625 // just show the Y plane of YUV buffers
626 glTexSubImage2D(GL_TEXTURE_2D, 0,
627 0, bounds.top, t.width, bounds.height(),
628 GL_LUMINANCE, GL_UNSIGNED_BYTE,
629 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 }
631 }
632}
633
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634// ---------------------------------------------------------------------------
635
636LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
637 Client* c, int32_t i)
638 : LayerBase(flinger, display), client(c),
639 lcblk( c ? &(c->ctrlblk->layers[i]) : 0 ),
640 mIndex(i)
641{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700642}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700644void LayerBaseClient::onFirstRef()
645{
646 if (client) {
647 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 // Initialize this layer's control block
649 memset(this->lcblk, 0, sizeof(layer_cblk_t));
650 this->lcblk->identity = mIdentity;
651 Region::writeEmpty(&(this->lcblk->region[0]), sizeof(flat_region_t));
652 Region::writeEmpty(&(this->lcblk->region[1]), sizeof(flat_region_t));
653 }
654}
655
656LayerBaseClient::~LayerBaseClient()
657{
658 if (client) {
659 client->free(mIndex);
660 }
661}
662
663int32_t LayerBaseClient::serverIndex() const {
664 if (client) {
665 return (client->cid<<16)|mIndex;
666 }
667 return 0xFFFF0000 | mIndex;
668}
669
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700670sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700672 sp<Surface> s;
673 Mutex::Autolock _l(mLock);
674 s = mClientSurface.promote();
675 if (s == 0) {
676 s = createSurface();
677 mClientSurface = s;
678 }
679 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680}
681
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700682sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
683{
684 return new Surface(clientIndex(), mIdentity,
685 const_cast<LayerBaseClient *>(this));
686}
687
688// ---------------------------------------------------------------------------
689
690LayerBaseClient::Surface::Surface(SurfaceID id, int identity,
691 const sp<LayerBaseClient>& owner)
692 : mToken(id), mIdentity(identity), mOwner(owner)
693{
694}
695
696
697LayerBaseClient::Surface::~Surface() {
698 // TODO: We now have a point here were we can clean-up the
699 // client's mess.
700 // This is also where surface id should be recycled.
701 //LOGD("Surface %d, heaps={%p, %p} destroyed",
702 // mId, mHeap[0].get(), mHeap[1].get());
703}
704
705sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
706 sp<LayerBaseClient> owner(mOwner.promote());
707 return owner;
708}
709
710
711void LayerBaseClient::Surface::getSurfaceData(
712 ISurfaceFlingerClient::surface_data_t* params) const
713{
714 params->token = mToken;
715 params->identity = mIdentity;
716}
717
718status_t LayerBaseClient::Surface::onTransact(
719 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
720{
721 switch (code) {
722 case REGISTER_BUFFERS:
723 case UNREGISTER_BUFFERS:
724 case CREATE_OVERLAY:
725 {
726 // codes that require permission check
727 IPCThreadState* ipc = IPCThreadState::self();
728 const int pid = ipc->getCallingPid();
729 const int self_pid = getpid();
730 if (LIKELY(pid != self_pid)) {
731 // we're called from a different process, do the real check
732 if (!checkCallingPermission(
733 String16("android.permission.ACCESS_SURFACE_FLINGER")))
734 {
735 const int uid = ipc->getCallingUid();
736 LOGE("Permission Denial: "
737 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
738 return PERMISSION_DENIED;
739 }
740 }
741 }
742 }
743 return BnSurface::onTransact(code, data, reply, flags);
744}
745
746sp<SurfaceBuffer> LayerBaseClient::Surface::getBuffer()
747{
748 return NULL;
749}
750
751status_t LayerBaseClient::Surface::registerBuffers(
752 const ISurface::BufferHeap& buffers)
753{
754 return INVALID_OPERATION;
755}
756
757void LayerBaseClient::Surface::postBuffer(ssize_t offset)
758{
759}
760
761void LayerBaseClient::Surface::unregisterBuffers()
762{
763}
764
765sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
766 uint32_t w, uint32_t h, int32_t format)
767{
768 return NULL;
769};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770
771// ---------------------------------------------------------------------------
772
773}; // namespace android