blob: 5b96e9dc834fa954530eb1c46084f4066f862697 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopian947f4f42009-05-22 01:27:01 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080033#include "SurfaceFlinger.h"
34#include "DisplayHardware/DisplayHardware.h"
35
36
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080049LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display)
50 : dpy(display), contentDirty(false),
51 mFlinger(flinger),
52 mTransformed(false),
Mathias Agopian44cac132009-09-23 18:34:53 -070053 mUseLinearFiltering(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 mOrientation(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 mTransactionFlags(0),
56 mPremultipliedAlpha(true),
The Android Open Source Project9066cfe2009-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 Agopiane1b6f242009-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 Project9066cfe2009-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 Agopian88516172009-09-29 22:32:36 -0700100void LayerBase::commitTransaction() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 mDrawingState = mCurrentState;
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopiane1b6f242009-09-29 22:39:22 -0700137 if (mCurrentState.requested_w == w && mCurrentState.requested_h == h)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 return false;
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700139 mCurrentState.requested_w = w;
140 mCurrentState.requested_h = h;
The Android Open Source Project9066cfe2009-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 Agopiane1b6f242009-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 Agopian70cab912009-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 Project9066cfe2009-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 Agopian44cac132009-09-23 18:34:53 -0700215
Mathias Agopian44cac132009-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 Project9066cfe2009-03-03 19:31:44 -0800227 // Commit the transaction
Mathias Agopian88516172009-09-29 22:32:36 -0700228 commitTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 return flags;
230}
231
The Android Open Source Project9066cfe2009-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 Agopian9779b2212009-09-07 16:32:45 -0700238 uint32_t w = s.w;
239 uint32_t h = s.h;
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopian6158b1b2009-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 Project9066cfe2009-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 Agopian6158b1b2009-05-11 00:03:41 -0700302 while (it != end) {
303 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-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 Agopian44cac132009-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 Project9066cfe2009-03-03 19:31:44 -0800352 return textureName;
353}
354
Rebecca Schultz Zavinc8546782009-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 Project9066cfe2009-03-03 19:31:44 -0800358{
359 const DisplayHardware& hw(graphicPlane(0).displayHardware());
360 const uint32_t fbHeight = hw.getHeight();
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700361 glColor4x(red,green,blue,alpha);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 glDisable(GL_TEXTURE_2D);
363 glDisable(GL_BLEND);
364 glDisable(GL_DITHER);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700365
366 Region::const_iterator it = clip.begin();
367 Region::const_iterator const end = clip.end();
Mathias Agopianf2d28b72009-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 Project9066cfe2009-03-03 19:31:44 -0800375 }
376}
377
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700378void LayerBase::clearWithOpenGL(const Region& clip) const
379{
380 clearWithOpenGL(clip,0,0,0,0);
381}
382
Mathias Agopian999543b2009-06-23 18:08:22 -0700383void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const
The Android Open Source Project9066cfe2009-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 Agopiandff8e582009-05-04 14:17:04 -0700388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 // bind our texture
Mathias Agopian999543b2009-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 Project9066cfe2009-03-03 19:31:44 -0800394 glEnable(GL_TEXTURE_2D);
395
The Android Open Source Project9066cfe2009-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 Agopianf2d28b72009-09-24 14:57:26 -0700430 Region::const_iterator it = clip.begin();
431 Region::const_iterator const end = clip.end();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 if (UNLIKELY(transformed()
433 || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) ))
434 {
435 //StopWatch watch("GL transformed");
Mathias Agopianf2d28b72009-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 Project9066cfe2009-03-03 19:31:44 -0800442
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700443 glMatrixMode(GL_TEXTURE);
444 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700446 // the texture's source is rotated
Chih-Chung Changac127dc2010-01-22 16:30:39 -0800447 switch (texture.transform) {
448 case HAL_TRANSFORM_ROT_90:
449 glTranslatef(0, 1, 0);
450 glRotatef(-90, 0, 0, 1);
451 break;
452 case HAL_TRANSFORM_ROT_180:
453 glTranslatef(1, 1, 0);
454 glRotatef(-180, 0, 0, 1);
455 break;
456 case HAL_TRANSFORM_ROT_270:
457 glTranslatef(1, 0, 0);
458 glRotatef(-270, 0, 0, 1);
459 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
Chih-Chung Changac127dc2010-01-22 16:30:39 -0800461
Mathias Agopian6950e422009-10-05 17:07:12 -0700462 if (texture.NPOTAdjust) {
463 glScalef(texture.wScale, texture.hScale, 1.0f);
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700464 }
465
466 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
467 glVertexPointer(2, GL_FIXED, 0, mVertices);
468 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
469
470 while (it != end) {
471 const Rect& r = *it++;
472 const GLint sy = fbHeight - (r.top + r.height());
473 glScissor(r.left, sy, r.width(), r.height());
474 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
475 }
476 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700478 GLint crop[4] = { 0, height, width, -height };
479 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
480 int x = tx();
481 int y = ty();
482 y = fbHeight - (y + height);
483 while (it != end) {
484 const Rect& r = *it++;
485 const GLint sy = fbHeight - (r.top + r.height());
486 glScissor(r.left, sy, r.width(), r.height());
487 glDrawTexiOES(x, y, 0, width, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489 }
490}
491
492void LayerBase::validateTexture(GLint textureName) const
493{
494 glBindTexture(GL_TEXTURE_2D, textureName);
495 // TODO: reload the texture if needed
496 // this is currently done in loadTexture() below
Mathias Agopian44cac132009-09-23 18:34:53 -0700497 if (mUseLinearFiltering) {
498 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
499 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
500 } else {
501 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
502 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
503 }
Mathias Agopiancc934762009-09-23 19:16:27 -0700504
505 if (needsDithering()) {
506 glEnable(GL_DITHER);
507 } else {
508 glDisable(GL_DITHER);
509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510}
511
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800512bool LayerBase::isSupportedYuvFormat(int format) const
513{
514 switch (format) {
515 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
516 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
517 case HAL_PIXEL_FORMAT_YCbCr_422_P:
518 case HAL_PIXEL_FORMAT_YCbCr_420_P:
519 case HAL_PIXEL_FORMAT_YCbCr_422_I:
520 case HAL_PIXEL_FORMAT_YCbCr_420_I:
521 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
522 return true;
523 }
524 return false;
525}
526
Mathias Agopian6950e422009-10-05 17:07:12 -0700527void LayerBase::loadTexture(Texture* texture,
Mathias Agopian999543b2009-06-23 18:08:22 -0700528 const Region& dirty, const GGLSurface& t) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529{
Mathias Agopian6950e422009-10-05 17:07:12 -0700530 if (texture->name == -1U) {
531 // uh?
532 return;
533 }
Mathias Agopian382e17d2009-10-21 16:27:21 -0700534
Mathias Agopian6950e422009-10-05 17:07:12 -0700535 glBindTexture(GL_TEXTURE_2D, texture->name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536
537 /*
538 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
Mathias Agopian1473f462009-04-10 14:24:30 -0700539 * GL_UNPACK_ALIGNMENT is a limited form of stride).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
541 * need to do something reasonable (here creating a bigger texture).
542 *
543 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
544 *
545 * This situation doesn't happen often, but some h/w have a limitation
546 * for their framebuffer (eg: must be multiple of 8 pixels), and
547 * we need to take that into account when using these buffers as
548 * textures.
549 *
550 * This should never be a problem with POT textures
551 */
Mathias Agopian1473f462009-04-10 14:24:30 -0700552
553 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
554 unpack = 1 << ((unpack > 3) ? 3 : unpack);
555 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 /*
558 * round to POT if needed
559 */
Mathias Agopian6950e422009-10-05 17:07:12 -0700560 if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) {
561 texture->NPOTAdjust = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700563
Mathias Agopian6950e422009-10-05 17:07:12 -0700564 if (texture->NPOTAdjust) {
565 // find the smallest power-of-two that will accommodate our surface
566 texture->potWidth = 1 << (31 - clz(t.width));
567 texture->potHeight = 1 << (31 - clz(t.height));
568 if (texture->potWidth < t.width) texture->potWidth <<= 1;
569 if (texture->potHeight < t.height) texture->potHeight <<= 1;
570 texture->wScale = float(t.width) / texture->potWidth;
571 texture->hScale = float(t.height) / texture->potHeight;
572 } else {
573 texture->potWidth = t.width;
574 texture->potHeight = t.height;
575 }
576
Mathias Agopian1473f462009-04-10 14:24:30 -0700577 Rect bounds(dirty.bounds());
578 GLvoid* data = 0;
Mathias Agopian6950e422009-10-05 17:07:12 -0700579 if (texture->width != t.width || texture->height != t.height) {
580 texture->width = t.width;
581 texture->height = t.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582
Mathias Agopian6950e422009-10-05 17:07:12 -0700583 // texture size changed, we need to create a new one
584 bounds.set(Rect(t.width, t.height));
585 if (t.width == texture->potWidth &&
586 t.height == texture->potHeight) {
587 // we can do it one pass
588 data = t.data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700590
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800591 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700592 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian6950e422009-10-05 17:07:12 -0700593 GL_RGB, texture->potWidth, texture->potHeight, 0,
Mathias Agopian1473f462009-04-10 14:24:30 -0700594 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800595 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700596 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian6950e422009-10-05 17:07:12 -0700597 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian1473f462009-04-10 14:24:30 -0700598 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800599 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
600 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700601 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian6950e422009-10-05 17:07:12 -0700602 GL_RGBA, texture->potWidth, texture->potHeight, 0,
Mathias Agopian1473f462009-04-10 14:24:30 -0700603 GL_RGBA, GL_UNSIGNED_BYTE, data);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800604 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700605 // just show the Y plane of YUV buffers
606 glTexImage2D(GL_TEXTURE_2D, 0,
Mathias Agopian6950e422009-10-05 17:07:12 -0700607 GL_LUMINANCE, texture->potWidth, texture->potHeight, 0,
Mathias Agopian1473f462009-04-10 14:24:30 -0700608 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
609 } else {
610 // oops, we don't handle this format!
611 LOGE("layer %p, texture=%d, using format %d, which is not "
Mathias Agopian6950e422009-10-05 17:07:12 -0700612 "supported by the GL", this, texture->name, t.format);
Mathias Agopian1473f462009-04-10 14:24:30 -0700613 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700614 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700615 if (!data) {
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800616 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700617 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);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800621 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700622 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 Agopian8f2423e2010-02-16 17:33:37 -0800626 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
627 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
Mathias Agopian1473f462009-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);
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800632 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700633 // just show the Y plane of YUV buffers
634 glTexSubImage2D(GL_TEXTURE_2D, 0,
635 0, bounds.top, t.width, bounds.height(),
636 GL_LUMINANCE, GL_UNSIGNED_BYTE,
637 t.data + bounds.top*t.stride);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639 }
640}
641
Mathias Agopian9042b452009-10-26 20:12:37 -0700642status_t LayerBase::initializeEglImage(
643 const sp<GraphicBuffer>& buffer, Texture* texture)
644{
645 status_t err = NO_ERROR;
646
647 // we need to recreate the texture
648 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
649
650 // free the previous image
651 if (texture->image != EGL_NO_IMAGE_KHR) {
652 eglDestroyImageKHR(dpy, texture->image);
653 texture->image = EGL_NO_IMAGE_KHR;
654 }
655
656 // construct an EGL_NATIVE_BUFFER_ANDROID
657 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
658
659 // create the new EGLImageKHR
660 const EGLint attrs[] = {
661 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
662 EGL_NONE, EGL_NONE
663 };
664 texture->image = eglCreateImageKHR(
665 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
666 (EGLClientBuffer)clientBuf, attrs);
667
668 LOGE_IF(texture->image == EGL_NO_IMAGE_KHR,
669 "eglCreateImageKHR() failed. err=0x%4x",
670 eglGetError());
671
672 if (texture->image != EGL_NO_IMAGE_KHR) {
673 glBindTexture(GL_TEXTURE_2D, texture->name);
674 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
675 (GLeglImageOES)texture->image);
676 GLint error = glGetError();
677 if (UNLIKELY(error != GL_NO_ERROR)) {
678 // this failed, for instance, because we don't support NPOT.
679 // FIXME: do something!
680 LOGE("layer=%p, glEGLImageTargetTexture2DOES(%p) "
681 "failed err=0x%04x",
682 this, texture->image, error);
683 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
684 err = INVALID_OPERATION;
685 } else {
686 // Everything went okay!
687 texture->NPOTAdjust = false;
688 texture->dirty = false;
689 texture->width = clientBuf->width;
690 texture->height = clientBuf->height;
691 }
692 } else {
693 err = INVALID_OPERATION;
694 }
695 return err;
696}
697
698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699// ---------------------------------------------------------------------------
700
Mathias Agopianc6603952009-06-23 20:06:46 -0700701int32_t LayerBaseClient::sIdentity = 0;
702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700704 const sp<Client>& client, int32_t i)
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700705 : LayerBase(flinger, display), lcblk(NULL), client(client),
Mathias Agopian9779b2212009-09-07 16:32:45 -0700706 mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707{
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700708 lcblk = new SharedBufferServer(
709 client->ctrlblk, i, NUM_BUFFERS,
710 mIdentity);
Mathias Agopian1473f462009-04-10 14:24:30 -0700711}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712
Mathias Agopian1473f462009-04-10 14:24:30 -0700713void LayerBaseClient::onFirstRef()
714{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700715 sp<Client> client(this->client.promote());
716 if (client != 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700717 client->bindLayer(this, mIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
719}
720
721LayerBaseClient::~LayerBaseClient()
722{
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700723 sp<Client> client(this->client.promote());
724 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 client->free(mIndex);
726 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700727 delete lcblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728}
729
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700730int32_t LayerBaseClient::serverIndex() const
731{
732 sp<Client> client(this->client.promote());
733 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 return (client->cid<<16)|mIndex;
735 }
736 return 0xFFFF0000 | mIndex;
737}
738
Mathias Agopian1473f462009-04-10 14:24:30 -0700739sp<LayerBaseClient::Surface> LayerBaseClient::getSurface()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740{
Mathias Agopian1473f462009-04-10 14:24:30 -0700741 sp<Surface> s;
742 Mutex::Autolock _l(mLock);
743 s = mClientSurface.promote();
744 if (s == 0) {
745 s = createSurface();
746 mClientSurface = s;
747 }
748 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749}
750
Mathias Agopian1473f462009-04-10 14:24:30 -0700751sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const
752{
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700753 return new Surface(mFlinger, clientIndex(), mIdentity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700754 const_cast<LayerBaseClient *>(this));
755}
756
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700757// called with SurfaceFlinger::mStateLock as soon as the layer is entered
758// in the purgatory list
759void LayerBaseClient::onRemoved()
760{
761 // wake up the condition
762 lcblk->setStatus(NO_INIT);
763}
764
Mathias Agopian1473f462009-04-10 14:24:30 -0700765// ---------------------------------------------------------------------------
766
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700767LayerBaseClient::Surface::Surface(
768 const sp<SurfaceFlinger>& flinger,
769 SurfaceID id, int identity,
Mathias Agopian1473f462009-04-10 14:24:30 -0700770 const sp<LayerBaseClient>& owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700771 : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner)
772{
Mathias Agopian1473f462009-04-10 14:24:30 -0700773}
774
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700775LayerBaseClient::Surface::~Surface()
776{
777 /*
778 * This is a good place to clean-up all client resources
779 */
780
781 // destroy client resources
782 sp<LayerBaseClient> layer = getOwner();
783 if (layer != 0) {
784 mFlinger->destroySurface(layer);
785 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700786}
787
788sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const {
789 sp<LayerBaseClient> owner(mOwner.promote());
790 return owner;
791}
792
Mathias Agopian1473f462009-04-10 14:24:30 -0700793status_t LayerBaseClient::Surface::onTransact(
Mathias Agopian151e8592009-06-15 18:24:59 -0700794 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
Mathias Agopian1473f462009-04-10 14:24:30 -0700795{
796 switch (code) {
797 case REGISTER_BUFFERS:
798 case UNREGISTER_BUFFERS:
799 case CREATE_OVERLAY:
800 {
Mathias Agopian151e8592009-06-15 18:24:59 -0700801 if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) {
802 IPCThreadState* ipc = IPCThreadState::self();
803 const int pid = ipc->getCallingPid();
804 const int uid = ipc->getCallingUid();
805 LOGE("Permission Denial: "
806 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
807 return PERMISSION_DENIED;
Mathias Agopian1473f462009-04-10 14:24:30 -0700808 }
809 }
810 }
811 return BnSurface::onTransact(code, data, reply, flags);
812}
813
Mathias Agopian6950e422009-10-05 17:07:12 -0700814sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700815{
816 return NULL;
817}
818
819status_t LayerBaseClient::Surface::registerBuffers(
820 const ISurface::BufferHeap& buffers)
821{
822 return INVALID_OPERATION;
823}
824
825void LayerBaseClient::Surface::postBuffer(ssize_t offset)
826{
827}
828
829void LayerBaseClient::Surface::unregisterBuffers()
830{
831}
832
833sp<OverlayRef> LayerBaseClient::Surface::createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800834 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
Mathias Agopian1473f462009-04-10 14:24:30 -0700835{
836 return NULL;
837};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838
839// ---------------------------------------------------------------------------
840
841}; // namespace android