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