blob: fdf6627e42eac47a92644c94e3866d22a4544284 [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),
57 mPremultipliedAlpha(true),
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
68const GraphicPlane& LayerBase::graphicPlane(int dpy) const
69{
70 return mFlinger->graphicPlane(dpy);
71}
72
73GraphicPlane& LayerBase::graphicPlane(int dpy)
74{
75 return mFlinger->graphicPlane(dpy);
76}
77
78void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
79{
80 uint32_t layerFlags = 0;
81 if (flags & ISurfaceComposer::eHidden)
82 layerFlags = ISurfaceComposer::eLayerHidden;
83
84 if (flags & ISurfaceComposer::eNonPremultiplied)
85 mPremultipliedAlpha = false;
86
Mathias Agopian7e4a5872009-09-29 22:39:22 -070087 mCurrentState.z = 0;
88 mCurrentState.w = w;
89 mCurrentState.h = h;
90 mCurrentState.requested_w = w;
91 mCurrentState.requested_h = h;
92 mCurrentState.alpha = 0xFF;
93 mCurrentState.flags = layerFlags;
94 mCurrentState.sequence = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mCurrentState.transform.set(0, 0);
96
97 // drawing state & current state are identical
98 mDrawingState = mCurrentState;
99}
100
Mathias Agopianba6be542009-09-29 22:32:36 -0700101void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103}
104void LayerBase::forceVisibilityTransaction() {
105 // this can be called without SurfaceFlinger.mStateLock, but if we
106 // can atomically increment the sequence number, it doesn't matter.
107 android_atomic_inc(&mCurrentState.sequence);
108 requestTransaction();
109}
110bool LayerBase::requestTransaction() {
111 int32_t old = setTransactionFlags(eTransactionNeeded);
112 return ((old & eTransactionNeeded) == 0);
113}
114uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
115 return android_atomic_and(~flags, &mTransactionFlags) & flags;
116}
117uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
118 return android_atomic_or(flags, &mTransactionFlags);
119}
120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121bool LayerBase::setPosition(int32_t x, int32_t y) {
122 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
123 return false;
124 mCurrentState.sequence++;
125 mCurrentState.transform.set(x, y);
126 requestTransaction();
127 return true;
128}
129bool LayerBase::setLayer(uint32_t z) {
130 if (mCurrentState.z == z)
131 return false;
132 mCurrentState.sequence++;
133 mCurrentState.z = z;
134 requestTransaction();
135 return true;
136}
137bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700138 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 return false;
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700140 mCurrentState.requested_w = w;
141 mCurrentState.requested_h = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 requestTransaction();
143 return true;
144}
145bool LayerBase::setAlpha(uint8_t alpha) {
146 if (mCurrentState.alpha == alpha)
147 return false;
148 mCurrentState.sequence++;
149 mCurrentState.alpha = alpha;
150 requestTransaction();
151 return true;
152}
153bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
154 // TODO: check the matrix has changed
155 mCurrentState.sequence++;
156 mCurrentState.transform.set(
157 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
158 requestTransaction();
159 return true;
160}
161bool LayerBase::setTransparentRegionHint(const Region& transparent) {
162 // TODO: check the region has changed
163 mCurrentState.sequence++;
164 mCurrentState.transparentRegion = transparent;
165 requestTransaction();
166 return true;
167}
168bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
169 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
170 if (mCurrentState.flags == newFlags)
171 return false;
172 mCurrentState.sequence++;
173 mCurrentState.flags = newFlags;
174 requestTransaction();
175 return true;
176}
177
178Rect LayerBase::visibleBounds() const
179{
180 return mTransformedBounds;
181}
182
183void LayerBase::setVisibleRegion(const Region& visibleRegion) {
184 // always called from main thread
185 visibleRegionScreen = visibleRegion;
186}
187
188void LayerBase::setCoveredRegion(const Region& coveredRegion) {
189 // always called from main thread
190 coveredRegionScreen = coveredRegion;
191}
192
193uint32_t LayerBase::doTransaction(uint32_t flags)
194{
195 const Layer::State& front(drawingState());
196 const Layer::State& temp(currentState());
197
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700198 if ((front.requested_w != temp.requested_w) ||
199 (front.requested_h != temp.requested_h)) {
200 // resize the layer, set the physical size to the requested size
201 Layer::State& editTemp(currentState());
202 editTemp.w = temp.requested_w;
203 editTemp.h = temp.requested_h;
204 }
205
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700206 if ((front.w != temp.w) || (front.h != temp.h)) {
207 // invalidate and recompute the visible regions if needed
208 flags |= Layer::eVisibleRegion;
209 this->contentDirty = true;
210 }
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 if (temp.sequence != front.sequence) {
213 // invalidate and recompute the visible regions if needed
214 flags |= eVisibleRegion;
215 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700216
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700217 const bool linearFiltering = mUseLinearFiltering;
218 mUseLinearFiltering = false;
219 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
220 // we may use linear filtering, if the matrix scales us
221 const uint8_t type = temp.transform.getType();
222 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
223 mUseLinearFiltering = true;
224 }
225 }
226 }
227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700229 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 return flags;
231}
232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233void 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
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700239 uint32_t w = s.w;
240 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 tr.transform(mVertices[0], 0, 0);
242 tr.transform(mVertices[1], 0, h);
243 tr.transform(mVertices[2], w, h);
244 tr.transform(mVertices[3], w, 0);
245 if (UNLIKELY(transformed)) {
246 // NOTE: here we could also punt if we have too many rectangles
247 // in the transparent region
248 if (tr.preserveRects()) {
249 // transform the transparent region
250 transparentRegionScreen = tr.transform(s.transparentRegion);
251 } else {
252 // transformation too complex, can't do the transparent region
253 // optimization.
254 transparentRegionScreen.clear();
255 }
256 } else {
257 transparentRegionScreen = s.transparentRegion;
258 }
259
260 // cache a few things...
261 mOrientation = tr.getOrientation();
262 mTransformedBounds = tr.makeBounds(w, h);
263 mTransformed = transformed;
264 mLeft = tr.tx();
265 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266}
267
268void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
269{
270}
271
272void LayerBase::unlockPageFlip(
273 const Transform& planeTransform, Region& outDirtyRegion)
274{
275 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
276 outDirtyRegion.orSelf(visibleRegionScreen);
277 }
278}
279
280void LayerBase::finishPageFlip()
281{
282}
283
284void LayerBase::invalidate()
285{
286 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
287 mFlinger->signalEvent();
288 }
289}
290
291void LayerBase::drawRegion(const Region& reg) const
292{
Mathias Agopian20f68782009-05-11 00:03:41 -0700293 Region::const_iterator it = reg.begin();
294 Region::const_iterator const end = reg.end();
295 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 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);
Mathias Agopian20f68782009-05-11 00:03:41 -0700303 while (it != end) {
304 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 const GLint sy = fbHeight - (r.top + r.height());
306 glScissor(r.left, sy, r.width(), r.height());
307 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
308 }
309 }
310}
311
312void LayerBase::draw(const Region& inClip) const
313{
314 // invalidate the region we'll update
315 Region clip(inClip); // copy-on-write, so no-op most of the time
316
317 // Remove the transparent area from the clipping region
318 const State& s = drawingState();
319 if (LIKELY(!s.transparentRegion.isEmpty())) {
320 clip.subtract(transparentRegionScreen);
321 if (clip.isEmpty()) {
322 // usually this won't happen because this should be taken care of
323 // by SurfaceFlinger::computeVisibleRegions()
324 return;
325 }
326 }
327
328 // reset GL state
329 glEnable(GL_SCISSOR_TEST);
330
331 onDraw(clip);
332
333 /*
334 glDisable(GL_TEXTURE_2D);
335 glDisable(GL_DITHER);
336 glEnable(GL_BLEND);
337 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
338 glColor4x(0, 0x8000, 0, 0x10000);
339 drawRegion(transparentRegionScreen);
340 glDisable(GL_BLEND);
341 */
342}
343
344GLuint LayerBase::createTexture() const
345{
346 GLuint textureName = -1;
347 glGenTextures(1, &textureName);
348 glBindTexture(GL_TEXTURE_2D, textureName);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
350 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700351 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
352 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 return textureName;
354}
355
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700356void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
357 GLclampx green, GLclampx blue,
358 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359{
360 const DisplayHardware& hw(graphicPlane(0).displayHardware());
361 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700362 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363 glDisable(GL_TEXTURE_2D);
364 glDisable(GL_BLEND);
365 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700366
367 Region::const_iterator it = clip.begin();
368 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700369 glEnable(GL_SCISSOR_TEST);
370 glVertexPointer(2, GL_FIXED, 0, mVertices);
371 while (it != end) {
372 const Rect& r = *it++;
373 const GLint sy = fbHeight - (r.top + r.height());
374 glScissor(r.left, sy, r.width(), r.height());
375 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 }
377}
378
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700379void LayerBase::clearWithOpenGL(const Region& clip) const
380{
381 clearWithOpenGL(clip,0,0,0,0);
382}
383
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700384void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385{
386 const DisplayHardware& hw(graphicPlane(0).displayHardware());
387 const uint32_t fbHeight = hw.getHeight();
388 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700389
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700391 validateTexture(texture.name);
392 uint32_t width = texture.width;
393 uint32_t height = texture.height;
394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 glEnable(GL_TEXTURE_2D);
396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 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
Mathias Agopian95a666b2009-09-24 14:57:26 -0700431 Region::const_iterator it = clip.begin();
432 Region::const_iterator const end = clip.end();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 if (UNLIKELY(transformed()
434 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
435 {
436 //StopWatch watch("GL transformed");
Mathias Agopian95a666b2009-09-24 14:57:26 -0700437 const GLfixed texCoords[4][2] = {
438 { 0, 0 },
439 { 0, 0x10000 },
440 { 0x10000, 0x10000 },
441 { 0x10000, 0 }
442 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443
Mathias Agopian95a666b2009-09-24 14:57:26 -0700444 glMatrixMode(GL_TEXTURE);
445 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446
Mathias Agopian95a666b2009-09-24 14:57:26 -0700447 // the texture's source is rotated
Chih-Chung Chang5994a332010-01-22 16:30:39 -0800448 switch (texture.transform) {
449 case HAL_TRANSFORM_ROT_90:
450 glTranslatef(0, 1, 0);
451 glRotatef(-90, 0, 0, 1);
452 break;
453 case HAL_TRANSFORM_ROT_180:
454 glTranslatef(1, 1, 0);
455 glRotatef(-180, 0, 0, 1);
456 break;
457 case HAL_TRANSFORM_ROT_270:
458 glTranslatef(1, 0, 0);
459 glRotatef(-270, 0, 0, 1);
460 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 }
Chih-Chung Chang5994a332010-01-22 16:30:39 -0800462
Mathias Agopian3330b202009-10-05 17:07:12 -0700463 if (texture.NPOTAdjust) {
464 glScalef(texture.wScale, texture.hScale, 1.0f);
Mathias Agopian95a666b2009-09-24 14:57:26 -0700465 }
466
467 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
468 glVertexPointer(2, GL_FIXED, 0, mVertices);
469 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
470
471 while (it != end) {
472 const Rect& r = *it++;
473 const GLint sy = fbHeight - (r.top + r.height());
474 glScissor(r.left, sy, r.width(), r.height());
475 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
476 }
477 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700479 GLint crop[4] = { 0, height, width, -height };
480 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
481 int x = tx();
482 int y = ty();
483 y = fbHeight - (y + height);
484 while (it != end) {
485 const Rect& r = *it++;
486 const GLint sy = fbHeight - (r.top + r.height());
487 glScissor(r.left, sy, r.width(), r.height());
488 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 }
490 }
491}
492
493void LayerBase::validateTexture(GLint textureName) const
494{
495 glBindTexture(GL_TEXTURE_2D, textureName);
496 // TODO: reload the texture if needed
497 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700498 if (mUseLinearFiltering) {
499 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
500 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
501 } else {
502 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
503 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
504 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700505
506 if (needsDithering()) {
507 glEnable(GL_DITHER);
508 } else {
509 glDisable(GL_DITHER);
510 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511}
512
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800513bool LayerBase::isSupportedYuvFormat(int format) const
514{
515 switch (format) {
516 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
517 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
518 case HAL_PIXEL_FORMAT_YCbCr_422_P:
519 case HAL_PIXEL_FORMAT_YCbCr_420_P:
520 case HAL_PIXEL_FORMAT_YCbCr_422_I:
521 case HAL_PIXEL_FORMAT_YCbCr_420_I:
522 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
523 return true;
524 }
525 return false;
526}
527
Mathias Agopian3330b202009-10-05 17:07:12 -0700528void LayerBase::loadTexture(Texture* texture,
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700529 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530{
Mathias Agopian3330b202009-10-05 17:07:12 -0700531 if (texture->name == -1U) {
532 // uh?
533 return;
534 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700535
Mathias Agopian3330b202009-10-05 17:07:12 -0700536 glBindTexture(GL_TEXTURE_2D, texture->name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537
538 /*
539 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700540 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
542 * need to do something reasonable (here creating a bigger texture).
543 *
544 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
545 *
546 * This situation doesn't happen often, but some h/w have a limitation
547 * for their framebuffer (eg: must be multiple of 8 pixels), and
548 * we need to take that into account when using these buffers as
549 * textures.
550 *
551 * This should never be a problem with POT textures
552 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553
554 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
555 unpack = 1 << ((unpack > 3) ? 3 : unpack);
556 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 /*
559 * round to POT if needed
560 */
Mathias Agopian3330b202009-10-05 17:07:12 -0700561 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
562 texture->NPOTAdjust = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564
Mathias Agopian3330b202009-10-05 17:07:12 -0700565 if (texture->NPOTAdjust) {
566 // find the smallest power-of-two that will accommodate our surface
567 texture->potWidth = 1 << (31 - clz(t.width));
568 texture->potHeight = 1 << (31 - clz(t.height));
569 if (texture->potWidth < t.width) texture->potWidth <<= 1;
570 if (texture->potHeight < t.height) texture->potHeight <<= 1;
571 texture->wScale = float(t.width) / texture->potWidth;
572 texture->hScale = float(t.height) / texture->potHeight;
573 } else {
574 texture->potWidth = t.width;
575 texture->potHeight = t.height;
576 }
577
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700578 Rect bounds(dirty.bounds());
579 GLvoid* data = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700580 if (texture->width != t.width || texture->height != t.height) {
581 texture->width = t.width;
582 texture->height = t.height;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583
Mathias Agopian3330b202009-10-05 17:07:12 -0700584 // texture size changed, we need to create a new one
585 bounds.set(Rect(t.width, t.height));
586 if (t.width == texture->potWidth &&
587 t.height == texture->potHeight) {
588 // we can do it one pass
589 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700591
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800592 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700593 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700594 GL_RGB, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700595 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800596 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700597 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700598 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800600 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
601 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700602 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700603 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604 GL_RGBA, GL_UNSIGNED_BYTE, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800605 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606 // just show the Y plane of YUV buffers
607 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700608 GL_LUMINANCE, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700609 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
610 } else {
611 // oops, we don't handle this format!
612 LOGE("layer %p, texture=%d, using format %d, which is not "
Mathias Agopian3330b202009-10-05 17:07:12 -0700613 "supported by the GL", this, texture->name, t.format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700615 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700616 if (!data) {
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800617 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700618 glTexSubImage2D(GL_TEXTURE_2D, 0,
619 0, bounds.top, t.width, bounds.height(),
620 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
621 t.data + bounds.top*t.stride*2);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800622 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700623 glTexSubImage2D(GL_TEXTURE_2D, 0,
624 0, bounds.top, t.width, bounds.height(),
625 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
626 t.data + bounds.top*t.stride*2);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800627 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
628 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700629 glTexSubImage2D(GL_TEXTURE_2D, 0,
630 0, bounds.top, t.width, bounds.height(),
631 GL_RGBA, GL_UNSIGNED_BYTE,
632 t.data + bounds.top*t.stride*4);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800633 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700634 // just show the Y plane of YUV buffers
635 glTexSubImage2D(GL_TEXTURE_2D, 0,
636 0, bounds.top, t.width, bounds.height(),
637 GL_LUMINANCE, GL_UNSIGNED_BYTE,
638 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 }
640 }
641}
642
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700643status_t LayerBase::initializeEglImage(
644 const sp<GraphicBuffer>& buffer, Texture* texture)
645{
646 status_t err = NO_ERROR;
647
648 // we need to recreate the texture
649 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
650
651 // free the previous image
652 if (texture->image != EGL_NO_IMAGE_KHR) {
653 eglDestroyImageKHR(dpy, texture->image);
654 texture->image = EGL_NO_IMAGE_KHR;
655 }
656
657 // construct an EGL_NATIVE_BUFFER_ANDROID
658 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
659
660 // create the new EGLImageKHR
661 const EGLint attrs[] = {
662 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
663 EGL_NONE, EGL_NONE
664 };
665 texture->image = eglCreateImageKHR(
666 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
667 (EGLClientBuffer)clientBuf, attrs);
668
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700669 if (texture->image != EGL_NO_IMAGE_KHR) {
670 glBindTexture(GL_TEXTURE_2D, texture->name);
671 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
672 (GLeglImageOES)texture->image);
673 GLint error = glGetError();
674 if (UNLIKELY(error != GL_NO_ERROR)) {
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700675 LOGE("layer=%p, glEGLImageTargetTexture2DOES(%p) "
676 "failed err=0x%04x",
677 this, texture->image, error);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700678 err = INVALID_OPERATION;
679 } else {
680 // Everything went okay!
681 texture->NPOTAdjust = false;
682 texture->dirty = false;
683 texture->width = clientBuf->width;
684 texture->height = clientBuf->height;
685 }
686 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800687 LOGE("layer=%p, eglCreateImageKHR() failed. err=0x%4x",
688 this, eglGetError());
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700689 err = INVALID_OPERATION;
690 }
691 return err;
692}
693
694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695// ---------------------------------------------------------------------------
696
Mathias Agopian2e123242009-06-23 20:06:46 -0700697int32_t LayerBaseClient::sIdentity = 0;
698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700700 const sp<Client>& client, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -0700701 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopian948d69f2010-03-08 19:29:09 -0800702 mDebug(false), mIndex(i),
703 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700705 lcblk = new SharedBufferServer(
706 client->ctrlblk, i, NUM_BUFFERS,
707 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700710void LayerBaseClient::onFirstRef()
711{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700712 sp<Client> client(this->client.promote());
713 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700714 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 }
716}
717
718LayerBaseClient::~LayerBaseClient()
719{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700720 sp<Client> client(this->client.promote());
721 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 client->free(mIndex);
723 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700724 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725}
726
Mathias Agopian285dbde2010-03-01 16:09:43 -0800727void LayerBaseClient::setName(const String8& name) {
728 mName = name;
729}
730
731String8 LayerBaseClient::getName() const {
732 return mName;
733}
734
Mathias Agopianf9d93272009-06-19 17:00:27 -0700735int32_t LayerBaseClient::serverIndex() const
736{
737 sp<Client> client(this->client.promote());
738 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 return (client->cid<<16)|mIndex;
740 }
741 return 0xFFFF0000 | mIndex;
742}
743
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700744sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700746 sp<Surface> s;
747 Mutex::Autolock _l(mLock);
748 s = mClientSurface.promote();
749 if (s == 0) {
750 s = createSurface();
751 mClientSurface = s;
752 }
753 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754}
755
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700756sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
757{
Mathias Agopian9a112062009-04-17 19:36:26 -0700758 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700759 const_cast<LayerBaseClient *>(this));
760}
761
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700762// called with SurfaceFlinger::mStateLock as soon as the layer is entered
763// in the purgatory list
764void LayerBaseClient::onRemoved()
765{
766 // wake up the condition
767 lcblk->setStatus(NO_INIT);
768}
769
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700770// ---------------------------------------------------------------------------
771
Mathias Agopian9a112062009-04-17 19:36:26 -0700772LayerBaseClient::Surface::Surface(
773 const sp<SurfaceFlinger>& flinger,
774 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700775 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700776 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
777{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700778}
779
Mathias Agopian9a112062009-04-17 19:36:26 -0700780LayerBaseClient::Surface::~Surface()
781{
782 /*
783 * This is a good place to clean-up all client resources
784 */
785
786 // destroy client resources
787 sp<LayerBaseClient> layer = getOwner();
788 if (layer != 0) {
789 mFlinger->destroySurface(layer);
790 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700791}
792
793sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
794 sp<LayerBaseClient> owner(mOwner.promote());
795 return owner;
796}
797
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700798status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700799 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700800{
801 switch (code) {
802 case REGISTER_BUFFERS:
803 case UNREGISTER_BUFFERS:
804 case CREATE_OVERLAY:
805 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700806 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
807 IPCThreadState* ipc = IPCThreadState::self();
808 const int pid = ipc->getCallingPid();
809 const int uid = ipc->getCallingUid();
810 LOGE("Permission Denial: "
811 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
812 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700813 }
814 }
815 }
816 return BnSurface::onTransact(code, data, reply, flags);
817}
818
Mathias Agopian3330b202009-10-05 17:07:12 -0700819sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700820{
821 return NULL;
822}
823
824status_t LayerBaseClient::Surface::registerBuffers(
825 const ISurface::BufferHeap& buffers)
826{
827 return INVALID_OPERATION;
828}
829
830void LayerBaseClient::Surface::postBuffer(ssize_t offset)
831{
832}
833
834void LayerBaseClient::Surface::unregisterBuffers()
835{
836}
837
838sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800839 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700840{
841 return NULL;
842};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843
844// ---------------------------------------------------------------------------
845
846}; // namespace android