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