blob: cc9c586b421ab2b52d7ce88959eda39398b415fb [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 Agopian1473f462009-04-10 14:24:30 -070023#include <utils/IPCThreadState.h>
24#include <utils/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"
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 Project9066cfe2009-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 Project9066cfe2009-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 Project9066cfe2009-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,
Mathias Agopiandff8e582009-05-04 14:17:04 -0700380 GLint textureName, const sp<const Buffer>& buffer, int transform) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381{
382 const DisplayHardware& hw(graphicPlane(0).displayHardware());
383 const uint32_t fbHeight = hw.getHeight();
384 const State& s(drawingState());
Mathias Agopiandff8e582009-05-04 14:17:04 -0700385 const uint32_t width = buffer->width;
386 const uint32_t height = buffer->height;
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 // 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
Mathias Agopiandff8e582009-05-04 14:17:04 -0700462 GLuint tw = 1 << (31 - clz(width));
463 GLuint th = 1 << (31 - clz(height));
464 if (tw < width) tw <<= 1;
465 if (th < height) th <<= 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 // this divide should be relatively fast because it's
467 // a power-of-two (optimized path in libgcc)
Mathias Agopiandff8e582009-05-04 14:17:04 -0700468 GLfloat ws = GLfloat(width) /tw;
469 GLfloat hs = GLfloat(height)/th;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 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;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700494 GLint crop[4] = { 0, height, width, -height };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
496 int x = tx();
497 int y = ty();
Mathias Agopiandff8e582009-05-04 14:17:04 -0700498 y = fbHeight - (y + height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 while (iterator.iterate(&r)) {
500 const GLint sy = fbHeight - (r.top + r.height());
501 glScissor(r.left, sy, r.width(), r.height());
Mathias Agopiandff8e582009-05-04 14:17:04 -0700502 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
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 Agopian1473f462009-04-10 14:24:30 -0700530 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-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 Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800560 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562regular:
Mathias Agopian1473f462009-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 Project9066cfe2009-03-03 19:31:44 -0800567
Mathias Agopian1473f462009-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 Project9066cfe2009-03-03 19:31:44 -0800572 data = t.data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 } else {
Mathias Agopian1473f462009-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 Project9066cfe2009-03-03 19:31:44 -0800577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
Mathias Agopian1473f462009-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 Project9066cfe2009-03-03 19:31:44 -0800630 }
631 }
632}
633
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -0700642}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643
Mathias Agopian1473f462009-04-10 14:24:30 -0700644void LayerBaseClient::onFirstRef()
645{
646 if (client) {
647 client->bindLayer(this, mIndex);
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -0700670sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671{
Mathias Agopian1473f462009-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 Project9066cfe2009-03-03 19:31:44 -0800680}
681
Mathias Agopian1473f462009-04-10 14:24:30 -0700682sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
683{
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700684 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700685 const_cast<LayerBaseClient *>(this));
686}
687
688// ---------------------------------------------------------------------------
689
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700690LayerBaseClient::Surface::Surface(
691 const sp<SurfaceFlinger>& flinger,
692 SurfaceID id, int identity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700693 const sp<LayerBaseClient>& owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700694 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
695{
Mathias Agopian1473f462009-04-10 14:24:30 -0700696}
697
698
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700699LayerBaseClient::Surface::~Surface()
700{
701 /*
702 * This is a good place to clean-up all client resources
703 */
704
705 // destroy client resources
706 sp<LayerBaseClient> layer = getOwner();
707 if (layer != 0) {
708 mFlinger->destroySurface(layer);
709 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700710}
711
712sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
713 sp<LayerBaseClient> owner(mOwner.promote());
714 return owner;
715}
716
717
718void LayerBaseClient::Surface::getSurfaceData(
719 ISurfaceFlingerClient::surface_data_t* params) const
720{
721 params->token = mToken;
722 params->identity = mIdentity;
723}
724
725status_t LayerBaseClient::Surface::onTransact(
726 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
727{
728 switch (code) {
729 case REGISTER_BUFFERS:
730 case UNREGISTER_BUFFERS:
731 case CREATE_OVERLAY:
732 {
733 // codes that require permission check
734 IPCThreadState* ipc = IPCThreadState::self();
735 const int pid = ipc->getCallingPid();
736 const int self_pid = getpid();
737 if (LIKELY(pid != self_pid)) {
738 // we're called from a different process, do the real check
739 if (!checkCallingPermission(
740 String16("android.permission.ACCESS_SURFACE_FLINGER")))
741 {
742 const int uid = ipc->getCallingUid();
743 LOGE("Permission Denial: "
744 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
745 return PERMISSION_DENIED;
746 }
747 }
748 }
749 }
750 return BnSurface::onTransact(code, data, reply, flags);
751}
752
753sp<SurfaceBuffer> LayerBaseClient::Surface::getBuffer()
754{
755 return NULL;
756}
757
758status_t LayerBaseClient::Surface::registerBuffers(
759 const ISurface::BufferHeap& buffers)
760{
761 return INVALID_OPERATION;
762}
763
764void LayerBaseClient::Surface::postBuffer(ssize_t offset)
765{
766}
767
768void LayerBaseClient::Surface::unregisterBuffers()
769{
770}
771
772sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
773 uint32_t w, uint32_t h, int32_t format)
774{
775 return NULL;
776};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777
778// ---------------------------------------------------------------------------
779
780}; // namespace android