blob: b376e72aa15fe8cbf0290bb7fba67b4a211378a0 [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
Mathias Agopian7e4a5872009-09-29 22:39:22 -070086 mCurrentState.z = 0;
87 mCurrentState.w = w;
88 mCurrentState.h = h;
89 mCurrentState.requested_w = w;
90 mCurrentState.requested_h = h;
91 mCurrentState.alpha = 0xFF;
92 mCurrentState.flags = layerFlags;
93 mCurrentState.sequence = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 mCurrentState.transform.set(0, 0);
95
96 // drawing state & current state are identical
97 mDrawingState = mCurrentState;
98}
99
Mathias Agopianba6be542009-09-29 22:32:36 -0700100void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102}
103void LayerBase::forceVisibilityTransaction() {
104 // this can be called without SurfaceFlinger.mStateLock, but if we
105 // can atomically increment the sequence number, it doesn't matter.
106 android_atomic_inc(&mCurrentState.sequence);
107 requestTransaction();
108}
109bool LayerBase::requestTransaction() {
110 int32_t old = setTransactionFlags(eTransactionNeeded);
111 return ((old & eTransactionNeeded) == 0);
112}
113uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
114 return android_atomic_and(~flags, &mTransactionFlags) & flags;
115}
116uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
117 return android_atomic_or(flags, &mTransactionFlags);
118}
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120bool LayerBase::setPosition(int32_t x, int32_t y) {
121 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
122 return false;
123 mCurrentState.sequence++;
124 mCurrentState.transform.set(x, y);
125 requestTransaction();
126 return true;
127}
128bool LayerBase::setLayer(uint32_t z) {
129 if (mCurrentState.z == z)
130 return false;
131 mCurrentState.sequence++;
132 mCurrentState.z = z;
133 requestTransaction();
134 return true;
135}
136bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700137 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 return false;
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700139 mCurrentState.requested_w = w;
140 mCurrentState.requested_h = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 requestTransaction();
142 return true;
143}
144bool LayerBase::setAlpha(uint8_t alpha) {
145 if (mCurrentState.alpha == alpha)
146 return false;
147 mCurrentState.sequence++;
148 mCurrentState.alpha = alpha;
149 requestTransaction();
150 return true;
151}
152bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
153 // TODO: check the matrix has changed
154 mCurrentState.sequence++;
155 mCurrentState.transform.set(
156 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
157 requestTransaction();
158 return true;
159}
160bool LayerBase::setTransparentRegionHint(const Region& transparent) {
161 // TODO: check the region has changed
162 mCurrentState.sequence++;
163 mCurrentState.transparentRegion = transparent;
164 requestTransaction();
165 return true;
166}
167bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
168 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
169 if (mCurrentState.flags == newFlags)
170 return false;
171 mCurrentState.sequence++;
172 mCurrentState.flags = newFlags;
173 requestTransaction();
174 return true;
175}
176
177Rect LayerBase::visibleBounds() const
178{
179 return mTransformedBounds;
180}
181
182void LayerBase::setVisibleRegion(const Region& visibleRegion) {
183 // always called from main thread
184 visibleRegionScreen = visibleRegion;
185}
186
187void LayerBase::setCoveredRegion(const Region& coveredRegion) {
188 // always called from main thread
189 coveredRegionScreen = coveredRegion;
190}
191
192uint32_t LayerBase::doTransaction(uint32_t flags)
193{
194 const Layer::State& front(drawingState());
195 const Layer::State& temp(currentState());
196
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700197 if ((front.requested_w != temp.requested_w) ||
198 (front.requested_h != temp.requested_h)) {
199 // resize the layer, set the physical size to the requested size
200 Layer::State& editTemp(currentState());
201 editTemp.w = temp.requested_w;
202 editTemp.h = temp.requested_h;
203 }
204
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700205 if ((front.w != temp.w) || (front.h != temp.h)) {
206 // invalidate and recompute the visible regions if needed
207 flags |= Layer::eVisibleRegion;
208 this->contentDirty = true;
209 }
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 if (temp.sequence != front.sequence) {
212 // invalidate and recompute the visible regions if needed
213 flags |= eVisibleRegion;
214 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700215
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700216 const bool linearFiltering = mUseLinearFiltering;
217 mUseLinearFiltering = false;
218 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
219 // we may use linear filtering, if the matrix scales us
220 const uint8_t type = temp.transform.getType();
221 if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) {
222 mUseLinearFiltering = true;
223 }
224 }
225 }
226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700228 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 return flags;
230}
231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232void LayerBase::validateVisibility(const Transform& planeTransform)
233{
234 const Layer::State& s(drawingState());
235 const Transform tr(planeTransform * s.transform);
236 const bool transformed = tr.transformed();
237
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700238 uint32_t w = s.w;
239 uint32_t h = s.h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 tr.transform(mVertices[0], 0, 0);
241 tr.transform(mVertices[1], 0, h);
242 tr.transform(mVertices[2], w, h);
243 tr.transform(mVertices[3], w, 0);
244 if (UNLIKELY(transformed)) {
245 // NOTE: here we could also punt if we have too many rectangles
246 // in the transparent region
247 if (tr.preserveRects()) {
248 // transform the transparent region
249 transparentRegionScreen = tr.transform(s.transparentRegion);
250 } else {
251 // transformation too complex, can't do the transparent region
252 // optimization.
253 transparentRegionScreen.clear();
254 }
255 } else {
256 transparentRegionScreen = s.transparentRegion;
257 }
258
259 // cache a few things...
260 mOrientation = tr.getOrientation();
261 mTransformedBounds = tr.makeBounds(w, h);
262 mTransformed = transformed;
263 mLeft = tr.tx();
264 mTop = tr.ty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265}
266
267void LayerBase::lockPageFlip(bool& recomputeVisibleRegions)
268{
269}
270
271void LayerBase::unlockPageFlip(
272 const Transform& planeTransform, Region& outDirtyRegion)
273{
274 if ((android_atomic_and(~1, &mInvalidate)&1) == 1) {
275 outDirtyRegion.orSelf(visibleRegionScreen);
276 }
277}
278
279void LayerBase::finishPageFlip()
280{
281}
282
283void LayerBase::invalidate()
284{
285 if ((android_atomic_or(1, &mInvalidate)&1) == 0) {
286 mFlinger->signalEvent();
287 }
288}
289
290void LayerBase::drawRegion(const Region& reg) const
291{
Mathias Agopian20f68782009-05-11 00:03:41 -0700292 Region::const_iterator it = reg.begin();
293 Region::const_iterator const end = reg.end();
294 if (it != end) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 Rect r;
296 const DisplayHardware& hw(graphicPlane(0).displayHardware());
297 const int32_t fbWidth = hw.getWidth();
298 const int32_t fbHeight = hw.getHeight();
299 const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 },
300 { fbWidth, fbHeight }, { 0, fbHeight } };
301 glVertexPointer(2, GL_SHORT, 0, vertices);
Mathias Agopian20f68782009-05-11 00:03:41 -0700302 while (it != end) {
303 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 const GLint sy = fbHeight - (r.top + r.height());
305 glScissor(r.left, sy, r.width(), r.height());
306 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
307 }
308 }
309}
310
311void LayerBase::draw(const Region& inClip) const
312{
313 // invalidate the region we'll update
314 Region clip(inClip); // copy-on-write, so no-op most of the time
315
316 // Remove the transparent area from the clipping region
317 const State& s = drawingState();
318 if (LIKELY(!s.transparentRegion.isEmpty())) {
319 clip.subtract(transparentRegionScreen);
320 if (clip.isEmpty()) {
321 // usually this won't happen because this should be taken care of
322 // by SurfaceFlinger::computeVisibleRegions()
323 return;
324 }
325 }
326
327 // reset GL state
328 glEnable(GL_SCISSOR_TEST);
329
330 onDraw(clip);
331
332 /*
333 glDisable(GL_TEXTURE_2D);
334 glDisable(GL_DITHER);
335 glEnable(GL_BLEND);
336 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
337 glColor4x(0, 0x8000, 0, 0x10000);
338 drawRegion(transparentRegionScreen);
339 glDisable(GL_BLEND);
340 */
341}
342
343GLuint LayerBase::createTexture() const
344{
345 GLuint textureName = -1;
346 glGenTextures(1, &textureName);
347 glBindTexture(GL_TEXTURE_2D, textureName);
348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700350 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
351 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 return textureName;
353}
354
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700355void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red,
356 GLclampx green, GLclampx blue,
357 GLclampx alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358{
359 const DisplayHardware& hw(graphicPlane(0).displayHardware());
360 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700361 glColor4x(red,green,blue,alpha);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 glDisable(GL_TEXTURE_2D);
363 glDisable(GL_BLEND);
364 glDisable(GL_DITHER);
Mathias Agopian20f68782009-05-11 00:03:41 -0700365
366 Region::const_iterator it = clip.begin();
367 Region::const_iterator const end = clip.end();
Mathias Agopian95a666b2009-09-24 14:57:26 -0700368 glEnable(GL_SCISSOR_TEST);
369 glVertexPointer(2, GL_FIXED, 0, mVertices);
370 while (it != end) {
371 const Rect& r = *it++;
372 const GLint sy = fbHeight - (r.top + r.height());
373 glScissor(r.left, sy, r.width(), r.height());
374 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 }
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
Mathias Agopian95a666b2009-09-24 14:57:26 -0700430 Region::const_iterator it = clip.begin();
431 Region::const_iterator const end = clip.end();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 if (UNLIKELY(transformed()
433 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
434 {
435 //StopWatch watch("GL transformed");
Mathias Agopian95a666b2009-09-24 14:57:26 -0700436 const GLfixed texCoords[4][2] = {
437 { 0, 0 },
438 { 0, 0x10000 },
439 { 0x10000, 0x10000 },
440 { 0x10000, 0 }
441 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442
Mathias Agopian95a666b2009-09-24 14:57:26 -0700443 glMatrixMode(GL_TEXTURE);
444 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445
Mathias Agopian95a666b2009-09-24 14:57:26 -0700446 // the texture's source is rotated
447 if (texture.transform == HAL_TRANSFORM_ROT_90) {
448 // TODO: handle the other orientations
449 glTranslatef(0, 1, 0);
450 glRotatef(-90, 0, 0, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700452
453 if (texture.NPOTAdjust) {
454 glScalef(texture.wScale, texture.hScale, 1.0f);
Mathias Agopian95a666b2009-09-24 14:57:26 -0700455 }
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 Agopian3330b202009-10-05 17:07:12 -0700503void LayerBase::loadTexture(Texture* texture,
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700504 const Region& dirty, const GGLSurface& t) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505{
Mathias Agopian3330b202009-10-05 17:07:12 -0700506 if (texture->name == -1U) {
507 // uh?
508 return;
509 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700510
Mathias Agopian3330b202009-10-05 17:07:12 -0700511 glBindTexture(GL_TEXTURE_2D, texture->name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512
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 */
Mathias Agopian3330b202009-10-05 17:07:12 -0700536 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
537 texture->NPOTAdjust = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539
Mathias Agopian3330b202009-10-05 17:07:12 -0700540 if (texture->NPOTAdjust) {
541 // find the smallest power-of-two that will accommodate our surface
542 texture->potWidth = 1 << (31 - clz(t.width));
543 texture->potHeight = 1 << (31 - clz(t.height));
544 if (texture->potWidth < t.width) texture->potWidth <<= 1;
545 if (texture->potHeight < t.height) texture->potHeight <<= 1;
546 texture->wScale = float(t.width) / texture->potWidth;
547 texture->hScale = float(t.height) / texture->potHeight;
548 } else {
549 texture->potWidth = t.width;
550 texture->potHeight = t.height;
551 }
552
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553 Rect bounds(dirty.bounds());
554 GLvoid* data = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700555 if (texture->width != t.width || texture->height != t.height) {
556 texture->width = t.width;
557 texture->height = t.height;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558
Mathias Agopian3330b202009-10-05 17:07:12 -0700559 // texture size changed, we need to create a new one
560 bounds.set(Rect(t.width, t.height));
561 if (t.width == texture->potWidth &&
562 t.height == texture->potHeight) {
563 // we can do it one pass
564 data = t.data;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700566
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
568 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700569 GL_RGB, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
571 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
572 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700573 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700575 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
576 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700578 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579 GL_RGBA, GL_UNSIGNED_BYTE, data);
580 } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP ||
581 t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) {
582 // just show the Y plane of YUV buffers
583 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian3330b202009-10-05 17:07:12 -0700584 GL_LUMINANCE, texture->potWidth, texture->potHeight, 0,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
586 } else {
587 // oops, we don't handle this format!
588 LOGE("layer %p, texture=%d, using format %d, which is not "
Mathias Agopian3330b202009-10-05 17:07:12 -0700589 "supported by the GL", this, texture->name, t.format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700591 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700592 if (!data) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700593 if (t.format == GGL_PIXEL_FORMAT_RGB_565) {
594 glTexSubImage2D(GL_TEXTURE_2D, 0,
595 0, bounds.top, t.width, bounds.height(),
596 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
597 t.data + bounds.top*t.stride*2);
598 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) {
599 glTexSubImage2D(GL_TEXTURE_2D, 0,
600 0, bounds.top, t.width, bounds.height(),
601 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
602 t.data + bounds.top*t.stride*2);
Mathias Agopian816d7d02009-09-14 18:10:30 -0700603 } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888 ||
604 t.format == GGL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605 glTexSubImage2D(GL_TEXTURE_2D, 0,
606 0, bounds.top, t.width, bounds.height(),
607 GL_RGBA, GL_UNSIGNED_BYTE,
608 t.data + bounds.top*t.stride*4);
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 glTexSubImage2D(GL_TEXTURE_2D, 0,
613 0, bounds.top, t.width, bounds.height(),
614 GL_LUMINANCE, GL_UNSIGNED_BYTE,
615 t.data + bounds.top*t.stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 }
617 }
618}
619
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620// ---------------------------------------------------------------------------
621
Mathias Agopian2e123242009-06-23 20:06:46 -0700622int32_t LayerBaseClient::sIdentity = 0;
623
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700625 const sp<Client>& client, int32_t i)
Mathias Agopian48d819a2009-09-10 19:41:18 -0700626 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700627 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700629 lcblk = new SharedBufferServer(
630 client->ctrlblk, i, NUM_BUFFERS,
631 mIdentity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700632}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700634void LayerBaseClient::onFirstRef()
635{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700636 sp<Client> client(this->client.promote());
637 if (client != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700638 client->bindLayer(this, mIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 }
640}
641
642LayerBaseClient::~LayerBaseClient()
643{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700644 sp<Client> client(this->client.promote());
645 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 client->free(mIndex);
647 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700648 delete lcblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649}
650
Mathias Agopianf9d93272009-06-19 17:00:27 -0700651int32_t LayerBaseClient::serverIndex() const
652{
653 sp<Client> client(this->client.promote());
654 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 return (client->cid<<16)|mIndex;
656 }
657 return 0xFFFF0000 | mIndex;
658}
659
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700660sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700662 sp<Surface> s;
663 Mutex::Autolock _l(mLock);
664 s = mClientSurface.promote();
665 if (s == 0) {
666 s = createSurface();
667 mClientSurface = s;
668 }
669 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670}
671
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700672sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
673{
Mathias Agopian9a112062009-04-17 19:36:26 -0700674 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700675 const_cast<LayerBaseClient *>(this));
676}
677
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700678// called with SurfaceFlinger::mStateLock as soon as the layer is entered
679// in the purgatory list
680void LayerBaseClient::onRemoved()
681{
682 // wake up the condition
683 lcblk->setStatus(NO_INIT);
684}
685
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700686// ---------------------------------------------------------------------------
687
Mathias Agopian9a112062009-04-17 19:36:26 -0700688LayerBaseClient::Surface::Surface(
689 const sp<SurfaceFlinger>& flinger,
690 SurfaceID id, int identity,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700691 const sp<LayerBaseClient>& owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700692 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
693{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700694}
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 Agopian3330b202009-10-05 17:07:12 -0700735sp<GraphicBuffer> 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