blob: a351253d8d099a79422731981c678bb11fbb0de7 [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
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 Projectedbf3b62009-03-03 19:31:44 -080055LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
56 : dpy(display), contentDirty(false),
57 mFlinger(flinger),
58 mTransformed(false),
Mathias Agopiana2fe0a22009-09-23 18:34:53 -070059 mUseLinearFiltering(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 mOrientation(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 mTransactionFlags(0),
62 mPremultipliedAlpha(true),
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-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 Projectedbf3b62009-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 Agopiana2fe0a22009-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 Projectedbf3b62009-03-03 19:31:44 -0800225 // Commit the transaction
226 commitTransaction(flags & eRestartTransaction);
227 return flags;
228}
229
The Android Open Source Projectedbf3b62009-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 Agopiancbb288b2009-09-07 16:32:45 -0700236 uint32_t w = s.w;
237 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-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 Agopian20f68782009-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 Projectedbf3b62009-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 Agopian20f68782009-05-11 00:03:41 -0700300 while (it != end) {
301 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-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 Agopiana2fe0a22009-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 Projectedbf3b62009-03-03 19:31:44 -0800350 return textureName;
351}
352
Rebecca Schultz Zavin29aa74c2009-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 Projectedbf3b62009-03-03 19:31:44 -0800356{
357 const DisplayHardware& hw(graphicPlane(0).displayHardware());
358 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700359 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 glDisable(GL_TEXTURE_2D);
361 glDisable(GL_BLEND);
362 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700363
364 Region::const_iterator it = clip.begin();
365 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700366 glEnable(GL_SCISSOR_TEST);
367 glVertexPointer(2, GL_FIXED, 0, mVertices);
368 while (it != end) {
369 const Rect& r = *it++;
370 const GLint sy = fbHeight - (r.top + r.height());
371 glScissor(r.left, sy, r.width(), r.height());
372 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373 }
374}
375
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700376void LayerBase::clearWithOpenGL(const Region& clip) const
377{
378 clearWithOpenGL(clip,0,0,0,0);
379}
380
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700381void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382{
383 const DisplayHardware& hw(graphicPlane(0).displayHardware());
384 const uint32_t fbHeight = hw.getHeight();
385 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700386
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700388 validateTexture(texture.name);
389 uint32_t width = texture.width;
390 uint32_t height = texture.height;
391
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 glEnable(GL_TEXTURE_2D);
393
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 if (UNLIKELY(s.alpha < 0xFF)) {
395 // We have an alpha-modulation. We need to modulate all
396 // texture components by alpha because we're always using
397 // premultiplied alpha.
398
399 // If the texture doesn't have an alpha channel we can
400 // use REPLACE and switch to non premultiplied alpha
401 // blending (SRCA/ONE_MINUS_SRCA).
402
403 GLenum env, src;
404 if (needsBlending()) {
405 env = GL_MODULATE;
406 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
407 } else {
408 env = GL_REPLACE;
409 src = GL_SRC_ALPHA;
410 }
411 const GGLfixed alpha = (s.alpha << 16)/255;
412 glColor4x(alpha, alpha, alpha, alpha);
413 glEnable(GL_BLEND);
414 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
415 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
416 } else {
417 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
418 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
419 if (needsBlending()) {
420 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
421 glEnable(GL_BLEND);
422 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
423 } else {
424 glDisable(GL_BLEND);
425 }
426 }
427
Mathias Agopian95a666b2009-09-24 14:57:26 -0700428 Region::const_iterator it = clip.begin();
429 Region::const_iterator const end = clip.end();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 if (UNLIKELY(transformed()
431 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
432 {
433 //StopWatch watch("GL transformed");
Mathias Agopian95a666b2009-09-24 14:57:26 -0700434 const GLfixed texCoords[4][2] = {
435 { 0, 0 },
436 { 0, 0x10000 },
437 { 0x10000, 0x10000 },
438 { 0x10000, 0 }
439 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440
Mathias Agopian95a666b2009-09-24 14:57:26 -0700441 glMatrixMode(GL_TEXTURE);
442 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443
Mathias Agopian95a666b2009-09-24 14:57:26 -0700444 // the texture's source is rotated
445 if (texture.transform == HAL_TRANSFORM_ROT_90) {
446 // TODO: handle the other orientations
447 glTranslatef(0, 1, 0);
448 glRotatef(-90, 0, 0, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 }
Mathias Agopian95a666b2009-09-24 14:57:26 -0700450
451 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
452 DisplayHardware::DIRECT_TEXTURE))) {
453 // find the smallest power-of-two that will accommodate our surface
454 GLuint tw = 1 << (31 - clz(width));
455 GLuint th = 1 << (31 - clz(height));
456 if (tw < width) tw <<= 1;
457 if (th < height) th <<= 1;
458 GLfloat ws = GLfloat(width) /tw;
459 GLfloat hs = GLfloat(height)/th;
460 glScalef(ws, hs, 1.0f);
461 }
462
463 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
464 glVertexPointer(2, GL_FIXED, 0, mVertices);
465 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
466
467 while (it != end) {
468 const Rect& r = *it++;
469 const GLint sy = fbHeight - (r.top + r.height());
470 glScissor(r.left, sy, r.width(), r.height());
471 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
472 }
473 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700475 GLint crop[4] = { 0, height, width, -height };
476 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
477 int x = tx();
478 int y = ty();
479 y = fbHeight - (y + height);
480 while (it != end) {
481 const Rect& r = *it++;
482 const GLint sy = fbHeight - (r.top + r.height());
483 glScissor(r.left, sy, r.width(), r.height());
484 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 }
486 }
487}
488
489void LayerBase::validateTexture(GLint textureName) const
490{
491 glBindTexture(GL_TEXTURE_2D, textureName);
492 // TODO: reload the texture if needed
493 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700494 if (mUseLinearFiltering) {
495 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
496 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
497 } else {
498 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
499 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
500 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700501
502 if (needsDithering()) {
503 glEnable(GL_DITHER);
504 } else {
505 glDisable(GL_DITHER);
506 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507}
508
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700509void LayerBase::loadTexture(Texture* texture, GLint textureName,
510 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511{
512 // TODO: defer the actual texture reload until LayerBase::validateTexture
513 // is called.
514
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700515 texture->name = textureName;
516 GLuint& textureWidth(texture->width);
517 GLuint& textureHeight(texture->height);
518
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 uint32_t flags = mFlags;
520 glBindTexture(GL_TEXTURE_2D, textureName);
521
522 GLuint tw = t.width;
523 GLuint th = t.height;
524
525 /*
526 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700527 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
529 * need to do something reasonable (here creating a bigger texture).
530 *
531 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
532 *
533 * This situation doesn't happen often, but some h/w have a limitation
534 * for their framebuffer (eg: must be multiple of 8 pixels), and
535 * we need to take that into account when using these buffers as
536 * textures.
537 *
538 * This should never be a problem with POT textures
539 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700540
541 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
542 unpack = 1 << ((unpack > 3) ? 3 : unpack);
543 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
544
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 /*
546 * round to POT if needed
547 */
548
549 GLuint texture_w = tw;
550 GLuint texture_h = th;
551 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
552 // find the smallest power-of-two that will accommodate our surface
553 texture_w = 1 << (31 - clz(t.width));
554 texture_h = 1 << (31 - clz(t.height));
555 if (texture_w < t.width) texture_w <<= 1;
556 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700558
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700560 Rect bounds(dirty.bounds());
561 GLvoid* data = 0;
562 if (texture_w!=textureWidth || texture_h!=textureHeight) {
563 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 if (!textureWidth || !textureHeight) {
566 // this is the first time, load the whole texture
567 if (texture_w==tw && texture_h==th) {
568 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700571 // we have to create the texture first because it
572 // doesn't match the size of the buffer
573 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700576
577 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
578 glTexImage2D(GL_TEXTURE_2D, 0,
579 GL_RGB, texture_w, texture_h, 0,
580 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
581 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
582 glTexImage2D(GL_TEXTURE_2D, 0,
583 GL_RGBA, texture_w, texture_h, 0,
584 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700585 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
586 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700587 glTexImage2D(GL_TEXTURE_2D, 0,
588 GL_RGBA, texture_w, texture_h, 0,
589 GL_RGBA, GL_UNSIGNED_BYTE, data);
590 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
591 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
592 // just show the Y plane of YUV buffers
593 glTexImage2D(GL_TEXTURE_2D, 0,
594 GL_LUMINANCE, texture_w, texture_h, 0,
595 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
596 } else {
597 // oops, we don't handle this format!
598 LOGE("layer %p, texture=%d, using format %d, which is not "
599 "supported by the GL", this, textureName, t.format);
600 textureName = -1;
601 }
602 textureWidth = texture_w;
603 textureHeight = texture_h;
604 }
605 if (!data && textureName>=0) {
606 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
607 glTexSubImage2D(GL_TEXTURE_2D, 0,
608 0, bounds.top, t.width, bounds.height(),
609 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
610 t.data + bounds.top*t.stride*2);
611 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
612 glTexSubImage2D(GL_TEXTURE_2D, 0,
613 0, bounds.top, t.width, bounds.height(),
614 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
615 t.data + bounds.top*t.stride*2);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700616 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
617 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700618 glTexSubImage2D(GL_TEXTURE_2D, 0,
619 0, bounds.top, t.width, bounds.height(),
620 GL_RGBA, GL_UNSIGNED_BYTE,
621 t.data + bounds.top*t.stride*4);
622 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
623 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
624 // just show the Y plane of YUV buffers
625 glTexSubImage2D(GL_TEXTURE_2D, 0,
626 0, bounds.top, t.width, bounds.height(),
627 GL_LUMINANCE, GL_UNSIGNED_BYTE,
628 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 }
630 }
631}
632
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633// ---------------------------------------------------------------------------
634
Mathias Agopian2e123242009-06-23 20:06:46 -0700635int32_t LayerBaseClient::sIdentity = 0;
636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700638 const sp<Client>& client, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -0700639 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700640 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700642 lcblk = new SharedBufferServer(
643 client->ctrlblk, i, NUM_BUFFERS,
644 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700647void LayerBaseClient::onFirstRef()
648{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700649 sp<Client> client(this->client.promote());
650 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700651 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 }
653}
654
655LayerBaseClient::~LayerBaseClient()
656{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700657 sp<Client> client(this->client.promote());
658 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 client->free(mIndex);
660 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700661 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662}
663
Mathias Agopianf9d93272009-06-19 17:00:27 -0700664int32_t LayerBaseClient::serverIndex() const
665{
666 sp<Client> client(this->client.promote());
667 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 return (client->cid<<16)|mIndex;
669 }
670 return 0xFFFF0000 | mIndex;
671}
672
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700673sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700675 sp<Surface> s;
676 Mutex::Autolock _l(mLock);
677 s = mClientSurface.promote();
678 if (s == 0) {
679 s = createSurface();
680 mClientSurface = s;
681 }
682 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683}
684
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700685sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
686{
Mathias Agopian9a112062009-04-17 19:36:26 -0700687 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700688 const_cast<LayerBaseClient *>(this));
689}
690
691// ---------------------------------------------------------------------------
692
Mathias Agopian9a112062009-04-17 19:36:26 -0700693LayerBaseClient::Surface::Surface(
694 const sp<SurfaceFlinger>& flinger,
695 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700696 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700697 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
698{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700699}
700
701
Mathias Agopian9a112062009-04-17 19:36:26 -0700702LayerBaseClient::Surface::~Surface()
703{
704 /*
705 * This is a good place to clean-up all client resources
706 */
707
708 // destroy client resources
709 sp<LayerBaseClient> layer = getOwner();
710 if (layer != 0) {
711 mFlinger->destroySurface(layer);
712 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700713}
714
715sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
716 sp<LayerBaseClient> owner(mOwner.promote());
717 return owner;
718}
719
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700720status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700721 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700722{
723 switch (code) {
724 case REGISTER_BUFFERS:
725 case UNREGISTER_BUFFERS:
726 case CREATE_OVERLAY:
727 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700728 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
729 IPCThreadState* ipc = IPCThreadState::self();
730 const int pid = ipc->getCallingPid();
731 const int uid = ipc->getCallingUid();
732 LOGE("Permission Denial: "
733 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
734 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700735 }
736 }
737 }
738 return BnSurface::onTransact(code, data, reply, flags);
739}
740
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700741sp<SurfaceBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700742{
743 return NULL;
744}
745
746status_t LayerBaseClient::Surface::registerBuffers(
747 const ISurface::BufferHeap& buffers)
748{
749 return INVALID_OPERATION;
750}
751
752void LayerBaseClient::Surface::postBuffer(ssize_t offset)
753{
754}
755
756void LayerBaseClient::Surface::unregisterBuffers()
757{
758}
759
760sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
761 uint32_t w, uint32_t h, int32_t format)
762{
763 return NULL;
764};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765
766// ---------------------------------------------------------------------------
767
768}; // namespace android