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 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | |
| 39 | // --------------------------------------------------------------------------- |
| 40 | |
| 41 | const uint32_t LayerBase::typeInfo = 1; |
| 42 | const char* const LayerBase::typeID = "LayerBase"; |
| 43 | |
| 44 | const uint32_t LayerBaseClient::typeInfo = LayerBase::typeInfo | 2; |
| 45 | const char* const LayerBaseClient::typeID = "LayerBaseClient"; |
| 46 | |
| 47 | // --------------------------------------------------------------------------- |
| 48 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display) |
| 50 | : dpy(display), contentDirty(false), |
| 51 | mFlinger(flinger), |
| 52 | mTransformed(false), |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 53 | mUseLinearFiltering(false), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | mOrientation(0), |
Mathias Agopian | ca6fab2 | 2010-02-19 17:51:58 -0800 | [diff] [blame] | 55 | mLeft(0), mTop(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | mTransactionFlags(0), |
Mathias Agopian | d129659 | 2010-03-09 19:17:47 -0800 | [diff] [blame] | 57 | mPremultipliedAlpha(true), mDebug(false), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | mInvalidate(0) |
| 59 | { |
| 60 | const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware()); |
| 61 | mFlags = hw.getFlags(); |
| 62 | } |
| 63 | |
| 64 | LayerBase::~LayerBase() |
| 65 | { |
| 66 | } |
| 67 | |
Mathias Agopian | d129659 | 2010-03-09 19:17:47 -0800 | [diff] [blame] | 68 | void LayerBase::setName(const String8& name) { |
| 69 | mName = name; |
| 70 | } |
| 71 | |
| 72 | String8 LayerBase::getName() const { |
| 73 | return mName; |
| 74 | } |
| 75 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | const GraphicPlane& LayerBase::graphicPlane(int dpy) const |
| 77 | { |
| 78 | return mFlinger->graphicPlane(dpy); |
| 79 | } |
| 80 | |
| 81 | GraphicPlane& LayerBase::graphicPlane(int dpy) |
| 82 | { |
| 83 | return mFlinger->graphicPlane(dpy); |
| 84 | } |
| 85 | |
| 86 | void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags) |
| 87 | { |
| 88 | uint32_t layerFlags = 0; |
| 89 | if (flags & ISurfaceComposer::eHidden) |
| 90 | layerFlags = ISurfaceComposer::eLayerHidden; |
| 91 | |
| 92 | if (flags & ISurfaceComposer::eNonPremultiplied) |
| 93 | mPremultipliedAlpha = false; |
| 94 | |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 95 | mCurrentState.z = 0; |
| 96 | mCurrentState.w = w; |
| 97 | mCurrentState.h = h; |
| 98 | mCurrentState.requested_w = w; |
| 99 | mCurrentState.requested_h = h; |
| 100 | mCurrentState.alpha = 0xFF; |
| 101 | mCurrentState.flags = layerFlags; |
| 102 | mCurrentState.sequence = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | mCurrentState.transform.set(0, 0); |
| 104 | |
| 105 | // drawing state & current state are identical |
| 106 | mDrawingState = mCurrentState; |
| 107 | } |
| 108 | |
Mathias Agopian | ba6be54 | 2009-09-29 22:32:36 -0700 | [diff] [blame] | 109 | void LayerBase::commitTransaction() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | mDrawingState = mCurrentState; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 146 | if (mCurrentState.requested_w == w && mCurrentState.requested_h == h) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | return false; |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 148 | mCurrentState.requested_w = w; |
| 149 | mCurrentState.requested_h = h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 206 | if ((front.requested_w != temp.requested_w) || |
| 207 | (front.requested_h != temp.requested_h)) { |
| 208 | // resize the layer, set the physical size to the requested size |
| 209 | Layer::State& editTemp(currentState()); |
| 210 | editTemp.w = temp.requested_w; |
| 211 | editTemp.h = temp.requested_h; |
| 212 | } |
| 213 | |
Mathias Agopian | 6656dbc | 2009-09-30 12:48:47 -0700 | [diff] [blame] | 214 | if ((front.w != temp.w) || (front.h != temp.h)) { |
| 215 | // invalidate and recompute the visible regions if needed |
| 216 | flags |= Layer::eVisibleRegion; |
Mathias Agopian | 6656dbc | 2009-09-30 12:48:47 -0700 | [diff] [blame] | 217 | } |
| 218 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | if (temp.sequence != front.sequence) { |
| 220 | // invalidate and recompute the visible regions if needed |
| 221 | flags |= eVisibleRegion; |
| 222 | this->contentDirty = true; |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 223 | |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 224 | const bool linearFiltering = mUseLinearFiltering; |
| 225 | mUseLinearFiltering = false; |
| 226 | if (!(mFlags & DisplayHardware::SLOW_CONFIG)) { |
| 227 | // we may use linear filtering, if the matrix scales us |
| 228 | const uint8_t type = temp.transform.getType(); |
| 229 | if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) { |
| 230 | mUseLinearFiltering = true; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | // Commit the transaction |
Mathias Agopian | ba6be54 | 2009-09-29 22:32:36 -0700 | [diff] [blame] | 236 | commitTransaction(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | return flags; |
| 238 | } |
| 239 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | void LayerBase::validateVisibility(const Transform& planeTransform) |
| 241 | { |
| 242 | const Layer::State& s(drawingState()); |
| 243 | const Transform tr(planeTransform * s.transform); |
| 244 | const bool transformed = tr.transformed(); |
| 245 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 246 | uint32_t w = s.w; |
| 247 | uint32_t h = s.h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | tr.transform(mVertices[0], 0, 0); |
| 249 | tr.transform(mVertices[1], 0, h); |
| 250 | tr.transform(mVertices[2], w, h); |
| 251 | tr.transform(mVertices[3], w, 0); |
| 252 | if (UNLIKELY(transformed)) { |
| 253 | // NOTE: here we could also punt if we have too many rectangles |
| 254 | // in the transparent region |
| 255 | if (tr.preserveRects()) { |
| 256 | // transform the transparent region |
| 257 | transparentRegionScreen = tr.transform(s.transparentRegion); |
| 258 | } else { |
| 259 | // transformation too complex, can't do the transparent region |
| 260 | // optimization. |
| 261 | transparentRegionScreen.clear(); |
| 262 | } |
| 263 | } else { |
| 264 | transparentRegionScreen = s.transparentRegion; |
| 265 | } |
| 266 | |
| 267 | // cache a few things... |
| 268 | mOrientation = tr.getOrientation(); |
| 269 | mTransformedBounds = tr.makeBounds(w, h); |
| 270 | mTransformed = transformed; |
| 271 | mLeft = tr.tx(); |
| 272 | mTop = tr.ty(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void LayerBase::lockPageFlip(bool& recomputeVisibleRegions) |
| 276 | { |
| 277 | } |
| 278 | |
| 279 | void LayerBase::unlockPageFlip( |
| 280 | const Transform& planeTransform, Region& outDirtyRegion) |
| 281 | { |
| 282 | if ((android_atomic_and(~1, &mInvalidate)&1) == 1) { |
| 283 | outDirtyRegion.orSelf(visibleRegionScreen); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void LayerBase::finishPageFlip() |
| 288 | { |
| 289 | } |
| 290 | |
| 291 | void LayerBase::invalidate() |
| 292 | { |
| 293 | if ((android_atomic_or(1, &mInvalidate)&1) == 0) { |
| 294 | mFlinger->signalEvent(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void LayerBase::drawRegion(const Region& reg) const |
| 299 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 300 | Region::const_iterator it = reg.begin(); |
| 301 | Region::const_iterator const end = reg.end(); |
| 302 | if (it != end) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | Rect r; |
| 304 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 305 | const int32_t fbWidth = hw.getWidth(); |
| 306 | const int32_t fbHeight = hw.getHeight(); |
| 307 | const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 }, |
| 308 | { fbWidth, fbHeight }, { 0, fbHeight } }; |
| 309 | glVertexPointer(2, GL_SHORT, 0, vertices); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 310 | while (it != end) { |
| 311 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | const GLint sy = fbHeight - (r.top + r.height()); |
| 313 | glScissor(r.left, sy, r.width(), r.height()); |
| 314 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void LayerBase::draw(const Region& inClip) const |
| 320 | { |
| 321 | // invalidate the region we'll update |
| 322 | Region clip(inClip); // copy-on-write, so no-op most of the time |
| 323 | |
| 324 | // Remove the transparent area from the clipping region |
| 325 | const State& s = drawingState(); |
| 326 | if (LIKELY(!s.transparentRegion.isEmpty())) { |
| 327 | clip.subtract(transparentRegionScreen); |
| 328 | if (clip.isEmpty()) { |
| 329 | // usually this won't happen because this should be taken care of |
| 330 | // by SurfaceFlinger::computeVisibleRegions() |
| 331 | return; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // reset GL state |
| 336 | glEnable(GL_SCISSOR_TEST); |
| 337 | |
| 338 | onDraw(clip); |
| 339 | |
| 340 | /* |
| 341 | glDisable(GL_TEXTURE_2D); |
| 342 | glDisable(GL_DITHER); |
| 343 | glEnable(GL_BLEND); |
| 344 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 345 | glColor4x(0, 0x8000, 0, 0x10000); |
| 346 | drawRegion(transparentRegionScreen); |
| 347 | glDisable(GL_BLEND); |
| 348 | */ |
| 349 | } |
| 350 | |
| 351 | GLuint LayerBase::createTexture() const |
| 352 | { |
| 353 | GLuint textureName = -1; |
| 354 | glGenTextures(1, &textureName); |
| 355 | glBindTexture(GL_TEXTURE_2D, textureName); |
| 356 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 357 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 358 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 359 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | return textureName; |
| 361 | } |
| 362 | |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 363 | void LayerBase::clearWithOpenGL(const Region& clip, GLclampx red, |
| 364 | GLclampx green, GLclampx blue, |
| 365 | GLclampx alpha) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | { |
| 367 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 368 | const uint32_t fbHeight = hw.getHeight(); |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 369 | glColor4x(red,green,blue,alpha); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | glDisable(GL_TEXTURE_2D); |
| 371 | glDisable(GL_BLEND); |
| 372 | glDisable(GL_DITHER); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 373 | |
| 374 | Region::const_iterator it = clip.begin(); |
| 375 | Region::const_iterator const end = clip.end(); |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 376 | glEnable(GL_SCISSOR_TEST); |
| 377 | glVertexPointer(2, GL_FIXED, 0, mVertices); |
| 378 | while (it != end) { |
| 379 | const Rect& r = *it++; |
| 380 | const GLint sy = fbHeight - (r.top + r.height()); |
| 381 | glScissor(r.left, sy, r.width(), r.height()); |
| 382 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 386 | void LayerBase::clearWithOpenGL(const Region& clip) const |
| 387 | { |
| 388 | clearWithOpenGL(clip,0,0,0,0); |
| 389 | } |
| 390 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 391 | 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] | 392 | { |
| 393 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 394 | const uint32_t fbHeight = hw.getHeight(); |
| 395 | const State& s(drawingState()); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 396 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | // bind our texture |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 398 | validateTexture(texture.name); |
| 399 | uint32_t width = texture.width; |
| 400 | uint32_t height = texture.height; |
| 401 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | glEnable(GL_TEXTURE_2D); |
| 403 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | if (UNLIKELY(s.alpha < 0xFF)) { |
| 405 | // We have an alpha-modulation. We need to modulate all |
| 406 | // texture components by alpha because we're always using |
| 407 | // premultiplied alpha. |
| 408 | |
| 409 | // If the texture doesn't have an alpha channel we can |
| 410 | // use REPLACE and switch to non premultiplied alpha |
| 411 | // blending (SRCA/ONE_MINUS_SRCA). |
| 412 | |
| 413 | GLenum env, src; |
| 414 | if (needsBlending()) { |
| 415 | env = GL_MODULATE; |
| 416 | src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA; |
| 417 | } else { |
| 418 | env = GL_REPLACE; |
| 419 | src = GL_SRC_ALPHA; |
| 420 | } |
| 421 | const GGLfixed alpha = (s.alpha << 16)/255; |
| 422 | glColor4x(alpha, alpha, alpha, alpha); |
| 423 | glEnable(GL_BLEND); |
| 424 | glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA); |
| 425 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env); |
| 426 | } else { |
| 427 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 428 | glColor4x(0x10000, 0x10000, 0x10000, 0x10000); |
| 429 | if (needsBlending()) { |
| 430 | GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA; |
| 431 | glEnable(GL_BLEND); |
| 432 | glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA); |
| 433 | } else { |
| 434 | glDisable(GL_BLEND); |
| 435 | } |
| 436 | } |
| 437 | |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 438 | Region::const_iterator it = clip.begin(); |
| 439 | Region::const_iterator const end = clip.end(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | if (UNLIKELY(transformed() |
| 441 | || !(mFlags & DisplayHardware::DRAW_TEXTURE_EXTENSION) )) |
| 442 | { |
| 443 | //StopWatch watch("GL transformed"); |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 444 | const GLfixed texCoords[4][2] = { |
| 445 | { 0, 0 }, |
| 446 | { 0, 0x10000 }, |
| 447 | { 0x10000, 0x10000 }, |
| 448 | { 0x10000, 0 } |
| 449 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 451 | glMatrixMode(GL_TEXTURE); |
| 452 | glLoadIdentity(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 454 | // the texture's source is rotated |
Chih-Chung Chang | 5994a33 | 2010-01-22 16:30:39 -0800 | [diff] [blame] | 455 | switch (texture.transform) { |
| 456 | case HAL_TRANSFORM_ROT_90: |
| 457 | glTranslatef(0, 1, 0); |
| 458 | glRotatef(-90, 0, 0, 1); |
| 459 | break; |
| 460 | case HAL_TRANSFORM_ROT_180: |
| 461 | glTranslatef(1, 1, 0); |
| 462 | glRotatef(-180, 0, 0, 1); |
| 463 | break; |
| 464 | case HAL_TRANSFORM_ROT_270: |
| 465 | glTranslatef(1, 0, 0); |
| 466 | glRotatef(-270, 0, 0, 1); |
| 467 | break; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | } |
Chih-Chung Chang | 5994a33 | 2010-01-22 16:30:39 -0800 | [diff] [blame] | 469 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 470 | if (texture.NPOTAdjust) { |
| 471 | glScalef(texture.wScale, texture.hScale, 1.0f); |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 475 | glVertexPointer(2, GL_FIXED, 0, mVertices); |
| 476 | glTexCoordPointer(2, GL_FIXED, 0, texCoords); |
| 477 | |
| 478 | while (it != end) { |
| 479 | const Rect& r = *it++; |
| 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 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | } else { |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 486 | GLint crop[4] = { 0, height, width, -height }; |
| 487 | glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); |
| 488 | int x = tx(); |
| 489 | int y = ty(); |
| 490 | y = fbHeight - (y + height); |
| 491 | while (it != end) { |
| 492 | const Rect& r = *it++; |
| 493 | const GLint sy = fbHeight - (r.top + r.height()); |
| 494 | glScissor(r.left, sy, r.width(), r.height()); |
| 495 | glDrawTexiOES(x, y, 0, width, height); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void LayerBase::validateTexture(GLint textureName) const |
| 501 | { |
| 502 | glBindTexture(GL_TEXTURE_2D, textureName); |
| 503 | // TODO: reload the texture if needed |
| 504 | // this is currently done in loadTexture() below |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 505 | if (mUseLinearFiltering) { |
| 506 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 507 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 508 | } else { |
| 509 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 510 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 511 | } |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 512 | |
| 513 | if (needsDithering()) { |
| 514 | glEnable(GL_DITHER); |
| 515 | } else { |
| 516 | glDisable(GL_DITHER); |
| 517 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 520 | bool LayerBase::isSupportedYuvFormat(int format) const |
| 521 | { |
| 522 | switch (format) { |
| 523 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 524 | case HAL_PIXEL_FORMAT_YCbCr_420_SP: |
| 525 | case HAL_PIXEL_FORMAT_YCbCr_422_P: |
| 526 | case HAL_PIXEL_FORMAT_YCbCr_420_P: |
| 527 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 528 | case HAL_PIXEL_FORMAT_YCbCr_420_I: |
| 529 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 530 | return true; |
| 531 | } |
| 532 | return false; |
| 533 | } |
| 534 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 535 | void LayerBase::loadTexture(Texture* texture, |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 536 | const Region& dirty, const GGLSurface& t) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 538 | if (texture->name == -1U) { |
| 539 | // uh? |
| 540 | return; |
| 541 | } |
Mathias Agopian | 57720c3 | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 542 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 543 | glBindTexture(GL_TEXTURE_2D, texture->name); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | |
| 545 | /* |
| 546 | * In OpenGL ES we can't specify a stride with glTexImage2D (however, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 547 | * GL_UNPACK_ALIGNMENT is a limited form of stride). |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we |
| 549 | * need to do something reasonable (here creating a bigger texture). |
| 550 | * |
| 551 | * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT); |
| 552 | * |
| 553 | * This situation doesn't happen often, but some h/w have a limitation |
| 554 | * for their framebuffer (eg: must be multiple of 8 pixels), and |
| 555 | * we need to take that into account when using these buffers as |
| 556 | * textures. |
| 557 | * |
| 558 | * This should never be a problem with POT textures |
| 559 | */ |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 560 | |
| 561 | int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format)); |
| 562 | unpack = 1 << ((unpack > 3) ? 3 : unpack); |
| 563 | glPixelStorei(GL_UNPACK_ALIGNMENT, unpack); |
| 564 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | /* |
| 566 | * round to POT if needed |
| 567 | */ |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 568 | if (!(mFlags & DisplayHardware::NPOT_EXTENSION)) { |
| 569 | texture->NPOTAdjust = true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 571 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 572 | if (texture->NPOTAdjust) { |
| 573 | // find the smallest power-of-two that will accommodate our surface |
| 574 | texture->potWidth = 1 << (31 - clz(t.width)); |
| 575 | texture->potHeight = 1 << (31 - clz(t.height)); |
| 576 | if (texture->potWidth < t.width) texture->potWidth <<= 1; |
| 577 | if (texture->potHeight < t.height) texture->potHeight <<= 1; |
| 578 | texture->wScale = float(t.width) / texture->potWidth; |
| 579 | texture->hScale = float(t.height) / texture->potHeight; |
| 580 | } else { |
| 581 | texture->potWidth = t.width; |
| 582 | texture->potHeight = t.height; |
| 583 | } |
| 584 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 585 | Rect bounds(dirty.bounds()); |
| 586 | GLvoid* data = 0; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 587 | if (texture->width != t.width || texture->height != t.height) { |
| 588 | texture->width = t.width; |
| 589 | texture->height = t.height; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 591 | // texture size changed, we need to create a new one |
| 592 | bounds.set(Rect(t.width, t.height)); |
| 593 | if (t.width == texture->potWidth && |
| 594 | t.height == texture->potHeight) { |
| 595 | // we can do it one pass |
| 596 | data = t.data; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 598 | |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 599 | if (t.format == HAL_PIXEL_FORMAT_RGB_565) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 600 | glTexImage2D(GL_TEXTURE_2D, 0, |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 601 | GL_RGB, texture->potWidth, texture->potHeight, 0, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 602 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data); |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 603 | } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 604 | glTexImage2D(GL_TEXTURE_2D, 0, |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 605 | GL_RGBA, texture->potWidth, texture->potHeight, 0, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 606 | GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data); |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 607 | } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 || |
| 608 | t.format == HAL_PIXEL_FORMAT_RGBX_8888) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 609 | glTexImage2D(GL_TEXTURE_2D, 0, |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 610 | GL_RGBA, texture->potWidth, texture->potHeight, 0, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 611 | GL_RGBA, GL_UNSIGNED_BYTE, data); |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 612 | } else if (isSupportedYuvFormat(t.format)) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 613 | // just show the Y plane of YUV buffers |
| 614 | glTexImage2D(GL_TEXTURE_2D, 0, |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 615 | GL_LUMINANCE, texture->potWidth, texture->potHeight, 0, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 616 | GL_LUMINANCE, GL_UNSIGNED_BYTE, data); |
| 617 | } else { |
| 618 | // oops, we don't handle this format! |
| 619 | LOGE("layer %p, texture=%d, using format %d, which is not " |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 620 | "supported by the GL", this, texture->name, t.format); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 621 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 622 | } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 623 | if (!data) { |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 624 | if (t.format == HAL_PIXEL_FORMAT_RGB_565) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 625 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 626 | 0, bounds.top, t.width, bounds.height(), |
| 627 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, |
| 628 | t.data + bounds.top*t.stride*2); |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 629 | } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 630 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 631 | 0, bounds.top, t.width, bounds.height(), |
| 632 | GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, |
| 633 | t.data + bounds.top*t.stride*2); |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 634 | } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 || |
| 635 | t.format == HAL_PIXEL_FORMAT_RGBX_8888) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 636 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 637 | 0, bounds.top, t.width, bounds.height(), |
| 638 | GL_RGBA, GL_UNSIGNED_BYTE, |
| 639 | t.data + bounds.top*t.stride*4); |
Mathias Agopian | 54ed4f6 | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 640 | } else if (isSupportedYuvFormat(t.format)) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 641 | // just show the Y plane of YUV buffers |
| 642 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 643 | 0, bounds.top, t.width, bounds.height(), |
| 644 | GL_LUMINANCE, GL_UNSIGNED_BYTE, |
| 645 | t.data + bounds.top*t.stride); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 650 | status_t LayerBase::initializeEglImage( |
| 651 | const sp<GraphicBuffer>& buffer, Texture* texture) |
| 652 | { |
| 653 | status_t err = NO_ERROR; |
| 654 | |
| 655 | // we need to recreate the texture |
| 656 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 657 | |
| 658 | // free the previous image |
| 659 | if (texture->image != EGL_NO_IMAGE_KHR) { |
| 660 | eglDestroyImageKHR(dpy, texture->image); |
| 661 | texture->image = EGL_NO_IMAGE_KHR; |
| 662 | } |
| 663 | |
| 664 | // construct an EGL_NATIVE_BUFFER_ANDROID |
| 665 | android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); |
| 666 | |
| 667 | // create the new EGLImageKHR |
| 668 | const EGLint attrs[] = { |
| 669 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 670 | EGL_NONE, EGL_NONE |
| 671 | }; |
| 672 | texture->image = eglCreateImageKHR( |
| 673 | dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 674 | (EGLClientBuffer)clientBuf, attrs); |
| 675 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 676 | if (texture->image != EGL_NO_IMAGE_KHR) { |
| 677 | glBindTexture(GL_TEXTURE_2D, texture->name); |
| 678 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, |
| 679 | (GLeglImageOES)texture->image); |
| 680 | GLint error = glGetError(); |
| 681 | if (UNLIKELY(error != GL_NO_ERROR)) { |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 682 | LOGE("layer=%p, glEGLImageTargetTexture2DOES(%p) " |
| 683 | "failed err=0x%04x", |
| 684 | this, texture->image, error); |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 685 | err = INVALID_OPERATION; |
| 686 | } else { |
| 687 | // Everything went okay! |
| 688 | texture->NPOTAdjust = false; |
| 689 | texture->dirty = false; |
| 690 | texture->width = clientBuf->width; |
| 691 | texture->height = clientBuf->height; |
| 692 | } |
| 693 | } else { |
Mathias Agopian | fcfeb4b | 2010-03-08 11:14:20 -0800 | [diff] [blame] | 694 | LOGE("layer=%p, eglCreateImageKHR() failed. err=0x%4x", |
| 695 | this, eglGetError()); |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 696 | err = INVALID_OPERATION; |
| 697 | } |
| 698 | return err; |
| 699 | } |
| 700 | |
| 701 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | // --------------------------------------------------------------------------- |
| 703 | |
Mathias Agopian | 2e12324 | 2009-06-23 20:06:46 -0700 | [diff] [blame] | 704 | int32_t LayerBaseClient::sIdentity = 0; |
| 705 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display, |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 707 | const sp<Client>& client, int32_t i) |
Mathias Agopian | d129659 | 2010-03-09 19:17:47 -0800 | [diff] [blame] | 708 | : LayerBase(flinger, display), lcblk(NULL), client(client), mIndex(i), |
Mathias Agopian | 948d69f | 2010-03-08 19:29:09 -0800 | [diff] [blame] | 709 | mIdentity(uint32_t(android_atomic_inc(&sIdentity))) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 711 | lcblk = new SharedBufferServer( |
| 712 | client->ctrlblk, i, NUM_BUFFERS, |
| 713 | mIdentity); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 714 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 716 | void LayerBaseClient::onFirstRef() |
| 717 | { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 718 | sp<Client> client(this->client.promote()); |
| 719 | if (client != 0) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 720 | client->bindLayer(this, mIndex); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | LayerBaseClient::~LayerBaseClient() |
| 725 | { |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 726 | sp<Client> client(this->client.promote()); |
| 727 | if (client != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | client->free(mIndex); |
| 729 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 730 | delete lcblk; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | } |
| 732 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 733 | int32_t LayerBaseClient::serverIndex() const |
| 734 | { |
| 735 | sp<Client> client(this->client.promote()); |
| 736 | if (client != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | return (client->cid<<16)|mIndex; |
| 738 | } |
| 739 | return 0xFFFF0000 | mIndex; |
| 740 | } |
| 741 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 742 | sp<LayerBaseClient::Surface> LayerBaseClient::getSurface() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 744 | sp<Surface> s; |
| 745 | Mutex::Autolock _l(mLock); |
| 746 | s = mClientSurface.promote(); |
| 747 | if (s == 0) { |
| 748 | s = createSurface(); |
| 749 | mClientSurface = s; |
| 750 | } |
| 751 | return s; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 754 | sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const |
| 755 | { |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 756 | return new Surface(mFlinger, clientIndex(), mIdentity, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 757 | const_cast<LayerBaseClient *>(this)); |
| 758 | } |
| 759 | |
Mathias Agopian | 0b3ad46 | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 760 | // called with SurfaceFlinger::mStateLock as soon as the layer is entered |
| 761 | // in the purgatory list |
| 762 | void LayerBaseClient::onRemoved() |
| 763 | { |
| 764 | // wake up the condition |
| 765 | lcblk->setStatus(NO_INIT); |
| 766 | } |
| 767 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 768 | // --------------------------------------------------------------------------- |
| 769 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 770 | LayerBaseClient::Surface::Surface( |
| 771 | const sp<SurfaceFlinger>& flinger, |
| 772 | SurfaceID id, int identity, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 773 | const sp<LayerBaseClient>& owner) |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 774 | : mFlinger(flinger), mToken(id), mIdentity(identity), mOwner(owner) |
| 775 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 778 | LayerBaseClient::Surface::~Surface() |
| 779 | { |
| 780 | /* |
| 781 | * This is a good place to clean-up all client resources |
| 782 | */ |
| 783 | |
| 784 | // destroy client resources |
| 785 | sp<LayerBaseClient> layer = getOwner(); |
| 786 | if (layer != 0) { |
| 787 | mFlinger->destroySurface(layer); |
| 788 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const { |
| 792 | sp<LayerBaseClient> owner(mOwner.promote()); |
| 793 | return owner; |
| 794 | } |
| 795 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 796 | status_t LayerBaseClient::Surface::onTransact( |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 797 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 798 | { |
| 799 | switch (code) { |
| 800 | case REGISTER_BUFFERS: |
| 801 | case UNREGISTER_BUFFERS: |
| 802 | case CREATE_OVERLAY: |
| 803 | { |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 804 | if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) { |
| 805 | IPCThreadState* ipc = IPCThreadState::self(); |
| 806 | const int pid = ipc->getCallingPid(); |
| 807 | const int uid = ipc->getCallingUid(); |
| 808 | LOGE("Permission Denial: " |
| 809 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
| 810 | return PERMISSION_DENIED; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | } |
| 814 | return BnSurface::onTransact(code, data, reply, flags); |
| 815 | } |
| 816 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 817 | sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int index, int usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 818 | { |
| 819 | return NULL; |
| 820 | } |
| 821 | |
| 822 | status_t LayerBaseClient::Surface::registerBuffers( |
| 823 | const ISurface::BufferHeap& buffers) |
| 824 | { |
| 825 | return INVALID_OPERATION; |
| 826 | } |
| 827 | |
| 828 | void LayerBaseClient::Surface::postBuffer(ssize_t offset) |
| 829 | { |
| 830 | } |
| 831 | |
| 832 | void LayerBaseClient::Surface::unregisterBuffers() |
| 833 | { |
| 834 | } |
| 835 | |
| 836 | sp<OverlayRef> LayerBaseClient::Surface::createOverlay( |
Chih-Chung Chang | 52e7200 | 2010-01-21 17:31:06 -0800 | [diff] [blame] | 837 | uint32_t w, uint32_t h, int32_t format, int32_t orientation) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 838 | { |
| 839 | return NULL; |
| 840 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | |
| 842 | // --------------------------------------------------------------------------- |
| 843 | |
| 844 | }; // namespace android |