blob: 48b0e47a83b0fd1e1eec668add67ac83d4e9f1d9 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22#include <utils/Log.h>
Mathias Agopian947f4f42009-05-22 01:27:01 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28
29#include <hardware/hardware.h>
30
31#include "clz.h"
32#include "LayerBase.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include "SurfaceFlinger.h"
34#include "DisplayHardware/DisplayHardware.h"
35
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037namespace android {
38
39// ---------------------------------------------------------------------------
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
42 : dpy(display), contentDirty(false),
43 mFlinger(flinger),
44 mTransformed(false),
Mathias Agopian44cac132009-09-23 18:34:53 -070045 mUseLinearFiltering(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 mOrientation(0),
Mathias Agopianed2ab7f2010-02-19 17:51:58 -080047 mLeft(0), mTop(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 mTransactionFlags(0),
Mathias Agopiana8a0aa82010-04-21 15:24:11 -070049 mPremultipliedAlpha(true), mName("unnamed"), mDebug(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 mInvalidate(0)
51{
52 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
53 mFlags = hw.getFlags();
54}
55
56LayerBase::~LayerBase()
57{
58}
59
Mathias Agopian015b5972010-03-09 19:17:47 -080060void LayerBase::setName(const String8& name) {
61 mName = name;
62}
63
64String8 LayerBase::getName() const {
65 return mName;
66}
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068const 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 Agopiane1b6f242009-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 Project9066cfe2009-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 Agopian88516172009-09-29 22:32:36 -0700101void LayerBase::commitTransaction() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 mDrawingState = mCurrentState;
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopiane1b6f242009-09-29 22:39:22 -0700138 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 return false;
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700140 mCurrentState.requested_w = w;
141 mCurrentState.requested_h = h;
The Android Open Source Project9066cfe2009-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 Agopiane1b6f242009-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 Agopian70cab912009-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;
Mathias Agopian70cab912009-09-30 12:48:47 -0700209 }
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 if (temp.sequence != front.sequence) {
212 // invalidate and recompute the visible regions if needed
213 flags |= eVisibleRegion;
214 this->contentDirty = true;
Mathias Agopian44cac132009-09-23 18:34:53 -0700215
Mathias Agopian44cac132009-09-23 18:34:53 -0700216 const bool linearFiltering = mUseLinearFiltering;
217 mUseLinearFiltering = false;
218 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
219 // we may use linear filtering, if the matrix scales us
220 const uint8_t type = temp.transform.getType();
221 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
222 mUseLinearFiltering = true;
223 }
224 }
225 }
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 // Commit the transaction
Mathias Agopian88516172009-09-29 22:32:36 -0700228 commitTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 return flags;
230}
231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232void LayerBase::validateVisibility(const Transform& planeTransform)
233{
234 const Layer::State& s(drawingState());
235 const Transform tr(planeTransform * s.transform);
236 const bool transformed = tr.transformed();
237
Mathias Agopian9779b2212009-09-07 16:32:45 -0700238 uint32_t w = s.w;
239 uint32_t h = s.h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 tr.transform(mVertices[0], 0, 0);
241 tr.transform(mVertices[1], 0, h);
242 tr.transform(mVertices[2], w, h);
243 tr.transform(mVertices[3], w, 0);
244 if (UNLIKELY(transformed)) {
245 // NOTE: here we could also punt if we have too many rectangles
246 // in the transparent region
247 if (tr.preserveRects()) {
248 // transform the transparent region
249 transparentRegionScreen = tr.transform(s.transparentRegion);
250 } else {
251 // transformation too complex, can't do the transparent region
252 // optimization.
253 transparentRegionScreen.clear();
254 }
255 } else {
256 transparentRegionScreen = s.transparentRegion;
257 }
258
259 // cache a few things...
260 mOrientation = tr.getOrientation();
261 mTransformedBounds = tr.makeBounds(w, h);
262 mTransformed = transformed;
263 mLeft = tr.tx();
264 mTop = tr.ty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265}
266
267void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
268{
269}
270
271void LayerBase::unlockPageFlip(
272 const Transform& planeTransform, Region& outDirtyRegion)
273{
274 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
275 outDirtyRegion.orSelf(visibleRegionScreen);
276 }
277}
278
279void LayerBase::finishPageFlip()
280{
281}
282
283void LayerBase::invalidate()
284{
285 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
286 mFlinger->signalEvent();
287 }
288}
289
290void LayerBase::drawRegion(const Region& reg) const
291{
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700292 Region::const_iterator it = reg.begin();
293 Region::const_iterator const end = reg.end();
294 if (it != end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 Rect r;
296 const DisplayHardware& hw(graphicPlane(0).displayHardware());
297 const int32_t fbWidth = hw.getWidth();
298 const int32_t fbHeight = hw.getHeight();
299 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
300 { fbWidth, fbHeight }, { 0, fbHeight } };
301 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700302 while (it != end) {
303 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 const GLint sy = fbHeight - (r.top + r.height());
305 glScissor(r.left, sy, r.width(), r.height());
306 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
307 }
308 }
309}
310
311void LayerBase::draw(const Region& inClip) const
312{
313 // invalidate the region we'll update
314 Region clip(inClip); // copy-on-write, so no-op most of the time
315
316 // Remove the transparent area from the clipping region
317 const State& s = drawingState();
318 if (LIKELY(!s.transparentRegion.isEmpty())) {
319 clip.subtract(transparentRegionScreen);
320 if (clip.isEmpty()) {
321 // usually this won't happen because this should be taken care of
322 // by SurfaceFlinger::computeVisibleRegions()
323 return;
324 }
325 }
326
327 // reset GL state
328 glEnable(GL_SCISSOR_TEST);
329
330 onDraw(clip);
331
332 /*
333 glDisable(GL_TEXTURE_2D);
334 glDisable(GL_DITHER);
335 glEnable(GL_BLEND);
336 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
337 glColor4x(0, 0x8000, 0, 0x10000);
338 drawRegion(transparentRegionScreen);
339 glDisable(GL_BLEND);
340 */
341}
342
343GLuint LayerBase::createTexture() const
344{
345 GLuint textureName = -1;
346 glGenTextures(1, &textureName);
347 glBindTexture(GL_TEXTURE_2D, textureName);
348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian44cac132009-09-23 18:34:53 -0700350 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
351 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 return textureName;
353}
354
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700355void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
356 GLclampx green, GLclampx blue,
357 GLclampx alpha) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358{
359 const DisplayHardware& hw(graphicPlane(0).displayHardware());
360 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700361 glColor4x(red,green,blue,alpha);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 glDisable(GL_TEXTURE_2D);
363 glDisable(GL_BLEND);
364 glDisable(GL_DITHER);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700365
366 Region::const_iterator it = clip.begin();
367 Region::const_iterator const end = clip.end();
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700368 glEnable(GL_SCISSOR_TEST);
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700369 glVertexPointer(2, GL_FLOAT, 0, mVertices);
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700370 while (it != end) {
371 const Rect& r = *it++;
372 const GLint sy = fbHeight - (r.top + r.height());
373 glScissor(r.left, sy, r.width(), r.height());
374 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 }
376}
377
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700378void LayerBase::clearWithOpenGL(const Region& clip) const
379{
380 clearWithOpenGL(clip,0,0,0,0);
381}
382
Mathias Agopian999543b2009-06-23 18:08:22 -0700383void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384{
385 const DisplayHardware& hw(graphicPlane(0).displayHardware());
386 const uint32_t fbHeight = hw.getHeight();
387 const State& s(drawingState());
Mathias Agopiandff8e582009-05-04 14:17:04 -0700388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 // bind our texture
Mathias Agopian999543b2009-06-23 18:08:22 -0700390 validateTexture(texture.name);
391 uint32_t width = texture.width;
392 uint32_t height = texture.height;
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 glEnable(GL_TEXTURE_2D);
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 if (UNLIKELY(s.alpha < 0xFF)) {
397 // We have an alpha-modulation. We need to modulate all
398 // texture components by alpha because we're always using
399 // premultiplied alpha.
400
401 // If the texture doesn't have an alpha channel we can
402 // use REPLACE and switch to non premultiplied alpha
403 // blending (SRCA/ONE_MINUS_SRCA).
404
405 GLenum env, src;
406 if (needsBlending()) {
407 env = GL_MODULATE;
408 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
409 } else {
410 env = GL_REPLACE;
411 src = GL_SRC_ALPHA;
412 }
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700413 const GLfloat alpha = s.alpha * (1.0f/255.0f);
414 glColor4f(alpha, alpha, alpha, alpha);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 glEnable(GL_BLEND);
416 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
417 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
418 } else {
419 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700420 glColor4f(1, 1, 1, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 if (needsBlending()) {
422 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
423 glEnable(GL_BLEND);
424 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
425 } else {
426 glDisable(GL_BLEND);
427 }
428 }
429
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700430 Region::const_iterator it = clip.begin();
431 Region::const_iterator const end = clip.end();
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700432 const GLfloat texCoords[4][2] = {
433 { 0, 0 },
434 { 0, 1 },
435 { 1, 1 },
436 { 1, 0 }
437 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700439 glMatrixMode(GL_TEXTURE);
440 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700442 // the texture's source is rotated
443 switch (texture.transform) {
444 case HAL_TRANSFORM_ROT_90:
445 glTranslatef(0, 1, 0);
446 glRotatef(-90, 0, 0, 1);
447 break;
448 case HAL_TRANSFORM_ROT_180:
449 glTranslatef(1, 1, 0);
450 glRotatef(-180, 0, 0, 1);
451 break;
452 case HAL_TRANSFORM_ROT_270:
453 glTranslatef(1, 0, 0);
454 glRotatef(-270, 0, 0, 1);
455 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700457
458 if (texture.NPOTAdjust) {
459 glScalef(texture.wScale, texture.hScale, 1.0f);
460 }
461
462 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
463 glVertexPointer(2, GL_FLOAT, 0, mVertices);
464 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
465
466 while (it != end) {
467 const Rect& r = *it++;
468 const GLint sy = fbHeight - (r.top + r.height());
469 glScissor(r.left, sy, r.width(), r.height());
470 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
471 }
472 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473}
474
475void LayerBase::validateTexture(GLint textureName) const
476{
477 glBindTexture(GL_TEXTURE_2D, textureName);
478 // TODO: reload the texture if needed
479 // this is currently done in loadTexture() below
Mathias Agopian44cac132009-09-23 18:34:53 -0700480 if (mUseLinearFiltering) {
481 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
482 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
483 } else {
484 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
485 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
486 }
Mathias Agopiancc934762009-09-23 19:16:27 -0700487
488 if (needsDithering()) {
489 glEnable(GL_DITHER);
490 } else {
491 glDisable(GL_DITHER);
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493}
494
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800495bool LayerBase::isSupportedYuvFormat(int format) const
496{
497 switch (format) {
498 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
499 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
500 case HAL_PIXEL_FORMAT_YCbCr_422_P:
501 case HAL_PIXEL_FORMAT_YCbCr_420_P:
502 case HAL_PIXEL_FORMAT_YCbCr_422_I:
503 case HAL_PIXEL_FORMAT_YCbCr_420_I:
504 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
505 return true;
506 }
507 return false;
508}
509
Mathias Agopian6950e422009-10-05 17:07:12 -0700510void LayerBase::loadTexture(Texture* texture,
Mathias Agopian999543b2009-06-23 18:08:22 -0700511 const Region& dirty, const GGLSurface& t) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512{
Mathias Agopian6950e422009-10-05 17:07:12 -0700513 if (texture->name == -1U) {
514 // uh?
515 return;
516 }
Mathias Agopian382e17d2009-10-21 16:27:21 -0700517
Mathias Agopian6950e422009-10-05 17:07:12 -0700518 glBindTexture(GL_TEXTURE_2D, texture->name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519
520 /*
521 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian1473f462009-04-10 14:24:30 -0700522 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
524 * need to do something reasonable (here creating a bigger texture).
525 *
526 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
527 *
528 * This situation doesn't happen often, but some h/w have a limitation
529 * for their framebuffer (eg: must be multiple of 8 pixels), and
530 * we need to take that into account when using these buffers as
531 * textures.
532 *
533 * This should never be a problem with POT textures
534 */
Mathias Agopian1473f462009-04-10 14:24:30 -0700535
536 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
537 unpack = 1 << ((unpack > 3) ? 3 : unpack);
538 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 /*
541 * round to POT if needed
542 */
Mathias Agopian6950e422009-10-05 17:07:12 -0700543 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
544 texture->NPOTAdjust = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700546
Mathias Agopian6950e422009-10-05 17:07:12 -0700547 if (texture->NPOTAdjust) {
548 // find the smallest power-of-two that will accommodate our surface
549 texture->potWidth = 1 << (31 - clz(t.width));
550 texture->potHeight = 1 << (31 - clz(t.height));
551 if (texture->potWidth < t.width) texture->potWidth <<= 1;
552 if (texture->potHeight < t.height) texture->potHeight <<= 1;
553 texture->wScale = float(t.width) / texture->potWidth;
554 texture->hScale = float(t.height) / texture->potHeight;
555 } else {
556 texture->potWidth = t.width;
557 texture->potHeight = t.height;
558 }
559
Mathias Agopian1473f462009-04-10 14:24:30 -0700560 Rect bounds(dirty.bounds());
561 GLvoid* data = 0;
Mathias Agopian6950e422009-10-05 17:07:12 -0700562 if (texture->width != t.width || texture->height != t.height) {
563 texture->width = t.width;
564 texture->height = t.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565
Mathias Agopian6950e422009-10-05 17:07:12 -0700566 // texture size changed, we need to create a new one
567 bounds.set(Rect(t.width, t.height));
568 if (t.width == texture->potWidth &&
569 t.height == texture->potHeight) {
570 // we can do it one pass
571 data = t.data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700573
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800574 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700575 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian6950e422009-10-05 17:07:12 -0700576 GL_RGB, texture->potWidth, texture->potHeight, 0,
Mathias Agopian1473f462009-04-10 14:24:30 -0700577 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800578 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700579 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian6950e422009-10-05 17:07:12 -0700580 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian1473f462009-04-10 14:24:30 -0700581 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800582 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
583 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700584 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian6950e422009-10-05 17:07:12 -0700585 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian1473f462009-04-10 14:24:30 -0700586 GL_RGBA, GL_UNSIGNED_BYTE, data);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800587 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700588 // just show the Y plane of YUV buffers
589 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian6950e422009-10-05 17:07:12 -0700590 GL_LUMINANCE, texture->potWidth, texture->potHeight, 0,
Mathias Agopian1473f462009-04-10 14:24:30 -0700591 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
592 } else {
593 // oops, we don't handle this format!
594 LOGE("layer %p, texture=%d, using format %d, which is not "
Mathias Agopian6950e422009-10-05 17:07:12 -0700595 "supported by the GL", this, texture->name, t.format);
Mathias Agopian1473f462009-04-10 14:24:30 -0700596 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700597 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700598 if (!data) {
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800599 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700600 glTexSubImage2D(GL_TEXTURE_2D, 0,
601 0, bounds.top, t.width, bounds.height(),
602 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
603 t.data + bounds.top*t.stride*2);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800604 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700605 glTexSubImage2D(GL_TEXTURE_2D, 0,
606 0, bounds.top, t.width, bounds.height(),
607 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
608 t.data + bounds.top*t.stride*2);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800609 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
610 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700611 glTexSubImage2D(GL_TEXTURE_2D, 0,
612 0, bounds.top, t.width, bounds.height(),
613 GL_RGBA, GL_UNSIGNED_BYTE,
614 t.data + bounds.top*t.stride*4);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800615 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700616 // just show the Y plane of YUV buffers
617 glTexSubImage2D(GL_TEXTURE_2D, 0,
618 0, bounds.top, t.width, bounds.height(),
619 GL_LUMINANCE, GL_UNSIGNED_BYTE,
620 t.data + bounds.top*t.stride);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622 }
623}
624
Mathias Agopian9042b452009-10-26 20:12:37 -0700625status_t LayerBase::initializeEglImage(
626 const sp<GraphicBuffer>& buffer, Texture* texture)
627{
628 status_t err = NO_ERROR;
629
630 // we need to recreate the texture
631 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
632
633 // free the previous image
634 if (texture->image != EGL_NO_IMAGE_KHR) {
635 eglDestroyImageKHR(dpy, texture->image);
636 texture->image = EGL_NO_IMAGE_KHR;
637 }
638
639 // construct an EGL_NATIVE_BUFFER_ANDROID
640 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
641
642 // create the new EGLImageKHR
643 const EGLint attrs[] = {
644 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
645 EGL_NONE, EGL_NONE
646 };
647 texture->image = eglCreateImageKHR(
648 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
649 (EGLClientBuffer)clientBuf, attrs);
650
Mathias Agopian9042b452009-10-26 20:12:37 -0700651 if (texture->image != EGL_NO_IMAGE_KHR) {
652 glBindTexture(GL_TEXTURE_2D, texture->name);
653 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
654 (GLeglImageOES)texture->image);
655 GLint error = glGetError();
656 if (UNLIKELY(error != GL_NO_ERROR)) {
Mathias Agopian9042b452009-10-26 20:12:37 -0700657 LOGE("layer=%p, glEGLImageTargetTexture2DOES(%p) "
658 "failed err=0x%04x",
659 this, texture->image, error);
Mathias Agopian9042b452009-10-26 20:12:37 -0700660 err = INVALID_OPERATION;
661 } else {
662 // Everything went okay!
663 texture->NPOTAdjust = false;
664 texture->dirty = false;
665 texture->width = clientBuf->width;
666 texture->height = clientBuf->height;
667 }
668 } else {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800669 LOGE("layer=%p, eglCreateImageKHR() failed. err=0x%4x",
670 this, eglGetError());
Mathias Agopian9042b452009-10-26 20:12:37 -0700671 err = INVALID_OPERATION;
672 }
673 return err;
674}
675
Mathias Agopian9bce8732010-04-20 17:55:49 -0700676void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
677{
678 const Layer::State& s(drawingState());
679 snprintf(buffer, SIZE,
680 "+ %s %p\n"
681 " "
682 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
683 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
684 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
685 getTypeId(), this, s.z, tx(), ty(), s.w, s.h,
686 needsBlending(), needsDithering(), contentDirty,
687 s.alpha, s.flags,
688 s.transform[0][0], s.transform[0][1],
689 s.transform[1][0], s.transform[1][1]);
690 result.append(buffer);
691}
Mathias Agopian9042b452009-10-26 20:12:37 -0700692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693// ---------------------------------------------------------------------------
694
Mathias Agopianc6603952009-06-23 20:06:46 -0700695int32_t LayerBaseClient::sIdentity = 0;
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700698 const sp<Client>& client, int32_t i)
Mathias Agopian015b5972010-03-09 19:17:47 -0800699 : LayerBase(flinger, display), lcblk(NULL), client(client), mIndex(i),
Mathias Agopian85b8d122010-03-08 19:29:09 -0800700 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701{
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700702 lcblk = new SharedBufferServer(
703 client->ctrlblk, i, NUM_BUFFERS,
704 mIdentity);
Mathias Agopian1473f462009-04-10 14:24:30 -0700705}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706
Mathias Agopian1473f462009-04-10 14:24:30 -0700707void LayerBaseClient::onFirstRef()
708{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700709 sp<Client> client(this->client.promote());
710 if (client != 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700711 client->bindLayer(this, mIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713}
714
715LayerBaseClient::~LayerBaseClient()
716{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700717 sp<Client> client(this->client.promote());
718 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 client->free(mIndex);
720 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700721 delete lcblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722}
723
Mathias Agopian9bce8732010-04-20 17:55:49 -0700724ssize_t LayerBaseClient::serverIndex() const
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700725{
726 sp<Client> client(this->client.promote());
727 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 return (client->cid<<16)|mIndex;
729 }
Mathias Agopian9bce8732010-04-20 17:55:49 -0700730 return ssize_t(0xFFFF0000 | mIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731}
732
Mathias Agopian1473f462009-04-10 14:24:30 -0700733sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734{
Mathias Agopian1473f462009-04-10 14:24:30 -0700735 sp<Surface> s;
736 Mutex::Autolock _l(mLock);
737 s = mClientSurface.promote();
738 if (s == 0) {
739 s = createSurface();
740 mClientSurface = s;
741 }
742 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743}
744
Mathias Agopian1473f462009-04-10 14:24:30 -0700745sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
746{
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700747 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700748 const_cast<LayerBaseClient *>(this));
749}
750
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700751// called with SurfaceFlinger::mStateLock as soon as the layer is entered
752// in the purgatory list
753void LayerBaseClient::onRemoved()
754{
755 // wake up the condition
756 lcblk->setStatus(NO_INIT);
757}
758
Mathias Agopian9bce8732010-04-20 17:55:49 -0700759void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
760{
761 LayerBase::dump(result, buffer, SIZE);
762
763 sp<Client> client(this->client.promote());
764 snprintf(buffer, SIZE,
765 " name=%s\n"
766 " id=0x%08x, client=0x%08x, identity=%u\n",
767 getName().string(),
768 clientIndex(), client.get() ? client->cid : 0,
769 getIdentity());
770
771 result.append(buffer);
772}
773
Mathias Agopian1473f462009-04-10 14:24:30 -0700774// ---------------------------------------------------------------------------
775
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700776LayerBaseClient::Surface::Surface(
777 const sp<SurfaceFlinger>& flinger,
778 SurfaceID id, int identity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700779 const sp<LayerBaseClient>& owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700780 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
781{
Mathias Agopian1473f462009-04-10 14:24:30 -0700782}
783
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700784LayerBaseClient::Surface::~Surface()
785{
786 /*
787 * This is a good place to clean-up all client resources
788 */
789
790 // destroy client resources
791 sp<LayerBaseClient> layer = getOwner();
792 if (layer != 0) {
793 mFlinger->destroySurface(layer);
794 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700795}
796
797sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
798 sp<LayerBaseClient> owner(mOwner.promote());
799 return owner;
800}
801
Mathias Agopian1473f462009-04-10 14:24:30 -0700802status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian151e8592009-06-15 18:24:59 -0700803 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian1473f462009-04-10 14:24:30 -0700804{
805 switch (code) {
806 case REGISTER_BUFFERS:
807 case UNREGISTER_BUFFERS:
808 case CREATE_OVERLAY:
809 {
Mathias Agopian151e8592009-06-15 18:24:59 -0700810 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
811 IPCThreadState* ipc = IPCThreadState::self();
812 const int pid = ipc->getCallingPid();
813 const int uid = ipc->getCallingUid();
814 LOGE("Permission Denial: "
815 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
816 return PERMISSION_DENIED;
Mathias Agopian1473f462009-04-10 14:24:30 -0700817 }
818 }
819 }
820 return BnSurface::onTransact(code, data, reply, flags);
821}
822
Mathias Agopian6950e422009-10-05 17:07:12 -0700823sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700824{
825 return NULL;
826}
827
828status_t LayerBaseClient::Surface::registerBuffers(
829 const ISurface::BufferHeap& buffers)
830{
831 return INVALID_OPERATION;
832}
833
834void LayerBaseClient::Surface::postBuffer(ssize_t offset)
835{
836}
837
838void LayerBaseClient::Surface::unregisterBuffers()
839{
840}
841
842sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800843 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
Mathias Agopian1473f462009-04-10 14:24:30 -0700844{
845 return NULL;
846};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847
848// ---------------------------------------------------------------------------
849
850}; // namespace android