blob: efbc77a28958e01d1b83cc9739e245a5b408ffb6 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22#include <utils/Log.h>
Mathias Agopian310f8da2009-05-22 01:27:01 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28
29#include <hardware/hardware.h>
30
31#include "clz.h"
32#include "LayerBase.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include "SurfaceFlinger.h"
34#include "DisplayHardware/DisplayHardware.h"
35
36
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037namespace android {
38
39// ---------------------------------------------------------------------------
40
41const uint32_t LayerBase::typeInfo = 1;
42const char* const LayerBase::typeID = "LayerBase";
43
44const uint32_t LayerBaseClient::typeInfo = LayerBase::typeInfo | 2;
45const char* const LayerBaseClient::typeID = "LayerBaseClient";
46
47// ---------------------------------------------------------------------------
48
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
50 : dpy(display), contentDirty(false),
51 mFlinger(flinger),
52 mTransformed(false),
Mathias Agopiana2fe0a22009-09-23 18:34:53 -070053 mUseLinearFiltering(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 mOrientation(0),
Mathias Agopianca6fab22010-02-19 17:51:58 -080055 mLeft(0), mTop(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056 mTransactionFlags(0),
Mathias Agopiand1296592010-03-09 19:17:47 -080057 mPremultipliedAlpha(true), mDebug(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058 mInvalidate(0)
59{
60 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
61 mFlags = hw.getFlags();
62}
63
64LayerBase::~LayerBase()
65{
66}
67
Mathias Agopiand1296592010-03-09 19:17:47 -080068void LayerBase::setName(const String8& name) {
69 mName = name;
70}
71
72String8 LayerBase::getName() const {
73 return mName;
74}
75
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076const 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
Mathias Agopian7e4a5872009-09-29 22:39:22 -070095 mCurrentState.z = 0;
96 mCurrentState.w = w;
97 mCurrentState.h = h;
98 mCurrentState.requested_w = w;
99 mCurrentState.requested_h = h;
100 mCurrentState.alpha = 0xFF;
101 mCurrentState.flags = layerFlags;
102 mCurrentState.sequence = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 mCurrentState.transform.set(0, 0);
104
105 // drawing state & current state are identical
106 mDrawingState = mCurrentState;
107}
108
Mathias Agopianba6be542009-09-29 22:32:36 -0700109void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111}
112void LayerBase::forceVisibilityTransaction() {
113 // this can be called without SurfaceFlinger.mStateLock, but if we
114 // can atomically increment the sequence number, it doesn't matter.
115 android_atomic_inc(&mCurrentState.sequence);
116 requestTransaction();
117}
118bool LayerBase::requestTransaction() {
119 int32_t old = setTransactionFlags(eTransactionNeeded);
120 return ((old & eTransactionNeeded) == 0);
121}
122uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
123 return android_atomic_and(~flags, &mTransactionFlags) & flags;
124}
125uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
126 return android_atomic_or(flags, &mTransactionFlags);
127}
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129bool LayerBase::setPosition(int32_t x, int32_t y) {
130 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
131 return false;
132 mCurrentState.sequence++;
133 mCurrentState.transform.set(x, y);
134 requestTransaction();
135 return true;
136}
137bool LayerBase::setLayer(uint32_t z) {
138 if (mCurrentState.z == z)
139 return false;
140 mCurrentState.sequence++;
141 mCurrentState.z = z;
142 requestTransaction();
143 return true;
144}
145bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700146 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 return false;
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700148 mCurrentState.requested_w = w;
149 mCurrentState.requested_h = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 requestTransaction();
151 return true;
152}
153bool LayerBase::setAlpha(uint8_t alpha) {
154 if (mCurrentState.alpha == alpha)
155 return false;
156 mCurrentState.sequence++;
157 mCurrentState.alpha = alpha;
158 requestTransaction();
159 return true;
160}
161bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
162 // TODO: check the matrix has changed
163 mCurrentState.sequence++;
164 mCurrentState.transform.set(
165 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
166 requestTransaction();
167 return true;
168}
169bool LayerBase::setTransparentRegionHint(const Region& transparent) {
170 // TODO: check the region has changed
171 mCurrentState.sequence++;
172 mCurrentState.transparentRegion = transparent;
173 requestTransaction();
174 return true;
175}
176bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
177 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
178 if (mCurrentState.flags == newFlags)
179 return false;
180 mCurrentState.sequence++;
181 mCurrentState.flags = newFlags;
182 requestTransaction();
183 return true;
184}
185
186Rect LayerBase::visibleBounds() const
187{
188 return mTransformedBounds;
189}
190
191void LayerBase::setVisibleRegion(const Region& visibleRegion) {
192 // always called from main thread
193 visibleRegionScreen = visibleRegion;
194}
195
196void LayerBase::setCoveredRegion(const Region& coveredRegion) {
197 // always called from main thread
198 coveredRegionScreen = coveredRegion;
199}
200
201uint32_t LayerBase::doTransaction(uint32_t flags)
202{
203 const Layer::State& front(drawingState());
204 const Layer::State& temp(currentState());
205
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700206 if ((front.requested_w != temp.requested_w) ||
207 (front.requested_h != temp.requested_h)) {
208 // resize the layer, set the physical size to the requested size
209 Layer::State& editTemp(currentState());
210 editTemp.w = temp.requested_w;
211 editTemp.h = temp.requested_h;
212 }
213
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700214 if ((front.w != temp.w) || (front.h != temp.h)) {
215 // invalidate and recompute the visible regions if needed
216 flags |= Layer::eVisibleRegion;
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700217 }
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 if (temp.sequence != front.sequence) {
220 // invalidate and recompute the visible regions if needed
221 flags |= eVisibleRegion;
222 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700223
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700224 const bool linearFiltering = mUseLinearFiltering;
225 mUseLinearFiltering = false;
226 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
227 // we may use linear filtering, if the matrix scales us
228 const uint8_t type = temp.transform.getType();
229 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
230 mUseLinearFiltering = true;
231 }
232 }
233 }
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700236 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 return flags;
238}
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240void LayerBase::validateVisibility(const Transform& planeTransform)
241{
242 const Layer::State& s(drawingState());
243 const Transform tr(planeTransform * s.transform);
244 const bool transformed = tr.transformed();
245
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700246 uint32_t w = s.w;
247 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 tr.transform(mVertices[0], 0, 0);
249 tr.transform(mVertices[1], 0, h);
250 tr.transform(mVertices[2], w, h);
251 tr.transform(mVertices[3], w, 0);
252 if (UNLIKELY(transformed)) {
253 // NOTE: here we could also punt if we have too many rectangles
254 // in the transparent region
255 if (tr.preserveRects()) {
256 // transform the transparent region
257 transparentRegionScreen = tr.transform(s.transparentRegion);
258 } else {
259 // transformation too complex, can't do the transparent region
260 // optimization.
261 transparentRegionScreen.clear();
262 }
263 } else {
264 transparentRegionScreen = s.transparentRegion;
265 }
266
267 // cache a few things...
268 mOrientation = tr.getOrientation();
269 mTransformedBounds = tr.makeBounds(w, h);
270 mTransformed = transformed;
271 mLeft = tr.tx();
272 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273}
274
275void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
276{
277}
278
279void LayerBase::unlockPageFlip(
280 const Transform& planeTransform, Region& outDirtyRegion)
281{
282 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
283 outDirtyRegion.orSelf(visibleRegionScreen);
284 }
285}
286
287void LayerBase::finishPageFlip()
288{
289}
290
291void LayerBase::invalidate()
292{
293 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
294 mFlinger->signalEvent();
295 }
296}
297
298void LayerBase::drawRegion(const Region& reg) const
299{
Mathias Agopian20f68782009-05-11 00:03:41 -0700300 Region::const_iterator it = reg.begin();
301 Region::const_iterator const end = reg.end();
302 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 Rect r;
304 const DisplayHardware& hw(graphicPlane(0).displayHardware());
305 const int32_t fbWidth = hw.getWidth();
306 const int32_t fbHeight = hw.getHeight();
307 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
308 { fbWidth, fbHeight }, { 0, fbHeight } };
309 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700310 while (it != end) {
311 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 const GLint sy = fbHeight - (r.top + r.height());
313 glScissor(r.left, sy, r.width(), r.height());
314 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
315 }
316 }
317}
318
319void LayerBase::draw(const Region& inClip) const
320{
321 // invalidate the region we'll update
322 Region clip(inClip); // copy-on-write, so no-op most of the time
323
324 // Remove the transparent area from the clipping region
325 const State& s = drawingState();
326 if (LIKELY(!s.transparentRegion.isEmpty())) {
327 clip.subtract(transparentRegionScreen);
328 if (clip.isEmpty()) {
329 // usually this won't happen because this should be taken care of
330 // by SurfaceFlinger::computeVisibleRegions()
331 return;
332 }
333 }
334
335 // reset GL state
336 glEnable(GL_SCISSOR_TEST);
337
338 onDraw(clip);
339
340 /*
341 glDisable(GL_TEXTURE_2D);
342 glDisable(GL_DITHER);
343 glEnable(GL_BLEND);
344 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
345 glColor4x(0, 0x8000, 0, 0x10000);
346 drawRegion(transparentRegionScreen);
347 glDisable(GL_BLEND);
348 */
349}
350
351GLuint LayerBase::createTexture() const
352{
353 GLuint textureName = -1;
354 glGenTextures(1, &textureName);
355 glBindTexture(GL_TEXTURE_2D, textureName);
356 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
357 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700358 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
359 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 return textureName;
361}
362
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700363void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
364 GLclampx green, GLclampx blue,
365 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366{
367 const DisplayHardware& hw(graphicPlane(0).displayHardware());
368 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700369 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370 glDisable(GL_TEXTURE_2D);
371 glDisable(GL_BLEND);
372 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700373
374 Region::const_iterator it = clip.begin();
375 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700376 glEnable(GL_SCISSOR_TEST);
377 glVertexPointer(2, GL_FIXED, 0, mVertices);
378 while (it != end) {
379 const Rect& r = *it++;
380 const GLint sy = fbHeight - (r.top + r.height());
381 glScissor(r.left, sy, r.width(), r.height());
382 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 }
384}
385
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700386void LayerBase::clearWithOpenGL(const Region& clip) const
387{
388 clearWithOpenGL(clip,0,0,0,0);
389}
390
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700391void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392{
393 const DisplayHardware& hw(graphicPlane(0).displayHardware());
394 const uint32_t fbHeight = hw.getHeight();
395 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700398 validateTexture(texture.name);
399 uint32_t width = texture.width;
400 uint32_t height = texture.height;
401
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 glEnable(GL_TEXTURE_2D);
403
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 if (UNLIKELY(s.alpha < 0xFF)) {
405 // We have an alpha-modulation. We need to modulate all
406 // texture components by alpha because we're always using
407 // premultiplied alpha.
408
409 // If the texture doesn't have an alpha channel we can
410 // use REPLACE and switch to non premultiplied alpha
411 // blending (SRCA/ONE_MINUS_SRCA).
412
413 GLenum env, src;
414 if (needsBlending()) {
415 env = GL_MODULATE;
416 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
417 } else {
418 env = GL_REPLACE;
419 src = GL_SRC_ALPHA;
420 }
421 const GGLfixed alpha = (s.alpha << 16)/255;
422 glColor4x(alpha, alpha, alpha, alpha);
423 glEnable(GL_BLEND);
424 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
425 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
426 } else {
427 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
428 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
429 if (needsBlending()) {
430 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
431 glEnable(GL_BLEND);
432 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
433 } else {
434 glDisable(GL_BLEND);
435 }
436 }
437
Mathias Agopian95a666b2009-09-24 14:57:26 -0700438 Region::const_iterator it = clip.begin();
439 Region::const_iterator const end = clip.end();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 if (UNLIKELY(transformed()
441 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
442 {
443 //StopWatch watch("GL transformed");
Mathias Agopian95a666b2009-09-24 14:57:26 -0700444 const GLfixed texCoords[4][2] = {
445 { 0, 0 },
446 { 0, 0x10000 },
447 { 0x10000, 0x10000 },
448 { 0x10000, 0 }
449 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450
Mathias Agopian95a666b2009-09-24 14:57:26 -0700451 glMatrixMode(GL_TEXTURE);
452 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453
Mathias Agopian95a666b2009-09-24 14:57:26 -0700454 // the texture's source is rotated
Chih-Chung Chang5994a332010-01-22 16:30:39 -0800455 switch (texture.transform) {
456 case HAL_TRANSFORM_ROT_90:
457 glTranslatef(0, 1, 0);
458 glRotatef(-90, 0, 0, 1);
459 break;
460 case HAL_TRANSFORM_ROT_180:
461 glTranslatef(1, 1, 0);
462 glRotatef(-180, 0, 0, 1);
463 break;
464 case HAL_TRANSFORM_ROT_270:
465 glTranslatef(1, 0, 0);
466 glRotatef(-270, 0, 0, 1);
467 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468 }
Chih-Chung Chang5994a332010-01-22 16:30:39 -0800469
Mathias Agopian3330b202009-10-05 17:07:12 -0700470 if (texture.NPOTAdjust) {
471 glScalef(texture.wScale, texture.hScale, 1.0f);
Mathias Agopian95a666b2009-09-24 14:57:26 -0700472 }
473
474 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
475 glVertexPointer(2, GL_FIXED, 0, mVertices);
476 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
477
478 while (it != end) {
479 const Rect& r = *it++;
480 const GLint sy = fbHeight - (r.top + r.height());
481 glScissor(r.left, sy, r.width(), r.height());
482 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
483 }
484 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700486 GLint crop[4] = { 0, height, width, -height };
487 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
488 int x = tx();
489 int y = ty();
490 y = fbHeight - (y + height);
491 while (it != end) {
492 const Rect& r = *it++;
493 const GLint sy = fbHeight - (r.top + r.height());
494 glScissor(r.left, sy, r.width(), r.height());
495 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496 }
497 }
498}
499
500void LayerBase::validateTexture(GLint textureName) const
501{
502 glBindTexture(GL_TEXTURE_2D, textureName);
503 // TODO: reload the texture if needed
504 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700505 if (mUseLinearFiltering) {
506 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
507 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
508 } else {
509 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
510 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
511 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700512
513 if (needsDithering()) {
514 glEnable(GL_DITHER);
515 } else {
516 glDisable(GL_DITHER);
517 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518}
519
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800520bool LayerBase::isSupportedYuvFormat(int format) const
521{
522 switch (format) {
523 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
524 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
525 case HAL_PIXEL_FORMAT_YCbCr_422_P:
526 case HAL_PIXEL_FORMAT_YCbCr_420_P:
527 case HAL_PIXEL_FORMAT_YCbCr_422_I:
528 case HAL_PIXEL_FORMAT_YCbCr_420_I:
529 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
530 return true;
531 }
532 return false;
533}
534
Mathias Agopian3330b202009-10-05 17:07:12 -0700535void LayerBase::loadTexture(Texture* texture,
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700536 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537{
Mathias Agopian3330b202009-10-05 17:07:12 -0700538 if (texture->name == -1U) {
539 // uh?
540 return;
541 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700542
Mathias Agopian3330b202009-10-05 17:07:12 -0700543 glBindTexture(GL_TEXTURE_2D, texture->name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544
545 /*
546 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700547 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
549 * need to do something reasonable (here creating a bigger texture).
550 *
551 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
552 *
553 * This situation doesn't happen often, but some h/w have a limitation
554 * for their framebuffer (eg: must be multiple of 8 pixels), and
555 * we need to take that into account when using these buffers as
556 * textures.
557 *
558 * This should never be a problem with POT textures
559 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700560
561 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
562 unpack = 1 << ((unpack > 3) ? 3 : unpack);
563 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
564
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 /*
566 * round to POT if needed
567 */
Mathias Agopian3330b202009-10-05 17:07:12 -0700568 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
569 texture->NPOTAdjust = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700571
Mathias Agopian3330b202009-10-05 17:07:12 -0700572 if (texture->NPOTAdjust) {
573 // find the smallest power-of-two that will accommodate our surface
574 texture->potWidth = 1 << (31 - clz(t.width));
575 texture->potHeight = 1 << (31 - clz(t.height));
576 if (texture->potWidth < t.width) texture->potWidth <<= 1;
577 if (texture->potHeight < t.height) texture->potHeight <<= 1;
578 texture->wScale = float(t.width) / texture->potWidth;
579 texture->hScale = float(t.height) / texture->potHeight;
580 } else {
581 texture->potWidth = t.width;
582 texture->potHeight = t.height;
583 }
584
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 Rect bounds(dirty.bounds());
586 GLvoid* data = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700587 if (texture->width != t.width || texture->height != t.height) {
588 texture->width = t.width;
589 texture->height = t.height;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590
Mathias Agopian3330b202009-10-05 17:07:12 -0700591 // texture size changed, we need to create a new one
592 bounds.set(Rect(t.width, t.height));
593 if (t.width == texture->potWidth &&
594 t.height == texture->potHeight) {
595 // we can do it one pass
596 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700598
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800599 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700600 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700601 GL_RGB, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700602 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800603 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700605 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800607 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
608 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700609 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700610 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700611 GL_RGBA, GL_UNSIGNED_BYTE, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800612 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700613 // just show the Y plane of YUV buffers
614 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700615 GL_LUMINANCE, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700616 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
617 } else {
618 // oops, we don't handle this format!
619 LOGE("layer %p, texture=%d, using format %d, which is not "
Mathias Agopian3330b202009-10-05 17:07:12 -0700620 "supported by the GL", this, texture->name, t.format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700621 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700622 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700623 if (!data) {
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800624 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700625 glTexSubImage2D(GL_TEXTURE_2D, 0,
626 0, bounds.top, t.width, bounds.height(),
627 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
628 t.data + bounds.top*t.stride*2);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800629 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700630 glTexSubImage2D(GL_TEXTURE_2D, 0,
631 0, bounds.top, t.width, bounds.height(),
632 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
633 t.data + bounds.top*t.stride*2);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800634 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
635 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700636 glTexSubImage2D(GL_TEXTURE_2D, 0,
637 0, bounds.top, t.width, bounds.height(),
638 GL_RGBA, GL_UNSIGNED_BYTE,
639 t.data + bounds.top*t.stride*4);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800640 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641 // just show the Y plane of YUV buffers
642 glTexSubImage2D(GL_TEXTURE_2D, 0,
643 0, bounds.top, t.width, bounds.height(),
644 GL_LUMINANCE, GL_UNSIGNED_BYTE,
645 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 }
647 }
648}
649
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700650status_t LayerBase::initializeEglImage(
651 const sp<GraphicBuffer>& buffer, Texture* texture)
652{
653 status_t err = NO_ERROR;
654
655 // we need to recreate the texture
656 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
657
658 // free the previous image
659 if (texture->image != EGL_NO_IMAGE_KHR) {
660 eglDestroyImageKHR(dpy, texture->image);
661 texture->image = EGL_NO_IMAGE_KHR;
662 }
663
664 // construct an EGL_NATIVE_BUFFER_ANDROID
665 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
666
667 // create the new EGLImageKHR
668 const EGLint attrs[] = {
669 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
670 EGL_NONE, EGL_NONE
671 };
672 texture->image = eglCreateImageKHR(
673 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
674 (EGLClientBuffer)clientBuf, attrs);
675
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700676 if (texture->image != EGL_NO_IMAGE_KHR) {
677 glBindTexture(GL_TEXTURE_2D, texture->name);
678 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
679 (GLeglImageOES)texture->image);
680 GLint error = glGetError();
681 if (UNLIKELY(error != GL_NO_ERROR)) {
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700682 LOGE("layer=%p, glEGLImageTargetTexture2DOES(%p) "
683 "failed err=0x%04x",
684 this, texture->image, error);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700685 err = INVALID_OPERATION;
686 } else {
687 // Everything went okay!
688 texture->NPOTAdjust = false;
689 texture->dirty = false;
690 texture->width = clientBuf->width;
691 texture->height = clientBuf->height;
692 }
693 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800694 LOGE("layer=%p, eglCreateImageKHR() failed. err=0x%4x",
695 this, eglGetError());
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700696 err = INVALID_OPERATION;
697 }
698 return err;
699}
700
701
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702// ---------------------------------------------------------------------------
703
Mathias Agopian2e123242009-06-23 20:06:46 -0700704int32_t LayerBaseClient::sIdentity = 0;
705
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700707 const sp<Client>& client, int32_t i)
Mathias Agopiand1296592010-03-09 19:17:47 -0800708 : LayerBase(flinger, display), lcblk(NULL), client(client), mIndex(i),
Mathias Agopian948d69f2010-03-08 19:29:09 -0800709 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700711 lcblk = new SharedBufferServer(
712 client->ctrlblk, i, NUM_BUFFERS,
713 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700714}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700716void LayerBaseClient::onFirstRef()
717{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700718 sp<Client> client(this->client.promote());
719 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700720 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 }
722}
723
724LayerBaseClient::~LayerBaseClient()
725{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700726 sp<Client> client(this->client.promote());
727 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 client->free(mIndex);
729 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700730 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731}
732
Mathias Agopianf9d93272009-06-19 17:00:27 -0700733int32_t LayerBaseClient::serverIndex() const
734{
735 sp<Client> client(this->client.promote());
736 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 return (client->cid<<16)|mIndex;
738 }
739 return 0xFFFF0000 | mIndex;
740}
741
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700742sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700744 sp<Surface> s;
745 Mutex::Autolock _l(mLock);
746 s = mClientSurface.promote();
747 if (s == 0) {
748 s = createSurface();
749 mClientSurface = s;
750 }
751 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752}
753
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700754sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
755{
Mathias Agopian9a112062009-04-17 19:36:26 -0700756 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700757 const_cast<LayerBaseClient *>(this));
758}
759
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700760// called with SurfaceFlinger::mStateLock as soon as the layer is entered
761// in the purgatory list
762void LayerBaseClient::onRemoved()
763{
764 // wake up the condition
765 lcblk->setStatus(NO_INIT);
766}
767
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700768// ---------------------------------------------------------------------------
769
Mathias Agopian9a112062009-04-17 19:36:26 -0700770LayerBaseClient::Surface::Surface(
771 const sp<SurfaceFlinger>& flinger,
772 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700773 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700774 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
775{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700776}
777
Mathias Agopian9a112062009-04-17 19:36:26 -0700778LayerBaseClient::Surface::~Surface()
779{
780 /*
781 * This is a good place to clean-up all client resources
782 */
783
784 // destroy client resources
785 sp<LayerBaseClient> layer = getOwner();
786 if (layer != 0) {
787 mFlinger->destroySurface(layer);
788 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700789}
790
791sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
792 sp<LayerBaseClient> owner(mOwner.promote());
793 return owner;
794}
795
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700796status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700797 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700798{
799 switch (code) {
800 case REGISTER_BUFFERS:
801 case UNREGISTER_BUFFERS:
802 case CREATE_OVERLAY:
803 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700804 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
805 IPCThreadState* ipc = IPCThreadState::self();
806 const int pid = ipc->getCallingPid();
807 const int uid = ipc->getCallingUid();
808 LOGE("Permission Denial: "
809 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
810 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700811 }
812 }
813 }
814 return BnSurface::onTransact(code, data, reply, flags);
815}
816
Mathias Agopian3330b202009-10-05 17:07:12 -0700817sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700818{
819 return NULL;
820}
821
822status_t LayerBaseClient::Surface::registerBuffers(
823 const ISurface::BufferHeap& buffers)
824{
825 return INVALID_OPERATION;
826}
827
828void LayerBaseClient::Surface::postBuffer(ssize_t offset)
829{
830}
831
832void LayerBaseClient::Surface::unregisterBuffers()
833{
834}
835
836sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800837 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700838{
839 return NULL;
840};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841
842// ---------------------------------------------------------------------------
843
844}; // namespace android