blob: 9ddf9720e412b333f1879ff5a1d6af6618ea9b97 [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"
33#include "LayerBlur.h"
34#include "SurfaceFlinger.h"
35#include "DisplayHardware/DisplayHardware.h"
36
37
38// We don't honor the premultiplied alpha flags, which means that
39// premultiplied surface may be composed using a non-premultiplied
40// equation. We do this because it may be a lot faster on some hardware
41// The correct value is HONOR_PREMULTIPLIED_ALPHA = 1
42#define HONOR_PREMULTIPLIED_ALPHA 0
43
44namespace android {
45
46// ---------------------------------------------------------------------------
47
48const uint32_t LayerBase::typeInfo = 1;
49const char* const LayerBase::typeID = "LayerBase";
50
51const uint32_t LayerBaseClient::typeInfo = LayerBase::typeInfo | 2;
52const char* const LayerBaseClient::typeID = "LayerBaseClient";
53
54// ---------------------------------------------------------------------------
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
57 : dpy(display), contentDirty(false),
58 mFlinger(flinger),
59 mTransformed(false),
60 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
130void LayerBase::setSizeChanged(uint32_t w, uint32_t h) {
131}
132
133bool LayerBase::setPosition(int32_t x, int32_t y) {
134 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
135 return false;
136 mCurrentState.sequence++;
137 mCurrentState.transform.set(x, y);
138 requestTransaction();
139 return true;
140}
141bool LayerBase::setLayer(uint32_t z) {
142 if (mCurrentState.z == z)
143 return false;
144 mCurrentState.sequence++;
145 mCurrentState.z = z;
146 requestTransaction();
147 return true;
148}
149bool LayerBase::setSize(uint32_t w, uint32_t h) {
150 if (mCurrentState.w == w && mCurrentState.h == h)
151 return false;
152 setSizeChanged(w, h);
153 mCurrentState.w = w;
154 mCurrentState.h = h;
155 requestTransaction();
156 return true;
157}
158bool LayerBase::setAlpha(uint8_t alpha) {
159 if (mCurrentState.alpha == alpha)
160 return false;
161 mCurrentState.sequence++;
162 mCurrentState.alpha = alpha;
163 requestTransaction();
164 return true;
165}
166bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
167 // TODO: check the matrix has changed
168 mCurrentState.sequence++;
169 mCurrentState.transform.set(
170 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
171 requestTransaction();
172 return true;
173}
174bool LayerBase::setTransparentRegionHint(const Region& transparent) {
175 // TODO: check the region has changed
176 mCurrentState.sequence++;
177 mCurrentState.transparentRegion = transparent;
178 requestTransaction();
179 return true;
180}
181bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
182 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
183 if (mCurrentState.flags == newFlags)
184 return false;
185 mCurrentState.sequence++;
186 mCurrentState.flags = newFlags;
187 requestTransaction();
188 return true;
189}
190
191Rect LayerBase::visibleBounds() const
192{
193 return mTransformedBounds;
194}
195
196void LayerBase::setVisibleRegion(const Region& visibleRegion) {
197 // always called from main thread
198 visibleRegionScreen = visibleRegion;
199}
200
201void LayerBase::setCoveredRegion(const Region& coveredRegion) {
202 // always called from main thread
203 coveredRegionScreen = coveredRegion;
204}
205
206uint32_t LayerBase::doTransaction(uint32_t flags)
207{
208 const Layer::State& front(drawingState());
209 const Layer::State& temp(currentState());
210
211 if (temp.sequence != front.sequence) {
212 // invalidate and recompute the visible regions if needed
213 flags |= eVisibleRegion;
214 this->contentDirty = true;
215 }
216
217 // Commit the transaction
218 commitTransaction(flags & eRestartTransaction);
219 return flags;
220}
221
222Point LayerBase::getPhysicalSize() const
223{
224 const Layer::State& front(drawingState());
225 return Point(front.w, front.h);
226}
227
228void LayerBase::validateVisibility(const Transform& planeTransform)
229{
230 const Layer::State& s(drawingState());
231 const Transform tr(planeTransform * s.transform);
232 const bool transformed = tr.transformed();
233
234 const Point size(getPhysicalSize());
235 uint32_t w = size.x;
236 uint32_t h = size.y;
237 tr.transform(mVertices[0], 0, 0);
238 tr.transform(mVertices[1], 0, h);
239 tr.transform(mVertices[2], w, h);
240 tr.transform(mVertices[3], w, 0);
241 if (UNLIKELY(transformed)) {
242 // NOTE: here we could also punt if we have too many rectangles
243 // in the transparent region
244 if (tr.preserveRects()) {
245 // transform the transparent region
246 transparentRegionScreen = tr.transform(s.transparentRegion);
247 } else {
248 // transformation too complex, can't do the transparent region
249 // optimization.
250 transparentRegionScreen.clear();
251 }
252 } else {
253 transparentRegionScreen = s.transparentRegion;
254 }
255
256 // cache a few things...
257 mOrientation = tr.getOrientation();
258 mTransformedBounds = tr.makeBounds(w, h);
259 mTransformed = transformed;
260 mLeft = tr.tx();
261 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262}
263
264void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
265{
266}
267
268void LayerBase::unlockPageFlip(
269 const Transform& planeTransform, Region& outDirtyRegion)
270{
271 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
272 outDirtyRegion.orSelf(visibleRegionScreen);
273 }
274}
275
276void LayerBase::finishPageFlip()
277{
278}
279
280void LayerBase::invalidate()
281{
282 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
283 mFlinger->signalEvent();
284 }
285}
286
287void LayerBase::drawRegion(const Region& reg) const
288{
Mathias Agopian20f68782009-05-11 00:03:41 -0700289 Region::const_iterator it = reg.begin();
290 Region::const_iterator const end = reg.end();
291 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 Rect r;
293 const DisplayHardware& hw(graphicPlane(0).displayHardware());
294 const int32_t fbWidth = hw.getWidth();
295 const int32_t fbHeight = hw.getHeight();
296 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
297 { fbWidth, fbHeight }, { 0, fbHeight } };
298 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700299 while (it != end) {
300 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 const GLint sy = fbHeight - (r.top + r.height());
302 glScissor(r.left, sy, r.width(), r.height());
303 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
304 }
305 }
306}
307
308void LayerBase::draw(const Region& inClip) const
309{
310 // invalidate the region we'll update
311 Region clip(inClip); // copy-on-write, so no-op most of the time
312
313 // Remove the transparent area from the clipping region
314 const State& s = drawingState();
315 if (LIKELY(!s.transparentRegion.isEmpty())) {
316 clip.subtract(transparentRegionScreen);
317 if (clip.isEmpty()) {
318 // usually this won't happen because this should be taken care of
319 // by SurfaceFlinger::computeVisibleRegions()
320 return;
321 }
322 }
323
324 // reset GL state
325 glEnable(GL_SCISSOR_TEST);
326
327 onDraw(clip);
328
329 /*
330 glDisable(GL_TEXTURE_2D);
331 glDisable(GL_DITHER);
332 glEnable(GL_BLEND);
333 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
334 glColor4x(0, 0x8000, 0, 0x10000);
335 drawRegion(transparentRegionScreen);
336 glDisable(GL_BLEND);
337 */
338}
339
340GLuint LayerBase::createTexture() const
341{
342 GLuint textureName = -1;
343 glGenTextures(1, &textureName);
344 glBindTexture(GL_TEXTURE_2D, textureName);
345 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
347 if (mFlags & DisplayHardware::SLOW_CONFIG) {
348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
350 } else {
351 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
352 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
353 }
354 return textureName;
355}
356
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700357void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
358 GLclampx green, GLclampx blue,
359 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360{
361 const DisplayHardware& hw(graphicPlane(0).displayHardware());
362 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700363 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 glDisable(GL_TEXTURE_2D);
365 glDisable(GL_BLEND);
366 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700367
368 Region::const_iterator it = clip.begin();
369 Region::const_iterator const end = clip.end();
370 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 glEnable(GL_SCISSOR_TEST);
372 glVertexPointer(2, GL_FIXED, 0, mVertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700373 while (it != end) {
374 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 const GLint sy = fbHeight - (r.top + r.height());
376 glScissor(r.left, sy, r.width(), r.height());
377 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
378 }
379 }
380}
381
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700382void LayerBase::clearWithOpenGL(const Region& clip) const
383{
384 clearWithOpenGL(clip,0,0,0,0);
385}
386
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700387void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388{
389 const DisplayHardware& hw(graphicPlane(0).displayHardware());
390 const uint32_t fbHeight = hw.getHeight();
391 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700392
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700394 validateTexture(texture.name);
395 uint32_t width = texture.width;
396 uint32_t height = texture.height;
397
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 glEnable(GL_TEXTURE_2D);
399
400 // Dithering...
401 if (s.flags & ISurfaceComposer::eLayerDither) {
402 glEnable(GL_DITHER);
403 } else {
404 glDisable(GL_DITHER);
405 }
406
407 if (UNLIKELY(s.alpha < 0xFF)) {
408 // We have an alpha-modulation. We need to modulate all
409 // texture components by alpha because we're always using
410 // premultiplied alpha.
411
412 // If the texture doesn't have an alpha channel we can
413 // use REPLACE and switch to non premultiplied alpha
414 // blending (SRCA/ONE_MINUS_SRCA).
415
416 GLenum env, src;
417 if (needsBlending()) {
418 env = GL_MODULATE;
419 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
420 } else {
421 env = GL_REPLACE;
422 src = GL_SRC_ALPHA;
423 }
424 const GGLfixed alpha = (s.alpha << 16)/255;
425 glColor4x(alpha, alpha, alpha, alpha);
426 glEnable(GL_BLEND);
427 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
428 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
429 } else {
430 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
431 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
432 if (needsBlending()) {
433 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
434 glEnable(GL_BLEND);
435 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
436 } else {
437 glDisable(GL_BLEND);
438 }
439 }
440
441 if (UNLIKELY(transformed()
442 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
443 {
444 //StopWatch watch("GL transformed");
Mathias Agopian20f68782009-05-11 00:03:41 -0700445 Region::const_iterator it = clip.begin();
446 Region::const_iterator const end = clip.end();
447 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448 // always use high-quality filtering with fast configurations
449 bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG);
450 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
451 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
452 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
453 }
454 const GLfixed texCoords[4][2] = {
455 { 0, 0 },
456 { 0, 0x10000 },
457 { 0x10000, 0x10000 },
458 { 0x10000, 0 }
459 };
460
461 glMatrixMode(GL_TEXTURE);
462 glLoadIdentity();
463
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700464 // the texture's source is rotated
465 if (texture.transform == HAL_TRANSFORM_ROT_90) {
466 // TODO: handle the other orientations
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 glTranslatef(0, 1, 0);
468 glRotatef(-90, 0, 0, 1);
469 }
470
Mathias Agopianf9cd64b2009-07-30 12:19:10 -0700471 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
472 DisplayHardware::DIRECT_TEXTURE))) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473 // find the smallest power-of-two that will accommodate our surface
Mathias Agopian0926f502009-05-04 14:17:04 -0700474 GLuint tw = 1 << (31 - clz(width));
475 GLuint th = 1 << (31 - clz(height));
476 if (tw < width) tw <<= 1;
477 if (th < height) th <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 // this divide should be relatively fast because it's
479 // a power-of-two (optimized path in libgcc)
Mathias Agopian0926f502009-05-04 14:17:04 -0700480 GLfloat ws = GLfloat(width) /tw;
481 GLfloat hs = GLfloat(height)/th;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482 glScalef(ws, hs, 1.0f);
483 }
484
485 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
486 glVertexPointer(2, GL_FIXED, 0, mVertices);
487 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
488
Mathias Agopian20f68782009-05-11 00:03:41 -0700489 while (it != end) {
490 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 const GLint sy = fbHeight - (r.top + r.height());
492 glScissor(r.left, sy, r.width(), r.height());
493 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
494 }
495
496 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
497 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
498 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
499 }
500 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
501 }
502 } else {
Mathias Agopian20f68782009-05-11 00:03:41 -0700503 Region::const_iterator it = clip.begin();
504 Region::const_iterator const end = clip.end();
505 if (it != end) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700506 GLint crop[4] = { 0, height, width, -height };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
508 int x = tx();
509 int y = ty();
Mathias Agopian0926f502009-05-04 14:17:04 -0700510 y = fbHeight - (y + height);
Mathias Agopian20f68782009-05-11 00:03:41 -0700511 while (it != end) {
512 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513 const GLint sy = fbHeight - (r.top + r.height());
514 glScissor(r.left, sy, r.width(), r.height());
Mathias Agopian0926f502009-05-04 14:17:04 -0700515 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 }
517 }
518 }
519}
520
521void LayerBase::validateTexture(GLint textureName) const
522{
523 glBindTexture(GL_TEXTURE_2D, textureName);
524 // TODO: reload the texture if needed
525 // this is currently done in loadTexture() below
526}
527
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700528void LayerBase::loadTexture(Texture* texture, GLint textureName,
529 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530{
531 // TODO: defer the actual texture reload until LayerBase::validateTexture
532 // is called.
533
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700534 texture->name = textureName;
535 GLuint& textureWidth(texture->width);
536 GLuint& textureHeight(texture->height);
537
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 uint32_t flags = mFlags;
539 glBindTexture(GL_TEXTURE_2D, textureName);
540
541 GLuint tw = t.width;
542 GLuint th = t.height;
543
544 /*
545 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
548 * need to do something reasonable (here creating a bigger texture).
549 *
550 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
551 *
552 * This situation doesn't happen often, but some h/w have a limitation
553 * for their framebuffer (eg: must be multiple of 8 pixels), and
554 * we need to take that into account when using these buffers as
555 * textures.
556 *
557 * This should never be a problem with POT textures
558 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559
560 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
561 unpack = 1 << ((unpack > 3) ? 3 : unpack);
562 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
563
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564 /*
565 * round to POT if needed
566 */
567
568 GLuint texture_w = tw;
569 GLuint texture_h = th;
570 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
571 // find the smallest power-of-two that will accommodate our surface
572 texture_w = 1 << (31 - clz(t.width));
573 texture_h = 1 << (31 - clz(t.height));
574 if (texture_w < t.width) texture_w <<= 1;
575 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579 Rect bounds(dirty.bounds());
580 GLvoid* data = 0;
581 if (texture_w!=textureWidth || texture_h!=textureHeight) {
582 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700584 if (!textureWidth || !textureHeight) {
585 // this is the first time, load the whole texture
586 if (texture_w==tw && texture_h==th) {
587 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590 // we have to create the texture first because it
591 // doesn't match the size of the buffer
592 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700595
596 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
597 glTexImage2D(GL_TEXTURE_2D, 0,
598 GL_RGB, texture_w, texture_h, 0,
599 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
600 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
601 glTexImage2D(GL_TEXTURE_2D, 0,
602 GL_RGBA, texture_w, texture_h, 0,
603 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
604 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
605 glTexImage2D(GL_TEXTURE_2D, 0,
606 GL_RGBA, texture_w, texture_h, 0,
607 GL_RGBA, GL_UNSIGNED_BYTE, data);
608 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
609 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
610 // just show the Y plane of YUV buffers
611 glTexImage2D(GL_TEXTURE_2D, 0,
612 GL_LUMINANCE, texture_w, texture_h, 0,
613 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
614 } else {
615 // oops, we don't handle this format!
616 LOGE("layer %p, texture=%d, using format %d, which is not "
617 "supported by the GL", this, textureName, t.format);
618 textureName = -1;
619 }
620 textureWidth = texture_w;
621 textureHeight = texture_h;
622 }
623 if (!data && textureName>=0) {
624 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
625 glTexSubImage2D(GL_TEXTURE_2D, 0,
626 0, bounds.top, t.width, bounds.height(),
627 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
628 t.data + bounds.top*t.stride*2);
629 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
630 glTexSubImage2D(GL_TEXTURE_2D, 0,
631 0, bounds.top, t.width, bounds.height(),
632 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
633 t.data + bounds.top*t.stride*2);
634 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
635 glTexSubImage2D(GL_TEXTURE_2D, 0,
636 0, bounds.top, t.width, bounds.height(),
637 GL_RGBA, GL_UNSIGNED_BYTE,
638 t.data + bounds.top*t.stride*4);
639 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
640 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
641 // just show the Y plane of YUV buffers
642 glTexSubImage2D(GL_TEXTURE_2D, 0,
643 0, bounds.top, t.width, bounds.height(),
644 GL_LUMINANCE, GL_UNSIGNED_BYTE,
645 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 }
647 }
648}
649
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650// ---------------------------------------------------------------------------
651
Mathias Agopian2e123242009-06-23 20:06:46 -0700652int32_t LayerBaseClient::sIdentity = 0;
653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700655 const sp<Client>& client, int32_t i)
656 : LayerBase(flinger, display), client(client),
657 lcblk( client!=0 ? &(client->ctrlblk->layers[i]) : 0 ),
Mathias Agopian2e123242009-06-23 20:06:46 -0700658 mIndex(i),
659 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700661}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700663void LayerBaseClient::onFirstRef()
664{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700665 sp<Client> client(this->client.promote());
666 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700667 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 // Initialize this layer's control block
669 memset(this->lcblk, 0, sizeof(layer_cblk_t));
670 this->lcblk->identity = mIdentity;
671 Region::writeEmpty(&(this->lcblk->region[0]), sizeof(flat_region_t));
672 Region::writeEmpty(&(this->lcblk->region[1]), sizeof(flat_region_t));
673 }
674}
675
676LayerBaseClient::~LayerBaseClient()
677{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700678 sp<Client> client(this->client.promote());
679 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 client->free(mIndex);
681 }
682}
683
Mathias Agopianf9d93272009-06-19 17:00:27 -0700684int32_t LayerBaseClient::serverIndex() const
685{
686 sp<Client> client(this->client.promote());
687 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 return (client->cid<<16)|mIndex;
689 }
690 return 0xFFFF0000 | mIndex;
691}
692
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700693sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700695 sp<Surface> s;
696 Mutex::Autolock _l(mLock);
697 s = mClientSurface.promote();
698 if (s == 0) {
699 s = createSurface();
700 mClientSurface = s;
701 }
702 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703}
704
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700705sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
706{
Mathias Agopian9a112062009-04-17 19:36:26 -0700707 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708 const_cast<LayerBaseClient *>(this));
709}
710
711// ---------------------------------------------------------------------------
712
Mathias Agopian9a112062009-04-17 19:36:26 -0700713LayerBaseClient::Surface::Surface(
714 const sp<SurfaceFlinger>& flinger,
715 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700716 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700717 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
718{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700719}
720
721
Mathias Agopian9a112062009-04-17 19:36:26 -0700722LayerBaseClient::Surface::~Surface()
723{
724 /*
725 * This is a good place to clean-up all client resources
726 */
727
728 // destroy client resources
729 sp<LayerBaseClient> layer = getOwner();
730 if (layer != 0) {
731 mFlinger->destroySurface(layer);
732 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700733}
734
735sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
736 sp<LayerBaseClient> owner(mOwner.promote());
737 return owner;
738}
739
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700740status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700741 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700742{
743 switch (code) {
744 case REGISTER_BUFFERS:
745 case UNREGISTER_BUFFERS:
746 case CREATE_OVERLAY:
747 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700748 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
749 IPCThreadState* ipc = IPCThreadState::self();
750 const int pid = ipc->getCallingPid();
751 const int uid = ipc->getCallingUid();
752 LOGE("Permission Denial: "
753 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
754 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700755 }
756 }
757 }
758 return BnSurface::onTransact(code, data, reply, flags);
759}
760
Mathias Agopian52212712009-08-11 22:34:02 -0700761sp<SurfaceBuffer> LayerBaseClient::Surface::getBuffer(int)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700762{
763 return NULL;
764}
765
766status_t LayerBaseClient::Surface::registerBuffers(
767 const ISurface::BufferHeap& buffers)
768{
769 return INVALID_OPERATION;
770}
771
772void LayerBaseClient::Surface::postBuffer(ssize_t offset)
773{
774}
775
776void LayerBaseClient::Surface::unregisterBuffers()
777{
778}
779
780sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
781 uint32_t w, uint32_t h, int32_t format)
782{
783 return NULL;
784};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785
786// ---------------------------------------------------------------------------
787
788}; // namespace android