blob: 7692d0f71639a2592721cd1bbfc197860d7416d1 [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
98void LayerBase::commitTransaction(bool skipSize) {
99 const uint32_t w = mDrawingState.w;
100 const uint32_t h = mDrawingState.h;
101 mDrawingState = mCurrentState;
102 if (skipSize) {
103 mDrawingState.w = w;
104 mDrawingState.h = h;
105 }
106}
107void LayerBase::forceVisibilityTransaction() {
108 // this can be called without SurfaceFlinger.mStateLock, but if we
109 // can atomically increment the sequence number, it doesn't matter.
110 android_atomic_inc(&mCurrentState.sequence);
111 requestTransaction();
112}
113bool LayerBase::requestTransaction() {
114 int32_t old = setTransactionFlags(eTransactionNeeded);
115 return ((old & eTransactionNeeded) == 0);
116}
117uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
118 return android_atomic_and(~flags, &mTransactionFlags) & flags;
119}
120uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
121 return android_atomic_or(flags, &mTransactionFlags);
122}
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124bool LayerBase::setPosition(int32_t x, int32_t y) {
125 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
126 return false;
127 mCurrentState.sequence++;
128 mCurrentState.transform.set(x, y);
129 requestTransaction();
130 return true;
131}
132bool LayerBase::setLayer(uint32_t z) {
133 if (mCurrentState.z == z)
134 return false;
135 mCurrentState.sequence++;
136 mCurrentState.z = z;
137 requestTransaction();
138 return true;
139}
140bool LayerBase::setSize(uint32_t w, uint32_t h) {
141 if (mCurrentState.w == w && mCurrentState.h == h)
142 return false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 mCurrentState.w = w;
144 mCurrentState.h = h;
145 requestTransaction();
146 return true;
147}
148bool LayerBase::setAlpha(uint8_t alpha) {
149 if (mCurrentState.alpha == alpha)
150 return false;
151 mCurrentState.sequence++;
152 mCurrentState.alpha = alpha;
153 requestTransaction();
154 return true;
155}
156bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
157 // TODO: check the matrix has changed
158 mCurrentState.sequence++;
159 mCurrentState.transform.set(
160 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
161 requestTransaction();
162 return true;
163}
164bool LayerBase::setTransparentRegionHint(const Region& transparent) {
165 // TODO: check the region has changed
166 mCurrentState.sequence++;
167 mCurrentState.transparentRegion = transparent;
168 requestTransaction();
169 return true;
170}
171bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
172 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
173 if (mCurrentState.flags == newFlags)
174 return false;
175 mCurrentState.sequence++;
176 mCurrentState.flags = newFlags;
177 requestTransaction();
178 return true;
179}
180
181Rect LayerBase::visibleBounds() const
182{
183 return mTransformedBounds;
184}
185
186void LayerBase::setVisibleRegion(const Region& visibleRegion) {
187 // always called from main thread
188 visibleRegionScreen = visibleRegion;
189}
190
191void LayerBase::setCoveredRegion(const Region& coveredRegion) {
192 // always called from main thread
193 coveredRegionScreen = coveredRegion;
194}
195
196uint32_t LayerBase::doTransaction(uint32_t flags)
197{
198 const Layer::State& front(drawingState());
199 const Layer::State& temp(currentState());
200
201 if (temp.sequence != front.sequence) {
202 // invalidate and recompute the visible regions if needed
203 flags |= eVisibleRegion;
204 this->contentDirty = true;
205 }
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700206
207 if (temp.sequence != front.sequence) {
208 const bool linearFiltering = mUseLinearFiltering;
209 mUseLinearFiltering = false;
210 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
211 // we may use linear filtering, if the matrix scales us
212 const uint8_t type = temp.transform.getType();
213 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
214 mUseLinearFiltering = true;
215 }
216 }
217 }
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 // Commit the transaction
220 commitTransaction(flags & eRestartTransaction);
221 return flags;
222}
223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224void LayerBase::validateVisibility(const Transform& planeTransform)
225{
226 const Layer::State& s(drawingState());
227 const Transform tr(planeTransform * s.transform);
228 const bool transformed = tr.transformed();
229
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700230 uint32_t w = s.w;
231 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 tr.transform(mVertices[0], 0, 0);
233 tr.transform(mVertices[1], 0, h);
234 tr.transform(mVertices[2], w, h);
235 tr.transform(mVertices[3], w, 0);
236 if (UNLIKELY(transformed)) {
237 // NOTE: here we could also punt if we have too many rectangles
238 // in the transparent region
239 if (tr.preserveRects()) {
240 // transform the transparent region
241 transparentRegionScreen = tr.transform(s.transparentRegion);
242 } else {
243 // transformation too complex, can't do the transparent region
244 // optimization.
245 transparentRegionScreen.clear();
246 }
247 } else {
248 transparentRegionScreen = s.transparentRegion;
249 }
250
251 // cache a few things...
252 mOrientation = tr.getOrientation();
253 mTransformedBounds = tr.makeBounds(w, h);
254 mTransformed = transformed;
255 mLeft = tr.tx();
256 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257}
258
259void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
260{
261}
262
263void LayerBase::unlockPageFlip(
264 const Transform& planeTransform, Region& outDirtyRegion)
265{
266 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
267 outDirtyRegion.orSelf(visibleRegionScreen);
268 }
269}
270
271void LayerBase::finishPageFlip()
272{
273}
274
275void LayerBase::invalidate()
276{
277 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
278 mFlinger->signalEvent();
279 }
280}
281
282void LayerBase::drawRegion(const Region& reg) const
283{
Mathias Agopian20f68782009-05-11 00:03:41 -0700284 Region::const_iterator it = reg.begin();
285 Region::const_iterator const end = reg.end();
286 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 Rect r;
288 const DisplayHardware& hw(graphicPlane(0).displayHardware());
289 const int32_t fbWidth = hw.getWidth();
290 const int32_t fbHeight = hw.getHeight();
291 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
292 { fbWidth, fbHeight }, { 0, fbHeight } };
293 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700294 while (it != end) {
295 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 const GLint sy = fbHeight - (r.top + r.height());
297 glScissor(r.left, sy, r.width(), r.height());
298 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
299 }
300 }
301}
302
303void LayerBase::draw(const Region& inClip) const
304{
305 // invalidate the region we'll update
306 Region clip(inClip); // copy-on-write, so no-op most of the time
307
308 // Remove the transparent area from the clipping region
309 const State& s = drawingState();
310 if (LIKELY(!s.transparentRegion.isEmpty())) {
311 clip.subtract(transparentRegionScreen);
312 if (clip.isEmpty()) {
313 // usually this won't happen because this should be taken care of
314 // by SurfaceFlinger::computeVisibleRegions()
315 return;
316 }
317 }
318
319 // reset GL state
320 glEnable(GL_SCISSOR_TEST);
321
322 onDraw(clip);
323
324 /*
325 glDisable(GL_TEXTURE_2D);
326 glDisable(GL_DITHER);
327 glEnable(GL_BLEND);
328 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
329 glColor4x(0, 0x8000, 0, 0x10000);
330 drawRegion(transparentRegionScreen);
331 glDisable(GL_BLEND);
332 */
333}
334
335GLuint LayerBase::createTexture() const
336{
337 GLuint textureName = -1;
338 glGenTextures(1, &textureName);
339 glBindTexture(GL_TEXTURE_2D, textureName);
340 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
341 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700342 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
343 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 return textureName;
345}
346
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700347void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
348 GLclampx green, GLclampx blue,
349 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350{
351 const DisplayHardware& hw(graphicPlane(0).displayHardware());
352 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700353 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 glDisable(GL_TEXTURE_2D);
355 glDisable(GL_BLEND);
356 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700357
358 Region::const_iterator it = clip.begin();
359 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700360 glEnable(GL_SCISSOR_TEST);
361 glVertexPointer(2, GL_FIXED, 0, mVertices);
362 while (it != end) {
363 const Rect& r = *it++;
364 const GLint sy = fbHeight - (r.top + r.height());
365 glScissor(r.left, sy, r.width(), r.height());
366 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 }
368}
369
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700370void LayerBase::clearWithOpenGL(const Region& clip) const
371{
372 clearWithOpenGL(clip,0,0,0,0);
373}
374
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700375void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376{
377 const DisplayHardware& hw(graphicPlane(0).displayHardware());
378 const uint32_t fbHeight = hw.getHeight();
379 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700380
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700382 validateTexture(texture.name);
383 uint32_t width = texture.width;
384 uint32_t height = texture.height;
385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 glEnable(GL_TEXTURE_2D);
387
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 if (UNLIKELY(s.alpha < 0xFF)) {
389 // We have an alpha-modulation. We need to modulate all
390 // texture components by alpha because we're always using
391 // premultiplied alpha.
392
393 // If the texture doesn't have an alpha channel we can
394 // use REPLACE and switch to non premultiplied alpha
395 // blending (SRCA/ONE_MINUS_SRCA).
396
397 GLenum env, src;
398 if (needsBlending()) {
399 env = GL_MODULATE;
400 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
401 } else {
402 env = GL_REPLACE;
403 src = GL_SRC_ALPHA;
404 }
405 const GGLfixed alpha = (s.alpha << 16)/255;
406 glColor4x(alpha, alpha, alpha, alpha);
407 glEnable(GL_BLEND);
408 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
409 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
410 } else {
411 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
412 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
413 if (needsBlending()) {
414 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
415 glEnable(GL_BLEND);
416 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
417 } else {
418 glDisable(GL_BLEND);
419 }
420 }
421
Mathias Agopian95a666b2009-09-24 14:57:26 -0700422 Region::const_iterator it = clip.begin();
423 Region::const_iterator const end = clip.end();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 if (UNLIKELY(transformed()
425 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
426 {
427 //StopWatch watch("GL transformed");
Mathias Agopian95a666b2009-09-24 14:57:26 -0700428 const GLfixed texCoords[4][2] = {
429 { 0, 0 },
430 { 0, 0x10000 },
431 { 0x10000, 0x10000 },
432 { 0x10000, 0 }
433 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434
Mathias Agopian95a666b2009-09-24 14:57:26 -0700435 glMatrixMode(GL_TEXTURE);
436 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437
Mathias Agopian95a666b2009-09-24 14:57:26 -0700438 // the texture's source is rotated
439 if (texture.transform == HAL_TRANSFORM_ROT_90) {
440 // TODO: handle the other orientations
441 glTranslatef(0, 1, 0);
442 glRotatef(-90, 0, 0, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 }
Mathias Agopian95a666b2009-09-24 14:57:26 -0700444
445 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
446 DisplayHardware::DIRECT_TEXTURE))) {
447 // find the smallest power-of-two that will accommodate our surface
448 GLuint tw = 1 << (31 - clz(width));
449 GLuint th = 1 << (31 - clz(height));
450 if (tw < width) tw <<= 1;
451 if (th < height) th <<= 1;
452 GLfloat ws = GLfloat(width) /tw;
453 GLfloat hs = GLfloat(height)/th;
454 glScalef(ws, hs, 1.0f);
455 }
456
457 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
458 glVertexPointer(2, GL_FIXED, 0, mVertices);
459 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
460
461 while (it != end) {
462 const Rect& r = *it++;
463 const GLint sy = fbHeight - (r.top + r.height());
464 glScissor(r.left, sy, r.width(), r.height());
465 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
466 }
467 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700469 GLint crop[4] = { 0, height, width, -height };
470 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
471 int x = tx();
472 int y = ty();
473 y = fbHeight - (y + height);
474 while (it != end) {
475 const Rect& r = *it++;
476 const GLint sy = fbHeight - (r.top + r.height());
477 glScissor(r.left, sy, r.width(), r.height());
478 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 }
480 }
481}
482
483void LayerBase::validateTexture(GLint textureName) const
484{
485 glBindTexture(GL_TEXTURE_2D, textureName);
486 // TODO: reload the texture if needed
487 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700488 if (mUseLinearFiltering) {
489 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
490 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
491 } else {
492 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
493 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
494 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700495
496 if (needsDithering()) {
497 glEnable(GL_DITHER);
498 } else {
499 glDisable(GL_DITHER);
500 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501}
502
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700503void LayerBase::loadTexture(Texture* texture, GLint textureName,
504 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505{
506 // TODO: defer the actual texture reload until LayerBase::validateTexture
507 // is called.
508
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700509 texture->name = textureName;
510 GLuint& textureWidth(texture->width);
511 GLuint& textureHeight(texture->height);
512
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513 uint32_t flags = mFlags;
514 glBindTexture(GL_TEXTURE_2D, textureName);
515
516 GLuint tw = t.width;
517 GLuint th = t.height;
518
519 /*
520 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700521 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
523 * need to do something reasonable (here creating a bigger texture).
524 *
525 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
526 *
527 * This situation doesn't happen often, but some h/w have a limitation
528 * for their framebuffer (eg: must be multiple of 8 pixels), and
529 * we need to take that into account when using these buffers as
530 * textures.
531 *
532 * This should never be a problem with POT textures
533 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700534
535 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
536 unpack = 1 << ((unpack > 3) ? 3 : unpack);
537 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
538
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 /*
540 * round to POT if needed
541 */
542
543 GLuint texture_w = tw;
544 GLuint texture_h = th;
545 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
546 // find the smallest power-of-two that will accommodate our surface
547 texture_w = 1 << (31 - clz(t.width));
548 texture_h = 1 << (31 - clz(t.height));
549 if (texture_w < t.width) texture_w <<= 1;
550 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700552
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554 Rect bounds(dirty.bounds());
555 GLvoid* data = 0;
556 if (texture_w!=textureWidth || texture_h!=textureHeight) {
557 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559 if (!textureWidth || !textureHeight) {
560 // this is the first time, load the whole texture
561 if (texture_w==tw && texture_h==th) {
562 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 // we have to create the texture first because it
566 // doesn't match the size of the buffer
567 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570
571 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
572 glTexImage2D(GL_TEXTURE_2D, 0,
573 GL_RGB, texture_w, texture_h, 0,
574 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
575 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
576 glTexImage2D(GL_TEXTURE_2D, 0,
577 GL_RGBA, texture_w, texture_h, 0,
578 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700579 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
580 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700581 glTexImage2D(GL_TEXTURE_2D, 0,
582 GL_RGBA, texture_w, texture_h, 0,
583 GL_RGBA, GL_UNSIGNED_BYTE, data);
584 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
585 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
586 // just show the Y plane of YUV buffers
587 glTexImage2D(GL_TEXTURE_2D, 0,
588 GL_LUMINANCE, texture_w, texture_h, 0,
589 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
590 } else {
591 // oops, we don't handle this format!
592 LOGE("layer %p, texture=%d, using format %d, which is not "
593 "supported by the GL", this, textureName, t.format);
594 textureName = -1;
595 }
596 textureWidth = texture_w;
597 textureHeight = texture_h;
598 }
599 if (!data && textureName>=0) {
600 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
601 glTexSubImage2D(GL_TEXTURE_2D, 0,
602 0, bounds.top, t.width, bounds.height(),
603 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
604 t.data + bounds.top*t.stride*2);
605 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
606 glTexSubImage2D(GL_TEXTURE_2D, 0,
607 0, bounds.top, t.width, bounds.height(),
608 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
609 t.data + bounds.top*t.stride*2);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700610 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
611 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612 glTexSubImage2D(GL_TEXTURE_2D, 0,
613 0, bounds.top, t.width, bounds.height(),
614 GL_RGBA, GL_UNSIGNED_BYTE,
615 t.data + bounds.top*t.stride*4);
616 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
617 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
618 // just show the Y plane of YUV buffers
619 glTexSubImage2D(GL_TEXTURE_2D, 0,
620 0, bounds.top, t.width, bounds.height(),
621 GL_LUMINANCE, GL_UNSIGNED_BYTE,
622 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 }
624 }
625}
626
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627// ---------------------------------------------------------------------------
628
Mathias Agopian2e123242009-06-23 20:06:46 -0700629int32_t LayerBaseClient::sIdentity = 0;
630
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700632 const sp<Client>& client, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -0700633 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700634 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700636 lcblk = new SharedBufferServer(
637 client->ctrlblk, i, NUM_BUFFERS,
638 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641void LayerBaseClient::onFirstRef()
642{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700643 sp<Client> client(this->client.promote());
644 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 }
647}
648
649LayerBaseClient::~LayerBaseClient()
650{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700651 sp<Client> client(this->client.promote());
652 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 client->free(mIndex);
654 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700655 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656}
657
Mathias Agopianf9d93272009-06-19 17:00:27 -0700658int32_t LayerBaseClient::serverIndex() const
659{
660 sp<Client> client(this->client.promote());
661 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 return (client->cid<<16)|mIndex;
663 }
664 return 0xFFFF0000 | mIndex;
665}
666
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700667sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700669 sp<Surface> s;
670 Mutex::Autolock _l(mLock);
671 s = mClientSurface.promote();
672 if (s == 0) {
673 s = createSurface();
674 mClientSurface = s;
675 }
676 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677}
678
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700679sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
680{
Mathias Agopian9a112062009-04-17 19:36:26 -0700681 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700682 const_cast<LayerBaseClient *>(this));
683}
684
685// ---------------------------------------------------------------------------
686
Mathias Agopian9a112062009-04-17 19:36:26 -0700687LayerBaseClient::Surface::Surface(
688 const sp<SurfaceFlinger>& flinger,
689 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700690 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700691 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
692{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700693}
694
695
Mathias Agopian9a112062009-04-17 19:36:26 -0700696LayerBaseClient::Surface::~Surface()
697{
698 /*
699 * This is a good place to clean-up all client resources
700 */
701
702 // destroy client resources
703 sp<LayerBaseClient> layer = getOwner();
704 if (layer != 0) {
705 mFlinger->destroySurface(layer);
706 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700707}
708
709sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
710 sp<LayerBaseClient> owner(mOwner.promote());
711 return owner;
712}
713
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700714status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700715 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700716{
717 switch (code) {
718 case REGISTER_BUFFERS:
719 case UNREGISTER_BUFFERS:
720 case CREATE_OVERLAY:
721 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700722 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
723 IPCThreadState* ipc = IPCThreadState::self();
724 const int pid = ipc->getCallingPid();
725 const int uid = ipc->getCallingUid();
726 LOGE("Permission Denial: "
727 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
728 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700729 }
730 }
731 }
732 return BnSurface::onTransact(code, data, reply, flags);
733}
734
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700735sp<SurfaceBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700736{
737 return NULL;
738}
739
740status_t LayerBaseClient::Surface::registerBuffers(
741 const ISurface::BufferHeap& buffers)
742{
743 return INVALID_OPERATION;
744}
745
746void LayerBaseClient::Surface::postBuffer(ssize_t offset)
747{
748}
749
750void LayerBaseClient::Surface::unregisterBuffers()
751{
752}
753
754sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
755 uint32_t w, uint32_t h, int32_t format)
756{
757 return NULL;
758};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759
760// ---------------------------------------------------------------------------
761
762}; // namespace android