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