blob: 140f10cdd7f157ead832c71663bed1e32e233cda [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;
217 this->contentDirty = true;
218 }
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 if (temp.sequence != front.sequence) {
221 // invalidate and recompute the visible regions if needed
222 flags |= eVisibleRegion;
223 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700224
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700225 const bool linearFiltering = mUseLinearFiltering;
226 mUseLinearFiltering = false;
227 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
228 // we may use linear filtering, if the matrix scales us
229 const uint8_t type = temp.transform.getType();
230 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
231 mUseLinearFiltering = true;
232 }
233 }
234 }
235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700237 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 return flags;
239}
240
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241void LayerBase::validateVisibility(const Transform& planeTransform)
242{
243 const Layer::State& s(drawingState());
244 const Transform tr(planeTransform * s.transform);
245 const bool transformed = tr.transformed();
246
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700247 uint32_t w = s.w;
248 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 tr.transform(mVertices[0], 0, 0);
250 tr.transform(mVertices[1], 0, h);
251 tr.transform(mVertices[2], w, h);
252 tr.transform(mVertices[3], w, 0);
253 if (UNLIKELY(transformed)) {
254 // NOTE: here we could also punt if we have too many rectangles
255 // in the transparent region
256 if (tr.preserveRects()) {
257 // transform the transparent region
258 transparentRegionScreen = tr.transform(s.transparentRegion);
259 } else {
260 // transformation too complex, can't do the transparent region
261 // optimization.
262 transparentRegionScreen.clear();
263 }
264 } else {
265 transparentRegionScreen = s.transparentRegion;
266 }
267
268 // cache a few things...
269 mOrientation = tr.getOrientation();
270 mTransformedBounds = tr.makeBounds(w, h);
271 mTransformed = transformed;
272 mLeft = tr.tx();
273 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274}
275
276void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
277{
278}
279
280void LayerBase::unlockPageFlip(
281 const Transform& planeTransform, Region& outDirtyRegion)
282{
283 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
284 outDirtyRegion.orSelf(visibleRegionScreen);
285 }
286}
287
288void LayerBase::finishPageFlip()
289{
290}
291
292void LayerBase::invalidate()
293{
294 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
295 mFlinger->signalEvent();
296 }
297}
298
299void LayerBase::drawRegion(const Region& reg) const
300{
Mathias Agopian20f68782009-05-11 00:03:41 -0700301 Region::const_iterator it = reg.begin();
302 Region::const_iterator const end = reg.end();
303 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 Rect r;
305 const DisplayHardware& hw(graphicPlane(0).displayHardware());
306 const int32_t fbWidth = hw.getWidth();
307 const int32_t fbHeight = hw.getHeight();
308 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
309 { fbWidth, fbHeight }, { 0, fbHeight } };
310 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700311 while (it != end) {
312 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 const GLint sy = fbHeight - (r.top + r.height());
314 glScissor(r.left, sy, r.width(), r.height());
315 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
316 }
317 }
318}
319
320void LayerBase::draw(const Region& inClip) const
321{
322 // invalidate the region we'll update
323 Region clip(inClip); // copy-on-write, so no-op most of the time
324
325 // Remove the transparent area from the clipping region
326 const State& s = drawingState();
327 if (LIKELY(!s.transparentRegion.isEmpty())) {
328 clip.subtract(transparentRegionScreen);
329 if (clip.isEmpty()) {
330 // usually this won't happen because this should be taken care of
331 // by SurfaceFlinger::computeVisibleRegions()
332 return;
333 }
334 }
335
336 // reset GL state
337 glEnable(GL_SCISSOR_TEST);
338
339 onDraw(clip);
340
341 /*
342 glDisable(GL_TEXTURE_2D);
343 glDisable(GL_DITHER);
344 glEnable(GL_BLEND);
345 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
346 glColor4x(0, 0x8000, 0, 0x10000);
347 drawRegion(transparentRegionScreen);
348 glDisable(GL_BLEND);
349 */
350}
351
352GLuint LayerBase::createTexture() const
353{
354 GLuint textureName = -1;
355 glGenTextures(1, &textureName);
356 glBindTexture(GL_TEXTURE_2D, textureName);
357 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
358 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700359 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
360 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 return textureName;
362}
363
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700364void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
365 GLclampx green, GLclampx blue,
366 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367{
368 const DisplayHardware& hw(graphicPlane(0).displayHardware());
369 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700370 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 glDisable(GL_TEXTURE_2D);
372 glDisable(GL_BLEND);
373 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700374
375 Region::const_iterator it = clip.begin();
376 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700377 glEnable(GL_SCISSOR_TEST);
378 glVertexPointer(2, GL_FIXED, 0, mVertices);
379 while (it != end) {
380 const Rect& r = *it++;
381 const GLint sy = fbHeight - (r.top + r.height());
382 glScissor(r.left, sy, r.width(), r.height());
383 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 }
385}
386
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700387void LayerBase::clearWithOpenGL(const Region& clip) const
388{
389 clearWithOpenGL(clip,0,0,0,0);
390}
391
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700392void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393{
394 const DisplayHardware& hw(graphicPlane(0).displayHardware());
395 const uint32_t fbHeight = hw.getHeight();
396 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700397
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700399 validateTexture(texture.name);
400 uint32_t width = texture.width;
401 uint32_t height = texture.height;
402
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 glEnable(GL_TEXTURE_2D);
404
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405 if (UNLIKELY(s.alpha < 0xFF)) {
406 // We have an alpha-modulation. We need to modulate all
407 // texture components by alpha because we're always using
408 // premultiplied alpha.
409
410 // If the texture doesn't have an alpha channel we can
411 // use REPLACE and switch to non premultiplied alpha
412 // blending (SRCA/ONE_MINUS_SRCA).
413
414 GLenum env, src;
415 if (needsBlending()) {
416 env = GL_MODULATE;
417 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
418 } else {
419 env = GL_REPLACE;
420 src = GL_SRC_ALPHA;
421 }
422 const GGLfixed alpha = (s.alpha << 16)/255;
423 glColor4x(alpha, alpha, alpha, alpha);
424 glEnable(GL_BLEND);
425 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
426 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
427 } else {
428 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
429 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
430 if (needsBlending()) {
431 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
432 glEnable(GL_BLEND);
433 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
434 } else {
435 glDisable(GL_BLEND);
436 }
437 }
438
Mathias Agopian95a666b2009-09-24 14:57:26 -0700439 Region::const_iterator it = clip.begin();
440 Region::const_iterator const end = clip.end();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 if (UNLIKELY(transformed()
442 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
443 {
444 //StopWatch watch("GL transformed");
Mathias Agopian95a666b2009-09-24 14:57:26 -0700445 const GLfixed texCoords[4][2] = {
446 { 0, 0 },
447 { 0, 0x10000 },
448 { 0x10000, 0x10000 },
449 { 0x10000, 0 }
450 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451
Mathias Agopian95a666b2009-09-24 14:57:26 -0700452 glMatrixMode(GL_TEXTURE);
453 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454
Mathias Agopian95a666b2009-09-24 14:57:26 -0700455 // the texture's source is rotated
Chih-Chung Chang5994a332010-01-22 16:30:39 -0800456 switch (texture.transform) {
457 case HAL_TRANSFORM_ROT_90:
458 glTranslatef(0, 1, 0);
459 glRotatef(-90, 0, 0, 1);
460 break;
461 case HAL_TRANSFORM_ROT_180:
462 glTranslatef(1, 1, 0);
463 glRotatef(-180, 0, 0, 1);
464 break;
465 case HAL_TRANSFORM_ROT_270:
466 glTranslatef(1, 0, 0);
467 glRotatef(-270, 0, 0, 1);
468 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 }
Chih-Chung Chang5994a332010-01-22 16:30:39 -0800470
Mathias Agopian3330b202009-10-05 17:07:12 -0700471 if (texture.NPOTAdjust) {
472 glScalef(texture.wScale, texture.hScale, 1.0f);
Mathias Agopian95a666b2009-09-24 14:57:26 -0700473 }
474
475 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
476 glVertexPointer(2, GL_FIXED, 0, mVertices);
477 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
478
479 while (it != end) {
480 const Rect& r = *it++;
481 const GLint sy = fbHeight - (r.top + r.height());
482 glScissor(r.left, sy, r.width(), r.height());
483 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
484 }
485 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700487 GLint crop[4] = { 0, height, width, -height };
488 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
489 int x = tx();
490 int y = ty();
491 y = fbHeight - (y + height);
492 while (it != end) {
493 const Rect& r = *it++;
494 const GLint sy = fbHeight - (r.top + r.height());
495 glScissor(r.left, sy, r.width(), r.height());
496 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 }
498 }
499}
500
501void LayerBase::validateTexture(GLint textureName) const
502{
503 glBindTexture(GL_TEXTURE_2D, textureName);
504 // TODO: reload the texture if needed
505 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700506 if (mUseLinearFiltering) {
507 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
508 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
509 } else {
510 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
511 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
512 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700513
514 if (needsDithering()) {
515 glEnable(GL_DITHER);
516 } else {
517 glDisable(GL_DITHER);
518 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519}
520
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800521bool LayerBase::isSupportedYuvFormat(int format) const
522{
523 switch (format) {
524 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
525 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
526 case HAL_PIXEL_FORMAT_YCbCr_422_P:
527 case HAL_PIXEL_FORMAT_YCbCr_420_P:
528 case HAL_PIXEL_FORMAT_YCbCr_422_I:
529 case HAL_PIXEL_FORMAT_YCbCr_420_I:
530 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
531 return true;
532 }
533 return false;
534}
535
Mathias Agopian3330b202009-10-05 17:07:12 -0700536void LayerBase::loadTexture(Texture* texture,
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700537 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538{
Mathias Agopian3330b202009-10-05 17:07:12 -0700539 if (texture->name == -1U) {
540 // uh?
541 return;
542 }
Mathias Agopian57720c32009-10-21 16:27:21 -0700543
Mathias Agopian3330b202009-10-05 17:07:12 -0700544 glBindTexture(GL_TEXTURE_2D, texture->name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545
546 /*
547 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
550 * need to do something reasonable (here creating a bigger texture).
551 *
552 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
553 *
554 * This situation doesn't happen often, but some h/w have a limitation
555 * for their framebuffer (eg: must be multiple of 8 pixels), and
556 * we need to take that into account when using these buffers as
557 * textures.
558 *
559 * This should never be a problem with POT textures
560 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700561
562 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
563 unpack = 1 << ((unpack > 3) ? 3 : unpack);
564 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
565
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 /*
567 * round to POT if needed
568 */
Mathias Agopian3330b202009-10-05 17:07:12 -0700569 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
570 texture->NPOTAdjust = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572
Mathias Agopian3330b202009-10-05 17:07:12 -0700573 if (texture->NPOTAdjust) {
574 // find the smallest power-of-two that will accommodate our surface
575 texture->potWidth = 1 << (31 - clz(t.width));
576 texture->potHeight = 1 << (31 - clz(t.height));
577 if (texture->potWidth < t.width) texture->potWidth <<= 1;
578 if (texture->potHeight < t.height) texture->potHeight <<= 1;
579 texture->wScale = float(t.width) / texture->potWidth;
580 texture->hScale = float(t.height) / texture->potHeight;
581 } else {
582 texture->potWidth = t.width;
583 texture->potHeight = t.height;
584 }
585
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586 Rect bounds(dirty.bounds());
587 GLvoid* data = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700588 if (texture->width != t.width || texture->height != t.height) {
589 texture->width = t.width;
590 texture->height = t.height;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591
Mathias Agopian3330b202009-10-05 17:07:12 -0700592 // texture size changed, we need to create a new one
593 bounds.set(Rect(t.width, t.height));
594 if (t.width == texture->potWidth &&
595 t.height == texture->potHeight) {
596 // we can do it one pass
597 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700599
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800600 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700601 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700602 GL_RGB, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800604 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700606 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700607 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800608 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
609 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700610 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700611 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612 GL_RGBA, GL_UNSIGNED_BYTE, data);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800613 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614 // just show the Y plane of YUV buffers
615 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700616 GL_LUMINANCE, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700617 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
618 } else {
619 // oops, we don't handle this format!
620 LOGE("layer %p, texture=%d, using format %d, which is not "
Mathias Agopian3330b202009-10-05 17:07:12 -0700621 "supported by the GL", this, texture->name, t.format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700622 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700623 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700624 if (!data) {
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800625 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700626 glTexSubImage2D(GL_TEXTURE_2D, 0,
627 0, bounds.top, t.width, bounds.height(),
628 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
629 t.data + bounds.top*t.stride*2);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800630 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700631 glTexSubImage2D(GL_TEXTURE_2D, 0,
632 0, bounds.top, t.width, bounds.height(),
633 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
634 t.data + bounds.top*t.stride*2);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800635 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
636 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700637 glTexSubImage2D(GL_TEXTURE_2D, 0,
638 0, bounds.top, t.width, bounds.height(),
639 GL_RGBA, GL_UNSIGNED_BYTE,
640 t.data + bounds.top*t.stride*4);
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800641 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700642 // just show the Y plane of YUV buffers
643 glTexSubImage2D(GL_TEXTURE_2D, 0,
644 0, bounds.top, t.width, bounds.height(),
645 GL_LUMINANCE, GL_UNSIGNED_BYTE,
646 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 }
648 }
649}
650
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700651status_t LayerBase::initializeEglImage(
652 const sp<GraphicBuffer>& buffer, Texture* texture)
653{
654 status_t err = NO_ERROR;
655
656 // we need to recreate the texture
657 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
658
659 // free the previous image
660 if (texture->image != EGL_NO_IMAGE_KHR) {
661 eglDestroyImageKHR(dpy, texture->image);
662 texture->image = EGL_NO_IMAGE_KHR;
663 }
664
665 // construct an EGL_NATIVE_BUFFER_ANDROID
666 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
667
668 // create the new EGLImageKHR
669 const EGLint attrs[] = {
670 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
671 EGL_NONE, EGL_NONE
672 };
673 texture->image = eglCreateImageKHR(
674 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
675 (EGLClientBuffer)clientBuf, attrs);
676
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700677 if (texture->image != EGL_NO_IMAGE_KHR) {
678 glBindTexture(GL_TEXTURE_2D, texture->name);
679 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
680 (GLeglImageOES)texture->image);
681 GLint error = glGetError();
682 if (UNLIKELY(error != GL_NO_ERROR)) {
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700683 LOGE("layer=%p, glEGLImageTargetTexture2DOES(%p) "
684 "failed err=0x%04x",
685 this, texture->image, error);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700686 err = INVALID_OPERATION;
687 } else {
688 // Everything went okay!
689 texture->NPOTAdjust = false;
690 texture->dirty = false;
691 texture->width = clientBuf->width;
692 texture->height = clientBuf->height;
693 }
694 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800695 LOGE("layer=%p, eglCreateImageKHR() failed. err=0x%4x",
696 this, eglGetError());
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700697 err = INVALID_OPERATION;
698 }
699 return err;
700}
701
702
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703// ---------------------------------------------------------------------------
704
Mathias Agopian2e123242009-06-23 20:06:46 -0700705int32_t LayerBaseClient::sIdentity = 0;
706
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700708 const sp<Client>& client, int32_t i)
Mathias Agopiand1296592010-03-09 19:17:47 -0800709 : LayerBase(flinger, display), lcblk(NULL), client(client), mIndex(i),
Mathias Agopian948d69f2010-03-08 19:29:09 -0800710 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700712 lcblk = new SharedBufferServer(
713 client->ctrlblk, i, NUM_BUFFERS,
714 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700715}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700717void LayerBaseClient::onFirstRef()
718{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700719 sp<Client> client(this->client.promote());
720 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700721 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 }
723}
724
725LayerBaseClient::~LayerBaseClient()
726{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700727 sp<Client> client(this->client.promote());
728 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729 client->free(mIndex);
730 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700731 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732}
733
Mathias Agopianf9d93272009-06-19 17:00:27 -0700734int32_t LayerBaseClient::serverIndex() const
735{
736 sp<Client> client(this->client.promote());
737 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 return (client->cid<<16)|mIndex;
739 }
740 return 0xFFFF0000 | mIndex;
741}
742
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700743sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700745 sp<Surface> s;
746 Mutex::Autolock _l(mLock);
747 s = mClientSurface.promote();
748 if (s == 0) {
749 s = createSurface();
750 mClientSurface = s;
751 }
752 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753}
754
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700755sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
756{
Mathias Agopian9a112062009-04-17 19:36:26 -0700757 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700758 const_cast<LayerBaseClient *>(this));
759}
760
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700761// called with SurfaceFlinger::mStateLock as soon as the layer is entered
762// in the purgatory list
763void LayerBaseClient::onRemoved()
764{
765 // wake up the condition
766 lcblk->setStatus(NO_INIT);
767}
768
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700769// ---------------------------------------------------------------------------
770
Mathias Agopian9a112062009-04-17 19:36:26 -0700771LayerBaseClient::Surface::Surface(
772 const sp<SurfaceFlinger>& flinger,
773 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700774 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700775 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
776{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700777}
778
Mathias Agopian9a112062009-04-17 19:36:26 -0700779LayerBaseClient::Surface::~Surface()
780{
781 /*
782 * This is a good place to clean-up all client resources
783 */
784
785 // destroy client resources
786 sp<LayerBaseClient> layer = getOwner();
787 if (layer != 0) {
788 mFlinger->destroySurface(layer);
789 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700790}
791
792sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
793 sp<LayerBaseClient> owner(mOwner.promote());
794 return owner;
795}
796
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700797status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700798 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700799{
800 switch (code) {
801 case REGISTER_BUFFERS:
802 case UNREGISTER_BUFFERS:
803 case CREATE_OVERLAY:
804 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700805 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
806 IPCThreadState* ipc = IPCThreadState::self();
807 const int pid = ipc->getCallingPid();
808 const int uid = ipc->getCallingUid();
809 LOGE("Permission Denial: "
810 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
811 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700812 }
813 }
814 }
815 return BnSurface::onTransact(code, data, reply, flags);
816}
817
Mathias Agopian3330b202009-10-05 17:07:12 -0700818sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700819{
820 return NULL;
821}
822
823status_t LayerBaseClient::Surface::registerBuffers(
824 const ISurface::BufferHeap& buffers)
825{
826 return INVALID_OPERATION;
827}
828
829void LayerBaseClient::Surface::postBuffer(ssize_t offset)
830{
831}
832
833void LayerBaseClient::Surface::unregisterBuffers()
834{
835}
836
837sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800838 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700839{
840 return NULL;
841};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842
843// ---------------------------------------------------------------------------
844
845}; // namespace android