The Android Open Source Project | 9066cfe | 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 | 9066cfe | 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 | 947f4f4 | 2009-05-22 01:27:01 -0700 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 9066cfe | 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 | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include "SurfaceFlinger.h" |
| 34 | #include "DisplayHardware/DisplayHardware.h" |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 35 | #include "TextureManager.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | |
| 40 | // --------------------------------------------------------------------------- |
| 41 | |
Mathias Agopian | 1efba9a | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 42 | int32_t LayerBase::sSequence = 1; |
| 43 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | LayerBase::LayerBase(SurfaceFlinger* flinger, DisplayID display) |
| 45 | : dpy(display), contentDirty(false), |
Mathias Agopian | 1efba9a | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 46 | sequence(uint32_t(android_atomic_inc(&sSequence))), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | mFlinger(flinger), |
Mathias Agopian | 9237703 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 48 | mNeedsFiltering(false), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | mOrientation(0), |
Mathias Agopian | ed2ab7f | 2010-02-19 17:51:58 -0800 | [diff] [blame] | 50 | mLeft(0), mTop(0), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | mTransactionFlags(0), |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 52 | mPremultipliedAlpha(true), mName("unnamed"), mDebug(false), |
The Android Open Source Project | 9066cfe | 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 | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 57 | mBufferCrop.makeInvalid(); |
| 58 | mBufferTransform = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | LayerBase::~LayerBase() |
| 62 | { |
| 63 | } |
| 64 | |
Mathias Agopian | 015b597 | 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 | 9066cfe | 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 | e1b6f24 | 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 | 9066cfe | 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 | 8851617 | 2009-09-29 22:32:36 -0700 | [diff] [blame] | 106 | void LayerBase::commitTransaction() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | mDrawingState = mCurrentState; |
The Android Open Source Project | 9066cfe | 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 | 9066cfe | 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 | e1b6f24 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 143 | if (mCurrentState.requested_w == w && mCurrentState.requested_h == h) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | return false; |
Mathias Agopian | e1b6f24 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 145 | mCurrentState.requested_w = w; |
| 146 | mCurrentState.requested_h = h; |
The Android Open Source Project | 9066cfe | 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 | 9066cfe | 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 | 9066cfe | 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 | e1b6f24 | 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 | 70cab91 | 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 | 70cab91 | 2009-09-30 12:48:47 -0700 | [diff] [blame] | 212 | } |
| 213 | |
The Android Open Source Project | 9066cfe | 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 | 44cac13 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 218 | |
Mathias Agopian | 1989af2 | 2010-12-02 21:32:29 -0800 | [diff] [blame] | 219 | // we may use linear filtering, if the matrix scales us |
| 220 | const uint8_t type = temp.transform.getType(); |
| 221 | mNeedsFiltering = (!temp.transform.preserveRects() || |
| 222 | (type >= Transform::SCALE)); |
Mathias Agopian | 44cac13 | 2009-09-23 18:34:53 -0700 | [diff] [blame] | 223 | } |
| 224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | // Commit the transaction |
Mathias Agopian | 8851617 | 2009-09-29 22:32:36 -0700 | [diff] [blame] | 226 | commitTransaction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | return flags; |
| 228 | } |
| 229 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | void LayerBase::validateVisibility(const Transform& planeTransform) |
| 231 | { |
| 232 | const Layer::State& s(drawingState()); |
| 233 | const Transform tr(planeTransform * s.transform); |
| 234 | const bool transformed = tr.transformed(); |
| 235 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 236 | uint32_t w = s.w; |
| 237 | uint32_t h = s.h; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | tr.transform(mVertices[0], 0, 0); |
| 239 | tr.transform(mVertices[1], 0, h); |
| 240 | tr.transform(mVertices[2], w, h); |
| 241 | tr.transform(mVertices[3], w, 0); |
| 242 | if (UNLIKELY(transformed)) { |
| 243 | // NOTE: here we could also punt if we have too many rectangles |
| 244 | // in the transparent region |
| 245 | if (tr.preserveRects()) { |
| 246 | // transform the transparent region |
| 247 | transparentRegionScreen = tr.transform(s.transparentRegion); |
| 248 | } else { |
| 249 | // transformation too complex, can't do the transparent region |
| 250 | // optimization. |
| 251 | transparentRegionScreen.clear(); |
| 252 | } |
| 253 | } else { |
| 254 | transparentRegionScreen = s.transparentRegion; |
| 255 | } |
| 256 | |
| 257 | // cache a few things... |
| 258 | mOrientation = tr.getOrientation(); |
| 259 | mTransformedBounds = tr.makeBounds(w, h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | mLeft = tr.tx(); |
| 261 | mTop = tr.ty(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void LayerBase::lockPageFlip(bool& recomputeVisibleRegions) |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | void LayerBase::unlockPageFlip( |
| 269 | const Transform& planeTransform, Region& outDirtyRegion) |
| 270 | { |
| 271 | if ((android_atomic_and(~1, &mInvalidate)&1) == 1) { |
| 272 | outDirtyRegion.orSelf(visibleRegionScreen); |
| 273 | } |
| 274 | } |
| 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | void LayerBase::invalidate() |
| 277 | { |
| 278 | if ((android_atomic_or(1, &mInvalidate)&1) == 0) { |
| 279 | mFlinger->signalEvent(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void LayerBase::drawRegion(const Region& reg) const |
| 284 | { |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 285 | Region::const_iterator it = reg.begin(); |
| 286 | Region::const_iterator const end = reg.end(); |
| 287 | if (it != end) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | Rect r; |
| 289 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 290 | const int32_t fbWidth = hw.getWidth(); |
| 291 | const int32_t fbHeight = hw.getHeight(); |
| 292 | const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 }, |
| 293 | { fbWidth, fbHeight }, { 0, fbHeight } }; |
| 294 | glVertexPointer(2, GL_SHORT, 0, vertices); |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 295 | while (it != end) { |
| 296 | const Rect& r = *it++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | const GLint sy = fbHeight - (r.top + r.height()); |
| 298 | glScissor(r.left, sy, r.width(), r.height()); |
| 299 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 304 | void LayerBase::setGeometry(hwc_layer_t* hwcl) { |
| 305 | hwcl->flags |= HWC_SKIP_LAYER; |
| 306 | } |
| 307 | |
| 308 | void LayerBase::setPerFrameData(hwc_layer_t* hwcl) { |
| 309 | hwcl->compositionType = HWC_FRAMEBUFFER; |
| 310 | hwcl->handle = NULL; |
| 311 | } |
| 312 | |
Mathias Agopian | 8514b92 | 2010-08-10 20:42:20 -0700 | [diff] [blame] | 313 | void LayerBase::draw(const Region& clip) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | // reset GL state |
| 316 | glEnable(GL_SCISSOR_TEST); |
| 317 | |
| 318 | onDraw(clip); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Mathias Agopian | 38ed2e3 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 321 | void LayerBase::drawForSreenShot() const |
| 322 | { |
| 323 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 324 | onDraw( Region(hw.bounds()) ); |
| 325 | } |
| 326 | |
Mathias Agopian | 0d3c006 | 2010-05-26 22:26:12 -0700 | [diff] [blame] | 327 | void LayerBase::clearWithOpenGL(const Region& clip, GLclampf red, |
| 328 | GLclampf green, GLclampf blue, |
| 329 | GLclampf alpha) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | { |
| 331 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 332 | const uint32_t fbHeight = hw.getHeight(); |
Mathias Agopian | 0d3c006 | 2010-05-26 22:26:12 -0700 | [diff] [blame] | 333 | glColor4f(red,green,blue,alpha); |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 334 | |
| 335 | TextureManager::deactivateTextures(); |
| 336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | glDisable(GL_BLEND); |
| 338 | glDisable(GL_DITHER); |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 339 | |
| 340 | Region::const_iterator it = clip.begin(); |
| 341 | Region::const_iterator const end = clip.end(); |
Mathias Agopian | f2d28b7 | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 342 | glEnable(GL_SCISSOR_TEST); |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 343 | glVertexPointer(2, GL_FLOAT, 0, mVertices); |
Mathias Agopian | f2d28b7 | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 344 | while (it != end) { |
| 345 | const Rect& r = *it++; |
| 346 | const GLint sy = fbHeight - (r.top + r.height()); |
| 347 | glScissor(r.left, sy, r.width(), r.height()); |
| 348 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Rebecca Schultz Zavin | c854678 | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 352 | void LayerBase::clearWithOpenGL(const Region& clip) const |
| 353 | { |
| 354 | clearWithOpenGL(clip,0,0,0,0); |
| 355 | } |
| 356 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 357 | template <typename T> |
| 358 | static inline |
| 359 | void swap(T& a, T& b) { |
| 360 | T t(a); |
| 361 | a = b; |
| 362 | b = t; |
| 363 | } |
| 364 | |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 365 | void LayerBase::drawWithOpenGL(const Region& clip, const Texture& texture) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | { |
| 367 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 368 | const uint32_t fbHeight = hw.getHeight(); |
| 369 | const State& s(drawingState()); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | // bind our texture |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 372 | TextureManager::activateTexture(texture, needsFiltering()); |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 373 | uint32_t width = texture.width; |
| 374 | uint32_t height = texture.height; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | |
Mathias Agopian | a0612e4 | 2010-04-12 15:34:55 -0700 | [diff] [blame] | 376 | GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | if (UNLIKELY(s.alpha < 0xFF)) { |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 378 | const GLfloat alpha = s.alpha * (1.0f/255.0f); |
Mathias Agopian | a0612e4 | 2010-04-12 15:34:55 -0700 | [diff] [blame] | 379 | if (mPremultipliedAlpha) { |
| 380 | glColor4f(alpha, alpha, alpha, alpha); |
| 381 | } else { |
| 382 | glColor4f(1, 1, 1, alpha); |
| 383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | glEnable(GL_BLEND); |
| 385 | glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA); |
Mathias Agopian | a0612e4 | 2010-04-12 15:34:55 -0700 | [diff] [blame] | 386 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } else { |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 388 | glColor4f(1, 1, 1, 1); |
Mathias Agopian | a0612e4 | 2010-04-12 15:34:55 -0700 | [diff] [blame] | 389 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | if (needsBlending()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | glEnable(GL_BLEND); |
| 392 | glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA); |
| 393 | } else { |
| 394 | glDisable(GL_BLEND); |
| 395 | } |
| 396 | } |
| 397 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 398 | /* |
| 399 | * compute texture coordinates |
| 400 | * here, we handle NPOT, cropping and buffer transformations |
| 401 | */ |
| 402 | |
| 403 | GLfloat cl, ct, cr, cb; |
| 404 | if (!mBufferCrop.isEmpty()) { |
| 405 | // source is cropped |
| 406 | const GLfloat us = (texture.NPOTAdjust ? texture.wScale : 1.0f) / width; |
| 407 | const GLfloat vs = (texture.NPOTAdjust ? texture.hScale : 1.0f) / height; |
| 408 | cl = mBufferCrop.left * us; |
| 409 | ct = mBufferCrop.top * vs; |
| 410 | cr = mBufferCrop.right * us; |
| 411 | cb = mBufferCrop.bottom * vs; |
| 412 | } else { |
| 413 | cl = 0; |
| 414 | ct = 0; |
| 415 | cr = (texture.NPOTAdjust ? texture.wScale : 1.0f); |
| 416 | cb = (texture.NPOTAdjust ? texture.hScale : 1.0f); |
| 417 | } |
| 418 | |
Mathias Agopian | e031ba8 | 2010-10-25 18:29:35 -0700 | [diff] [blame] | 419 | /* |
| 420 | * For the buffer transformation, we apply the rotation last. |
| 421 | * Since we're transforming the texture-coordinates, we need |
| 422 | * to apply the inverse of the buffer transformation: |
| 423 | * inverse( FLIP_V -> FLIP_H -> ROT_90 ) |
| 424 | * <=> inverse( ROT_90 * FLIP_H * FLIP_V ) |
| 425 | * = inverse(FLIP_V) * inverse(FLIP_H) * inverse(ROT_90) |
| 426 | * = FLIP_V * FLIP_H * ROT_270 |
| 427 | * <=> ROT_270 -> FLIP_H -> FLIP_V |
| 428 | * |
| 429 | * The rotation is performed first, in the texture coordinate space. |
| 430 | * |
| 431 | */ |
| 432 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 433 | struct TexCoords { |
| 434 | GLfloat u; |
| 435 | GLfloat v; |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 436 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 438 | enum { |
| 439 | // name of the corners in the texture map |
| 440 | LB = 0, // left-bottom |
| 441 | LT = 1, // left-top |
| 442 | RT = 2, // right-top |
| 443 | RB = 3 // right-bottom |
| 444 | }; |
| 445 | |
| 446 | // vertices in screen space |
| 447 | int vLT = LB; |
| 448 | int vLB = LT; |
| 449 | int vRB = RT; |
| 450 | int vRT = RB; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 452 | // the texture's source is rotated |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 453 | uint32_t transform = mBufferTransform; |
| 454 | if (transform & HAL_TRANSFORM_ROT_90) { |
| 455 | vLT = RB; |
| 456 | vLB = LB; |
| 457 | vRB = LT; |
| 458 | vRT = RT; |
| 459 | } |
| 460 | if (transform & HAL_TRANSFORM_FLIP_V) { |
| 461 | swap(vLT, vLB); |
Mathias Agopian | 5ba5997 | 2010-10-11 14:19:24 -0700 | [diff] [blame] | 462 | swap(vRT, vRB); |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 463 | } |
| 464 | if (transform & HAL_TRANSFORM_FLIP_H) { |
Mathias Agopian | 5ba5997 | 2010-10-11 14:19:24 -0700 | [diff] [blame] | 465 | swap(vLT, vRT); |
| 466 | swap(vLB, vRB); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | } |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 468 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 469 | TexCoords texCoords[4]; |
| 470 | texCoords[vLT].u = cl; |
| 471 | texCoords[vLT].v = ct; |
| 472 | texCoords[vLB].u = cl; |
| 473 | texCoords[vLB].v = cb; |
| 474 | texCoords[vRB].u = cr; |
| 475 | texCoords[vRB].v = cb; |
| 476 | texCoords[vRT].u = cr; |
| 477 | texCoords[vRT].v = ct; |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 478 | |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 479 | if (needsDithering()) { |
| 480 | glEnable(GL_DITHER); |
| 481 | } else { |
| 482 | glDisable(GL_DITHER); |
| 483 | } |
| 484 | |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 485 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 486 | glVertexPointer(2, GL_FLOAT, 0, mVertices); |
| 487 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); |
| 488 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 489 | Region::const_iterator it = clip.begin(); |
| 490 | Region::const_iterator const end = clip.end(); |
Mathias Agopian | e5c0a7b | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 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 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 496 | } |
| 497 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 500 | void LayerBase::setBufferCrop(const Rect& crop) { |
Jamie Gennis | 2ece4cd | 2011-01-28 18:21:54 -0800 | [diff] [blame^] | 501 | if (mBufferCrop != crop) { |
| 502 | mBufferCrop = crop; |
| 503 | mFlinger->invalidateHwcGeometry(); |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | void LayerBase::setBufferTransform(uint32_t transform) { |
Mathias Agopian | fb4dcb1 | 2011-01-13 17:53:01 -0800 | [diff] [blame] | 508 | if (mBufferTransform != transform) { |
| 509 | mBufferTransform = transform; |
| 510 | mFlinger->invalidateHwcGeometry(); |
| 511 | } |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 514 | void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const |
| 515 | { |
| 516 | const Layer::State& s(drawingState()); |
| 517 | snprintf(buffer, SIZE, |
| 518 | "+ %s %p\n" |
| 519 | " " |
| 520 | "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), " |
| 521 | "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, " |
| 522 | "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n", |
| 523 | getTypeId(), this, s.z, tx(), ty(), s.w, s.h, |
| 524 | needsBlending(), needsDithering(), contentDirty, |
| 525 | s.alpha, s.flags, |
| 526 | s.transform[0][0], s.transform[0][1], |
| 527 | s.transform[1][0], s.transform[1][1]); |
| 528 | result.append(buffer); |
| 529 | } |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 530 | |
Mathias Agopian | 06a61e2 | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 531 | void LayerBase::shortDump(String8& result, char* scratch, size_t size) const |
| 532 | { |
| 533 | LayerBase::dump(result, scratch, size); |
| 534 | } |
| 535 | |
| 536 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | // --------------------------------------------------------------------------- |
| 538 | |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 539 | int32_t LayerBaseClient::sIdentity = 1; |
Mathias Agopian | c660395 | 2009-06-23 20:06:46 -0700 | [diff] [blame] | 540 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 542 | const sp<Client>& client) |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 543 | : LayerBase(flinger, display), mClientRef(client), |
Mathias Agopian | 85b8d12 | 2010-03-08 19:29:09 -0800 | [diff] [blame] | 544 | mIdentity(uint32_t(android_atomic_inc(&sIdentity))) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 546 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | LayerBaseClient::~LayerBaseClient() |
| 549 | { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 550 | sp<Client> c(mClientRef.promote()); |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 551 | if (c != 0) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 552 | c->detachLayer(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 556 | sp<LayerBaseClient::Surface> LayerBaseClient::getSurface() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 558 | sp<Surface> s; |
| 559 | Mutex::Autolock _l(mLock); |
| 560 | s = mClientSurface.promote(); |
| 561 | if (s == 0) { |
| 562 | s = createSurface(); |
| 563 | mClientSurface = s; |
Mathias Agopian | d35c666 | 2011-01-25 20:17:45 -0800 | [diff] [blame] | 564 | mClientSurfaceBinder = s->asBinder(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 565 | } |
| 566 | return s; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Mathias Agopian | d35c666 | 2011-01-25 20:17:45 -0800 | [diff] [blame] | 569 | wp<IBinder> LayerBaseClient::getSurfaceBinder() const { |
| 570 | return mClientSurfaceBinder; |
| 571 | } |
| 572 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 573 | sp<LayerBaseClient::Surface> LayerBaseClient::createSurface() const |
| 574 | { |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 575 | return new Surface(mFlinger, mIdentity, |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 576 | const_cast<LayerBaseClient *>(this)); |
| 577 | } |
| 578 | |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 579 | void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const |
| 580 | { |
| 581 | LayerBase::dump(result, buffer, SIZE); |
| 582 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 583 | sp<Client> client(mClientRef.promote()); |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 584 | snprintf(buffer, SIZE, |
| 585 | " name=%s\n" |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 586 | " client=%p, identity=%u\n", |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 587 | getName().string(), |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 588 | client.get(), getIdentity()); |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 589 | |
| 590 | result.append(buffer); |
| 591 | } |
| 592 | |
Mathias Agopian | 06a61e2 | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 593 | |
| 594 | void LayerBaseClient::shortDump(String8& result, char* scratch, size_t size) const |
| 595 | { |
| 596 | LayerBaseClient::dump(result, scratch, size); |
| 597 | } |
| 598 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 599 | // --------------------------------------------------------------------------- |
| 600 | |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 601 | LayerBaseClient::Surface::Surface( |
| 602 | const sp<SurfaceFlinger>& flinger, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 603 | int identity, |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 604 | const sp<LayerBaseClient>& owner) |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 605 | : mFlinger(flinger), mIdentity(identity), mOwner(owner) |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 606 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 609 | LayerBaseClient::Surface::~Surface() |
| 610 | { |
| 611 | /* |
| 612 | * This is a good place to clean-up all client resources |
| 613 | */ |
| 614 | |
| 615 | // destroy client resources |
| 616 | sp<LayerBaseClient> layer = getOwner(); |
| 617 | if (layer != 0) { |
| 618 | mFlinger->destroySurface(layer); |
| 619 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | sp<LayerBaseClient> LayerBaseClient::Surface::getOwner() const { |
| 623 | sp<LayerBaseClient> owner(mOwner.promote()); |
| 624 | return owner; |
| 625 | } |
| 626 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 627 | status_t LayerBaseClient::Surface::onTransact( |
Mathias Agopian | 151e859 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 628 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 629 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 630 | return BnSurface::onTransact(code, data, reply, flags); |
| 631 | } |
| 632 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 633 | sp<GraphicBuffer> LayerBaseClient::Surface::requestBuffer(int bufferIdx, |
| 634 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 635 | { |
| 636 | return NULL; |
| 637 | } |
| 638 | |
Mathias Agopian | 59751db | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 639 | status_t LayerBaseClient::Surface::setBufferCount(int bufferCount) |
| 640 | { |
| 641 | return INVALID_OPERATION; |
| 642 | } |
| 643 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | // --------------------------------------------------------------------------- |
| 645 | |
| 646 | }; // namespace android |