blob: 5a93b2d46161a4c7752cd49d37c3896199ea0af6 [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
37// We don't honor the premultiplied alpha flags, which means that
38// premultiplied surface may be composed using a non-premultiplied
39// equation. We do this because it may be a lot faster on some hardware
40// The correct value is HONOR_PREMULTIPLIED_ALPHA = 1
41#define HONOR_PREMULTIPLIED_ALPHA 0
42
43namespace android {
44
45// ---------------------------------------------------------------------------
46
47const uint32_t LayerBase::typeInfo = 1;
48const char* const LayerBase::typeID = "LayerBase";
49
50const uint32_t LayerBaseClient::typeInfo = LayerBase::typeInfo | 2;
51const char* const LayerBaseClient::typeID = "LayerBaseClient";
52
53// ---------------------------------------------------------------------------
54
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
56 : dpy(display), contentDirty(false),
57 mFlinger(flinger),
58 mTransformed(false),
Mathias Agopiana2fe0a22009-09-23 18:34:53 -070059 mUseLinearFiltering(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130bool LayerBase::setPosition(int32_t x, int32_t y) {
131 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
132 return false;
133 mCurrentState.sequence++;
134 mCurrentState.transform.set(x, y);
135 requestTransaction();
136 return true;
137}
138bool LayerBase::setLayer(uint32_t z) {
139 if (mCurrentState.z == z)
140 return false;
141 mCurrentState.sequence++;
142 mCurrentState.z = z;
143 requestTransaction();
144 return true;
145}
146bool LayerBase::setSize(uint32_t w, uint32_t h) {
147 if (mCurrentState.w == w && mCurrentState.h == h)
148 return false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 mCurrentState.w = w;
150 mCurrentState.h = h;
151 requestTransaction();
152 return true;
153}
154bool LayerBase::setAlpha(uint8_t alpha) {
155 if (mCurrentState.alpha == alpha)
156 return false;
157 mCurrentState.sequence++;
158 mCurrentState.alpha = alpha;
159 requestTransaction();
160 return true;
161}
162bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
163 // TODO: check the matrix has changed
164 mCurrentState.sequence++;
165 mCurrentState.transform.set(
166 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
167 requestTransaction();
168 return true;
169}
170bool LayerBase::setTransparentRegionHint(const Region& transparent) {
171 // TODO: check the region has changed
172 mCurrentState.sequence++;
173 mCurrentState.transparentRegion = transparent;
174 requestTransaction();
175 return true;
176}
177bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
178 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
179 if (mCurrentState.flags == newFlags)
180 return false;
181 mCurrentState.sequence++;
182 mCurrentState.flags = newFlags;
183 requestTransaction();
184 return true;
185}
186
187Rect LayerBase::visibleBounds() const
188{
189 return mTransformedBounds;
190}
191
192void LayerBase::setVisibleRegion(const Region& visibleRegion) {
193 // always called from main thread
194 visibleRegionScreen = visibleRegion;
195}
196
197void LayerBase::setCoveredRegion(const Region& coveredRegion) {
198 // always called from main thread
199 coveredRegionScreen = coveredRegion;
200}
201
202uint32_t LayerBase::doTransaction(uint32_t flags)
203{
204 const Layer::State& front(drawingState());
205 const Layer::State& temp(currentState());
206
207 if (temp.sequence != front.sequence) {
208 // invalidate and recompute the visible regions if needed
209 flags |= eVisibleRegion;
210 this->contentDirty = true;
211 }
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700212
213 if (temp.sequence != front.sequence) {
214 const bool linearFiltering = mUseLinearFiltering;
215 mUseLinearFiltering = false;
216 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
217 // we may use linear filtering, if the matrix scales us
218 const uint8_t type = temp.transform.getType();
219 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
220 mUseLinearFiltering = true;
221 }
222 }
223 }
224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 // Commit the transaction
226 commitTransaction(flags & eRestartTransaction);
227 return flags;
228}
229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230void LayerBase::validateVisibility(const Transform& planeTransform)
231{
232 const Layer::State& s(drawingState());
233 const Transform tr(planeTransform * s.transform);
234 const bool transformed = tr.transformed();
235
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700236 uint32_t w = s.w;
237 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 tr.transform(mVertices[0], 0, 0);
239 tr.transform(mVertices[1], 0, h);
240 tr.transform(mVertices[2], w, h);
241 tr.transform(mVertices[3], w, 0);
242 if (UNLIKELY(transformed)) {
243 // NOTE: here we could also punt if we have too many rectangles
244 // in the transparent region
245 if (tr.preserveRects()) {
246 // transform the transparent region
247 transparentRegionScreen = tr.transform(s.transparentRegion);
248 } else {
249 // transformation too complex, can't do the transparent region
250 // optimization.
251 transparentRegionScreen.clear();
252 }
253 } else {
254 transparentRegionScreen = s.transparentRegion;
255 }
256
257 // cache a few things...
258 mOrientation = tr.getOrientation();
259 mTransformedBounds = tr.makeBounds(w, h);
260 mTransformed = transformed;
261 mLeft = tr.tx();
262 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263}
264
265void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
266{
267}
268
269void LayerBase::unlockPageFlip(
270 const Transform& planeTransform, Region& outDirtyRegion)
271{
272 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
273 outDirtyRegion.orSelf(visibleRegionScreen);
274 }
275}
276
277void LayerBase::finishPageFlip()
278{
279}
280
281void LayerBase::invalidate()
282{
283 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
284 mFlinger->signalEvent();
285 }
286}
287
288void LayerBase::drawRegion(const Region& reg) const
289{
Mathias Agopian20f68782009-05-11 00:03:41 -0700290 Region::const_iterator it = reg.begin();
291 Region::const_iterator const end = reg.end();
292 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 Rect r;
294 const DisplayHardware& hw(graphicPlane(0).displayHardware());
295 const int32_t fbWidth = hw.getWidth();
296 const int32_t fbHeight = hw.getHeight();
297 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
298 { fbWidth, fbHeight }, { 0, fbHeight } };
299 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700300 while (it != end) {
301 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 const GLint sy = fbHeight - (r.top + r.height());
303 glScissor(r.left, sy, r.width(), r.height());
304 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
305 }
306 }
307}
308
309void LayerBase::draw(const Region& inClip) const
310{
311 // invalidate the region we'll update
312 Region clip(inClip); // copy-on-write, so no-op most of the time
313
314 // Remove the transparent area from the clipping region
315 const State& s = drawingState();
316 if (LIKELY(!s.transparentRegion.isEmpty())) {
317 clip.subtract(transparentRegionScreen);
318 if (clip.isEmpty()) {
319 // usually this won't happen because this should be taken care of
320 // by SurfaceFlinger::computeVisibleRegions()
321 return;
322 }
323 }
324
325 // reset GL state
326 glEnable(GL_SCISSOR_TEST);
327
328 onDraw(clip);
329
330 /*
331 glDisable(GL_TEXTURE_2D);
332 glDisable(GL_DITHER);
333 glEnable(GL_BLEND);
334 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
335 glColor4x(0, 0x8000, 0, 0x10000);
336 drawRegion(transparentRegionScreen);
337 glDisable(GL_BLEND);
338 */
339}
340
341GLuint LayerBase::createTexture() const
342{
343 GLuint textureName = -1;
344 glGenTextures(1, &textureName);
345 glBindTexture(GL_TEXTURE_2D, textureName);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
347 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 return textureName;
351}
352
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700353void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
354 GLclampx green, GLclampx blue,
355 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356{
357 const DisplayHardware& hw(graphicPlane(0).displayHardware());
358 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700359 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 glDisable(GL_TEXTURE_2D);
361 glDisable(GL_BLEND);
362 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700363
364 Region::const_iterator it = clip.begin();
365 Region::const_iterator const end = clip.end();
366 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 glEnable(GL_SCISSOR_TEST);
368 glVertexPointer(2, GL_FIXED, 0, mVertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700369 while (it != end) {
370 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 const GLint sy = fbHeight - (r.top + r.height());
372 glScissor(r.left, sy, r.width(), r.height());
373 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
374 }
375 }
376}
377
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700378void LayerBase::clearWithOpenGL(const Region& clip) const
379{
380 clearWithOpenGL(clip,0,0,0,0);
381}
382
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700383void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384{
385 const DisplayHardware& hw(graphicPlane(0).displayHardware());
386 const uint32_t fbHeight = hw.getHeight();
387 const State& s(drawingState());
Mathias Agopian0926f502009-05-04 14:17:04 -0700388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 // bind our texture
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700390 validateTexture(texture.name);
391 uint32_t width = texture.width;
392 uint32_t height = texture.height;
393
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 glEnable(GL_TEXTURE_2D);
395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396 if (UNLIKELY(s.alpha < 0xFF)) {
397 // We have an alpha-modulation. We need to modulate all
398 // texture components by alpha because we're always using
399 // premultiplied alpha.
400
401 // If the texture doesn't have an alpha channel we can
402 // use REPLACE and switch to non premultiplied alpha
403 // blending (SRCA/ONE_MINUS_SRCA).
404
405 GLenum env, src;
406 if (needsBlending()) {
407 env = GL_MODULATE;
408 src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
409 } else {
410 env = GL_REPLACE;
411 src = GL_SRC_ALPHA;
412 }
413 const GGLfixed alpha = (s.alpha << 16)/255;
414 glColor4x(alpha, alpha, alpha, alpha);
415 glEnable(GL_BLEND);
416 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
417 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
418 } else {
419 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
420 glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
421 if (needsBlending()) {
422 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
423 glEnable(GL_BLEND);
424 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
425 } else {
426 glDisable(GL_BLEND);
427 }
428 }
429
430 if (UNLIKELY(transformed()
431 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
432 {
433 //StopWatch watch("GL transformed");
Mathias Agopian20f68782009-05-11 00:03:41 -0700434 Region::const_iterator it = clip.begin();
435 Region::const_iterator const end = clip.end();
436 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 const GLfixed texCoords[4][2] = {
438 { 0, 0 },
439 { 0, 0x10000 },
440 { 0x10000, 0x10000 },
441 { 0x10000, 0 }
442 };
443
444 glMatrixMode(GL_TEXTURE);
445 glLoadIdentity();
446
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700447 // the texture's source is rotated
448 if (texture.transform == HAL_TRANSFORM_ROT_90) {
449 // TODO: handle the other orientations
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450 glTranslatef(0, 1, 0);
451 glRotatef(-90, 0, 0, 1);
452 }
453
Mathias Agopianf9cd64b2009-07-30 12:19:10 -0700454 if (!(mFlags & (DisplayHardware::NPOT_EXTENSION |
455 DisplayHardware::DIRECT_TEXTURE))) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 // find the smallest power-of-two that will accommodate our surface
Mathias Agopian0926f502009-05-04 14:17:04 -0700457 GLuint tw = 1 << (31 - clz(width));
458 GLuint th = 1 << (31 - clz(height));
459 if (tw < width) tw <<= 1;
460 if (th < height) th <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 // this divide should be relatively fast because it's
462 // a power-of-two (optimized path in libgcc)
Mathias Agopian0926f502009-05-04 14:17:04 -0700463 GLfloat ws = GLfloat(width) /tw;
464 GLfloat hs = GLfloat(height)/th;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465 glScalef(ws, hs, 1.0f);
466 }
467
468 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
469 glVertexPointer(2, GL_FIXED, 0, mVertices);
470 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
471
Mathias Agopian20f68782009-05-11 00:03:41 -0700472 while (it != end) {
473 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474 const GLint sy = fbHeight - (r.top + r.height());
475 glScissor(r.left, sy, r.width(), r.height());
476 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
477 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
479 }
480 } else {
Mathias Agopian20f68782009-05-11 00:03:41 -0700481 Region::const_iterator it = clip.begin();
482 Region::const_iterator const end = clip.end();
483 if (it != end) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700484 GLint crop[4] = { 0, height, width, -height };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
486 int x = tx();
487 int y = ty();
Mathias Agopian0926f502009-05-04 14:17:04 -0700488 y = fbHeight - (y + height);
Mathias Agopian20f68782009-05-11 00:03:41 -0700489 while (it != end) {
490 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 const GLint sy = fbHeight - (r.top + r.height());
492 glScissor(r.left, sy, r.width(), r.height());
Mathias Agopian0926f502009-05-04 14:17:04 -0700493 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 }
495 }
496 }
497}
498
499void LayerBase::validateTexture(GLint textureName) const
500{
501 glBindTexture(GL_TEXTURE_2D, textureName);
502 // TODO: reload the texture if needed
503 // this is currently done in loadTexture() below
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700504 if (mUseLinearFiltering) {
505 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
506 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
507 } else {
508 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
509 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
510 }
Mathias Agopian401c2572009-09-23 19:16:27 -0700511
512 if (needsDithering()) {
513 glEnable(GL_DITHER);
514 } else {
515 glDisable(GL_DITHER);
516 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517}
518
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700519void LayerBase::loadTexture(Texture* texture, GLint textureName,
520 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521{
522 // TODO: defer the actual texture reload until LayerBase::validateTexture
523 // is called.
524
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700525 texture->name = textureName;
526 GLuint& textureWidth(texture->width);
527 GLuint& textureHeight(texture->height);
528
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 uint32_t flags = mFlags;
530 glBindTexture(GL_TEXTURE_2D, textureName);
531
532 GLuint tw = t.width;
533 GLuint th = t.height;
534
535 /*
536 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
539 * need to do something reasonable (here creating a bigger texture).
540 *
541 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
542 *
543 * This situation doesn't happen often, but some h/w have a limitation
544 * for their framebuffer (eg: must be multiple of 8 pixels), and
545 * we need to take that into account when using these buffers as
546 * textures.
547 *
548 * This should never be a problem with POT textures
549 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700550
551 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
552 unpack = 1 << ((unpack > 3) ? 3 : unpack);
553 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
554
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 /*
556 * round to POT if needed
557 */
558
559 GLuint texture_w = tw;
560 GLuint texture_h = th;
561 if (!(flags & DisplayHardware::NPOT_EXTENSION)) {
562 // find the smallest power-of-two that will accommodate our surface
563 texture_w = 1 << (31 - clz(t.width));
564 texture_h = 1 << (31 - clz(t.height));
565 if (texture_w < t.width) texture_w <<= 1;
566 if (texture_h < t.height) texture_h <<= 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700568
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569regular:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570 Rect bounds(dirty.bounds());
571 GLvoid* data = 0;
572 if (texture_w!=textureWidth || texture_h!=textureHeight) {
573 // texture size changed, we need to create a new one
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700575 if (!textureWidth || !textureHeight) {
576 // this is the first time, load the whole texture
577 if (texture_w==tw && texture_h==th) {
578 // we can do it one pass
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 } else {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700581 // we have to create the texture first because it
582 // doesn't match the size of the buffer
583 bounds.set(Rect(tw, th));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586
587 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
588 glTexImage2D(GL_TEXTURE_2D, 0,
589 GL_RGB, texture_w, texture_h, 0,
590 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
591 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
592 glTexImage2D(GL_TEXTURE_2D, 0,
593 GL_RGBA, texture_w, texture_h, 0,
594 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700595 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
596 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700597 glTexImage2D(GL_TEXTURE_2D, 0,
598 GL_RGBA, texture_w, texture_h, 0,
599 GL_RGBA, GL_UNSIGNED_BYTE, data);
600 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
601 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
602 // just show the Y plane of YUV buffers
603 glTexImage2D(GL_TEXTURE_2D, 0,
604 GL_LUMINANCE, texture_w, texture_h, 0,
605 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
606 } else {
607 // oops, we don't handle this format!
608 LOGE("layer %p, texture=%d, using format %d, which is not "
609 "supported by the GL", this, textureName, t.format);
610 textureName = -1;
611 }
612 textureWidth = texture_w;
613 textureHeight = texture_h;
614 }
615 if (!data && textureName>=0) {
616 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
617 glTexSubImage2D(GL_TEXTURE_2D, 0,
618 0, bounds.top, t.width, bounds.height(),
619 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
620 t.data + bounds.top*t.stride*2);
621 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
622 glTexSubImage2D(GL_TEXTURE_2D, 0,
623 0, bounds.top, t.width, bounds.height(),
624 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
625 t.data + bounds.top*t.stride*2);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700626 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
627 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700628 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)
Mathias Agopian48d819a2009-09-10 19:41:18 -0700649 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700650 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700652 lcblk = new SharedBufferServer(
653 client->ctrlblk, i, NUM_BUFFERS,
654 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700655}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700657void LayerBaseClient::onFirstRef()
658{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700659 sp<Client> client(this->client.promote());
660 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700661 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 }
663}
664
665LayerBaseClient::~LayerBaseClient()
666{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700667 sp<Client> client(this->client.promote());
668 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 client->free(mIndex);
670 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700671 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672}
673
Mathias Agopianf9d93272009-06-19 17:00:27 -0700674int32_t LayerBaseClient::serverIndex() const
675{
676 sp<Client> client(this->client.promote());
677 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 return (client->cid<<16)|mIndex;
679 }
680 return 0xFFFF0000 | mIndex;
681}
682
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700683sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700685 sp<Surface> s;
686 Mutex::Autolock _l(mLock);
687 s = mClientSurface.promote();
688 if (s == 0) {
689 s = createSurface();
690 mClientSurface = s;
691 }
692 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693}
694
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700695sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
696{
Mathias Agopian9a112062009-04-17 19:36:26 -0700697 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700698 const_cast<LayerBaseClient *>(this));
699}
700
701// ---------------------------------------------------------------------------
702
Mathias Agopian9a112062009-04-17 19:36:26 -0700703LayerBaseClient::Surface::Surface(
704 const sp<SurfaceFlinger>& flinger,
705 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700706 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700707 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
708{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700709}
710
711
Mathias Agopian9a112062009-04-17 19:36:26 -0700712LayerBaseClient::Surface::~Surface()
713{
714 /*
715 * This is a good place to clean-up all client resources
716 */
717
718 // destroy client resources
719 sp<LayerBaseClient> layer = getOwner();
720 if (layer != 0) {
721 mFlinger->destroySurface(layer);
722 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700723}
724
725sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
726 sp<LayerBaseClient> owner(mOwner.promote());
727 return owner;
728}
729
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700730status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian375f5632009-06-15 18:24:59 -0700731 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700732{
733 switch (code) {
734 case REGISTER_BUFFERS:
735 case UNREGISTER_BUFFERS:
736 case CREATE_OVERLAY:
737 {
Mathias Agopian375f5632009-06-15 18:24:59 -0700738 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
739 IPCThreadState* ipc = IPCThreadState::self();
740 const int pid = ipc->getCallingPid();
741 const int uid = ipc->getCallingUid();
742 LOGE("Permission Denial: "
743 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
744 return PERMISSION_DENIED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700745 }
746 }
747 }
748 return BnSurface::onTransact(code, data, reply, flags);
749}
750
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700751sp<SurfaceBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700752{
753 return NULL;
754}
755
756status_t LayerBaseClient::Surface::registerBuffers(
757 const ISurface::BufferHeap& buffers)
758{
759 return INVALID_OPERATION;
760}
761
762void LayerBaseClient::Surface::postBuffer(ssize_t offset)
763{
764}
765
766void LayerBaseClient::Surface::unregisterBuffers()
767{
768}
769
770sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
771 uint32_t w, uint32_t h, int32_t format)
772{
773 return NULL;
774};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775
776// ---------------------------------------------------------------------------
777
778}; // namespace android