blob: 28ef27c1076e14aa1a6ef930a7a90f5b008f5e6a [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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include "SurfaceFlinger.h"
34#include "DisplayHardware/DisplayHardware.h"
35
36
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037namespace android {
38
39// ---------------------------------------------------------------------------
40
41const uint32_t LayerBase::typeInfo = 1;
42const char* const LayerBase::typeID = "LayerBase";
43
44const uint32_t LayerBaseClient::typeInfo = LayerBase::typeInfo | 2;
45const char* const LayerBaseClient::typeID = "LayerBaseClient";
46
47// ---------------------------------------------------------------------------
48
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
50 : dpy(display), contentDirty(false),
51 mFlinger(flinger),
52 mTransformed(false),
Mathias Agopiana2fe0a22009-09-23 18:34:53 -070053 mUseLinearFiltering(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 mOrientation(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055 mTransactionFlags(0),
56 mPremultipliedAlpha(true),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057 mInvalidate(0)
58{
59 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
60 mFlags = hw.getFlags();
61}
62
63LayerBase::~LayerBase()
64{
65}
66
67const GraphicPlane& LayerBase::graphicPlane(int dpy) const
68{
69 return mFlinger->graphicPlane(dpy);
70}
71
72GraphicPlane& LayerBase::graphicPlane(int dpy)
73{
74 return mFlinger->graphicPlane(dpy);
75}
76
77void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
78{
79 uint32_t layerFlags = 0;
80 if (flags & ISurfaceComposer::eHidden)
81 layerFlags = ISurfaceComposer::eLayerHidden;
82
83 if (flags & ISurfaceComposer::eNonPremultiplied)
84 mPremultipliedAlpha = false;
85
86 mCurrentState.z = 0;
87 mCurrentState.w = w;
88 mCurrentState.h = h;
89 mCurrentState.alpha = 0xFF;
90 mCurrentState.flags = layerFlags;
91 mCurrentState.sequence = 0;
92 mCurrentState.transform.set(0, 0);
93
94 // drawing state & current state are identical
95 mDrawingState = mCurrentState;
96}
97
Mathias Agopianba6be542009-09-29 22:32:36 -070098void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100}
101void LayerBase::forceVisibilityTransaction() {
102 // this can be called without SurfaceFlinger.mStateLock, but if we
103 // can atomically increment the sequence number, it doesn't matter.
104 android_atomic_inc(&mCurrentState.sequence);
105 requestTransaction();
106}
107bool LayerBase::requestTransaction() {
108 int32_t old = setTransactionFlags(eTransactionNeeded);
109 return ((old & eTransactionNeeded) == 0);
110}
111uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
112 return android_atomic_and(~flags, &mTransactionFlags) & flags;
113}
114uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
115 return android_atomic_or(flags, &mTransactionFlags);
116}
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118bool LayerBase::setPosition(int32_t x, int32_t y) {
119 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
120 return false;
121 mCurrentState.sequence++;
122 mCurrentState.transform.set(x, y);
123 requestTransaction();
124 return true;
125}
126bool LayerBase::setLayer(uint32_t z) {
127 if (mCurrentState.z == z)
128 return false;
129 mCurrentState.sequence++;
130 mCurrentState.z = z;
131 requestTransaction();
132 return true;
133}
134bool LayerBase::setSize(uint32_t w, uint32_t h) {
135 if (mCurrentState.w == w && mCurrentState.h == h)
136 return false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mCurrentState.w = w;
138 mCurrentState.h = h;
139 requestTransaction();
140 return true;
141}
142bool LayerBase::setAlpha(uint8_t alpha) {
143 if (mCurrentState.alpha == alpha)
144 return false;
145 mCurrentState.sequence++;
146 mCurrentState.alpha = alpha;
147 requestTransaction();
148 return true;
149}
150bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
151 // TODO: check the matrix has changed
152 mCurrentState.sequence++;
153 mCurrentState.transform.set(
154 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
155 requestTransaction();
156 return true;
157}
158bool LayerBase::setTransparentRegionHint(const Region& transparent) {
159 // TODO: check the region has changed
160 mCurrentState.sequence++;
161 mCurrentState.transparentRegion = transparent;
162 requestTransaction();
163 return true;
164}
165bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
166 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
167 if (mCurrentState.flags == newFlags)
168 return false;
169 mCurrentState.sequence++;
170 mCurrentState.flags = newFlags;
171 requestTransaction();
172 return true;
173}
174
175Rect LayerBase::visibleBounds() const
176{
177 return mTransformedBounds;
178}
179
180void LayerBase::setVisibleRegion(const Region& visibleRegion) {
181 // always called from main thread
182 visibleRegionScreen = visibleRegion;
183}
184
185void LayerBase::setCoveredRegion(const Region& coveredRegion) {
186 // always called from main thread
187 coveredRegionScreen = coveredRegion;
188}
189
190uint32_t LayerBase::doTransaction(uint32_t flags)
191{
192 const Layer::State& front(drawingState());
193 const Layer::State& temp(currentState());
194
195 if (temp.sequence != front.sequence) {
196 // invalidate and recompute the visible regions if needed
197 flags |= eVisibleRegion;
198 this->contentDirty = true;
199 }
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700200
201 if (temp.sequence != front.sequence) {
202 const bool linearFiltering = mUseLinearFiltering;
203 mUseLinearFiltering = false;
204 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
205 // we may use linear filtering, if the matrix scales us
206 const uint8_t type = temp.transform.getType();
207 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
208 mUseLinearFiltering = true;
209 }
210 }
211 }
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700214 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 return flags;
216}
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218void LayerBase::validateVisibility(const Transform& planeTransform)
219{
220 const Layer::State& s(drawingState());
221 const Transform tr(planeTransform * s.transform);
222 const bool transformed = tr.transformed();
223
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700224 uint32_t w = s.w;
225 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 tr.transform(mVertices[0], 0, 0);
227 tr.transform(mVertices[1], 0, h);
228 tr.transform(mVertices[2], w, h);
229 tr.transform(mVertices[3], w, 0);
230 if (UNLIKELY(transformed)) {
231 // NOTE: here we could also punt if we have too many rectangles
232 // in the transparent region
233 if (tr.preserveRects()) {
234 // transform the transparent region
235 transparentRegionScreen = tr.transform(s.transparentRegion);
236 } else {
237 // transformation too complex, can't do the transparent region
238 // optimization.
239 transparentRegionScreen.clear();
240 }
241 } else {
242 transparentRegionScreen = s.transparentRegion;
243 }
244
245 // cache a few things...
246 mOrientation = tr.getOrientation();
247 mTransformedBounds = tr.makeBounds(w, h);
248 mTransformed = transformed;
249 mLeft = tr.tx();
250 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251}
252
253void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
254{
255}
256
257void LayerBase::unlockPageFlip(
258 const Transform& planeTransform, Region& outDirtyRegion)
259{
260 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
261 outDirtyRegion.orSelf(visibleRegionScreen);
262 }
263}
264
265void LayerBase::finishPageFlip()
266{
267}
268
269void LayerBase::invalidate()
270{
271 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
272 mFlinger->signalEvent();
273 }
274}
275
276void LayerBase::drawRegion(const Region& reg) const
277{
Mathias Agopian20f68782009-05-11 00:03:41 -0700278 Region::const_iterator it = reg.begin();
279 Region::const_iterator const end = reg.end();
280 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281 Rect r;
282 const DisplayHardware& hw(graphicPlane(0).displayHardware());
283 const int32_t fbWidth = hw.getWidth();
284 const int32_t fbHeight = hw.getHeight();
285 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
286 { fbWidth, fbHeight }, { 0, fbHeight } };
287 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700288 while (it != end) {
289 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 const GLint sy = fbHeight - (r.top + r.height());
291 glScissor(r.left, sy, r.width(), r.height());
292 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
293 }
294 }
295}
296
297void LayerBase::draw(const Region& inClip) const
298{
299 // invalidate the region we'll update
300 Region clip(inClip); // copy-on-write, so no-op most of the time
301
302 // Remove the transparent area from the clipping region
303 const State& s = drawingState();
304 if (LIKELY(!s.transparentRegion.isEmpty())) {
305 clip.subtract(transparentRegionScreen);
306 if (clip.isEmpty()) {
307 // usually this won't happen because this should be taken care of
308 // by SurfaceFlinger::computeVisibleRegions()
309 return;
310 }
311 }
312
313 // reset GL state
314 glEnable(GL_SCISSOR_TEST);
315
316 onDraw(clip);
317
318 /*
319 glDisable(GL_TEXTURE_2D);
320 glDisable(GL_DITHER);
321 glEnable(GL_BLEND);
322 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
323 glColor4x(0, 0x8000, 0, 0x10000);
324 drawRegion(transparentRegionScreen);
325 glDisable(GL_BLEND);
326 */
327}
328
329GLuint LayerBase::createTexture() const
330{
331 GLuint textureName = -1;
332 glGenTextures(1, &textureName);
333 glBindTexture(GL_TEXTURE_2D, textureName);
334 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
335 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700336 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
337 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 return textureName;
339}
340
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700341void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
342 GLclampx green, GLclampx blue,
343 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344{
345 const DisplayHardware& hw(graphicPlane(0).displayHardware());
346 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700347 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 glDisable(GL_TEXTURE_2D);
349 glDisable(GL_BLEND);
350 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700351
352 Region::const_iterator it = clip.begin();
353 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700354 glEnable(GL_SCISSOR_TEST);
355 glVertexPointer(2, GL_FIXED, 0, mVertices);
356 while (it != end) {
357 const Rect& r = *it++;
358 const GLint sy = fbHeight - (r.top + r.height());
359 glScissor(r.left, sy, r.width(), r.height());
360 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 }
362}
363
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700364void LayerBase::clearWithOpenGL(const Region& clip) const
365{
366 clearWithOpenGL(clip,0,0,0,0);
367}
368
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700369void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370{
371 const DisplayHardware& hw(graphicPlane(0).displayHardware());
372 const uint32_t fbHeight = hw.getHeight();
373 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700376 validateTexture(texture.name);
377 uint32_t width = texture.width;
378 uint32_t height = texture.height;
379
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 glEnable(GL_TEXTURE_2D);
381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 if (UNLIKELY(s.alpha < 0xFF)) {
383 // We have an alpha-modulation. We need to modulate all
384 // texture components by alpha because we're always using
385 // premultiplied alpha.
386
387 // If the texture doesn't have an alpha channel we can
388 // use REPLACE and switch to non premultiplied alpha
389 // blending (SRCA/ONE_MINUS_SRCA).
390
391 GLenum env, src;
392 if (needsBlending()) {
393 env = GL_MODULATE;
394 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
395 } else {
396 env = GL_REPLACE;
397 src = GL_SRC_ALPHA;
398 }
399 const GGLfixed alpha = (s.alpha << 16)/255;
400 glColor4x(alpha, alpha, alpha, alpha);
401 glEnable(GL_BLEND);
402 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
403 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
404 } else {
405 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
406 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
407 if (needsBlending()) {
408 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
409 glEnable(GL_BLEND);
410 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
411 } else {
412 glDisable(GL_BLEND);
413 }
414 }
415
Mathias Agopian95a666b2009-09-24 14:57:26 -0700416 Region::const_iterator it = clip.begin();
417 Region::const_iterator const end = clip.end();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 if (UNLIKELY(transformed()
419 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
420 {
421 //StopWatch watch("GL transformed");
Mathias Agopian95a666b2009-09-24 14:57:26 -0700422 const GLfixed texCoords[4][2] = {
423 { 0, 0 },
424 { 0, 0x10000 },
425 { 0x10000, 0x10000 },
426 { 0x10000, 0 }
427 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428
Mathias Agopian95a666b2009-09-24 14:57:26 -0700429 glMatrixMode(GL_TEXTURE);
430 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431
Mathias Agopian95a666b2009-09-24 14:57:26 -0700432 // the texture's source is rotated
433 if (texture.transform == HAL_TRANSFORM_ROT_90) {
434 // TODO: handle the other orientations
435 glTranslatef(0, 1, 0);
436 glRotatef(-90, 0, 0, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 }
Mathias Agopian95a666b2009-09-24 14:57:26 -0700438
439 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
440 DisplayHardware::DIRECT_TEXTURE))) {
441 // find the smallest power-of-two that will accommodate our surface
442 GLuint tw = 1 << (31 - clz(width));
443 GLuint th = 1 << (31 - clz(height));
444 if (tw < width) tw <<= 1;
445 if (th < height) th <<= 1;
446 GLfloat ws = GLfloat(width) /tw;
447 GLfloat hs = GLfloat(height)/th;
448 glScalef(ws, hs, 1.0f);
449 }
450
451 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
452 glVertexPointer(2, GL_FIXED, 0, mVertices);
453 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
454
455 while (it != end) {
456 const Rect& r = *it++;
457 const GLint sy = fbHeight - (r.top + r.height());
458 glScissor(r.left, sy, r.width(), r.height());
459 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
460 }
461 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700463 GLint crop[4] = { 0, height, width, -height };
464 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
465 int x = tx();
466 int y = ty();
467 y = fbHeight - (y + height);
468 while (it != end) {
469 const Rect& r = *it++;
470 const GLint sy = fbHeight - (r.top + r.height());
471 glScissor(r.left, sy, r.width(), r.height());
472 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473 }
474 }
475}
476
477void LayerBase::validateTexture(GLint textureName) const
478{
479 glBindTexture(GL_TEXTURE_2D, textureName);
480 // TODO: reload the texture if needed
481 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700482 if (mUseLinearFiltering) {
483 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
484 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
485 } else {
486 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
487 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
488 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700489
490 if (needsDithering()) {
491 glEnable(GL_DITHER);
492 } else {
493 glDisable(GL_DITHER);
494 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495}
496
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700497void LayerBase::loadTexture(Texture* texture, GLint textureName,
498 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499{
500 // TODO: defer the actual texture reload until LayerBase::validateTexture
501 // is called.
502
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700503 texture->name = textureName;
504 GLuint& textureWidth(texture->width);
505 GLuint& textureHeight(texture->height);
506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 uint32_t flags = mFlags;
508 glBindTexture(GL_TEXTURE_2D, textureName);
509
510 GLuint tw = t.width;
511 GLuint th = t.height;
512
513 /*
514 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700515 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
517 * need to do something reasonable (here creating a bigger texture).
518 *
519 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
520 *
521 * This situation doesn't happen often, but some h/w have a limitation
522 * for their framebuffer (eg: must be multiple of 8 pixels), and
523 * we need to take that into account when using these buffers as
524 * textures.
525 *
526 * This should never be a problem with POT textures
527 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700528
529 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
530 unpack = 1 << ((unpack > 3) ? 3 : unpack);
531 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 /*
534 * round to POT if needed
535 */
536
537 GLuint texture_w = tw;
538 GLuint texture_h = th;
539 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
540 // find the smallest power-of-two that will accommodate our surface
541 texture_w = 1 << (31 - clz(t.width));
542 texture_h = 1 << (31 - clz(t.height));
543 if (texture_w < t.width) texture_w <<= 1;
544 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548 Rect bounds(dirty.bounds());
549 GLvoid* data = 0;
550 if (texture_w!=textureWidth || texture_h!=textureHeight) {
551 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553 if (!textureWidth || !textureHeight) {
554 // this is the first time, load the whole texture
555 if (texture_w==tw && texture_h==th) {
556 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559 // we have to create the texture first because it
560 // doesn't match the size of the buffer
561 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564
565 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
566 glTexImage2D(GL_TEXTURE_2D, 0,
567 GL_RGB, texture_w, texture_h, 0,
568 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
569 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
570 glTexImage2D(GL_TEXTURE_2D, 0,
571 GL_RGBA, texture_w, texture_h, 0,
572 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700573 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
574 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700575 glTexImage2D(GL_TEXTURE_2D, 0,
576 GL_RGBA, texture_w, texture_h, 0,
577 GL_RGBA, GL_UNSIGNED_BYTE, data);
578 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
579 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
580 // just show the Y plane of YUV buffers
581 glTexImage2D(GL_TEXTURE_2D, 0,
582 GL_LUMINANCE, texture_w, texture_h, 0,
583 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
584 } else {
585 // oops, we don't handle this format!
586 LOGE("layer %p, texture=%d, using format %d, which is not "
587 "supported by the GL", this, textureName, t.format);
588 textureName = -1;
589 }
590 textureWidth = texture_w;
591 textureHeight = texture_h;
592 }
593 if (!data && textureName>=0) {
594 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
595 glTexSubImage2D(GL_TEXTURE_2D, 0,
596 0, bounds.top, t.width, bounds.height(),
597 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
598 t.data + bounds.top*t.stride*2);
599 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
600 glTexSubImage2D(GL_TEXTURE_2D, 0,
601 0, bounds.top, t.width, bounds.height(),
602 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
603 t.data + bounds.top*t.stride*2);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700604 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
605 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606 glTexSubImage2D(GL_TEXTURE_2D, 0,
607 0, bounds.top, t.width, bounds.height(),
608 GL_RGBA, GL_UNSIGNED_BYTE,
609 t.data + bounds.top*t.stride*4);
610 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
611 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
612 // just show the Y plane of YUV buffers
613 glTexSubImage2D(GL_TEXTURE_2D, 0,
614 0, bounds.top, t.width, bounds.height(),
615 GL_LUMINANCE, GL_UNSIGNED_BYTE,
616 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 }
618 }
619}
620
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621// ---------------------------------------------------------------------------
622
Mathias Agopian2e123242009-06-23 20:06:46 -0700623int32_t LayerBaseClient::sIdentity = 0;
624
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700626 const sp<Client>& client, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -0700627 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700628 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700630 lcblk = new SharedBufferServer(
631 client->ctrlblk, i, NUM_BUFFERS,
632 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700633}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700635void LayerBaseClient::onFirstRef()
636{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700637 sp<Client> client(this->client.promote());
638 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 }
641}
642
643LayerBaseClient::~LayerBaseClient()
644{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700645 sp<Client> client(this->client.promote());
646 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 client->free(mIndex);
648 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700649 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650}
651
Mathias Agopianf9d93272009-06-19 17:00:27 -0700652int32_t LayerBaseClient::serverIndex() const
653{
654 sp<Client> client(this->client.promote());
655 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 return (client->cid<<16)|mIndex;
657 }
658 return 0xFFFF0000 | mIndex;
659}
660
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700661sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700663 sp<Surface> s;
664 Mutex::Autolock _l(mLock);
665 s = mClientSurface.promote();
666 if (s == 0) {
667 s = createSurface();
668 mClientSurface = s;
669 }
670 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671}
672
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700673sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
674{
Mathias Agopian9a112062009-04-17 19:36:26 -0700675 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700676 const_cast<LayerBaseClient *>(this));
677}
678
679// ---------------------------------------------------------------------------
680
Mathias Agopian9a112062009-04-17 19:36:26 -0700681LayerBaseClient::Surface::Surface(
682 const sp<SurfaceFlinger>& flinger,
683 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700684 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700685 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
686{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700687}
688
689
Mathias Agopian9a112062009-04-17 19:36:26 -0700690LayerBaseClient::Surface::~Surface()
691{
692 /*
693 * This is a good place to clean-up all client resources
694 */
695
696 // destroy client resources
697 sp<LayerBaseClient> layer = getOwner();
698 if (layer != 0) {
699 mFlinger->destroySurface(layer);
700 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700701}
702
703sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
704 sp<LayerBaseClient> owner(mOwner.promote());
705 return owner;
706}
707
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700709 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700710{
711 switch (code) {
712 case REGISTER_BUFFERS:
713 case UNREGISTER_BUFFERS:
714 case CREATE_OVERLAY:
715 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700716 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
717 IPCThreadState* ipc = IPCThreadState::self();
718 const int pid = ipc->getCallingPid();
719 const int uid = ipc->getCallingUid();
720 LOGE("Permission Denial: "
721 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
722 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700723 }
724 }
725 }
726 return BnSurface::onTransact(code, data, reply, flags);
727}
728
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700729sp<SurfaceBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700730{
731 return NULL;
732}
733
734status_t LayerBaseClient::Surface::registerBuffers(
735 const ISurface::BufferHeap& buffers)
736{
737 return INVALID_OPERATION;
738}
739
740void LayerBaseClient::Surface::postBuffer(ssize_t offset)
741{
742}
743
744void LayerBaseClient::Surface::unregisterBuffers()
745{
746}
747
748sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
749 uint32_t w, uint32_t h, int32_t format)
750{
751 return NULL;
752};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753
754// ---------------------------------------------------------------------------
755
756}; // namespace android