blob: c678711a5439b862257b89a2750358fcfb4d3703 [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
383void LayerBase::drawWithOpenGL(const Region& clip,
Mathias Agopian0926f502009-05-04 14:17:04 -0700384 GLint textureName, const sp<const Buffer>& buffer, int transform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385{
386 const DisplayHardware& hw(graphicPlane(0).displayHardware());
387 const uint32_t fbHeight = hw.getHeight();
388 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700389 const uint32_t width = buffer->width;
390 const uint32_t height = buffer->height;
391
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 // bind our texture
393 validateTexture(textureName);
394 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
460 if (transform == HAL_TRANSFORM_ROT_90) {
461 glTranslatef(0, 1, 0);
462 glRotatef(-90, 0, 0, 1);
463 }
464
465 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
466 // find the smallest power-of-two that will accommodate our surface
Mathias Agopian0926f502009-05-04 14:17:04 -0700467 GLuint tw = 1 << (31 - clz(width));
468 GLuint th = 1 << (31 - clz(height));
469 if (tw < width) tw <<= 1;
470 if (th < height) th <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 // this divide should be relatively fast because it's
472 // a power-of-two (optimized path in libgcc)
Mathias Agopian0926f502009-05-04 14:17:04 -0700473 GLfloat ws = GLfloat(width) /tw;
474 GLfloat hs = GLfloat(height)/th;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 glScalef(ws, hs, 1.0f);
476 }
477
478 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
479 glVertexPointer(2, GL_FIXED, 0, mVertices);
480 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
481
Mathias Agopian20f68782009-05-11 00:03:41 -0700482 while (it != end) {
483 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484 const GLint sy = fbHeight - (r.top + r.height());
485 glScissor(r.left, sy, r.width(), r.height());
486 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
487 }
488
489 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
490 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
491 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
492 }
493 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
494 }
495 } else {
Mathias Agopian20f68782009-05-11 00:03:41 -0700496 Region::const_iterator it = clip.begin();
497 Region::const_iterator const end = clip.end();
498 if (it != end) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700499 GLint crop[4] = { 0, height, width, -height };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
501 int x = tx();
502 int y = ty();
Mathias Agopian0926f502009-05-04 14:17:04 -0700503 y = fbHeight - (y + height);
Mathias Agopian20f68782009-05-11 00:03:41 -0700504 while (it != end) {
505 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506 const GLint sy = fbHeight - (r.top + r.height());
507 glScissor(r.left, sy, r.width(), r.height());
Mathias Agopian0926f502009-05-04 14:17:04 -0700508 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 }
510 }
511 }
512}
513
514void LayerBase::validateTexture(GLint textureName) const
515{
516 glBindTexture(GL_TEXTURE_2D, textureName);
517 // TODO: reload the texture if needed
518 // this is currently done in loadTexture() below
519}
520
521void LayerBase::loadTexture(const Region& dirty,
522 GLint textureName, const GGLSurface& t,
523 GLuint& textureWidth, GLuint& textureHeight) const
524{
525 // TODO: defer the actual texture reload until LayerBase::validateTexture
526 // is called.
527
528 uint32_t flags = mFlags;
529 glBindTexture(GL_TEXTURE_2D, textureName);
530
531 GLuint tw = t.width;
532 GLuint th = t.height;
533
534 /*
535 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
538 * need to do something reasonable (here creating a bigger texture).
539 *
540 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
541 *
542 * This situation doesn't happen often, but some h/w have a limitation
543 * for their framebuffer (eg: must be multiple of 8 pixels), and
544 * we need to take that into account when using these buffers as
545 * textures.
546 *
547 * This should never be a problem with POT textures
548 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700549
550 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
551 unpack = 1 << ((unpack > 3) ? 3 : unpack);
552 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
553
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 /*
555 * round to POT if needed
556 */
557
558 GLuint texture_w = tw;
559 GLuint texture_h = th;
560 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
561 // find the smallest power-of-two that will accommodate our surface
562 texture_w = 1 << (31 - clz(t.width));
563 texture_h = 1 << (31 - clz(t.height));
564 if (texture_w < t.width) texture_w <<= 1;
565 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700569 Rect bounds(dirty.bounds());
570 GLvoid* data = 0;
571 if (texture_w!=textureWidth || texture_h!=textureHeight) {
572 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 if (!textureWidth || !textureHeight) {
575 // this is the first time, load the whole texture
576 if (texture_w==tw && texture_h==th) {
577 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700580 // we have to create the texture first because it
581 // doesn't match the size of the buffer
582 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585
586 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
587 glTexImage2D(GL_TEXTURE_2D, 0,
588 GL_RGB, texture_w, texture_h, 0,
589 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
590 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
591 glTexImage2D(GL_TEXTURE_2D, 0,
592 GL_RGBA, texture_w, texture_h, 0,
593 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
594 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
595 glTexImage2D(GL_TEXTURE_2D, 0,
596 GL_RGBA, texture_w, texture_h, 0,
597 GL_RGBA, GL_UNSIGNED_BYTE, data);
598 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
599 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
600 // just show the Y plane of YUV buffers
601 glTexImage2D(GL_TEXTURE_2D, 0,
602 GL_LUMINANCE, texture_w, texture_h, 0,
603 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
604 } else {
605 // oops, we don't handle this format!
606 LOGE("layer %p, texture=%d, using format %d, which is not "
607 "supported by the GL", this, textureName, t.format);
608 textureName = -1;
609 }
610 textureWidth = texture_w;
611 textureHeight = texture_h;
612 }
613 if (!data && textureName>=0) {
614 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
615 glTexSubImage2D(GL_TEXTURE_2D, 0,
616 0, bounds.top, t.width, bounds.height(),
617 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
618 t.data + bounds.top*t.stride*2);
619 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
620 glTexSubImage2D(GL_TEXTURE_2D, 0,
621 0, bounds.top, t.width, bounds.height(),
622 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
623 t.data + bounds.top*t.stride*2);
624 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
625 glTexSubImage2D(GL_TEXTURE_2D, 0,
626 0, bounds.top, t.width, bounds.height(),
627 GL_RGBA, GL_UNSIGNED_BYTE,
628 t.data + bounds.top*t.stride*4);
629 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
630 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
631 // just show the Y plane of YUV buffers
632 glTexSubImage2D(GL_TEXTURE_2D, 0,
633 0, bounds.top, t.width, bounds.height(),
634 GL_LUMINANCE, GL_UNSIGNED_BYTE,
635 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 }
637 }
638}
639
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640// ---------------------------------------------------------------------------
641
642LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
643 Client* c, int32_t i)
644 : LayerBase(flinger, display), client(c),
645 lcblk( c ? &(c->ctrlblk->layers[i]) : 0 ),
646 mIndex(i)
647{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700648}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700650void LayerBaseClient::onFirstRef()
651{
652 if (client) {
653 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 // Initialize this layer's control block
655 memset(this->lcblk, 0, sizeof(layer_cblk_t));
656 this->lcblk->identity = mIdentity;
657 Region::writeEmpty(&(this->lcblk->region[0]), sizeof(flat_region_t));
658 Region::writeEmpty(&(this->lcblk->region[1]), sizeof(flat_region_t));
659 }
660}
661
662LayerBaseClient::~LayerBaseClient()
663{
664 if (client) {
665 client->free(mIndex);
666 }
667}
668
669int32_t LayerBaseClient::serverIndex() const {
670 if (client) {
671 return (client->cid<<16)|mIndex;
672 }
673 return 0xFFFF0000 | mIndex;
674}
675
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700676sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700678 sp<Surface> s;
679 Mutex::Autolock _l(mLock);
680 s = mClientSurface.promote();
681 if (s == 0) {
682 s = createSurface();
683 mClientSurface = s;
684 }
685 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686}
687
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700688sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
689{
Mathias Agopian9a112062009-04-17 19:36:26 -0700690 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700691 const_cast<LayerBaseClient *>(this));
692}
693
694// ---------------------------------------------------------------------------
695
Mathias Agopian9a112062009-04-17 19:36:26 -0700696LayerBaseClient::Surface::Surface(
697 const sp<SurfaceFlinger>& flinger,
698 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700699 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700700 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
701{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700702}
703
704
Mathias Agopian9a112062009-04-17 19:36:26 -0700705LayerBaseClient::Surface::~Surface()
706{
707 /*
708 * This is a good place to clean-up all client resources
709 */
710
711 // destroy client resources
712 sp<LayerBaseClient> layer = getOwner();
713 if (layer != 0) {
714 mFlinger->destroySurface(layer);
715 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700716}
717
718sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
719 sp<LayerBaseClient> owner(mOwner.promote());
720 return owner;
721}
722
723
724void LayerBaseClient::Surface::getSurfaceData(
725 ISurfaceFlingerClient::surface_data_t* params) const
726{
727 params->token = mToken;
728 params->identity = mIdentity;
729}
730
731status_t LayerBaseClient::Surface::onTransact(
732 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
733{
734 switch (code) {
735 case REGISTER_BUFFERS:
736 case UNREGISTER_BUFFERS:
737 case CREATE_OVERLAY:
738 {
739 // codes that require permission check
740 IPCThreadState* ipc = IPCThreadState::self();
741 const int pid = ipc->getCallingPid();
742 const int self_pid = getpid();
743 if (LIKELY(pid != self_pid)) {
744 // we're called from a different process, do the real check
745 if (!checkCallingPermission(
746 String16("android.permission.ACCESS_SURFACE_FLINGER")))
747 {
748 const int uid = ipc->getCallingUid();
749 LOGE("Permission Denial: "
750 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
751 return PERMISSION_DENIED;
752 }
753 }
754 }
755 }
756 return BnSurface::onTransact(code, data, reply, flags);
757}
758
759sp<SurfaceBuffer> LayerBaseClient::Surface::getBuffer()
760{
761 return NULL;
762}
763
764status_t LayerBaseClient::Surface::registerBuffers(
765 const ISurface::BufferHeap& buffers)
766{
767 return INVALID_OPERATION;
768}
769
770void LayerBaseClient::Surface::postBuffer(ssize_t offset)
771{
772}
773
774void LayerBaseClient::Surface::unregisterBuffers()
775{
776}
777
778sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
779 uint32_t w, uint32_t h, int32_t format)
780{
781 return NULL;
782};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783
784// ---------------------------------------------------------------------------
785
786}; // namespace android