blob: 83814ccf9a9ffd6d7cde0a72e75c8b9e6e6c1cb6 [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
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 Project9066cfe2009-03-03 19:31:44 -080049LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
50 : dpy(display), contentDirty(false),
51 mFlinger(flinger),
52 mTransformed(false),
Mathias Agopian44cac132009-09-23 18:34:53 -070053 mUseLinearFiltering(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 mOrientation(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 mTransactionFlags(0),
56 mPremultipliedAlpha(true),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 mInvalidate(0)
58{
59 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
60 mFlags = hw.getFlags();
61}
62
63LayerBase::~LayerBase()
64{
65}
66
67const GraphicPlane& LayerBase::graphicPlane(int dpy) const
68{
69 return mFlinger->graphicPlane(dpy);
70}
71
72GraphicPlane& LayerBase::graphicPlane(int dpy)
73{
74 return mFlinger->graphicPlane(dpy);
75}
76
77void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
78{
79 uint32_t layerFlags = 0;
80 if (flags & ISurfaceComposer::eHidden)
81 layerFlags = ISurfaceComposer::eLayerHidden;
82
83 if (flags & ISurfaceComposer::eNonPremultiplied)
84 mPremultipliedAlpha = false;
85
Mathias Agopiane1b6f242009-09-29 22:39:22 -070086 mCurrentState.z = 0;
87 mCurrentState.w = w;
88 mCurrentState.h = h;
89 mCurrentState.requested_w = w;
90 mCurrentState.requested_h = h;
91 mCurrentState.alpha = 0xFF;
92 mCurrentState.flags = layerFlags;
93 mCurrentState.sequence = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 mCurrentState.transform.set(0, 0);
95
96 // drawing state & current state are identical
97 mDrawingState = mCurrentState;
98}
99
Mathias Agopian88516172009-09-29 22:32:36 -0700100void LayerBase::commitTransaction() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 mDrawingState = mCurrentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102}
103void LayerBase::forceVisibilityTransaction() {
104 // this can be called without SurfaceFlinger.mStateLock, but if we
105 // can atomically increment the sequence number, it doesn't matter.
106 android_atomic_inc(&mCurrentState.sequence);
107 requestTransaction();
108}
109bool LayerBase::requestTransaction() {
110 int32_t old = setTransactionFlags(eTransactionNeeded);
111 return ((old & eTransactionNeeded) == 0);
112}
113uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
114 return android_atomic_and(~flags, &mTransactionFlags) & flags;
115}
116uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
117 return android_atomic_or(flags, &mTransactionFlags);
118}
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120bool LayerBase::setPosition(int32_t x, int32_t y) {
121 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
122 return false;
123 mCurrentState.sequence++;
124 mCurrentState.transform.set(x, y);
125 requestTransaction();
126 return true;
127}
128bool LayerBase::setLayer(uint32_t z) {
129 if (mCurrentState.z == z)
130 return false;
131 mCurrentState.sequence++;
132 mCurrentState.z = z;
133 requestTransaction();
134 return true;
135}
136bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700137 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 return false;
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700139 mCurrentState.requested_w = w;
140 mCurrentState.requested_h = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 requestTransaction();
142 return true;
143}
144bool LayerBase::setAlpha(uint8_t alpha) {
145 if (mCurrentState.alpha == alpha)
146 return false;
147 mCurrentState.sequence++;
148 mCurrentState.alpha = alpha;
149 requestTransaction();
150 return true;
151}
152bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
153 // TODO: check the matrix has changed
154 mCurrentState.sequence++;
155 mCurrentState.transform.set(
156 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
157 requestTransaction();
158 return true;
159}
160bool LayerBase::setTransparentRegionHint(const Region& transparent) {
161 // TODO: check the region has changed
162 mCurrentState.sequence++;
163 mCurrentState.transparentRegion = transparent;
164 requestTransaction();
165 return true;
166}
167bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
168 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
169 if (mCurrentState.flags == newFlags)
170 return false;
171 mCurrentState.sequence++;
172 mCurrentState.flags = newFlags;
173 requestTransaction();
174 return true;
175}
176
177Rect LayerBase::visibleBounds() const
178{
179 return mTransformedBounds;
180}
181
182void LayerBase::setVisibleRegion(const Region& visibleRegion) {
183 // always called from main thread
184 visibleRegionScreen = visibleRegion;
185}
186
187void LayerBase::setCoveredRegion(const Region& coveredRegion) {
188 // always called from main thread
189 coveredRegionScreen = coveredRegion;
190}
191
192uint32_t LayerBase::doTransaction(uint32_t flags)
193{
194 const Layer::State& front(drawingState());
195 const Layer::State& temp(currentState());
196
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700197 if ((front.requested_w != temp.requested_w) ||
198 (front.requested_h != temp.requested_h)) {
199 // resize the layer, set the physical size to the requested size
200 Layer::State& editTemp(currentState());
201 editTemp.w = temp.requested_w;
202 editTemp.h = temp.requested_h;
203 }
204
Mathias Agopian70cab912009-09-30 12:48:47 -0700205 if ((front.w != temp.w) || (front.h != temp.h)) {
206 // invalidate and recompute the visible regions if needed
207 flags |= Layer::eVisibleRegion;
208 this->contentDirty = true;
209 }
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 Agopian9779b222009-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);
369 glVertexPointer(2, GL_FIXED, 0, mVertices);
370 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 }
413 const GGLfixed alpha = (s.alpha << 16)/255;
414 glColor4x(alpha, alpha, alpha, alpha);
415 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);
420 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
421 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();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 if (UNLIKELY(transformed()
433 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
434 {
435 //StopWatch watch("GL transformed");
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700436 const GLfixed texCoords[4][2] = {
437 { 0, 0 },
438 { 0, 0x10000 },
439 { 0x10000, 0x10000 },
440 { 0x10000, 0 }
441 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700443 glMatrixMode(GL_TEXTURE);
444 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700446 // the texture's source is rotated
447 if (texture.transform == HAL_TRANSFORM_ROT_90) {
448 // TODO: handle the other orientations
449 glTranslatef(0, 1, 0);
450 glRotatef(-90, 0, 0, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700452
453 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
454 DisplayHardware::DIRECT_TEXTURE))) {
455 // find the smallest power-of-two that will accommodate our surface
456 GLuint tw = 1 << (31 - clz(width));
457 GLuint th = 1 << (31 - clz(height));
458 if (tw < width) tw <<= 1;
459 if (th < height) th <<= 1;
460 GLfloat ws = GLfloat(width) /tw;
461 GLfloat hs = GLfloat(height)/th;
462 glScalef(ws, hs, 1.0f);
463 }
464
465 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
466 glVertexPointer(2, GL_FIXED, 0, mVertices);
467 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
468
469 while (it != end) {
470 const Rect& r = *it++;
471 const GLint sy = fbHeight - (r.top + r.height());
472 glScissor(r.left, sy, r.width(), r.height());
473 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
474 }
475 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700477 GLint crop[4] = { 0, height, width, -height };
478 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
479 int x = tx();
480 int y = ty();
481 y = fbHeight - (y + height);
482 while (it != end) {
483 const Rect& r = *it++;
484 const GLint sy = fbHeight - (r.top + r.height());
485 glScissor(r.left, sy, r.width(), r.height());
486 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488 }
489}
490
491void LayerBase::validateTexture(GLint textureName) const
492{
493 glBindTexture(GL_TEXTURE_2D, textureName);
494 // TODO: reload the texture if needed
495 // this is currently done in loadTexture() below
Mathias Agopian44cac132009-09-23 18:34:53 -0700496 if (mUseLinearFiltering) {
497 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
498 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
499 } else {
500 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
501 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
502 }
Mathias Agopiancc934762009-09-23 19:16:27 -0700503
504 if (needsDithering()) {
505 glEnable(GL_DITHER);
506 } else {
507 glDisable(GL_DITHER);
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509}
510
Mathias Agopian999543b2009-06-23 18:08:22 -0700511void LayerBase::loadTexture(Texture* texture, GLint textureName,
512 const Region& dirty, const GGLSurface& t) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513{
514 // TODO: defer the actual texture reload until LayerBase::validateTexture
515 // is called.
516
Mathias Agopian999543b2009-06-23 18:08:22 -0700517 texture->name = textureName;
518 GLuint& textureWidth(texture->width);
519 GLuint& textureHeight(texture->height);
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 uint32_t flags = mFlags;
522 glBindTexture(GL_TEXTURE_2D, textureName);
523
524 GLuint tw = t.width;
525 GLuint th = t.height;
526
527 /*
528 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian1473f462009-04-10 14:24:30 -0700529 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
531 * need to do something reasonable (here creating a bigger texture).
532 *
533 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
534 *
535 * This situation doesn't happen often, but some h/w have a limitation
536 * for their framebuffer (eg: must be multiple of 8 pixels), and
537 * we need to take that into account when using these buffers as
538 * textures.
539 *
540 * This should never be a problem with POT textures
541 */
Mathias Agopian1473f462009-04-10 14:24:30 -0700542
543 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
544 unpack = 1 << ((unpack > 3) ? 3 : unpack);
545 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 /*
548 * round to POT if needed
549 */
550
551 GLuint texture_w = tw;
552 GLuint texture_h = th;
553 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
554 // find the smallest power-of-two that will accommodate our surface
555 texture_w = 1 << (31 - clz(t.width));
556 texture_h = 1 << (31 - clz(t.height));
557 if (texture_w < t.width) texture_w <<= 1;
558 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561regular:
Mathias Agopian1473f462009-04-10 14:24:30 -0700562 Rect bounds(dirty.bounds());
563 GLvoid* data = 0;
564 if (texture_w!=textureWidth || texture_h!=textureHeight) {
565 // texture size changed, we need to create a new one
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566
Mathias Agopian1473f462009-04-10 14:24:30 -0700567 if (!textureWidth || !textureHeight) {
568 // this is the first time, load the whole texture
569 if (texture_w==tw && texture_h==th) {
570 // we can do it one pass
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 data = t.data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 } else {
Mathias Agopian1473f462009-04-10 14:24:30 -0700573 // we have to create the texture first because it
574 // doesn't match the size of the buffer
575 bounds.set(Rect(tw, th));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700578
579 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
580 glTexImage2D(GL_TEXTURE_2D, 0,
581 GL_RGB, texture_w, texture_h, 0,
582 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
583 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
584 glTexImage2D(GL_TEXTURE_2D, 0,
585 GL_RGBA, texture_w, texture_h, 0,
586 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian64a7c6b2009-09-14 18:10:30 -0700587 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
588 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700589 glTexImage2D(GL_TEXTURE_2D, 0,
590 GL_RGBA, texture_w, texture_h, 0,
591 GL_RGBA, GL_UNSIGNED_BYTE, data);
592 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
593 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
594 // just show the Y plane of YUV buffers
595 glTexImage2D(GL_TEXTURE_2D, 0,
596 GL_LUMINANCE, texture_w, texture_h, 0,
597 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
598 } else {
599 // oops, we don't handle this format!
600 LOGE("layer %p, texture=%d, using format %d, which is not "
601 "supported by the GL", this, textureName, t.format);
602 textureName = -1;
603 }
604 textureWidth = texture_w;
605 textureHeight = texture_h;
606 }
607 if (!data && textureName>=0) {
608 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
609 glTexSubImage2D(GL_TEXTURE_2D, 0,
610 0, bounds.top, t.width, bounds.height(),
611 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
612 t.data + bounds.top*t.stride*2);
613 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
614 glTexSubImage2D(GL_TEXTURE_2D, 0,
615 0, bounds.top, t.width, bounds.height(),
616 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
617 t.data + bounds.top*t.stride*2);
Mathias Agopian64a7c6b2009-09-14 18:10:30 -0700618 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
619 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700620 glTexSubImage2D(GL_TEXTURE_2D, 0,
621 0, bounds.top, t.width, bounds.height(),
622 GL_RGBA, GL_UNSIGNED_BYTE,
623 t.data + bounds.top*t.stride*4);
624 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
625 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
626 // just show the Y plane of YUV buffers
627 glTexSubImage2D(GL_TEXTURE_2D, 0,
628 0, bounds.top, t.width, bounds.height(),
629 GL_LUMINANCE, GL_UNSIGNED_BYTE,
630 t.data + bounds.top*t.stride);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632 }
633}
634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635// ---------------------------------------------------------------------------
636
Mathias Agopianc6603952009-06-23 20:06:46 -0700637int32_t LayerBaseClient::sIdentity = 0;
638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700640 const sp<Client>& client, int32_t i)
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700641 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopian9779b222009-09-07 16:32:45 -0700642 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643{
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700644 lcblk = new SharedBufferServer(
645 client->ctrlblk, i, NUM_BUFFERS,
646 mIdentity);
Mathias Agopian1473f462009-04-10 14:24:30 -0700647}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648
Mathias Agopian1473f462009-04-10 14:24:30 -0700649void LayerBaseClient::onFirstRef()
650{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700651 sp<Client> client(this->client.promote());
652 if (client != 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700653 client->bindLayer(this, mIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655}
656
657LayerBaseClient::~LayerBaseClient()
658{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700659 sp<Client> client(this->client.promote());
660 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 client->free(mIndex);
662 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700663 delete lcblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664}
665
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700666int32_t LayerBaseClient::serverIndex() const
667{
668 sp<Client> client(this->client.promote());
669 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 return (client->cid<<16)|mIndex;
671 }
672 return 0xFFFF0000 | mIndex;
673}
674
Mathias Agopian1473f462009-04-10 14:24:30 -0700675sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676{
Mathias Agopian1473f462009-04-10 14:24:30 -0700677 sp<Surface> s;
678 Mutex::Autolock _l(mLock);
679 s = mClientSurface.promote();
680 if (s == 0) {
681 s = createSurface();
682 mClientSurface = s;
683 }
684 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685}
686
Mathias Agopian1473f462009-04-10 14:24:30 -0700687sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
688{
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700689 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700690 const_cast<LayerBaseClient *>(this));
691}
692
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700693// called with SurfaceFlinger::mStateLock as soon as the layer is entered
694// in the purgatory list
695void LayerBaseClient::onRemoved()
696{
697 // wake up the condition
698 lcblk->setStatus(NO_INIT);
699}
700
Mathias Agopian1473f462009-04-10 14:24:30 -0700701// ---------------------------------------------------------------------------
702
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700703LayerBaseClient::Surface::Surface(
704 const sp<SurfaceFlinger>& flinger,
705 SurfaceID id, int identity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700706 const sp<LayerBaseClient>& owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700707 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
708{
Mathias Agopian1473f462009-04-10 14:24:30 -0700709}
710
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700711LayerBaseClient::Surface::~Surface()
712{
713 /*
714 * This is a good place to clean-up all client resources
715 */
716
717 // destroy client resources
718 sp<LayerBaseClient> layer = getOwner();
719 if (layer != 0) {
720 mFlinger->destroySurface(layer);
721 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700722}
723
724sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
725 sp<LayerBaseClient> owner(mOwner.promote());
726 return owner;
727}
728
Mathias Agopian1473f462009-04-10 14:24:30 -0700729status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian151e8592009-06-15 18:24:59 -0700730 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian1473f462009-04-10 14:24:30 -0700731{
732 switch (code) {
733 case REGISTER_BUFFERS:
734 case UNREGISTER_BUFFERS:
735 case CREATE_OVERLAY:
736 {
Mathias Agopian151e8592009-06-15 18:24:59 -0700737 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
738 IPCThreadState* ipc = IPCThreadState::self();
739 const int pid = ipc->getCallingPid();
740 const int uid = ipc->getCallingUid();
741 LOGE("Permission Denial: "
742 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
743 return PERMISSION_DENIED;
Mathias Agopian1473f462009-04-10 14:24:30 -0700744 }
745 }
746 }
747 return BnSurface::onTransact(code, data, reply, flags);
748}
749
Mathias Agopian9779b222009-09-07 16:32:45 -0700750sp<SurfaceBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700751{
752 return NULL;
753}
754
755status_t LayerBaseClient::Surface::registerBuffers(
756 const ISurface::BufferHeap& buffers)
757{
758 return INVALID_OPERATION;
759}
760
761void LayerBaseClient::Surface::postBuffer(ssize_t offset)
762{
763}
764
765void LayerBaseClient::Surface::unregisterBuffers()
766{
767}
768
769sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
770 uint32_t w, uint32_t h, int32_t format)
771{
772 return NULL;
773};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774
775// ---------------------------------------------------------------------------
776
777}; // namespace android