blob: 8b9a84230098b0ffcbb73a0d5984985d4ba26e5a [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
37// We don't honor the premultiplied alpha flags, which means that
38// premultiplied surface may be composed using a non-premultiplied
39// equation. We do this because it may be a lot faster on some hardware
40// The correct value is HONOR_PREMULTIPLIED_ALPHA = 1
41#define HONOR_PREMULTIPLIED_ALPHA 0
42
43namespace android {
44
45// ---------------------------------------------------------------------------
46
47const uint32_t LayerBase::typeInfo = 1;
48const char* const LayerBase::typeID = "LayerBase";
49
50const uint32_t LayerBaseClient::typeInfo = LayerBase::typeInfo | 2;
51const char* const LayerBaseClient::typeID = "LayerBaseClient";
52
53// ---------------------------------------------------------------------------
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
56 : dpy(display), contentDirty(false),
57 mFlinger(flinger),
58 mTransformed(false),
Mathias Agopian44cac132009-09-23 18:34:53 -070059 mUseLinearFiltering(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 mOrientation(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 mTransactionFlags(0),
62 mPremultipliedAlpha(true),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 mInvalidate(0)
64{
65 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
66 mFlags = hw.getFlags();
67}
68
69LayerBase::~LayerBase()
70{
71}
72
73const GraphicPlane& LayerBase::graphicPlane(int dpy) const
74{
75 return mFlinger->graphicPlane(dpy);
76}
77
78GraphicPlane& LayerBase::graphicPlane(int dpy)
79{
80 return mFlinger->graphicPlane(dpy);
81}
82
83void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
84{
85 uint32_t layerFlags = 0;
86 if (flags & ISurfaceComposer::eHidden)
87 layerFlags = ISurfaceComposer::eLayerHidden;
88
89 if (flags & ISurfaceComposer::eNonPremultiplied)
90 mPremultipliedAlpha = false;
91
92 mCurrentState.z = 0;
93 mCurrentState.w = w;
94 mCurrentState.h = h;
95 mCurrentState.alpha = 0xFF;
96 mCurrentState.flags = layerFlags;
97 mCurrentState.sequence = 0;
98 mCurrentState.transform.set(0, 0);
99
100 // drawing state & current state are identical
101 mDrawingState = mCurrentState;
102}
103
104void LayerBase::commitTransaction(bool skipSize) {
105 const uint32_t w = mDrawingState.w;
106 const uint32_t h = mDrawingState.h;
107 mDrawingState = mCurrentState;
108 if (skipSize) {
109 mDrawingState.w = w;
110 mDrawingState.h = h;
111 }
112}
113void LayerBase::forceVisibilityTransaction() {
114 // this can be called without SurfaceFlinger.mStateLock, but if we
115 // can atomically increment the sequence number, it doesn't matter.
116 android_atomic_inc(&mCurrentState.sequence);
117 requestTransaction();
118}
119bool LayerBase::requestTransaction() {
120 int32_t old = setTransactionFlags(eTransactionNeeded);
121 return ((old & eTransactionNeeded) == 0);
122}
123uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
124 return android_atomic_and(~flags, &mTransactionFlags) & flags;
125}
126uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
127 return android_atomic_or(flags, &mTransactionFlags);
128}
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130bool LayerBase::setPosition(int32_t x, int32_t y) {
131 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
132 return false;
133 mCurrentState.sequence++;
134 mCurrentState.transform.set(x, y);
135 requestTransaction();
136 return true;
137}
138bool LayerBase::setLayer(uint32_t z) {
139 if (mCurrentState.z == z)
140 return false;
141 mCurrentState.sequence++;
142 mCurrentState.z = z;
143 requestTransaction();
144 return true;
145}
146bool LayerBase::setSize(uint32_t w, uint32_t h) {
147 if (mCurrentState.w == w && mCurrentState.h == h)
148 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 mCurrentState.w = w;
150 mCurrentState.h = h;
151 requestTransaction();
152 return true;
153}
154bool LayerBase::setAlpha(uint8_t alpha) {
155 if (mCurrentState.alpha == alpha)
156 return false;
157 mCurrentState.sequence++;
158 mCurrentState.alpha = alpha;
159 requestTransaction();
160 return true;
161}
162bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
163 // TODO: check the matrix has changed
164 mCurrentState.sequence++;
165 mCurrentState.transform.set(
166 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
167 requestTransaction();
168 return true;
169}
170bool LayerBase::setTransparentRegionHint(const Region& transparent) {
171 // TODO: check the region has changed
172 mCurrentState.sequence++;
173 mCurrentState.transparentRegion = transparent;
174 requestTransaction();
175 return true;
176}
177bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
178 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
179 if (mCurrentState.flags == newFlags)
180 return false;
181 mCurrentState.sequence++;
182 mCurrentState.flags = newFlags;
183 requestTransaction();
184 return true;
185}
186
187Rect LayerBase::visibleBounds() const
188{
189 return mTransformedBounds;
190}
191
192void LayerBase::setVisibleRegion(const Region& visibleRegion) {
193 // always called from main thread
194 visibleRegionScreen = visibleRegion;
195}
196
197void LayerBase::setCoveredRegion(const Region& coveredRegion) {
198 // always called from main thread
199 coveredRegionScreen = coveredRegion;
200}
201
202uint32_t LayerBase::doTransaction(uint32_t flags)
203{
204 const Layer::State& front(drawingState());
205 const Layer::State& temp(currentState());
206
207 if (temp.sequence != front.sequence) {
208 // invalidate and recompute the visible regions if needed
209 flags |= eVisibleRegion;
210 this->contentDirty = true;
211 }
Mathias Agopian44cac132009-09-23 18:34:53 -0700212
213 if (temp.sequence != front.sequence) {
214 const bool linearFiltering = mUseLinearFiltering;
215 mUseLinearFiltering = false;
216 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
217 // we may use linear filtering, if the matrix scales us
218 const uint8_t type = temp.transform.getType();
219 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
220 mUseLinearFiltering = true;
221 }
222 }
223 }
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 // Commit the transaction
226 commitTransaction(flags & eRestartTransaction);
227 return flags;
228}
229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230void LayerBase::validateVisibility(const Transform& planeTransform)
231{
232 const Layer::State& s(drawingState());
233 const Transform tr(planeTransform * s.transform);
234 const bool transformed = tr.transformed();
235
Mathias Agopian9779b2212009-09-07 16:32:45 -0700236 uint32_t w = s.w;
237 uint32_t h = s.h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 tr.transform(mVertices[0], 0, 0);
239 tr.transform(mVertices[1], 0, h);
240 tr.transform(mVertices[2], w, h);
241 tr.transform(mVertices[3], w, 0);
242 if (UNLIKELY(transformed)) {
243 // NOTE: here we could also punt if we have too many rectangles
244 // in the transparent region
245 if (tr.preserveRects()) {
246 // transform the transparent region
247 transparentRegionScreen = tr.transform(s.transparentRegion);
248 } else {
249 // transformation too complex, can't do the transparent region
250 // optimization.
251 transparentRegionScreen.clear();
252 }
253 } else {
254 transparentRegionScreen = s.transparentRegion;
255 }
256
257 // cache a few things...
258 mOrientation = tr.getOrientation();
259 mTransformedBounds = tr.makeBounds(w, h);
260 mTransformed = transformed;
261 mLeft = tr.tx();
262 mTop = tr.ty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263}
264
265void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
266{
267}
268
269void LayerBase::unlockPageFlip(
270 const Transform& planeTransform, Region& outDirtyRegion)
271{
272 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
273 outDirtyRegion.orSelf(visibleRegionScreen);
274 }
275}
276
277void LayerBase::finishPageFlip()
278{
279}
280
281void LayerBase::invalidate()
282{
283 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
284 mFlinger->signalEvent();
285 }
286}
287
288void LayerBase::drawRegion(const Region& reg) const
289{
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700290 Region::const_iterator it = reg.begin();
291 Region::const_iterator const end = reg.end();
292 if (it != end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 Rect r;
294 const DisplayHardware& hw(graphicPlane(0).displayHardware());
295 const int32_t fbWidth = hw.getWidth();
296 const int32_t fbHeight = hw.getHeight();
297 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
298 { fbWidth, fbHeight }, { 0, fbHeight } };
299 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700300 while (it != end) {
301 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 const GLint sy = fbHeight - (r.top + r.height());
303 glScissor(r.left, sy, r.width(), r.height());
304 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
305 }
306 }
307}
308
309void LayerBase::draw(const Region& inClip) const
310{
311 // invalidate the region we'll update
312 Region clip(inClip); // copy-on-write, so no-op most of the time
313
314 // Remove the transparent area from the clipping region
315 const State& s = drawingState();
316 if (LIKELY(!s.transparentRegion.isEmpty())) {
317 clip.subtract(transparentRegionScreen);
318 if (clip.isEmpty()) {
319 // usually this won't happen because this should be taken care of
320 // by SurfaceFlinger::computeVisibleRegions()
321 return;
322 }
323 }
324
325 // reset GL state
326 glEnable(GL_SCISSOR_TEST);
327
328 onDraw(clip);
329
330 /*
331 glDisable(GL_TEXTURE_2D);
332 glDisable(GL_DITHER);
333 glEnable(GL_BLEND);
334 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
335 glColor4x(0, 0x8000, 0, 0x10000);
336 drawRegion(transparentRegionScreen);
337 glDisable(GL_BLEND);
338 */
339}
340
341GLuint LayerBase::createTexture() const
342{
343 GLuint textureName = -1;
344 glGenTextures(1, &textureName);
345 glBindTexture(GL_TEXTURE_2D, textureName);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
347 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian44cac132009-09-23 18:34:53 -0700348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 return textureName;
351}
352
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700353void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
354 GLclampx green, GLclampx blue,
355 GLclampx alpha) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356{
357 const DisplayHardware& hw(graphicPlane(0).displayHardware());
358 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700359 glColor4x(red,green,blue,alpha);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 glDisable(GL_TEXTURE_2D);
361 glDisable(GL_BLEND);
362 glDisable(GL_DITHER);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700363
364 Region::const_iterator it = clip.begin();
365 Region::const_iterator const end = clip.end();
366 if (it != end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 glEnable(GL_SCISSOR_TEST);
368 glVertexPointer(2, GL_FIXED, 0, mVertices);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700369 while (it != end) {
370 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 const GLint sy = fbHeight - (r.top + r.height());
372 glScissor(r.left, sy, r.width(), r.height());
373 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
374 }
375 }
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
396 // Dithering...
Mathias Agopian7164b8d2009-09-04 17:27:16 -0700397 bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG);
398 if (fast || s.flags & ISurfaceComposer::eLayerDither) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 glEnable(GL_DITHER);
400 } else {
401 glDisable(GL_DITHER);
402 }
403
404 if (UNLIKELY(s.alpha < 0xFF)) {
405 // We have an alpha-modulation. We need to modulate all
406 // texture components by alpha because we're always using
407 // premultiplied alpha.
408
409 // If the texture doesn't have an alpha channel we can
410 // use REPLACE and switch to non premultiplied alpha
411 // blending (SRCA/ONE_MINUS_SRCA).
412
413 GLenum env, src;
414 if (needsBlending()) {
415 env = GL_MODULATE;
416 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
417 } else {
418 env = GL_REPLACE;
419 src = GL_SRC_ALPHA;
420 }
421 const GGLfixed alpha = (s.alpha << 16)/255;
422 glColor4x(alpha, alpha, alpha, alpha);
423 glEnable(GL_BLEND);
424 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
425 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
426 } else {
427 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
428 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
429 if (needsBlending()) {
430 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
431 glEnable(GL_BLEND);
432 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
433 } else {
434 glDisable(GL_BLEND);
435 }
436 }
437
438 if (UNLIKELY(transformed()
439 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
440 {
441 //StopWatch watch("GL transformed");
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700442 Region::const_iterator it = clip.begin();
443 Region::const_iterator const end = clip.end();
444 if (it != end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 const GLfixed texCoords[4][2] = {
446 { 0, 0 },
447 { 0, 0x10000 },
448 { 0x10000, 0x10000 },
449 { 0x10000, 0 }
450 };
451
452 glMatrixMode(GL_TEXTURE);
453 glLoadIdentity();
454
Mathias Agopian999543b2009-06-23 18:08:22 -0700455 // the texture's source is rotated
456 if (texture.transform == HAL_TRANSFORM_ROT_90) {
457 // TODO: handle the other orientations
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 glTranslatef(0, 1, 0);
459 glRotatef(-90, 0, 0, 1);
460 }
461
Mathias Agopianf293b2f2009-07-30 12:19:10 -0700462 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
463 DisplayHardware::DIRECT_TEXTURE))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 // find the smallest power-of-two that will accommodate our surface
Mathias Agopiandff8e582009-05-04 14:17:04 -0700465 GLuint tw = 1 << (31 - clz(width));
466 GLuint th = 1 << (31 - clz(height));
467 if (tw < width) tw <<= 1;
468 if (th < height) th <<= 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 // this divide should be relatively fast because it's
470 // a power-of-two (optimized path in libgcc)
Mathias Agopiandff8e582009-05-04 14:17:04 -0700471 GLfloat ws = GLfloat(width) /tw;
472 GLfloat hs = GLfloat(height)/th;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 glScalef(ws, hs, 1.0f);
474 }
475
476 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
477 glVertexPointer(2, GL_FIXED, 0, mVertices);
478 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
479
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700480 while (it != end) {
481 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 const GLint sy = fbHeight - (r.top + r.height());
483 glScissor(r.left, sy, r.width(), r.height());
484 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
487 }
488 } else {
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700489 Region::const_iterator it = clip.begin();
490 Region::const_iterator const end = clip.end();
491 if (it != end) {
Mathias Agopiandff8e582009-05-04 14:17:04 -0700492 GLint crop[4] = { 0, height, width, -height };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
494 int x = tx();
495 int y = ty();
Mathias Agopiandff8e582009-05-04 14:17:04 -0700496 y = fbHeight - (y + height);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700497 while (it != end) {
498 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 const GLint sy = fbHeight - (r.top + r.height());
500 glScissor(r.left, sy, r.width(), r.height());
Mathias Agopiandff8e582009-05-04 14:17:04 -0700501 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503 }
504 }
505}
506
507void LayerBase::validateTexture(GLint textureName) const
508{
509 glBindTexture(GL_TEXTURE_2D, textureName);
510 // TODO: reload the texture if needed
511 // this is currently done in loadTexture() below
Mathias Agopian44cac132009-09-23 18:34:53 -0700512 if (mUseLinearFiltering) {
513 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
514 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
515 } else {
516 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
517 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519}
520
Mathias Agopian999543b2009-06-23 18:08:22 -0700521void LayerBase::loadTexture(Texture* texture, GLint textureName,
522 const Region& dirty, const GGLSurface& t) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523{
524 // TODO: defer the actual texture reload until LayerBase::validateTexture
525 // is called.
526
Mathias Agopian999543b2009-06-23 18:08:22 -0700527 texture->name = textureName;
528 GLuint& textureWidth(texture->width);
529 GLuint& textureHeight(texture->height);
530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 uint32_t flags = mFlags;
532 glBindTexture(GL_TEXTURE_2D, textureName);
533
534 GLuint tw = t.width;
535 GLuint th = t.height;
536
537 /*
538 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian1473f462009-04-10 14:24:30 -0700539 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
541 * need to do something reasonable (here creating a bigger texture).
542 *
543 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
544 *
545 * This situation doesn't happen often, but some h/w have a limitation
546 * for their framebuffer (eg: must be multiple of 8 pixels), and
547 * we need to take that into account when using these buffers as
548 * textures.
549 *
550 * This should never be a problem with POT textures
551 */
Mathias Agopian1473f462009-04-10 14:24:30 -0700552
553 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
554 unpack = 1 << ((unpack > 3) ? 3 : unpack);
555 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 /*
558 * round to POT if needed
559 */
560
561 GLuint texture_w = tw;
562 GLuint texture_h = th;
563 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
564 // find the smallest power-of-two that will accommodate our surface
565 texture_w = 1 << (31 - clz(t.width));
566 texture_h = 1 << (31 - clz(t.height));
567 if (texture_w < t.width) texture_w <<= 1;
568 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571regular:
Mathias Agopian1473f462009-04-10 14:24:30 -0700572 Rect bounds(dirty.bounds());
573 GLvoid* data = 0;
574 if (texture_w!=textureWidth || texture_h!=textureHeight) {
575 // texture size changed, we need to create a new one
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576
Mathias Agopian1473f462009-04-10 14:24:30 -0700577 if (!textureWidth || !textureHeight) {
578 // this is the first time, load the whole texture
579 if (texture_w==tw && texture_h==th) {
580 // we can do it one pass
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 data = t.data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 } else {
Mathias Agopian1473f462009-04-10 14:24:30 -0700583 // we have to create the texture first because it
584 // doesn't match the size of the buffer
585 bounds.set(Rect(tw, th));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700588
589 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
590 glTexImage2D(GL_TEXTURE_2D, 0,
591 GL_RGB, texture_w, texture_h, 0,
592 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
593 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
594 glTexImage2D(GL_TEXTURE_2D, 0,
595 GL_RGBA, texture_w, texture_h, 0,
596 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian64a7c6bf2009-09-14 18:10:30 -0700597 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
598 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700599 glTexImage2D(GL_TEXTURE_2D, 0,
600 GL_RGBA, texture_w, texture_h, 0,
601 GL_RGBA, GL_UNSIGNED_BYTE, data);
602 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
603 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
604 // just show the Y plane of YUV buffers
605 glTexImage2D(GL_TEXTURE_2D, 0,
606 GL_LUMINANCE, texture_w, texture_h, 0,
607 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
608 } else {
609 // oops, we don't handle this format!
610 LOGE("layer %p, texture=%d, using format %d, which is not "
611 "supported by the GL", this, textureName, t.format);
612 textureName = -1;
613 }
614 textureWidth = texture_w;
615 textureHeight = texture_h;
616 }
617 if (!data && textureName>=0) {
618 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
619 glTexSubImage2D(GL_TEXTURE_2D, 0,
620 0, bounds.top, t.width, bounds.height(),
621 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
622 t.data + bounds.top*t.stride*2);
623 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
624 glTexSubImage2D(GL_TEXTURE_2D, 0,
625 0, bounds.top, t.width, bounds.height(),
626 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
627 t.data + bounds.top*t.stride*2);
Mathias Agopian64a7c6bf2009-09-14 18:10:30 -0700628 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
629 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700630 glTexSubImage2D(GL_TEXTURE_2D, 0,
631 0, bounds.top, t.width, bounds.height(),
632 GL_RGBA, GL_UNSIGNED_BYTE,
633 t.data + bounds.top*t.stride*4);
634 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
635 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
636 // just show the Y plane of YUV buffers
637 glTexSubImage2D(GL_TEXTURE_2D, 0,
638 0, bounds.top, t.width, bounds.height(),
639 GL_LUMINANCE, GL_UNSIGNED_BYTE,
640 t.data + bounds.top*t.stride);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642 }
643}
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645// ---------------------------------------------------------------------------
646
Mathias Agopianc6603952009-06-23 20:06:46 -0700647int32_t LayerBaseClient::sIdentity = 0;
648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700650 const sp<Client>& client, int32_t i)
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700651 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopian9779b2212009-09-07 16:32:45 -0700652 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653{
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700654 lcblk = new SharedBufferServer(
655 client->ctrlblk, i, NUM_BUFFERS,
656 mIdentity);
Mathias Agopian1473f462009-04-10 14:24:30 -0700657}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
Mathias Agopian1473f462009-04-10 14:24:30 -0700659void LayerBaseClient::onFirstRef()
660{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700661 sp<Client> client(this->client.promote());
662 if (client != 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700663 client->bindLayer(this, mIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665}
666
667LayerBaseClient::~LayerBaseClient()
668{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700669 sp<Client> client(this->client.promote());
670 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 client->free(mIndex);
672 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700673 delete lcblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674}
675
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700676int32_t LayerBaseClient::serverIndex() const
677{
678 sp<Client> client(this->client.promote());
679 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 return (client->cid<<16)|mIndex;
681 }
682 return 0xFFFF0000 | mIndex;
683}
684
Mathias Agopian1473f462009-04-10 14:24:30 -0700685sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686{
Mathias Agopian1473f462009-04-10 14:24:30 -0700687 sp<Surface> s;
688 Mutex::Autolock _l(mLock);
689 s = mClientSurface.promote();
690 if (s == 0) {
691 s = createSurface();
692 mClientSurface = s;
693 }
694 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695}
696
Mathias Agopian1473f462009-04-10 14:24:30 -0700697sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
698{
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700699 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700700 const_cast<LayerBaseClient *>(this));
701}
702
703// ---------------------------------------------------------------------------
704
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700705LayerBaseClient::Surface::Surface(
706 const sp<SurfaceFlinger>& flinger,
707 SurfaceID id, int identity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700708 const sp<LayerBaseClient>& owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700709 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
710{
Mathias Agopian1473f462009-04-10 14:24:30 -0700711}
712
713
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700714LayerBaseClient::Surface::~Surface()
715{
716 /*
717 * This is a good place to clean-up all client resources
718 */
719
720 // destroy client resources
721 sp<LayerBaseClient> layer = getOwner();
722 if (layer != 0) {
723 mFlinger->destroySurface(layer);
724 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700725}
726
727sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
728 sp<LayerBaseClient> owner(mOwner.promote());
729 return owner;
730}
731
Mathias Agopian1473f462009-04-10 14:24:30 -0700732status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian151e8592009-06-15 18:24:59 -0700733 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian1473f462009-04-10 14:24:30 -0700734{
735 switch (code) {
736 case REGISTER_BUFFERS:
737 case UNREGISTER_BUFFERS:
738 case CREATE_OVERLAY:
739 {
Mathias Agopian151e8592009-06-15 18:24:59 -0700740 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
741 IPCThreadState* ipc = IPCThreadState::self();
742 const int pid = ipc->getCallingPid();
743 const int uid = ipc->getCallingUid();
744 LOGE("Permission Denial: "
745 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
746 return PERMISSION_DENIED;
Mathias Agopian1473f462009-04-10 14:24:30 -0700747 }
748 }
749 }
750 return BnSurface::onTransact(code, data, reply, flags);
751}
752
Mathias Agopian9779b2212009-09-07 16:32:45 -0700753sp<SurfaceBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700754{
755 return NULL;
756}
757
758status_t LayerBaseClient::Surface::registerBuffers(
759 const ISurface::BufferHeap& buffers)
760{
761 return INVALID_OPERATION;
762}
763
764void LayerBaseClient::Surface::postBuffer(ssize_t offset)
765{
766}
767
768void LayerBaseClient::Surface::unregisterBuffers()
769{
770}
771
772sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
773 uint32_t w, uint32_t h, int32_t format)
774{
775 return NULL;
776};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777
778// ---------------------------------------------------------------------------
779
780}; // namespace android