The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <utils/Errors.h> |
| 22 | #include <utils/Log.h> |
Mathias Agopian | 310f8da | 2009-05-22 01:27:01 -0700 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #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 | |
| 43 | namespace android { |
| 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | const uint32_t LayerBase::typeInfo = 1; |
| 48 | const char* const LayerBase::typeID = "LayerBase"; |
| 49 | |
| 50 | const uint32_t LayerBaseClient::typeInfo = LayerBase::typeInfo | 2; |
| 51 | const char* const LayerBaseClient::typeID = "LayerBaseClient"; |
| 52 | |
| 53 | // --------------------------------------------------------------------------- |
| 54 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display) |
| 56 | : dpy(display), contentDirty(false), |
| 57 | mFlinger(flinger), |
| 58 | mTransformed(false), |
| 59 | mOrientation(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | mTransactionFlags(0), |
| 61 | mPremultipliedAlpha(true), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | mInvalidate(0) |
| 63 | { |
| 64 | const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware()); |
| 65 | mFlags = hw.getFlags(); |
| 66 | } |
| 67 | |
| 68 | LayerBase::~LayerBase() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | const GraphicPlane& LayerBase::graphicPlane(int dpy) const |
| 73 | { |
| 74 | return mFlinger->graphicPlane(dpy); |
| 75 | } |
| 76 | |
| 77 | GraphicPlane& LayerBase::graphicPlane(int dpy) |
| 78 | { |
| 79 | return mFlinger->graphicPlane(dpy); |
| 80 | } |
| 81 | |
| 82 | void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags) |
| 83 | { |
| 84 | uint32_t layerFlags = 0; |
| 85 | if (flags & ISurfaceComposer::eHidden) |
| 86 | layerFlags = ISurfaceComposer::eLayerHidden; |
| 87 | |
| 88 | if (flags & ISurfaceComposer::eNonPremultiplied) |
| 89 | mPremultipliedAlpha = false; |
| 90 | |
| 91 | mCurrentState.z = 0; |
| 92 | mCurrentState.w = w; |
| 93 | mCurrentState.h = h; |
| 94 | mCurrentState.alpha = 0xFF; |
| 95 | mCurrentState.flags = layerFlags; |
| 96 | mCurrentState.sequence = 0; |
| 97 | mCurrentState.transform.set(0, 0); |
| 98 | |
| 99 | // drawing state & current state are identical |
| 100 | mDrawingState = mCurrentState; |
| 101 | } |
| 102 | |
| 103 | void LayerBase::commitTransaction(bool skipSize) { |
| 104 | const uint32_t w = mDrawingState.w; |
| 105 | const uint32_t h = mDrawingState.h; |
| 106 | mDrawingState = mCurrentState; |
| 107 | if (skipSize) { |
| 108 | mDrawingState.w = w; |
| 109 | mDrawingState.h = h; |
| 110 | } |
| 111 | } |
| 112 | void LayerBase::forceVisibilityTransaction() { |
| 113 | // this can be called without SurfaceFlinger.mStateLock, but if we |
| 114 | // can atomically increment the sequence number, it doesn't matter. |
| 115 | android_atomic_inc(&mCurrentState.sequence); |
| 116 | requestTransaction(); |
| 117 | } |
| 118 | bool LayerBase::requestTransaction() { |
| 119 | int32_t old = setTransactionFlags(eTransactionNeeded); |
| 120 | return ((old & eTransactionNeeded) == 0); |
| 121 | } |
| 122 | uint32_t LayerBase::getTransactionFlags(uint32_t flags) { |
| 123 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 124 | } |
| 125 | uint32_t LayerBase::setTransactionFlags(uint32_t flags) { |
| 126 | return android_atomic_or(flags, &mTransactionFlags); |
| 127 | } |
| 128 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | bool LayerBase::setPosition(int32_t x, int32_t y) { |
| 130 | if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y) |
| 131 | return false; |
| 132 | mCurrentState.sequence++; |
| 133 | mCurrentState.transform.set(x, y); |
| 134 | requestTransaction(); |
| 135 | return true; |
| 136 | } |
| 137 | bool LayerBase::setLayer(uint32_t z) { |
| 138 | if (mCurrentState.z == z) |
| 139 | return false; |
| 140 | mCurrentState.sequence++; |
| 141 | mCurrentState.z = z; |
| 142 | requestTransaction(); |
| 143 | return true; |
| 144 | } |
| 145 | bool LayerBase::setSize(uint32_t w, uint32_t h) { |
| 146 | if (mCurrentState.w == w && mCurrentState.h == h) |
| 147 | return false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | mCurrentState.w = w; |
| 149 | mCurrentState.h = h; |
| 150 | requestTransaction(); |
| 151 | return true; |
| 152 | } |
| 153 | bool LayerBase::setAlpha(uint8_t alpha) { |
| 154 | if (mCurrentState.alpha == alpha) |
| 155 | return false; |
| 156 | mCurrentState.sequence++; |
| 157 | mCurrentState.alpha = alpha; |
| 158 | requestTransaction(); |
| 159 | return true; |
| 160 | } |
| 161 | bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) { |
| 162 | // TODO: check the matrix has changed |
| 163 | mCurrentState.sequence++; |
| 164 | mCurrentState.transform.set( |
| 165 | matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy); |
| 166 | requestTransaction(); |
| 167 | return true; |
| 168 | } |
| 169 | bool LayerBase::setTransparentRegionHint(const Region& transparent) { |
| 170 | // TODO: check the region has changed |
| 171 | mCurrentState.sequence++; |
| 172 | mCurrentState.transparentRegion = transparent; |
| 173 | requestTransaction(); |
| 174 | return true; |
| 175 | } |
| 176 | bool LayerBase::setFlags(uint8_t flags, uint8_t mask) { |
| 177 | const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask); |
| 178 | if (mCurrentState.flags == newFlags) |
| 179 | return false; |
| 180 | mCurrentState.sequence++; |
| 181 | mCurrentState.flags = newFlags; |
| 182 | requestTransaction(); |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | Rect LayerBase::visibleBounds() const |
| 187 | { |
| 188 | return mTransformedBounds; |
| 189 | } |
| 190 | |
| 191 | void LayerBase::setVisibleRegion(const Region& visibleRegion) { |
| 192 | // always called from main thread |
| 193 | visibleRegionScreen = visibleRegion; |
| 194 | } |
| 195 | |
| 196 | void LayerBase::setCoveredRegion(const Region& coveredRegion) { |
| 197 | // always called from main thread |
| 198 | coveredRegionScreen = coveredRegion; |
| 199 | } |
| 200 | |
| 201 | uint32_t LayerBase::doTransaction(uint32_t flags) |
| 202 | { |
| 203 | const Layer::State& front(drawingState()); |
| 204 | const Layer::State& temp(currentState()); |
| 205 | |
| 206 | if (temp.sequence != front.sequence) { |
| 207 | // invalidate and recompute the visible regions if needed |
| 208 | flags |= eVisibleRegion; |
| 209 | this->contentDirty = true; |
| 210 | } |
| 211 | |
| 212 | // Commit the transaction |
| 213 | commitTransaction(flags & eRestartTransaction); |
| 214 | return flags; |
| 215 | } |
| 216 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | void LayerBase::validateVisibility(const Transform& planeTransform) |
| 218 | { |
| 219 | const Layer::State& s(drawingState()); |
| 220 | const Transform tr(planeTransform * s.transform); |
| 221 | const bool transformed = tr.transformed(); |
| 222 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 223 | uint32_t w = s.w; |
| 224 | uint32_t h = s.h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | tr.transform(mVertices[0], 0, 0); |
| 226 | tr.transform(mVertices[1], 0, h); |
| 227 | tr.transform(mVertices[2], w, h); |
| 228 | tr.transform(mVertices[3], w, 0); |
| 229 | if (UNLIKELY(transformed)) { |
| 230 | // NOTE: here we could also punt if we have too many rectangles |
| 231 | // in the transparent region |
| 232 | if (tr.preserveRects()) { |
| 233 | // transform the transparent region |
| 234 | transparentRegionScreen = tr.transform(s.transparentRegion); |
| 235 | } else { |
| 236 | // transformation too complex, can't do the transparent region |
| 237 | // optimization. |
| 238 | transparentRegionScreen.clear(); |
| 239 | } |
| 240 | } else { |
| 241 | transparentRegionScreen = s.transparentRegion; |
| 242 | } |
| 243 | |
| 244 | // cache a few things... |
| 245 | mOrientation = tr.getOrientation(); |
| 246 | mTransformedBounds = tr.makeBounds(w, h); |
| 247 | mTransformed = transformed; |
| 248 | mLeft = tr.tx(); |
| 249 | mTop = tr.ty(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void LayerBase::lockPageFlip(bool& recomputeVisibleRegions) |
| 253 | { |
| 254 | } |
| 255 | |
| 256 | void LayerBase::unlockPageFlip( |
| 257 | const Transform& planeTransform, Region& outDirtyRegion) |
| 258 | { |
| 259 | if ((android_atomic_and(~1, &mInvalidate)&1) == 1) { |
| 260 | outDirtyRegion.orSelf(visibleRegionScreen); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void LayerBase::finishPageFlip() |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | void LayerBase::invalidate() |
| 269 | { |
| 270 | if ((android_atomic_or(1, &mInvalidate)&1) == 0) { |
| 271 | mFlinger->signalEvent(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void LayerBase::drawRegion(const Region& reg) const |
| 276 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 277 | Region::const_iterator it = reg.begin(); |
| 278 | Region::const_iterator const end = reg.end(); |
| 279 | if (it != end) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | Rect r; |
| 281 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 282 | const int32_t fbWidth = hw.getWidth(); |
| 283 | const int32_t fbHeight = hw.getHeight(); |
| 284 | const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 }, |
| 285 | { fbWidth, fbHeight }, { 0, fbHeight } }; |
| 286 | glVertexPointer(2, GL_SHORT, 0, vertices); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 287 | while (it != end) { |
| 288 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | const GLint sy = fbHeight - (r.top + r.height()); |
| 290 | glScissor(r.left, sy, r.width(), r.height()); |
| 291 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void LayerBase::draw(const Region& inClip) const |
| 297 | { |
| 298 | // invalidate the region we'll update |
| 299 | Region clip(inClip); // copy-on-write, so no-op most of the time |
| 300 | |
| 301 | // Remove the transparent area from the clipping region |
| 302 | const State& s = drawingState(); |
| 303 | if (LIKELY(!s.transparentRegion.isEmpty())) { |
| 304 | clip.subtract(transparentRegionScreen); |
| 305 | if (clip.isEmpty()) { |
| 306 | // usually this won't happen because this should be taken care of |
| 307 | // by SurfaceFlinger::computeVisibleRegions() |
| 308 | return; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // reset GL state |
| 313 | glEnable(GL_SCISSOR_TEST); |
| 314 | |
| 315 | onDraw(clip); |
| 316 | |
| 317 | /* |
| 318 | glDisable(GL_TEXTURE_2D); |
| 319 | glDisable(GL_DITHER); |
| 320 | glEnable(GL_BLEND); |
| 321 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 322 | glColor4x(0, 0x8000, 0, 0x10000); |
| 323 | drawRegion(transparentRegionScreen); |
| 324 | glDisable(GL_BLEND); |
| 325 | */ |
| 326 | } |
| 327 | |
| 328 | GLuint LayerBase::createTexture() const |
| 329 | { |
| 330 | GLuint textureName = -1; |
| 331 | glGenTextures(1, &textureName); |
| 332 | glBindTexture(GL_TEXTURE_2D, textureName); |
| 333 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 334 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 335 | if (mFlags & DisplayHardware::SLOW_CONFIG) { |
| 336 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 337 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 338 | } else { |
| 339 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 340 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 341 | } |
| 342 | return textureName; |
| 343 | } |
| 344 | |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 345 | void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red, |
| 346 | GLclampx green, GLclampx blue, |
| 347 | GLclampx alpha) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | { |
| 349 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 350 | const uint32_t fbHeight = hw.getHeight(); |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 351 | glColor4x(red,green,blue,alpha); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | glDisable(GL_TEXTURE_2D); |
| 353 | glDisable(GL_BLEND); |
| 354 | glDisable(GL_DITHER); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 355 | |
| 356 | Region::const_iterator it = clip.begin(); |
| 357 | Region::const_iterator const end = clip.end(); |
| 358 | if (it != end) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | glEnable(GL_SCISSOR_TEST); |
| 360 | glVertexPointer(2, GL_FIXED, 0, mVertices); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 361 | while (it != end) { |
| 362 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | const GLint sy = fbHeight - (r.top + r.height()); |
| 364 | glScissor(r.left, sy, r.width(), r.height()); |
| 365 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 370 | void LayerBase::clearWithOpenGL(const Region& clip) const |
| 371 | { |
| 372 | clearWithOpenGL(clip,0,0,0,0); |
| 373 | } |
| 374 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 375 | void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | { |
| 377 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 378 | const uint32_t fbHeight = hw.getHeight(); |
| 379 | const State& s(drawingState()); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 380 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | // bind our texture |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 382 | validateTexture(texture.name); |
| 383 | uint32_t width = texture.width; |
| 384 | uint32_t height = texture.height; |
| 385 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | glEnable(GL_TEXTURE_2D); |
| 387 | |
| 388 | // Dithering... |
Mathias Agopian | 888eee6 | 2009-09-04 17:27:16 -0700 | [diff] [blame] | 389 | bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG); |
| 390 | if (fast || s.flags & ISurfaceComposer::eLayerDither) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | glEnable(GL_DITHER); |
| 392 | } else { |
| 393 | glDisable(GL_DITHER); |
| 394 | } |
| 395 | |
| 396 | 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 Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 434 | Region::const_iterator it = clip.begin(); |
| 435 | Region::const_iterator const end = clip.end(); |
| 436 | if (it != end) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | // always use high-quality filtering with fast configurations |
| 438 | bool fast = !(mFlags & DisplayHardware::SLOW_CONFIG); |
| 439 | if (!fast && s.flags & ISurfaceComposer::eLayerFilter) { |
| 440 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 441 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 442 | } |
| 443 | const GLfixed texCoords[4][2] = { |
| 444 | { 0, 0 }, |
| 445 | { 0, 0x10000 }, |
| 446 | { 0x10000, 0x10000 }, |
| 447 | { 0x10000, 0 } |
| 448 | }; |
| 449 | |
| 450 | glMatrixMode(GL_TEXTURE); |
| 451 | glLoadIdentity(); |
| 452 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 453 | // the texture's source is rotated |
| 454 | if (texture.transform == HAL_TRANSFORM_ROT_90) { |
| 455 | // TODO: handle the other orientations |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | glTranslatef(0, 1, 0); |
| 457 | glRotatef(-90, 0, 0, 1); |
| 458 | } |
| 459 | |
Mathias Agopian | f9cd64b | 2009-07-30 12:19:10 -0700 | [diff] [blame] | 460 | if (!(mFlags & (DisplayHardware::NPOT_EXTENSION | |
| 461 | DisplayHardware::DIRECT_TEXTURE))) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | // find the smallest power-of-two that will accommodate our surface |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 463 | GLuint tw = 1 << (31 - clz(width)); |
| 464 | GLuint th = 1 << (31 - clz(height)); |
| 465 | if (tw < width) tw <<= 1; |
| 466 | if (th < height) th <<= 1; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | // this divide should be relatively fast because it's |
| 468 | // a power-of-two (optimized path in libgcc) |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 469 | GLfloat ws = GLfloat(width) /tw; |
| 470 | GLfloat hs = GLfloat(height)/th; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | glScalef(ws, hs, 1.0f); |
| 472 | } |
| 473 | |
| 474 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 475 | glVertexPointer(2, GL_FIXED, 0, mVertices); |
| 476 | glTexCoordPointer(2, GL_FIXED, 0, texCoords); |
| 477 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 478 | while (it != end) { |
| 479 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | const GLint sy = fbHeight - (r.top + r.height()); |
| 481 | glScissor(r.left, sy, r.width(), r.height()); |
| 482 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 483 | } |
| 484 | |
| 485 | if (!fast && s.flags & ISurfaceComposer::eLayerFilter) { |
| 486 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 487 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 488 | } |
| 489 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 490 | } |
| 491 | } else { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 492 | Region::const_iterator it = clip.begin(); |
| 493 | Region::const_iterator const end = clip.end(); |
| 494 | if (it != end) { |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 495 | GLint crop[4] = { 0, height, width, -height }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); |
| 497 | int x = tx(); |
| 498 | int y = ty(); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 499 | y = fbHeight - (y + height); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 500 | while (it != end) { |
| 501 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | const GLint sy = fbHeight - (r.top + r.height()); |
| 503 | glScissor(r.left, sy, r.width(), r.height()); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 504 | glDrawTexiOES(x, y, 0, width, height); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | void LayerBase::validateTexture(GLint textureName) const |
| 511 | { |
| 512 | glBindTexture(GL_TEXTURE_2D, textureName); |
| 513 | // TODO: reload the texture if needed |
| 514 | // this is currently done in loadTexture() below |
| 515 | } |
| 516 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 517 | void LayerBase::loadTexture(Texture* texture, GLint textureName, |
| 518 | const Region& dirty, const GGLSurface& t) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | { |
| 520 | // TODO: defer the actual texture reload until LayerBase::validateTexture |
| 521 | // is called. |
| 522 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 523 | texture->name = textureName; |
| 524 | GLuint& textureWidth(texture->width); |
| 525 | GLuint& textureHeight(texture->height); |
| 526 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | uint32_t flags = mFlags; |
| 528 | glBindTexture(GL_TEXTURE_2D, textureName); |
| 529 | |
| 530 | GLuint tw = t.width; |
| 531 | GLuint th = t.height; |
| 532 | |
| 533 | /* |
| 534 | * In OpenGL ES we can't specify a stride with glTexImage2D (however, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 535 | * GL_UNPACK_ALIGNMENT is a limited form of stride). |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we |
| 537 | * need to do something reasonable (here creating a bigger texture). |
| 538 | * |
| 539 | * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT); |
| 540 | * |
| 541 | * This situation doesn't happen often, but some h/w have a limitation |
| 542 | * for their framebuffer (eg: must be multiple of 8 pixels), and |
| 543 | * we need to take that into account when using these buffers as |
| 544 | * textures. |
| 545 | * |
| 546 | * This should never be a problem with POT textures |
| 547 | */ |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 548 | |
| 549 | int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format)); |
| 550 | unpack = 1 << ((unpack > 3) ? 3 : unpack); |
| 551 | glPixelStorei(GL_UNPACK_ALIGNMENT, unpack); |
| 552 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | /* |
| 554 | * round to POT if needed |
| 555 | */ |
| 556 | |
| 557 | GLuint texture_w = tw; |
| 558 | GLuint texture_h = th; |
| 559 | if (!(flags & DisplayHardware::NPOT_EXTENSION)) { |
| 560 | // find the smallest power-of-two that will accommodate our surface |
| 561 | texture_w = 1 << (31 - clz(t.width)); |
| 562 | texture_h = 1 << (31 - clz(t.height)); |
| 563 | if (texture_w < t.width) texture_w <<= 1; |
| 564 | if (texture_h < t.height) texture_h <<= 1; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 566 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | regular: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 568 | Rect bounds(dirty.bounds()); |
| 569 | GLvoid* data = 0; |
| 570 | if (texture_w!=textureWidth || texture_h!=textureHeight) { |
| 571 | // texture size changed, we need to create a new one |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 573 | if (!textureWidth || !textureHeight) { |
| 574 | // this is the first time, load the whole texture |
| 575 | if (texture_w==tw && texture_h==th) { |
| 576 | // we can do it one pass |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | data = t.data; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | } else { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 579 | // we have to create the texture first because it |
| 580 | // doesn't match the size of the buffer |
| 581 | bounds.set(Rect(tw, th)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 584 | |
| 585 | if (t.format == GGL_PIXEL_FORMAT_RGB_565) { |
| 586 | glTexImage2D(GL_TEXTURE_2D, 0, |
| 587 | GL_RGB, texture_w, texture_h, 0, |
| 588 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data); |
| 589 | } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) { |
| 590 | glTexImage2D(GL_TEXTURE_2D, 0, |
| 591 | GL_RGBA, texture_w, texture_h, 0, |
| 592 | GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data); |
| 593 | } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) { |
| 594 | glTexImage2D(GL_TEXTURE_2D, 0, |
| 595 | GL_RGBA, texture_w, texture_h, 0, |
| 596 | GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 597 | } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP || |
| 598 | t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) { |
| 599 | // just show the Y plane of YUV buffers |
| 600 | glTexImage2D(GL_TEXTURE_2D, 0, |
| 601 | GL_LUMINANCE, texture_w, texture_h, 0, |
| 602 | GL_LUMINANCE, GL_UNSIGNED_BYTE, data); |
| 603 | } else { |
| 604 | // oops, we don't handle this format! |
| 605 | LOGE("layer %p, texture=%d, using format %d, which is not " |
| 606 | "supported by the GL", this, textureName, t.format); |
| 607 | textureName = -1; |
| 608 | } |
| 609 | textureWidth = texture_w; |
| 610 | textureHeight = texture_h; |
| 611 | } |
| 612 | if (!data && textureName>=0) { |
| 613 | if (t.format == GGL_PIXEL_FORMAT_RGB_565) { |
| 614 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 615 | 0, bounds.top, t.width, bounds.height(), |
| 616 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, |
| 617 | t.data + bounds.top*t.stride*2); |
| 618 | } else if (t.format == GGL_PIXEL_FORMAT_RGBA_4444) { |
| 619 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 620 | 0, bounds.top, t.width, bounds.height(), |
| 621 | GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, |
| 622 | t.data + bounds.top*t.stride*2); |
| 623 | } else if (t.format == GGL_PIXEL_FORMAT_RGBA_8888) { |
| 624 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 625 | 0, bounds.top, t.width, bounds.height(), |
| 626 | GL_RGBA, GL_UNSIGNED_BYTE, |
| 627 | t.data + bounds.top*t.stride*4); |
| 628 | } else if ( t.format == GGL_PIXEL_FORMAT_YCbCr_422_SP || |
| 629 | t.format == GGL_PIXEL_FORMAT_YCbCr_420_SP) { |
| 630 | // just show the Y plane of YUV buffers |
| 631 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 632 | 0, bounds.top, t.width, bounds.height(), |
| 633 | GL_LUMINANCE, GL_UNSIGNED_BYTE, |
| 634 | t.data + bounds.top*t.stride); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | // --------------------------------------------------------------------------- |
| 640 | |
Mathias Agopian | 2e12324 | 2009-06-23 20:06:46 -0700 | [diff] [blame] | 641 | int32_t LayerBaseClient::sIdentity = 0; |
| 642 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display, |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 644 | const sp<Client>& client, int32_t i) |
| 645 | : LayerBase(flinger, display), client(client), |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 646 | mIndex(i), mIdentity(uint32_t(android_atomic_inc(&sIdentity))) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 648 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 650 | void LayerBaseClient::onFirstRef() |
| 651 | { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 652 | sp<Client> client(this->client.promote()); |
| 653 | if (client != 0) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 654 | client->bindLayer(this, mIndex); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 655 | // Initialize this layer's identity |
| 656 | client->ctrlblk->setIdentity(mIndex, mIdentity); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | LayerBaseClient::~LayerBaseClient() |
| 661 | { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 662 | sp<Client> client(this->client.promote()); |
| 663 | if (client != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | client->free(mIndex); |
| 665 | } |
| 666 | } |
| 667 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 668 | int32_t LayerBaseClient::serverIndex() const |
| 669 | { |
| 670 | sp<Client> client(this->client.promote()); |
| 671 | if (client != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | return (client->cid<<16)|mIndex; |
| 673 | } |
| 674 | return 0xFFFF0000 | mIndex; |
| 675 | } |
| 676 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 677 | sp<LayerBaseClient::Surface> LayerBaseClient::getSurface() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 679 | sp<Surface> s; |
| 680 | Mutex::Autolock _l(mLock); |
| 681 | s = mClientSurface.promote(); |
| 682 | if (s == 0) { |
| 683 | s = createSurface(); |
| 684 | mClientSurface = s; |
| 685 | } |
| 686 | return s; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 689 | sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const |
| 690 | { |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 691 | return new Surface(mFlinger, clientIndex(), mIdentity, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 692 | const_cast<LayerBaseClient *>(this)); |
| 693 | } |
| 694 | |
| 695 | // --------------------------------------------------------------------------- |
| 696 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 697 | LayerBaseClient::Surface::Surface( |
| 698 | const sp<SurfaceFlinger>& flinger, |
| 699 | SurfaceID id, int identity, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 700 | const sp<LayerBaseClient>& owner) |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 701 | : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner) |
| 702 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 706 | LayerBaseClient::Surface::~Surface() |
| 707 | { |
| 708 | /* |
| 709 | * This is a good place to clean-up all client resources |
| 710 | */ |
| 711 | |
| 712 | // destroy client resources |
| 713 | sp<LayerBaseClient> layer = getOwner(); |
| 714 | if (layer != 0) { |
| 715 | mFlinger->destroySurface(layer); |
| 716 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const { |
| 720 | sp<LayerBaseClient> owner(mOwner.promote()); |
| 721 | return owner; |
| 722 | } |
| 723 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 724 | status_t LayerBaseClient::Surface::onTransact( |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 725 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 726 | { |
| 727 | switch (code) { |
| 728 | case REGISTER_BUFFERS: |
| 729 | case UNREGISTER_BUFFERS: |
| 730 | case CREATE_OVERLAY: |
| 731 | { |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 732 | if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) { |
| 733 | IPCThreadState* ipc = IPCThreadState::self(); |
| 734 | const int pid = ipc->getCallingPid(); |
| 735 | const int uid = ipc->getCallingUid(); |
| 736 | LOGE("Permission Denial: " |
| 737 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
| 738 | return PERMISSION_DENIED; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | } |
| 742 | return BnSurface::onTransact(code, data, reply, flags); |
| 743 | } |
| 744 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 745 | sp<SurfaceBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 746 | { |
| 747 | return NULL; |
| 748 | } |
| 749 | |
| 750 | status_t LayerBaseClient::Surface::registerBuffers( |
| 751 | const ISurface::BufferHeap& buffers) |
| 752 | { |
| 753 | return INVALID_OPERATION; |
| 754 | } |
| 755 | |
| 756 | void LayerBaseClient::Surface::postBuffer(ssize_t offset) |
| 757 | { |
| 758 | } |
| 759 | |
| 760 | void LayerBaseClient::Surface::unregisterBuffers() |
| 761 | { |
| 762 | } |
| 763 | |
| 764 | sp<OverlayRef> LayerBaseClient::Surface::createOverlay( |
| 765 | uint32_t w, uint32_t h, int32_t format) |
| 766 | { |
| 767 | return NULL; |
| 768 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | |
| 770 | // --------------------------------------------------------------------------- |
| 771 | |
| 772 | }; // namespace android |