blob: df1407d4a106ed2f47da90bf611760511d87bcfe [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),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055 mTransactionFlags(0),
56 mPremultipliedAlpha(true),
The Android Open Source Projectedbf3b62009-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 Agopian7e4a5872009-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 Projectedbf3b62009-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 Agopianba6be542009-09-29 22:32:36 -0700100void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-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 Agopian7e4a5872009-09-29 22:39:22 -0700137 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 return false;
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700139 mCurrentState.requested_w = w;
140 mCurrentState.requested_h = h;
The Android Open Source Projectedbf3b62009-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 Agopian7e4a5872009-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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 if (temp.sequence != front.sequence) {
206 // invalidate and recompute the visible regions if needed
207 flags |= eVisibleRegion;
208 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700209
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700210 const bool linearFiltering = mUseLinearFiltering;
211 mUseLinearFiltering = false;
212 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
213 // we may use linear filtering, if the matrix scales us
214 const uint8_t type = temp.transform.getType();
215 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
216 mUseLinearFiltering = true;
217 }
218 }
219 }
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700222 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 return flags;
224}
225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226void LayerBase::validateVisibility(const Transform& planeTransform)
227{
228 const Layer::State& s(drawingState());
229 const Transform tr(planeTransform * s.transform);
230 const bool transformed = tr.transformed();
231
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700232 uint32_t w = s.w;
233 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 tr.transform(mVertices[0], 0, 0);
235 tr.transform(mVertices[1], 0, h);
236 tr.transform(mVertices[2], w, h);
237 tr.transform(mVertices[3], w, 0);
238 if (UNLIKELY(transformed)) {
239 // NOTE: here we could also punt if we have too many rectangles
240 // in the transparent region
241 if (tr.preserveRects()) {
242 // transform the transparent region
243 transparentRegionScreen = tr.transform(s.transparentRegion);
244 } else {
245 // transformation too complex, can't do the transparent region
246 // optimization.
247 transparentRegionScreen.clear();
248 }
249 } else {
250 transparentRegionScreen = s.transparentRegion;
251 }
252
253 // cache a few things...
254 mOrientation = tr.getOrientation();
255 mTransformedBounds = tr.makeBounds(w, h);
256 mTransformed = transformed;
257 mLeft = tr.tx();
258 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259}
260
261void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
262{
263}
264
265void LayerBase::unlockPageFlip(
266 const Transform& planeTransform, Region& outDirtyRegion)
267{
268 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
269 outDirtyRegion.orSelf(visibleRegionScreen);
270 }
271}
272
273void LayerBase::finishPageFlip()
274{
275}
276
277void LayerBase::invalidate()
278{
279 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
280 mFlinger->signalEvent();
281 }
282}
283
284void LayerBase::drawRegion(const Region& reg) const
285{
Mathias Agopian20f68782009-05-11 00:03:41 -0700286 Region::const_iterator it = reg.begin();
287 Region::const_iterator const end = reg.end();
288 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 Rect r;
290 const DisplayHardware& hw(graphicPlane(0).displayHardware());
291 const int32_t fbWidth = hw.getWidth();
292 const int32_t fbHeight = hw.getHeight();
293 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
294 { fbWidth, fbHeight }, { 0, fbHeight } };
295 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700296 while (it != end) {
297 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 const GLint sy = fbHeight - (r.top + r.height());
299 glScissor(r.left, sy, r.width(), r.height());
300 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
301 }
302 }
303}
304
305void LayerBase::draw(const Region& inClip) const
306{
307 // invalidate the region we'll update
308 Region clip(inClip); // copy-on-write, so no-op most of the time
309
310 // Remove the transparent area from the clipping region
311 const State& s = drawingState();
312 if (LIKELY(!s.transparentRegion.isEmpty())) {
313 clip.subtract(transparentRegionScreen);
314 if (clip.isEmpty()) {
315 // usually this won't happen because this should be taken care of
316 // by SurfaceFlinger::computeVisibleRegions()
317 return;
318 }
319 }
320
321 // reset GL state
322 glEnable(GL_SCISSOR_TEST);
323
324 onDraw(clip);
325
326 /*
327 glDisable(GL_TEXTURE_2D);
328 glDisable(GL_DITHER);
329 glEnable(GL_BLEND);
330 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
331 glColor4x(0, 0x8000, 0, 0x10000);
332 drawRegion(transparentRegionScreen);
333 glDisable(GL_BLEND);
334 */
335}
336
337GLuint LayerBase::createTexture() const
338{
339 GLuint textureName = -1;
340 glGenTextures(1, &textureName);
341 glBindTexture(GL_TEXTURE_2D, textureName);
342 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
343 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700344 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
345 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 return textureName;
347}
348
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700349void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
350 GLclampx green, GLclampx blue,
351 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352{
353 const DisplayHardware& hw(graphicPlane(0).displayHardware());
354 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700355 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 glDisable(GL_TEXTURE_2D);
357 glDisable(GL_BLEND);
358 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700359
360 Region::const_iterator it = clip.begin();
361 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700362 glEnable(GL_SCISSOR_TEST);
363 glVertexPointer(2, GL_FIXED, 0, mVertices);
364 while (it != end) {
365 const Rect& r = *it++;
366 const GLint sy = fbHeight - (r.top + r.height());
367 glScissor(r.left, sy, r.width(), r.height());
368 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 }
370}
371
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700372void LayerBase::clearWithOpenGL(const Region& clip) const
373{
374 clearWithOpenGL(clip,0,0,0,0);
375}
376
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700377void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378{
379 const DisplayHardware& hw(graphicPlane(0).displayHardware());
380 const uint32_t fbHeight = hw.getHeight();
381 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700384 validateTexture(texture.name);
385 uint32_t width = texture.width;
386 uint32_t height = texture.height;
387
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 glEnable(GL_TEXTURE_2D);
389
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 if (UNLIKELY(s.alpha < 0xFF)) {
391 // We have an alpha-modulation. We need to modulate all
392 // texture components by alpha because we're always using
393 // premultiplied alpha.
394
395 // If the texture doesn't have an alpha channel we can
396 // use REPLACE and switch to non premultiplied alpha
397 // blending (SRCA/ONE_MINUS_SRCA).
398
399 GLenum env, src;
400 if (needsBlending()) {
401 env = GL_MODULATE;
402 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
403 } else {
404 env = GL_REPLACE;
405 src = GL_SRC_ALPHA;
406 }
407 const GGLfixed alpha = (s.alpha << 16)/255;
408 glColor4x(alpha, alpha, alpha, alpha);
409 glEnable(GL_BLEND);
410 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
411 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
412 } else {
413 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
414 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
415 if (needsBlending()) {
416 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
417 glEnable(GL_BLEND);
418 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
419 } else {
420 glDisable(GL_BLEND);
421 }
422 }
423
Mathias Agopian95a666b2009-09-24 14:57:26 -0700424 Region::const_iterator it = clip.begin();
425 Region::const_iterator const end = clip.end();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 if (UNLIKELY(transformed()
427 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
428 {
429 //StopWatch watch("GL transformed");
Mathias Agopian95a666b2009-09-24 14:57:26 -0700430 const GLfixed texCoords[4][2] = {
431 { 0, 0 },
432 { 0, 0x10000 },
433 { 0x10000, 0x10000 },
434 { 0x10000, 0 }
435 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436
Mathias Agopian95a666b2009-09-24 14:57:26 -0700437 glMatrixMode(GL_TEXTURE);
438 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439
Mathias Agopian95a666b2009-09-24 14:57:26 -0700440 // the texture's source is rotated
441 if (texture.transform == HAL_TRANSFORM_ROT_90) {
442 // TODO: handle the other orientations
443 glTranslatef(0, 1, 0);
444 glRotatef(-90, 0, 0, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 }
Mathias Agopian95a666b2009-09-24 14:57:26 -0700446
447 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
448 DisplayHardware::DIRECT_TEXTURE))) {
449 // find the smallest power-of-two that will accommodate our surface
450 GLuint tw = 1 << (31 - clz(width));
451 GLuint th = 1 << (31 - clz(height));
452 if (tw < width) tw <<= 1;
453 if (th < height) th <<= 1;
454 GLfloat ws = GLfloat(width) /tw;
455 GLfloat hs = GLfloat(height)/th;
456 glScalef(ws, hs, 1.0f);
457 }
458
459 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
460 glVertexPointer(2, GL_FIXED, 0, mVertices);
461 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
462
463 while (it != end) {
464 const Rect& r = *it++;
465 const GLint sy = fbHeight - (r.top + r.height());
466 glScissor(r.left, sy, r.width(), r.height());
467 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
468 }
469 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700471 GLint crop[4] = { 0, height, width, -height };
472 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
473 int x = tx();
474 int y = ty();
475 y = fbHeight - (y + height);
476 while (it != end) {
477 const Rect& r = *it++;
478 const GLint sy = fbHeight - (r.top + r.height());
479 glScissor(r.left, sy, r.width(), r.height());
480 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 }
482 }
483}
484
485void LayerBase::validateTexture(GLint textureName) const
486{
487 glBindTexture(GL_TEXTURE_2D, textureName);
488 // TODO: reload the texture if needed
489 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700490 if (mUseLinearFiltering) {
491 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
492 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
493 } else {
494 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
495 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
496 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700497
498 if (needsDithering()) {
499 glEnable(GL_DITHER);
500 } else {
501 glDisable(GL_DITHER);
502 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503}
504
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700505void LayerBase::loadTexture(Texture* texture, GLint textureName,
506 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507{
508 // TODO: defer the actual texture reload until LayerBase::validateTexture
509 // is called.
510
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700511 texture->name = textureName;
512 GLuint& textureWidth(texture->width);
513 GLuint& textureHeight(texture->height);
514
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 uint32_t flags = mFlags;
516 glBindTexture(GL_TEXTURE_2D, textureName);
517
518 GLuint tw = t.width;
519 GLuint th = t.height;
520
521 /*
522 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700523 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
525 * need to do something reasonable (here creating a bigger texture).
526 *
527 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
528 *
529 * This situation doesn't happen often, but some h/w have a limitation
530 * for their framebuffer (eg: must be multiple of 8 pixels), and
531 * we need to take that into account when using these buffers as
532 * textures.
533 *
534 * This should never be a problem with POT textures
535 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536
537 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
538 unpack = 1 << ((unpack > 3) ? 3 : unpack);
539 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
540
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 /*
542 * round to POT if needed
543 */
544
545 GLuint texture_w = tw;
546 GLuint texture_h = th;
547 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
548 // find the smallest power-of-two that will accommodate our surface
549 texture_w = 1 << (31 - clz(t.width));
550 texture_h = 1 << (31 - clz(t.height));
551 if (texture_w < t.width) texture_w <<= 1;
552 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700556 Rect bounds(dirty.bounds());
557 GLvoid* data = 0;
558 if (texture_w!=textureWidth || texture_h!=textureHeight) {
559 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700561 if (!textureWidth || !textureHeight) {
562 // this is the first time, load the whole texture
563 if (texture_w==tw && texture_h==th) {
564 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567 // we have to create the texture first because it
568 // doesn't match the size of the buffer
569 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572
573 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
574 glTexImage2D(GL_TEXTURE_2D, 0,
575 GL_RGB, texture_w, texture_h, 0,
576 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
577 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
578 glTexImage2D(GL_TEXTURE_2D, 0,
579 GL_RGBA, texture_w, texture_h, 0,
580 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700581 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
582 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700583 glTexImage2D(GL_TEXTURE_2D, 0,
584 GL_RGBA, texture_w, texture_h, 0,
585 GL_RGBA, GL_UNSIGNED_BYTE, data);
586 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
587 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
588 // just show the Y plane of YUV buffers
589 glTexImage2D(GL_TEXTURE_2D, 0,
590 GL_LUMINANCE, texture_w, texture_h, 0,
591 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 "
595 "supported by the GL", this, textureName, t.format);
596 textureName = -1;
597 }
598 textureWidth = texture_w;
599 textureHeight = texture_h;
600 }
601 if (!data && textureName>=0) {
602 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
603 glTexSubImage2D(GL_TEXTURE_2D, 0,
604 0, bounds.top, t.width, bounds.height(),
605 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
606 t.data + bounds.top*t.stride*2);
607 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
608 glTexSubImage2D(GL_TEXTURE_2D, 0,
609 0, bounds.top, t.width, bounds.height(),
610 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
611 t.data + bounds.top*t.stride*2);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700612 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
613 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614 glTexSubImage2D(GL_TEXTURE_2D, 0,
615 0, bounds.top, t.width, bounds.height(),
616 GL_RGBA, GL_UNSIGNED_BYTE,
617 t.data + bounds.top*t.stride*4);
618 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
619 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
620 // just show the Y plane of YUV buffers
621 glTexSubImage2D(GL_TEXTURE_2D, 0,
622 0, bounds.top, t.width, bounds.height(),
623 GL_LUMINANCE, GL_UNSIGNED_BYTE,
624 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 }
626 }
627}
628
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629// ---------------------------------------------------------------------------
630
Mathias Agopian2e123242009-06-23 20:06:46 -0700631int32_t LayerBaseClient::sIdentity = 0;
632
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700634 const sp<Client>& client, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -0700635 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700636 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700638 lcblk = new SharedBufferServer(
639 client->ctrlblk, i, NUM_BUFFERS,
640 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700643void LayerBaseClient::onFirstRef()
644{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700645 sp<Client> client(this->client.promote());
646 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700647 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 }
649}
650
651LayerBaseClient::~LayerBaseClient()
652{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700653 sp<Client> client(this->client.promote());
654 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 client->free(mIndex);
656 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700657 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658}
659
Mathias Agopianf9d93272009-06-19 17:00:27 -0700660int32_t LayerBaseClient::serverIndex() const
661{
662 sp<Client> client(this->client.promote());
663 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 return (client->cid<<16)|mIndex;
665 }
666 return 0xFFFF0000 | mIndex;
667}
668
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700669sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700671 sp<Surface> s;
672 Mutex::Autolock _l(mLock);
673 s = mClientSurface.promote();
674 if (s == 0) {
675 s = createSurface();
676 mClientSurface = s;
677 }
678 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679}
680
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700681sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
682{
Mathias Agopian9a112062009-04-17 19:36:26 -0700683 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700684 const_cast<LayerBaseClient *>(this));
685}
686
687// ---------------------------------------------------------------------------
688
Mathias Agopian9a112062009-04-17 19:36:26 -0700689LayerBaseClient::Surface::Surface(
690 const sp<SurfaceFlinger>& flinger,
691 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700692 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700693 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
694{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700695}
696
697
Mathias Agopian9a112062009-04-17 19:36:26 -0700698LayerBaseClient::Surface::~Surface()
699{
700 /*
701 * This is a good place to clean-up all client resources
702 */
703
704 // destroy client resources
705 sp<LayerBaseClient> layer = getOwner();
706 if (layer != 0) {
707 mFlinger->destroySurface(layer);
708 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700709}
710
711sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
712 sp<LayerBaseClient> owner(mOwner.promote());
713 return owner;
714}
715
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700716status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700717 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700718{
719 switch (code) {
720 case REGISTER_BUFFERS:
721 case UNREGISTER_BUFFERS:
722 case CREATE_OVERLAY:
723 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700724 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
725 IPCThreadState* ipc = IPCThreadState::self();
726 const int pid = ipc->getCallingPid();
727 const int uid = ipc->getCallingUid();
728 LOGE("Permission Denial: "
729 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
730 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700731 }
732 }
733 }
734 return BnSurface::onTransact(code, data, reply, flags);
735}
736
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700737sp<SurfaceBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700738{
739 return NULL;
740}
741
742status_t LayerBaseClient::Surface::registerBuffers(
743 const ISurface::BufferHeap& buffers)
744{
745 return INVALID_OPERATION;
746}
747
748void LayerBaseClient::Surface::postBuffer(ssize_t offset)
749{
750}
751
752void LayerBaseClient::Surface::unregisterBuffers()
753{
754}
755
756sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
757 uint32_t w, uint32_t h, int32_t format)
758{
759 return NULL;
760};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761
762// ---------------------------------------------------------------------------
763
764}; // namespace android