blob: 5fa14b2c939971f1aebc2125fa84c789fa2c4ea4 [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 -080056int32_t LayerBase::sIdentity = 0;
57
58LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
59 : dpy(display), contentDirty(false),
60 mFlinger(flinger),
61 mTransformed(false),
62 mOrientation(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 mTransactionFlags(0),
64 mPremultipliedAlpha(true),
65 mIdentity(uint32_t(android_atomic_inc(&sIdentity))),
66 mInvalidate(0)
67{
68 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
69 mFlags = hw.getFlags();
70}
71
72LayerBase::~LayerBase()
73{
74}
75
76const GraphicPlane& LayerBase::graphicPlane(int dpy) const
77{
78 return mFlinger->graphicPlane(dpy);
79}
80
81GraphicPlane& LayerBase::graphicPlane(int dpy)
82{
83 return mFlinger->graphicPlane(dpy);
84}
85
86void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
87{
88 uint32_t layerFlags = 0;
89 if (flags & ISurfaceComposer::eHidden)
90 layerFlags = ISurfaceComposer::eLayerHidden;
91
92 if (flags & ISurfaceComposer::eNonPremultiplied)
93 mPremultipliedAlpha = false;
94
95 mCurrentState.z = 0;
96 mCurrentState.w = w;
97 mCurrentState.h = h;
98 mCurrentState.alpha = 0xFF;
99 mCurrentState.flags = layerFlags;
100 mCurrentState.sequence = 0;
101 mCurrentState.transform.set(0, 0);
102
103 // drawing state & current state are identical
104 mDrawingState = mCurrentState;
105}
106
107void LayerBase::commitTransaction(bool skipSize) {
108 const uint32_t w = mDrawingState.w;
109 const uint32_t h = mDrawingState.h;
110 mDrawingState = mCurrentState;
111 if (skipSize) {
112 mDrawingState.w = w;
113 mDrawingState.h = h;
114 }
115}
116void LayerBase::forceVisibilityTransaction() {
117 // this can be called without SurfaceFlinger.mStateLock, but if we
118 // can atomically increment the sequence number, it doesn't matter.
119 android_atomic_inc(&mCurrentState.sequence);
120 requestTransaction();
121}
122bool LayerBase::requestTransaction() {
123 int32_t old = setTransactionFlags(eTransactionNeeded);
124 return ((old & eTransactionNeeded) == 0);
125}
126uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
127 return android_atomic_and(~flags, &mTransactionFlags) & flags;
128}
129uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
130 return android_atomic_or(flags, &mTransactionFlags);
131}
132
133void LayerBase::setSizeChanged(uint32_t w, uint32_t h) {
134}
135
136bool LayerBase::setPosition(int32_t x, int32_t y) {
137 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
138 return false;
139 mCurrentState.sequence++;
140 mCurrentState.transform.set(x, y);
141 requestTransaction();
142 return true;
143}
144bool LayerBase::setLayer(uint32_t z) {
145 if (mCurrentState.z == z)
146 return false;
147 mCurrentState.sequence++;
148 mCurrentState.z = z;
149 requestTransaction();
150 return true;
151}
152bool LayerBase::setSize(uint32_t w, uint32_t h) {
153 if (mCurrentState.w == w && mCurrentState.h == h)
154 return false;
155 setSizeChanged(w, h);
156 mCurrentState.w = w;
157 mCurrentState.h = h;
158 requestTransaction();
159 return true;
160}
161bool LayerBase::setAlpha(uint8_t alpha) {
162 if (mCurrentState.alpha == alpha)
163 return false;
164 mCurrentState.sequence++;
165 mCurrentState.alpha = alpha;
166 requestTransaction();
167 return true;
168}
169bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
170 // TODO: check the matrix has changed
171 mCurrentState.sequence++;
172 mCurrentState.transform.set(
173 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
174 requestTransaction();
175 return true;
176}
177bool LayerBase::setTransparentRegionHint(const Region& transparent) {
178 // TODO: check the region has changed
179 mCurrentState.sequence++;
180 mCurrentState.transparentRegion = transparent;
181 requestTransaction();
182 return true;
183}
184bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
185 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
186 if (mCurrentState.flags == newFlags)
187 return false;
188 mCurrentState.sequence++;
189 mCurrentState.flags = newFlags;
190 requestTransaction();
191 return true;
192}
193
194Rect LayerBase::visibleBounds() const
195{
196 return mTransformedBounds;
197}
198
199void LayerBase::setVisibleRegion(const Region& visibleRegion) {
200 // always called from main thread
201 visibleRegionScreen = visibleRegion;
202}
203
204void LayerBase::setCoveredRegion(const Region& coveredRegion) {
205 // always called from main thread
206 coveredRegionScreen = coveredRegion;
207}
208
209uint32_t LayerBase::doTransaction(uint32_t flags)
210{
211 const Layer::State& front(drawingState());
212 const Layer::State& temp(currentState());
213
214 if (temp.sequence != front.sequence) {
215 // invalidate and recompute the visible regions if needed
216 flags |= eVisibleRegion;
217 this->contentDirty = true;
218 }
219
220 // Commit the transaction
221 commitTransaction(flags & eRestartTransaction);
222 return flags;
223}
224
225Point LayerBase::getPhysicalSize() const
226{
227 const Layer::State& front(drawingState());
228 return Point(front.w, front.h);
229}
230
231void LayerBase::validateVisibility(const Transform& planeTransform)
232{
233 const Layer::State& s(drawingState());
234 const Transform tr(planeTransform * s.transform);
235 const bool transformed = tr.transformed();
236
237 const Point size(getPhysicalSize());
238 uint32_t w = size.x;
239 uint32_t h = size.y;
240 tr.transform(mVertices[0], 0, 0);
241 tr.transform(mVertices[1], 0, h);
242 tr.transform(mVertices[2], w, h);
243 tr.transform(mVertices[3], w, 0);
244 if (UNLIKELY(transformed)) {
245 // NOTE: here we could also punt if we have too many rectangles
246 // in the transparent region
247 if (tr.preserveRects()) {
248 // transform the transparent region
249 transparentRegionScreen = tr.transform(s.transparentRegion);
250 } else {
251 // transformation too complex, can't do the transparent region
252 // optimization.
253 transparentRegionScreen.clear();
254 }
255 } else {
256 transparentRegionScreen = s.transparentRegion;
257 }
258
259 // cache a few things...
260 mOrientation = tr.getOrientation();
261 mTransformedBounds = tr.makeBounds(w, h);
262 mTransformed = transformed;
263 mLeft = tr.tx();
264 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265}
266
267void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
268{
269}
270
271void LayerBase::unlockPageFlip(
272 const Transform& planeTransform, Region& outDirtyRegion)
273{
274 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
275 outDirtyRegion.orSelf(visibleRegionScreen);
276 }
277}
278
279void LayerBase::finishPageFlip()
280{
281}
282
283void LayerBase::invalidate()
284{
285 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
286 mFlinger->signalEvent();
287 }
288}
289
290void LayerBase::drawRegion(const Region& reg) const
291{
Mathias Agopian20f68782009-05-11 00:03:41 -0700292 Region::const_iterator it = reg.begin();
293 Region::const_iterator const end = reg.end();
294 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 Rect r;
296 const DisplayHardware& hw(graphicPlane(0).displayHardware());
297 const int32_t fbWidth = hw.getWidth();
298 const int32_t fbHeight = hw.getHeight();
299 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
300 { fbWidth, fbHeight }, { 0, fbHeight } };
301 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700302 while (it != end) {
303 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 const GLint sy = fbHeight - (r.top + r.height());
305 glScissor(r.left, sy, r.width(), r.height());
306 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
307 }
308 }
309}
310
311void LayerBase::draw(const Region& inClip) const
312{
313 // invalidate the region we'll update
314 Region clip(inClip); // copy-on-write, so no-op most of the time
315
316 // Remove the transparent area from the clipping region
317 const State& s = drawingState();
318 if (LIKELY(!s.transparentRegion.isEmpty())) {
319 clip.subtract(transparentRegionScreen);
320 if (clip.isEmpty()) {
321 // usually this won't happen because this should be taken care of
322 // by SurfaceFlinger::computeVisibleRegions()
323 return;
324 }
325 }
326
327 // reset GL state
328 glEnable(GL_SCISSOR_TEST);
329
330 onDraw(clip);
331
332 /*
333 glDisable(GL_TEXTURE_2D);
334 glDisable(GL_DITHER);
335 glEnable(GL_BLEND);
336 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
337 glColor4x(0, 0x8000, 0, 0x10000);
338 drawRegion(transparentRegionScreen);
339 glDisable(GL_BLEND);
340 */
341}
342
343GLuint LayerBase::createTexture() const
344{
345 GLuint textureName = -1;
346 glGenTextures(1, &textureName);
347 glBindTexture(GL_TEXTURE_2D, textureName);
348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
350 if (mFlags & DisplayHardware::SLOW_CONFIG) {
351 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
352 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
353 } else {
354 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
355 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
356 }
357 return textureName;
358}
359
360void LayerBase::clearWithOpenGL(const Region& clip) const
361{
362 const DisplayHardware& hw(graphicPlane(0).displayHardware());
363 const uint32_t fbHeight = hw.getHeight();
364 glColor4x(0,0,0,0);
365 glDisable(GL_TEXTURE_2D);
366 glDisable(GL_BLEND);
367 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700368
369 Region::const_iterator it = clip.begin();
370 Region::const_iterator const end = clip.end();
371 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 glEnable(GL_SCISSOR_TEST);
373 glVertexPointer(2, GL_FIXED, 0, mVertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700374 while (it != end) {
375 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 const GLint sy = fbHeight - (r.top + r.height());
377 glScissor(r.left, sy, r.width(), r.height());
378 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
379 }
380 }
381}
382
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700383void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-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 Agopian0926f502009-05-04 14:17:04 -0700388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 // bind our texture
Mathias Agopian1fed11c2009-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 Projectedbf3b62009-03-03 19:31:44 -0800394 glEnable(GL_TEXTURE_2D);
395
396 // Dithering...
397 if (s.flags & ISurfaceComposer::eLayerDither) {
398 glEnable(GL_DITHER);
399 } else {
400 glDisable(GL_DITHER);
401 }
402
403 if (UNLIKELY(s.alpha < 0xFF)) {
404 // We have an alpha-modulation. We need to modulate all
405 // texture components by alpha because we're always using
406 // premultiplied alpha.
407
408 // If the texture doesn't have an alpha channel we can
409 // use REPLACE and switch to non premultiplied alpha
410 // blending (SRCA/ONE_MINUS_SRCA).
411
412 GLenum env, src;
413 if (needsBlending()) {
414 env = GL_MODULATE;
415 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
416 } else {
417 env = GL_REPLACE;
418 src = GL_SRC_ALPHA;
419 }
420 const GGLfixed alpha = (s.alpha << 16)/255;
421 glColor4x(alpha, alpha, alpha, alpha);
422 glEnable(GL_BLEND);
423 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
424 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
425 } else {
426 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
427 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
428 if (needsBlending()) {
429 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
430 glEnable(GL_BLEND);
431 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
432 } else {
433 glDisable(GL_BLEND);
434 }
435 }
436
437 if (UNLIKELY(transformed()
438 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
439 {
440 //StopWatch watch("GL transformed");
Mathias Agopian20f68782009-05-11 00:03:41 -0700441 Region::const_iterator it = clip.begin();
442 Region::const_iterator const end = clip.end();
443 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444 // always use high-quality filtering with fast configurations
445 bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG);
446 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
447 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
448 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
449 }
450 const GLfixed texCoords[4][2] = {
451 { 0, 0 },
452 { 0, 0x10000 },
453 { 0x10000, 0x10000 },
454 { 0x10000, 0 }
455 };
456
457 glMatrixMode(GL_TEXTURE);
458 glLoadIdentity();
459
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700460 // the texture's source is rotated
461 if (texture.transform == HAL_TRANSFORM_ROT_90) {
462 // TODO: handle the other orientations
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 glTranslatef(0, 1, 0);
464 glRotatef(-90, 0, 0, 1);
465 }
466
467 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
468 // find the smallest power-of-two that will accommodate our surface
Mathias Agopian0926f502009-05-04 14:17:04 -0700469 GLuint tw = 1 << (31 - clz(width));
470 GLuint th = 1 << (31 - clz(height));
471 if (tw < width) tw <<= 1;
472 if (th < height) th <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473 // this divide should be relatively fast because it's
474 // a power-of-two (optimized path in libgcc)
Mathias Agopian0926f502009-05-04 14:17:04 -0700475 GLfloat ws = GLfloat(width) /tw;
476 GLfloat hs = GLfloat(height)/th;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477 glScalef(ws, hs, 1.0f);
478 }
479
480 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
481 glVertexPointer(2, GL_FIXED, 0, mVertices);
482 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
483
Mathias Agopian20f68782009-05-11 00:03:41 -0700484 while (it != end) {
485 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 const GLint sy = fbHeight - (r.top + r.height());
487 glScissor(r.left, sy, r.width(), r.height());
488 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
489 }
490
491 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
492 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
493 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
494 }
495 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
496 }
497 } else {
Mathias Agopian20f68782009-05-11 00:03:41 -0700498 Region::const_iterator it = clip.begin();
499 Region::const_iterator const end = clip.end();
500 if (it != end) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700501 GLint crop[4] = { 0, height, width, -height };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
503 int x = tx();
504 int y = ty();
Mathias Agopian0926f502009-05-04 14:17:04 -0700505 y = fbHeight - (y + height);
Mathias Agopian20f68782009-05-11 00:03:41 -0700506 while (it != end) {
507 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 const GLint sy = fbHeight - (r.top + r.height());
509 glScissor(r.left, sy, r.width(), r.height());
Mathias Agopian0926f502009-05-04 14:17:04 -0700510 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 }
512 }
513 }
514}
515
516void LayerBase::validateTexture(GLint textureName) const
517{
518 glBindTexture(GL_TEXTURE_2D, textureName);
519 // TODO: reload the texture if needed
520 // this is currently done in loadTexture() below
521}
522
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700523void LayerBase::loadTexture(Texture* texture, GLint textureName,
524 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525{
526 // TODO: defer the actual texture reload until LayerBase::validateTexture
527 // is called.
528
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700529 texture->name = textureName;
530 GLuint& textureWidth(texture->width);
531 GLuint& textureHeight(texture->height);
532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 uint32_t flags = mFlags;
534 glBindTexture(GL_TEXTURE_2D, textureName);
535
536 GLuint tw = t.width;
537 GLuint th = t.height;
538
539 /*
540 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700541 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
543 * need to do something reasonable (here creating a bigger texture).
544 *
545 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
546 *
547 * This situation doesn't happen often, but some h/w have a limitation
548 * for their framebuffer (eg: must be multiple of 8 pixels), and
549 * we need to take that into account when using these buffers as
550 * textures.
551 *
552 * This should never be a problem with POT textures
553 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554
555 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
556 unpack = 1 << ((unpack > 3) ? 3 : unpack);
557 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
558
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 /*
560 * round to POT if needed
561 */
562
563 GLuint texture_w = tw;
564 GLuint texture_h = th;
565 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
566 // find the smallest power-of-two that will accommodate our surface
567 texture_w = 1 << (31 - clz(t.width));
568 texture_h = 1 << (31 - clz(t.height));
569 if (texture_w < t.width) texture_w <<= 1;
570 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 Rect bounds(dirty.bounds());
575 GLvoid* data = 0;
576 if (texture_w!=textureWidth || texture_h!=textureHeight) {
577 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579 if (!textureWidth || !textureHeight) {
580 // this is the first time, load the whole texture
581 if (texture_w==tw && texture_h==th) {
582 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 // we have to create the texture first because it
586 // doesn't match the size of the buffer
587 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590
591 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
592 glTexImage2D(GL_TEXTURE_2D, 0,
593 GL_RGB, texture_w, texture_h, 0,
594 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
595 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
596 glTexImage2D(GL_TEXTURE_2D, 0,
597 GL_RGBA, texture_w, texture_h, 0,
598 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
599 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
600 glTexImage2D(GL_TEXTURE_2D, 0,
601 GL_RGBA, texture_w, texture_h, 0,
602 GL_RGBA, GL_UNSIGNED_BYTE, data);
603 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
604 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
605 // just show the Y plane of YUV buffers
606 glTexImage2D(GL_TEXTURE_2D, 0,
607 GL_LUMINANCE, texture_w, texture_h, 0,
608 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
609 } else {
610 // oops, we don't handle this format!
611 LOGE("layer %p, texture=%d, using format %d, which is not "
612 "supported by the GL", this, textureName, t.format);
613 textureName = -1;
614 }
615 textureWidth = texture_w;
616 textureHeight = texture_h;
617 }
618 if (!data && textureName>=0) {
619 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
620 glTexSubImage2D(GL_TEXTURE_2D, 0,
621 0, bounds.top, t.width, bounds.height(),
622 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
623 t.data + bounds.top*t.stride*2);
624 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
625 glTexSubImage2D(GL_TEXTURE_2D, 0,
626 0, bounds.top, t.width, bounds.height(),
627 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
628 t.data + bounds.top*t.stride*2);
629 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
630 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 Projectedbf3b62009-03-03 19:31:44 -0800641 }
642 }
643}
644
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645// ---------------------------------------------------------------------------
646
647LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700648 const sp<Client>& client, int32_t i)
649 : LayerBase(flinger, display), client(client),
650 lcblk( client!=0 ? &(client->ctrlblk->layers[i]) : 0 ),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651 mIndex(i)
652{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700653}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700655void LayerBaseClient::onFirstRef()
656{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700657 sp<Client> client(this->client.promote());
658 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700659 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 // Initialize this layer's control block
661 memset(this->lcblk, 0, sizeof(layer_cblk_t));
662 this->lcblk->identity = mIdentity;
663 Region::writeEmpty(&(this->lcblk->region[0]), sizeof(flat_region_t));
664 Region::writeEmpty(&(this->lcblk->region[1]), sizeof(flat_region_t));
665 }
666}
667
668LayerBaseClient::~LayerBaseClient()
669{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700670 sp<Client> client(this->client.promote());
671 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 client->free(mIndex);
673 }
674}
675
Mathias Agopianf9d93272009-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 Projectedbf3b62009-03-03 19:31:44 -0800680 return (client->cid<<16)|mIndex;
681 }
682 return 0xFFFF0000 | mIndex;
683}
684
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700685sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686{
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -0800695}
696
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700697sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
698{
Mathias Agopian9a112062009-04-17 19:36:26 -0700699 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700700 const_cast<LayerBaseClient *>(this));
701}
702
703// ---------------------------------------------------------------------------
704
Mathias Agopian9a112062009-04-17 19:36:26 -0700705LayerBaseClient::Surface::Surface(
706 const sp<SurfaceFlinger>& flinger,
707 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700709 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
710{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700711}
712
713
Mathias Agopian9a112062009-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 Agopian076b1cc2009-04-10 14:24:30 -0700725}
726
727sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
728 sp<LayerBaseClient> owner(mOwner.promote());
729 return owner;
730}
731
732
733void LayerBaseClient::Surface::getSurfaceData(
734 ISurfaceFlingerClient::surface_data_t* params) const
735{
736 params->token = mToken;
737 params->identity = mIdentity;
738}
739
740status_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
761sp<SurfaceBuffer> LayerBaseClient::Surface::getBuffer()
762{
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