blob: ec38fe9fd54f057dea12d30fe74c6d1ad74c1c45 [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...
Mathias Agopian888eee62009-09-04 17:27:16 -0700401 bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG);
402 if (fast || s.flags & ISurfaceComposer::eLayerDither) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 glEnable(GL_DITHER);
404 } else {
405 glDisable(GL_DITHER);
406 }
407
408 if (UNLIKELY(s.alpha < 0xFF)) {
409 // We have an alpha-modulation. We need to modulate all
410 // texture components by alpha because we're always using
411 // premultiplied alpha.
412
413 // If the texture doesn't have an alpha channel we can
414 // use REPLACE and switch to non premultiplied alpha
415 // blending (SRCA/ONE_MINUS_SRCA).
416
417 GLenum env, src;
418 if (needsBlending()) {
419 env = GL_MODULATE;
420 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
421 } else {
422 env = GL_REPLACE;
423 src = GL_SRC_ALPHA;
424 }
425 const GGLfixed alpha = (s.alpha << 16)/255;
426 glColor4x(alpha, alpha, alpha, alpha);
427 glEnable(GL_BLEND);
428 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
429 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
430 } else {
431 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
432 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
433 if (needsBlending()) {
434 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
435 glEnable(GL_BLEND);
436 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
437 } else {
438 glDisable(GL_BLEND);
439 }
440 }
441
442 if (UNLIKELY(transformed()
443 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
444 {
445 //StopWatch watch("GL transformed");
Mathias Agopian20f68782009-05-11 00:03:41 -0700446 Region::const_iterator it = clip.begin();
447 Region::const_iterator const end = clip.end();
448 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 // always use high-quality filtering with fast configurations
450 bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG);
451 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
452 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
453 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
454 }
455 const GLfixed texCoords[4][2] = {
456 { 0, 0 },
457 { 0, 0x10000 },
458 { 0x10000, 0x10000 },
459 { 0x10000, 0 }
460 };
461
462 glMatrixMode(GL_TEXTURE);
463 glLoadIdentity();
464
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700465 // the texture's source is rotated
466 if (texture.transform == HAL_TRANSFORM_ROT_90) {
467 // TODO: handle the other orientations
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468 glTranslatef(0, 1, 0);
469 glRotatef(-90, 0, 0, 1);
470 }
471
Mathias Agopianf9cd64b2009-07-30 12:19:10 -0700472 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
473 DisplayHardware::DIRECT_TEXTURE))) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474 // find the smallest power-of-two that will accommodate our surface
Mathias Agopian0926f502009-05-04 14:17:04 -0700475 GLuint tw = 1 << (31 - clz(width));
476 GLuint th = 1 << (31 - clz(height));
477 if (tw < width) tw <<= 1;
478 if (th < height) th <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 // this divide should be relatively fast because it's
480 // a power-of-two (optimized path in libgcc)
Mathias Agopian0926f502009-05-04 14:17:04 -0700481 GLfloat ws = GLfloat(width) /tw;
482 GLfloat hs = GLfloat(height)/th;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 glScalef(ws, hs, 1.0f);
484 }
485
486 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
487 glVertexPointer(2, GL_FIXED, 0, mVertices);
488 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
489
Mathias Agopian20f68782009-05-11 00:03:41 -0700490 while (it != end) {
491 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492 const GLint sy = fbHeight - (r.top + r.height());
493 glScissor(r.left, sy, r.width(), r.height());
494 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
495 }
496
497 if (!fast && s.flags & ISurfaceComposer::eLayerFilter) {
498 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
499 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
500 }
501 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
502 }
503 } else {
Mathias Agopian20f68782009-05-11 00:03:41 -0700504 Region::const_iterator it = clip.begin();
505 Region::const_iterator const end = clip.end();
506 if (it != end) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700507 GLint crop[4] = { 0, height, width, -height };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
509 int x = tx();
510 int y = ty();
Mathias Agopian0926f502009-05-04 14:17:04 -0700511 y = fbHeight - (y + height);
Mathias Agopian20f68782009-05-11 00:03:41 -0700512 while (it != end) {
513 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 const GLint sy = fbHeight - (r.top + r.height());
515 glScissor(r.left, sy, r.width(), r.height());
Mathias Agopian0926f502009-05-04 14:17:04 -0700516 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 }
518 }
519 }
520}
521
522void LayerBase::validateTexture(GLint textureName) const
523{
524 glBindTexture(GL_TEXTURE_2D, textureName);
525 // TODO: reload the texture if needed
526 // this is currently done in loadTexture() below
527}
528
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700529void LayerBase::loadTexture(Texture* texture, GLint textureName,
530 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531{
532 // TODO: defer the actual texture reload until LayerBase::validateTexture
533 // is called.
534
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700535 texture->name = textureName;
536 GLuint& textureWidth(texture->width);
537 GLuint& textureHeight(texture->height);
538
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 uint32_t flags = mFlags;
540 glBindTexture(GL_TEXTURE_2D, textureName);
541
542 GLuint tw = t.width;
543 GLuint th = t.height;
544
545 /*
546 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700547 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
549 * need to do something reasonable (here creating a bigger texture).
550 *
551 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
552 *
553 * This situation doesn't happen often, but some h/w have a limitation
554 * for their framebuffer (eg: must be multiple of 8 pixels), and
555 * we need to take that into account when using these buffers as
556 * textures.
557 *
558 * This should never be a problem with POT textures
559 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700560
561 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
562 unpack = 1 << ((unpack > 3) ? 3 : unpack);
563 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
564
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 /*
566 * round to POT if needed
567 */
568
569 GLuint texture_w = tw;
570 GLuint texture_h = th;
571 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
572 // find the smallest power-of-two that will accommodate our surface
573 texture_w = 1 << (31 - clz(t.width));
574 texture_h = 1 << (31 - clz(t.height));
575 if (texture_w < t.width) texture_w <<= 1;
576 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700580 Rect bounds(dirty.bounds());
581 GLvoid* data = 0;
582 if (texture_w!=textureWidth || texture_h!=textureHeight) {
583 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 if (!textureWidth || !textureHeight) {
586 // this is the first time, load the whole texture
587 if (texture_w==tw && texture_h==th) {
588 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700591 // we have to create the texture first because it
592 // doesn't match the size of the buffer
593 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700596
597 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
598 glTexImage2D(GL_TEXTURE_2D, 0,
599 GL_RGB, texture_w, texture_h, 0,
600 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
601 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
602 glTexImage2D(GL_TEXTURE_2D, 0,
603 GL_RGBA, texture_w, texture_h, 0,
604 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
605 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
606 glTexImage2D(GL_TEXTURE_2D, 0,
607 GL_RGBA, texture_w, texture_h, 0,
608 GL_RGBA, GL_UNSIGNED_BYTE, data);
609 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
610 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
611 // just show the Y plane of YUV buffers
612 glTexImage2D(GL_TEXTURE_2D, 0,
613 GL_LUMINANCE, texture_w, texture_h, 0,
614 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
615 } else {
616 // oops, we don't handle this format!
617 LOGE("layer %p, texture=%d, using format %d, which is not "
618 "supported by the GL", this, textureName, t.format);
619 textureName = -1;
620 }
621 textureWidth = texture_w;
622 textureHeight = texture_h;
623 }
624 if (!data && textureName>=0) {
625 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
626 glTexSubImage2D(GL_TEXTURE_2D, 0,
627 0, bounds.top, t.width, bounds.height(),
628 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
629 t.data + bounds.top*t.stride*2);
630 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
631 glTexSubImage2D(GL_TEXTURE_2D, 0,
632 0, bounds.top, t.width, bounds.height(),
633 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
634 t.data + bounds.top*t.stride*2);
635 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) {
636 glTexSubImage2D(GL_TEXTURE_2D, 0,
637 0, bounds.top, t.width, bounds.height(),
638 GL_RGBA, GL_UNSIGNED_BYTE,
639 t.data + bounds.top*t.stride*4);
640 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
641 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
642 // just show the Y plane of YUV buffers
643 glTexSubImage2D(GL_TEXTURE_2D, 0,
644 0, bounds.top, t.width, bounds.height(),
645 GL_LUMINANCE, GL_UNSIGNED_BYTE,
646 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 }
648 }
649}
650
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651// ---------------------------------------------------------------------------
652
Mathias Agopian2e123242009-06-23 20:06:46 -0700653int32_t LayerBaseClient::sIdentity = 0;
654
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700656 const sp<Client>& client, int32_t i)
657 : LayerBase(flinger, display), client(client),
658 lcblk( client!=0 ? &(client->ctrlblk->layers[i]) : 0 ),
Mathias Agopian2e123242009-06-23 20:06:46 -0700659 mIndex(i),
660 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700662}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700664void LayerBaseClient::onFirstRef()
665{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700666 sp<Client> client(this->client.promote());
667 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700668 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 // Initialize this layer's control block
670 memset(this->lcblk, 0, sizeof(layer_cblk_t));
671 this->lcblk->identity = mIdentity;
672 Region::writeEmpty(&(this->lcblk->region[0]), sizeof(flat_region_t));
673 Region::writeEmpty(&(this->lcblk->region[1]), sizeof(flat_region_t));
674 }
675}
676
677LayerBaseClient::~LayerBaseClient()
678{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700679 sp<Client> client(this->client.promote());
680 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 client->free(mIndex);
682 }
683}
684
Mathias Agopianf9d93272009-06-19 17:00:27 -0700685int32_t LayerBaseClient::serverIndex() const
686{
687 sp<Client> client(this->client.promote());
688 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 return (client->cid<<16)|mIndex;
690 }
691 return 0xFFFF0000 | mIndex;
692}
693
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700694sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700696 sp<Surface> s;
697 Mutex::Autolock _l(mLock);
698 s = mClientSurface.promote();
699 if (s == 0) {
700 s = createSurface();
701 mClientSurface = s;
702 }
703 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704}
705
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700706sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
707{
Mathias Agopian9a112062009-04-17 19:36:26 -0700708 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700709 const_cast<LayerBaseClient *>(this));
710}
711
712// ---------------------------------------------------------------------------
713
Mathias Agopian9a112062009-04-17 19:36:26 -0700714LayerBaseClient::Surface::Surface(
715 const sp<SurfaceFlinger>& flinger,
716 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700717 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700718 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
719{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700720}
721
722
Mathias Agopian9a112062009-04-17 19:36:26 -0700723LayerBaseClient::Surface::~Surface()
724{
725 /*
726 * This is a good place to clean-up all client resources
727 */
728
729 // destroy client resources
730 sp<LayerBaseClient> layer = getOwner();
731 if (layer != 0) {
732 mFlinger->destroySurface(layer);
733 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700734}
735
736sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
737 sp<LayerBaseClient> owner(mOwner.promote());
738 return owner;
739}
740
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700741status_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
Mathias Agopian52212712009-08-11 22:34:02 -0700762sp<SurfaceBuffer> LayerBaseClient::Surface::getBuffer(int)
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