blob: fbce73ddd3be7b8541f5ad5349b8e8e63b52a89d [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
357void LayerBase::clearWithOpenGL(const Region& clip) const
358{
359 const DisplayHardware& hw(graphicPlane(0).displayHardware());
360 const uint32_t fbHeight = hw.getHeight();
361 glColor4x(0,0,0,0);
362 glDisable(GL_TEXTURE_2D);
363 glDisable(GL_BLEND);
364 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700365
366 Region::const_iterator it = clip.begin();
367 Region::const_iterator const end = clip.end();
368 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 glEnable(GL_SCISSOR_TEST);
370 glVertexPointer(2, GL_FIXED, 0, mVertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700371 while (it != end) {
372 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373 const GLint sy = fbHeight - (r.top + r.height());
374 glScissor(r.left, sy, r.width(), r.height());
375 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
376 }
377 }
378}
379
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700380void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381{
382 const DisplayHardware& hw(graphicPlane(0).displayHardware());
383 const uint32_t fbHeight = hw.getHeight();
384 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700387 validateTexture(texture.name);
388 uint32_t width = texture.width;
389 uint32_t height = texture.height;
390
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 glEnable(GL_TEXTURE_2D);
392
393 // Dithering...
394 if (s.flags & ISurfaceComposer::eLayerDither) {
395 glEnable(GL_DITHER);
396 } else {
397 glDisable(GL_DITHER);
398 }
399
400 if (UNLIKELY(s.alpha < 0xFF)) {
401 // We have an alpha-modulation. We need to modulate all
402 // texture components by alpha because we're always using
403 // premultiplied alpha.
404
405 // If the texture doesn't have an alpha channel we can
406 // use REPLACE and switch to non premultiplied alpha
407 // blending (SRCA/ONE_MINUS_SRCA).
408
409 GLenum env, src;
410 if (needsBlending()) {
411 env = GL_MODULATE;
412 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
413 } else {
414 env = GL_REPLACE;
415 src = GL_SRC_ALPHA;
416 }
417 const GGLfixed alpha = (s.alpha << 16)/255;
418 glColor4x(alpha, alpha, alpha, alpha);
419 glEnable(GL_BLEND);
420 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
421 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
422 } else {
423 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
424 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
425 if (needsBlending()) {
426 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
427 glEnable(GL_BLEND);
428 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
429 } else {
430 glDisable(GL_BLEND);
431 }
432 }
433
434 if (UNLIKELY(transformed()
435 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
436 {
437 //StopWatch watch("GL transformed");
Mathias Agopian20f68782009-05-11 00:03:41 -0700438 Region::const_iterator it = clip.begin();
439 Region::const_iterator const end = clip.end();
440 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 // always use high-quality filtering with fast configurations
442 bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG);
443 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
444 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
445 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
446 }
447 const GLfixed texCoords[4][2] = {
448 { 0, 0 },
449 { 0, 0x10000 },
450 { 0x10000, 0x10000 },
451 { 0x10000, 0 }
452 };
453
454 glMatrixMode(GL_TEXTURE);
455 glLoadIdentity();
456
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700457 // the texture's source is rotated
458 if (texture.transform == HAL_TRANSFORM_ROT_90) {
459 // TODO: handle the other orientations
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460 glTranslatef(0, 1, 0);
461 glRotatef(-90, 0, 0, 1);
462 }
463
Mathias Agopianf9cd64b2009-07-30 12:19:10 -0700464 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
465 DisplayHardware::DIRECT_TEXTURE))) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466 // 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
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700521void LayerBase::loadTexture(Texture* texture, GLint textureName,
522 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523{
524 // TODO: defer the actual texture reload until LayerBase::validateTexture
525 // is called.
526
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700527 texture->name = textureName;
528 GLuint& textureWidth(texture->width);
529 GLuint& textureHeight(texture->height);
530
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 uint32_t flags = mFlags;
532 glBindTexture(GL_TEXTURE_2D, textureName);
533
534 GLuint tw = t.width;
535 GLuint th = t.height;
536
537 /*
538 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
541 * need to do something reasonable (here creating a bigger texture).
542 *
543 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
544 *
545 * This situation doesn't happen often, but some h/w have a limitation
546 * for their framebuffer (eg: must be multiple of 8 pixels), and
547 * we need to take that into account when using these buffers as
548 * textures.
549 *
550 * This should never be a problem with POT textures
551 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700552
553 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
554 unpack = 1 << ((unpack > 3) ? 3 : unpack);
555 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 /*
558 * round to POT if needed
559 */
560
561 GLuint texture_w = tw;
562 GLuint texture_h = th;
563 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
564 // find the smallest power-of-two that will accommodate our surface
565 texture_w = 1 << (31 - clz(t.width));
566 texture_h = 1 << (31 - clz(t.height));
567 if (texture_w < t.width) texture_w <<= 1;
568 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572 Rect bounds(dirty.bounds());
573 GLvoid* data = 0;
574 if (texture_w!=textureWidth || texture_h!=textureHeight) {
575 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577 if (!textureWidth || !textureHeight) {
578 // this is the first time, load the whole texture
579 if (texture_w==tw && texture_h==th) {
580 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700583 // we have to create the texture first because it
584 // doesn't match the size of the buffer
585 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588
589 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
590 glTexImage2D(GL_TEXTURE_2D, 0,
591 GL_RGB, texture_w, texture_h, 0,
592 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
593 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
594 glTexImage2D(GL_TEXTURE_2D, 0,
595 GL_RGBA, texture_w, texture_h, 0,
596 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
597 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
598 glTexImage2D(GL_TEXTURE_2D, 0,
599 GL_RGBA, texture_w, texture_h, 0,
600 GL_RGBA, GL_UNSIGNED_BYTE, data);
601 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
602 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
603 // just show the Y plane of YUV buffers
604 glTexImage2D(GL_TEXTURE_2D, 0,
605 GL_LUMINANCE, texture_w, texture_h, 0,
606 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
607 } else {
608 // oops, we don't handle this format!
609 LOGE("layer %p, texture=%d, using format %d, which is not "
610 "supported by the GL", this, textureName, t.format);
611 textureName = -1;
612 }
613 textureWidth = texture_w;
614 textureHeight = texture_h;
615 }
616 if (!data && textureName>=0) {
617 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
618 glTexSubImage2D(GL_TEXTURE_2D, 0,
619 0, bounds.top, t.width, bounds.height(),
620 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
621 t.data + bounds.top*t.stride*2);
622 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
623 glTexSubImage2D(GL_TEXTURE_2D, 0,
624 0, bounds.top, t.width, bounds.height(),
625 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
626 t.data + bounds.top*t.stride*2);
627 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
628 glTexSubImage2D(GL_TEXTURE_2D, 0,
629 0, bounds.top, t.width, bounds.height(),
630 GL_RGBA, GL_UNSIGNED_BYTE,
631 t.data + bounds.top*t.stride*4);
632 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
633 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
634 // just show the Y plane of YUV buffers
635 glTexSubImage2D(GL_TEXTURE_2D, 0,
636 0, bounds.top, t.width, bounds.height(),
637 GL_LUMINANCE, GL_UNSIGNED_BYTE,
638 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 }
640 }
641}
642
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643// ---------------------------------------------------------------------------
644
Mathias Agopian2e123242009-06-23 20:06:46 -0700645int32_t LayerBaseClient::sIdentity = 0;
646
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647LayerBaseClient::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 ),
Mathias Agopian2e123242009-06-23 20:06:46 -0700651 mIndex(i),
652 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700654}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700656void LayerBaseClient::onFirstRef()
657{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700658 sp<Client> client(this->client.promote());
659 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700660 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 // Initialize this layer's control block
662 memset(this->lcblk, 0, sizeof(layer_cblk_t));
663 this->lcblk->identity = mIdentity;
664 Region::writeEmpty(&(this->lcblk->region[0]), sizeof(flat_region_t));
665 Region::writeEmpty(&(this->lcblk->region[1]), sizeof(flat_region_t));
666 }
667}
668
669LayerBaseClient::~LayerBaseClient()
670{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700671 sp<Client> client(this->client.promote());
672 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 client->free(mIndex);
674 }
675}
676
Mathias Agopianf9d93272009-06-19 17:00:27 -0700677int32_t LayerBaseClient::serverIndex() const
678{
679 sp<Client> client(this->client.promote());
680 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 return (client->cid<<16)|mIndex;
682 }
683 return 0xFFFF0000 | mIndex;
684}
685
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700686sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700688 sp<Surface> s;
689 Mutex::Autolock _l(mLock);
690 s = mClientSurface.promote();
691 if (s == 0) {
692 s = createSurface();
693 mClientSurface = s;
694 }
695 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696}
697
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700698sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
699{
Mathias Agopian9a112062009-04-17 19:36:26 -0700700 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700701 const_cast<LayerBaseClient *>(this));
702}
703
704// ---------------------------------------------------------------------------
705
Mathias Agopian9a112062009-04-17 19:36:26 -0700706LayerBaseClient::Surface::Surface(
707 const sp<SurfaceFlinger>& flinger,
708 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700709 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700710 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
711{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700712}
713
714
Mathias Agopian9a112062009-04-17 19:36:26 -0700715LayerBaseClient::Surface::~Surface()
716{
717 /*
718 * This is a good place to clean-up all client resources
719 */
720
721 // destroy client resources
722 sp<LayerBaseClient> layer = getOwner();
723 if (layer != 0) {
724 mFlinger->destroySurface(layer);
725 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700726}
727
728sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
729 sp<LayerBaseClient> owner(mOwner.promote());
730 return owner;
731}
732
733
734void LayerBaseClient::Surface::getSurfaceData(
735 ISurfaceFlingerClient::surface_data_t* params) const
736{
737 params->token = mToken;
738 params->identity = mIdentity;
739}
740
741status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700742 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700743{
744 switch (code) {
745 case REGISTER_BUFFERS:
746 case UNREGISTER_BUFFERS:
747 case CREATE_OVERLAY:
748 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700749 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
750 IPCThreadState* ipc = IPCThreadState::self();
751 const int pid = ipc->getCallingPid();
752 const int uid = ipc->getCallingUid();
753 LOGE("Permission Denial: "
754 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
755 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700756 }
757 }
758 }
759 return BnSurface::onTransact(code, data, reply, flags);
760}
761
Fred Quintanab2fd4662009-08-11 20:49:35 -0700762sp<SurfaceBuffer> LayerBaseClient::Surface::getBuffer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700763{
764 return NULL;
765}
766
767status_t LayerBaseClient::Surface::registerBuffers(
768 const ISurface::BufferHeap& buffers)
769{
770 return INVALID_OPERATION;
771}
772
773void LayerBaseClient::Surface::postBuffer(ssize_t offset)
774{
775}
776
777void LayerBaseClient::Surface::unregisterBuffers()
778{
779}
780
781sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
782 uint32_t w, uint32_t h, int32_t format)
783{
784 return NULL;
785};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786
787// ---------------------------------------------------------------------------
788
789}; // namespace android