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" |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 35 | #include "TextureManager.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | |
| 40 | // --------------------------------------------------------------------------- |
| 41 | |
Mathias Agopian | f6679fc | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 42 | int32_t LayerBase::sSequence = 1; |
| 43 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display) |
| 45 | : dpy(display), contentDirty(false), |
Mathias Agopian | f6679fc | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 46 | sequence(uint32_t(android_atomic_inc(&sSequence))), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | mFlinger(flinger), |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 48 | mNeedsFiltering(false), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | mOrientation(0), |
Mathias Agopian | ca6fab2 | 2010-02-19 17:51:58 -0800 | [diff] [blame] | 50 | mLeft(0), mTop(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | mTransactionFlags(0), |
Mathias Agopian | 245e4d7 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 52 | mPremultipliedAlpha(true), mName("unnamed"), mDebug(false), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | mInvalidate(0) |
| 54 | { |
| 55 | const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware()); |
| 56 | mFlags = hw.getFlags(); |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 57 | mBufferCrop.makeInvalid(); |
| 58 | mBufferTransform = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | LayerBase::~LayerBase() |
| 62 | { |
| 63 | } |
| 64 | |
Mathias Agopian | d129659 | 2010-03-09 19:17:47 -0800 | [diff] [blame] | 65 | void LayerBase::setName(const String8& name) { |
| 66 | mName = name; |
| 67 | } |
| 68 | |
| 69 | String8 LayerBase::getName() const { |
| 70 | return mName; |
| 71 | } |
| 72 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | const GraphicPlane& LayerBase::graphicPlane(int dpy) const |
| 74 | { |
| 75 | return mFlinger->graphicPlane(dpy); |
| 76 | } |
| 77 | |
| 78 | GraphicPlane& LayerBase::graphicPlane(int dpy) |
| 79 | { |
| 80 | return mFlinger->graphicPlane(dpy); |
| 81 | } |
| 82 | |
| 83 | void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags) |
| 84 | { |
| 85 | uint32_t layerFlags = 0; |
| 86 | if (flags & ISurfaceComposer::eHidden) |
| 87 | layerFlags = ISurfaceComposer::eLayerHidden; |
| 88 | |
| 89 | if (flags & ISurfaceComposer::eNonPremultiplied) |
| 90 | mPremultipliedAlpha = false; |
| 91 | |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 92 | mCurrentState.z = 0; |
| 93 | mCurrentState.w = w; |
| 94 | mCurrentState.h = h; |
| 95 | mCurrentState.requested_w = w; |
| 96 | mCurrentState.requested_h = h; |
| 97 | mCurrentState.alpha = 0xFF; |
| 98 | mCurrentState.flags = layerFlags; |
| 99 | mCurrentState.sequence = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | mCurrentState.transform.set(0, 0); |
| 101 | |
| 102 | // drawing state & current state are identical |
| 103 | mDrawingState = mCurrentState; |
| 104 | } |
| 105 | |
Mathias Agopian | ba6be54 | 2009-09-29 22:32:36 -0700 | [diff] [blame] | 106 | void LayerBase::commitTransaction() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | mDrawingState = mCurrentState; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | } |
| 109 | void LayerBase::forceVisibilityTransaction() { |
| 110 | // this can be called without SurfaceFlinger.mStateLock, but if we |
| 111 | // can atomically increment the sequence number, it doesn't matter. |
| 112 | android_atomic_inc(&mCurrentState.sequence); |
| 113 | requestTransaction(); |
| 114 | } |
| 115 | bool LayerBase::requestTransaction() { |
| 116 | int32_t old = setTransactionFlags(eTransactionNeeded); |
| 117 | return ((old & eTransactionNeeded) == 0); |
| 118 | } |
| 119 | uint32_t LayerBase::getTransactionFlags(uint32_t flags) { |
| 120 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 121 | } |
| 122 | uint32_t LayerBase::setTransactionFlags(uint32_t flags) { |
| 123 | return android_atomic_or(flags, &mTransactionFlags); |
| 124 | } |
| 125 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | bool LayerBase::setPosition(int32_t x, int32_t y) { |
| 127 | if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y) |
| 128 | return false; |
| 129 | mCurrentState.sequence++; |
| 130 | mCurrentState.transform.set(x, y); |
| 131 | requestTransaction(); |
| 132 | return true; |
| 133 | } |
| 134 | bool LayerBase::setLayer(uint32_t z) { |
| 135 | if (mCurrentState.z == z) |
| 136 | return false; |
| 137 | mCurrentState.sequence++; |
| 138 | mCurrentState.z = z; |
| 139 | requestTransaction(); |
| 140 | return true; |
| 141 | } |
| 142 | bool LayerBase::setSize(uint32_t w, uint32_t h) { |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 143 | if (mCurrentState.requested_w == w && mCurrentState.requested_h == h) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | return false; |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 145 | mCurrentState.requested_w = w; |
| 146 | mCurrentState.requested_h = h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | requestTransaction(); |
| 148 | return true; |
| 149 | } |
| 150 | bool LayerBase::setAlpha(uint8_t alpha) { |
| 151 | if (mCurrentState.alpha == alpha) |
| 152 | return false; |
| 153 | mCurrentState.sequence++; |
| 154 | mCurrentState.alpha = alpha; |
| 155 | requestTransaction(); |
| 156 | return true; |
| 157 | } |
| 158 | bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | mCurrentState.sequence++; |
| 160 | mCurrentState.transform.set( |
| 161 | matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy); |
| 162 | requestTransaction(); |
| 163 | return true; |
| 164 | } |
| 165 | bool LayerBase::setTransparentRegionHint(const Region& transparent) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | mCurrentState.sequence++; |
| 167 | mCurrentState.transparentRegion = transparent; |
| 168 | requestTransaction(); |
| 169 | return true; |
| 170 | } |
| 171 | bool LayerBase::setFlags(uint8_t flags, uint8_t mask) { |
| 172 | const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask); |
| 173 | if (mCurrentState.flags == newFlags) |
| 174 | return false; |
| 175 | mCurrentState.sequence++; |
| 176 | mCurrentState.flags = newFlags; |
| 177 | requestTransaction(); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | Rect LayerBase::visibleBounds() const |
| 182 | { |
| 183 | return mTransformedBounds; |
| 184 | } |
| 185 | |
| 186 | void LayerBase::setVisibleRegion(const Region& visibleRegion) { |
| 187 | // always called from main thread |
| 188 | visibleRegionScreen = visibleRegion; |
| 189 | } |
| 190 | |
| 191 | void LayerBase::setCoveredRegion(const Region& coveredRegion) { |
| 192 | // always called from main thread |
| 193 | coveredRegionScreen = coveredRegion; |
| 194 | } |
| 195 | |
| 196 | uint32_t LayerBase::doTransaction(uint32_t flags) |
| 197 | { |
| 198 | const Layer::State& front(drawingState()); |
| 199 | const Layer::State& temp(currentState()); |
| 200 | |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 201 | if ((front.requested_w != temp.requested_w) || |
| 202 | (front.requested_h != temp.requested_h)) { |
| 203 | // resize the layer, set the physical size to the requested size |
| 204 | Layer::State& editTemp(currentState()); |
| 205 | editTemp.w = temp.requested_w; |
| 206 | editTemp.h = temp.requested_h; |
| 207 | } |
| 208 | |
Mathias Agopian | 6656dbc | 2009-09-30 12:48:47 -0700 | [diff] [blame] | 209 | if ((front.w != temp.w) || (front.h != temp.h)) { |
| 210 | // invalidate and recompute the visible regions if needed |
| 211 | flags |= Layer::eVisibleRegion; |
Mathias Agopian | 6656dbc | 2009-09-30 12:48:47 -0700 | [diff] [blame] | 212 | } |
| 213 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | if (temp.sequence != front.sequence) { |
| 215 | // invalidate and recompute the visible regions if needed |
| 216 | flags |= eVisibleRegion; |
| 217 | this->contentDirty = true; |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 218 | |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 219 | mNeedsFiltering = false; |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 220 | if (!(mFlags & DisplayHardware::SLOW_CONFIG)) { |
| 221 | // we may use linear filtering, if the matrix scales us |
| 222 | const uint8_t type = temp.transform.getType(); |
| 223 | if (!temp.transform.preserveRects() || (type >= Transform::SCALE)) { |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 224 | mNeedsFiltering = true; |
Mathias Agopian | a2fe0a2 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | // Commit the transaction |
Mathias Agopian | ba6be54 | 2009-09-29 22:32:36 -0700 | [diff] [blame] | 230 | commitTransaction(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | return flags; |
| 232 | } |
| 233 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | void LayerBase::validateVisibility(const Transform& planeTransform) |
| 235 | { |
| 236 | const Layer::State& s(drawingState()); |
| 237 | const Transform tr(planeTransform * s.transform); |
| 238 | const bool transformed = tr.transformed(); |
| 239 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 240 | uint32_t w = s.w; |
| 241 | uint32_t h = s.h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | tr.transform(mVertices[0], 0, 0); |
| 243 | tr.transform(mVertices[1], 0, h); |
| 244 | tr.transform(mVertices[2], w, h); |
| 245 | tr.transform(mVertices[3], w, 0); |
| 246 | if (UNLIKELY(transformed)) { |
| 247 | // NOTE: here we could also punt if we have too many rectangles |
| 248 | // in the transparent region |
| 249 | if (tr.preserveRects()) { |
| 250 | // transform the transparent region |
| 251 | transparentRegionScreen = tr.transform(s.transparentRegion); |
| 252 | } else { |
| 253 | // transformation too complex, can't do the transparent region |
| 254 | // optimization. |
| 255 | transparentRegionScreen.clear(); |
| 256 | } |
| 257 | } else { |
| 258 | transparentRegionScreen = s.transparentRegion; |
| 259 | } |
| 260 | |
| 261 | // cache a few things... |
| 262 | mOrientation = tr.getOrientation(); |
| 263 | mTransformedBounds = tr.makeBounds(w, h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | mLeft = tr.tx(); |
| 265 | mTop = tr.ty(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void LayerBase::lockPageFlip(bool& recomputeVisibleRegions) |
| 269 | { |
| 270 | } |
| 271 | |
| 272 | void LayerBase::unlockPageFlip( |
| 273 | const Transform& planeTransform, Region& outDirtyRegion) |
| 274 | { |
| 275 | if ((android_atomic_and(~1, &mInvalidate)&1) == 1) { |
| 276 | outDirtyRegion.orSelf(visibleRegionScreen); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void LayerBase::finishPageFlip() |
| 281 | { |
| 282 | } |
| 283 | |
| 284 | void LayerBase::invalidate() |
| 285 | { |
| 286 | if ((android_atomic_or(1, &mInvalidate)&1) == 0) { |
| 287 | mFlinger->signalEvent(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void LayerBase::drawRegion(const Region& reg) const |
| 292 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 293 | Region::const_iterator it = reg.begin(); |
| 294 | Region::const_iterator const end = reg.end(); |
| 295 | if (it != end) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | Rect r; |
| 297 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 298 | const int32_t fbWidth = hw.getWidth(); |
| 299 | const int32_t fbHeight = hw.getHeight(); |
| 300 | const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 }, |
| 301 | { fbWidth, fbHeight }, { 0, fbHeight } }; |
| 302 | glVertexPointer(2, GL_SHORT, 0, vertices); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 303 | while (it != end) { |
| 304 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | const GLint sy = fbHeight - (r.top + r.height()); |
| 306 | glScissor(r.left, sy, r.width(), r.height()); |
| 307 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
Mathias Agopian | bc7e31a | 2010-08-10 20:42:20 -0700 | [diff] [blame] | 312 | void LayerBase::draw(const Region& clip) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | // reset GL state |
| 315 | glEnable(GL_SCISSOR_TEST); |
| 316 | |
| 317 | onDraw(clip); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Mathias Agopian | df85c45 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 320 | void LayerBase::drawForSreenShot() const |
| 321 | { |
| 322 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 323 | onDraw( Region(hw.bounds()) ); |
| 324 | } |
| 325 | |
Mathias Agopian | 010fccb | 2010-05-26 22:26:12 -0700 | [diff] [blame] | 326 | void LayerBase::clearWithOpenGL(const Region& clip, GLclampf red, |
| 327 | GLclampf green, GLclampf blue, |
| 328 | GLclampf alpha) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | { |
| 330 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 331 | const uint32_t fbHeight = hw.getHeight(); |
Mathias Agopian | 010fccb | 2010-05-26 22:26:12 -0700 | [diff] [blame] | 332 | glColor4f(red,green,blue,alpha); |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 333 | |
| 334 | TextureManager::deactivateTextures(); |
| 335 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | glDisable(GL_BLEND); |
| 337 | glDisable(GL_DITHER); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 338 | |
| 339 | Region::const_iterator it = clip.begin(); |
| 340 | Region::const_iterator const end = clip.end(); |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 341 | glEnable(GL_SCISSOR_TEST); |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 342 | glVertexPointer(2, GL_FLOAT, 0, mVertices); |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 343 | while (it != end) { |
| 344 | const Rect& r = *it++; |
| 345 | const GLint sy = fbHeight - (r.top + r.height()); |
| 346 | glScissor(r.left, sy, r.width(), r.height()); |
| 347 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 351 | void LayerBase::clearWithOpenGL(const Region& clip) const |
| 352 | { |
| 353 | clearWithOpenGL(clip,0,0,0,0); |
| 354 | } |
| 355 | |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 356 | template <typename T> |
| 357 | static inline |
| 358 | void swap(T& a, T& b) { |
| 359 | T t(a); |
| 360 | a = b; |
| 361 | b = t; |
| 362 | } |
| 363 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 364 | 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] | 365 | { |
| 366 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 367 | const uint32_t fbHeight = hw.getHeight(); |
| 368 | const State& s(drawingState()); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 369 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | // bind our texture |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 371 | TextureManager::activateTexture(texture, needsFiltering()); |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 372 | uint32_t width = texture.width; |
| 373 | uint32_t height = texture.height; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | |
Mathias Agopian | 4975326 | 2010-04-12 15:34:55 -0700 | [diff] [blame] | 375 | GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | if (UNLIKELY(s.alpha < 0xFF)) { |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 377 | const GLfloat alpha = s.alpha * (1.0f/255.0f); |
Mathias Agopian | 4975326 | 2010-04-12 15:34:55 -0700 | [diff] [blame] | 378 | if (mPremultipliedAlpha) { |
| 379 | glColor4f(alpha, alpha, alpha, alpha); |
| 380 | } else { |
| 381 | glColor4f(1, 1, 1, alpha); |
| 382 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | glEnable(GL_BLEND); |
| 384 | glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA); |
Mathias Agopian | 4975326 | 2010-04-12 15:34:55 -0700 | [diff] [blame] | 385 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | } else { |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 387 | glColor4f(1, 1, 1, 1); |
Mathias Agopian | 4975326 | 2010-04-12 15:34:55 -0700 | [diff] [blame] | 388 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | if (needsBlending()) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | glEnable(GL_BLEND); |
| 391 | glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA); |
| 392 | } else { |
| 393 | glDisable(GL_BLEND); |
| 394 | } |
| 395 | } |
| 396 | |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 397 | /* |
| 398 | * compute texture coordinates |
| 399 | * here, we handle NPOT, cropping and buffer transformations |
| 400 | */ |
| 401 | |
| 402 | GLfloat cl, ct, cr, cb; |
| 403 | if (!mBufferCrop.isEmpty()) { |
| 404 | // source is cropped |
| 405 | const GLfloat us = (texture.NPOTAdjust ? texture.wScale : 1.0f) / width; |
| 406 | const GLfloat vs = (texture.NPOTAdjust ? texture.hScale : 1.0f) / height; |
| 407 | cl = mBufferCrop.left * us; |
| 408 | ct = mBufferCrop.top * vs; |
| 409 | cr = mBufferCrop.right * us; |
| 410 | cb = mBufferCrop.bottom * vs; |
| 411 | } else { |
| 412 | cl = 0; |
| 413 | ct = 0; |
| 414 | cr = (texture.NPOTAdjust ? texture.wScale : 1.0f); |
| 415 | cb = (texture.NPOTAdjust ? texture.hScale : 1.0f); |
| 416 | } |
| 417 | |
| 418 | struct TexCoords { |
| 419 | GLfloat u; |
| 420 | GLfloat v; |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 421 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 423 | enum { |
| 424 | // name of the corners in the texture map |
| 425 | LB = 0, // left-bottom |
| 426 | LT = 1, // left-top |
| 427 | RT = 2, // right-top |
| 428 | RB = 3 // right-bottom |
| 429 | }; |
| 430 | |
| 431 | // vertices in screen space |
| 432 | int vLT = LB; |
| 433 | int vLB = LT; |
| 434 | int vRB = RT; |
| 435 | int vRT = RB; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 437 | // the texture's source is rotated |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 438 | uint32_t transform = mBufferTransform; |
| 439 | if (transform & HAL_TRANSFORM_ROT_90) { |
| 440 | vLT = RB; |
| 441 | vLB = LB; |
| 442 | vRB = LT; |
| 443 | vRT = RT; |
| 444 | } |
| 445 | if (transform & HAL_TRANSFORM_FLIP_V) { |
| 446 | swap(vLT, vLB); |
| 447 | swap(vRB, vRT); |
| 448 | } |
| 449 | if (transform & HAL_TRANSFORM_FLIP_H) { |
| 450 | swap(vLT, vRB); |
| 451 | swap(vLB, vRT); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | } |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 453 | |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 454 | TexCoords texCoords[4]; |
| 455 | texCoords[vLT].u = cl; |
| 456 | texCoords[vLT].v = ct; |
| 457 | texCoords[vLB].u = cl; |
| 458 | texCoords[vLB].v = cb; |
| 459 | texCoords[vRB].u = cr; |
| 460 | texCoords[vRB].v = cb; |
| 461 | texCoords[vRT].u = cr; |
| 462 | texCoords[vRT].v = ct; |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 463 | |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 464 | if (needsDithering()) { |
| 465 | glEnable(GL_DITHER); |
| 466 | } else { |
| 467 | glDisable(GL_DITHER); |
| 468 | } |
| 469 | |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 470 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 471 | glVertexPointer(2, GL_FLOAT, 0, mVertices); |
| 472 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); |
| 473 | |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 474 | Region::const_iterator it = clip.begin(); |
| 475 | Region::const_iterator const end = clip.end(); |
Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 476 | while (it != end) { |
| 477 | const Rect& r = *it++; |
| 478 | const GLint sy = fbHeight - (r.top + r.height()); |
| 479 | glScissor(r.left, sy, r.width(), r.height()); |
| 480 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 481 | } |
| 482 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 485 | void LayerBase::setBufferCrop(const Rect& crop) { |
| 486 | if (!crop.isEmpty()) { |
| 487 | mBufferCrop = crop; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | void LayerBase::setBufferTransform(uint32_t transform) { |
| 492 | mBufferTransform = transform; |
| 493 | } |
| 494 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 495 | void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const |
| 496 | { |
| 497 | const Layer::State& s(drawingState()); |
| 498 | snprintf(buffer, SIZE, |
| 499 | "+ %s %p\n" |
| 500 | " " |
| 501 | "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), " |
| 502 | "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, " |
| 503 | "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n", |
| 504 | getTypeId(), this, s.z, tx(), ty(), s.w, s.h, |
| 505 | needsBlending(), needsDithering(), contentDirty, |
| 506 | s.alpha, s.flags, |
| 507 | s.transform[0][0], s.transform[0][1], |
| 508 | s.transform[1][0], s.transform[1][1]); |
| 509 | result.append(buffer); |
| 510 | } |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 511 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | // --------------------------------------------------------------------------- |
| 513 | |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 514 | int32_t LayerBaseClient::sIdentity = 1; |
Mathias Agopian | 2e12324 | 2009-06-23 20:06:46 -0700 | [diff] [blame] | 515 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display, |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 517 | const sp<Client>& client) |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 518 | : LayerBase(flinger, display), mClientRef(client), |
Mathias Agopian | 948d69f | 2010-03-08 19:29:09 -0800 | [diff] [blame] | 519 | mIdentity(uint32_t(android_atomic_inc(&sIdentity))) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 521 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | LayerBaseClient::~LayerBaseClient() |
| 524 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 525 | sp<Client> c(mClientRef.promote()); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 526 | if (c != 0) { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 527 | c->detachLayer(this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 531 | sp<LayerBaseClient::Surface> LayerBaseClient::getSurface() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 533 | sp<Surface> s; |
| 534 | Mutex::Autolock _l(mLock); |
| 535 | s = mClientSurface.promote(); |
| 536 | if (s == 0) { |
| 537 | s = createSurface(); |
| 538 | mClientSurface = s; |
| 539 | } |
| 540 | return s; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 543 | sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const |
| 544 | { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 545 | return new Surface(mFlinger, mIdentity, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 546 | const_cast<LayerBaseClient *>(this)); |
| 547 | } |
| 548 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 549 | void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const |
| 550 | { |
| 551 | LayerBase::dump(result, buffer, SIZE); |
| 552 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 553 | sp<Client> client(mClientRef.promote()); |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 554 | snprintf(buffer, SIZE, |
| 555 | " name=%s\n" |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 556 | " client=%p, identity=%u\n", |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 557 | getName().string(), |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 558 | client.get(), getIdentity()); |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 559 | |
| 560 | result.append(buffer); |
| 561 | } |
| 562 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 563 | // --------------------------------------------------------------------------- |
| 564 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 565 | LayerBaseClient::Surface::Surface( |
| 566 | const sp<SurfaceFlinger>& flinger, |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 567 | int identity, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 568 | const sp<LayerBaseClient>& owner) |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 569 | : mFlinger(flinger), mIdentity(identity), mOwner(owner) |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 570 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 573 | LayerBaseClient::Surface::~Surface() |
| 574 | { |
| 575 | /* |
| 576 | * This is a good place to clean-up all client resources |
| 577 | */ |
| 578 | |
| 579 | // destroy client resources |
| 580 | sp<LayerBaseClient> layer = getOwner(); |
| 581 | if (layer != 0) { |
| 582 | mFlinger->destroySurface(layer); |
| 583 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const { |
| 587 | sp<LayerBaseClient> owner(mOwner.promote()); |
| 588 | return owner; |
| 589 | } |
| 590 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 591 | status_t LayerBaseClient::Surface::onTransact( |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 592 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 593 | { |
| 594 | switch (code) { |
| 595 | case REGISTER_BUFFERS: |
| 596 | case UNREGISTER_BUFFERS: |
| 597 | case CREATE_OVERLAY: |
| 598 | { |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 599 | if (!mFlinger->mAccessSurfaceFlinger.checkCalling()) { |
| 600 | IPCThreadState* ipc = IPCThreadState::self(); |
| 601 | const int pid = ipc->getCallingPid(); |
| 602 | const int uid = ipc->getCallingUid(); |
| 603 | LOGE("Permission Denial: " |
| 604 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
| 605 | return PERMISSION_DENIED; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
| 609 | return BnSurface::onTransact(code, data, reply, flags); |
| 610 | } |
| 611 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 612 | sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int bufferIdx, |
| 613 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 614 | { |
| 615 | return NULL; |
| 616 | } |
| 617 | |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 618 | status_t LayerBaseClient::Surface::setBufferCount(int bufferCount) |
| 619 | { |
| 620 | return INVALID_OPERATION; |
| 621 | } |
| 622 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 623 | status_t LayerBaseClient::Surface::registerBuffers( |
| 624 | const ISurface::BufferHeap& buffers) |
| 625 | { |
| 626 | return INVALID_OPERATION; |
| 627 | } |
| 628 | |
| 629 | void LayerBaseClient::Surface::postBuffer(ssize_t offset) |
| 630 | { |
| 631 | } |
| 632 | |
| 633 | void LayerBaseClient::Surface::unregisterBuffers() |
| 634 | { |
| 635 | } |
| 636 | |
| 637 | sp<OverlayRef> LayerBaseClient::Surface::createOverlay( |
Chih-Chung Chang | 52e7200 | 2010-01-21 17:31:06 -0800 | [diff] [blame] | 638 | uint32_t w, uint32_t h, int32_t format, int32_t orientation) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 639 | { |
| 640 | return NULL; |
| 641 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | |
| 643 | // --------------------------------------------------------------------------- |
| 644 | |
| 645 | }; // namespace android |