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 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 21 | #include <cutils/compiler.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | #include <utils/Errors.h> |
| 26 | #include <utils/Log.h> |
| 27 | #include <utils/StopWatch.h> |
| 28 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 29 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | #include <ui/PixelFormat.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 31 | |
| 32 | #include <surfaceflinger/Surface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | #include "clz.h" |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 35 | #include "DisplayHardware/DisplayHardware.h" |
| 36 | #include "DisplayHardware/HWComposer.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 37 | #include "GLExtensions.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | #include "Layer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #include "SurfaceFlinger.h" |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 40 | #include "SurfaceTextureLayer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | #define DEBUG_RESIZE 0 |
| 43 | |
| 44 | |
| 45 | namespace android { |
| 46 | |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 47 | template <typename T> inline T min(T a, T b) { |
| 48 | return a<b ? a : b; |
| 49 | } |
| 50 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | // --------------------------------------------------------------------------- |
| 52 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 53 | Layer::Layer(SurfaceFlinger* flinger, |
| 54 | DisplayID display, const sp<Client>& client) |
| 55 | : LayerBaseClient(flinger, display, client), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 56 | mTextureName(-1U), |
| 57 | mQueuedFrames(0), |
| 58 | mCurrentTransform(0), |
| 59 | mCurrentOpacity(true), |
Mathias Agopian | 5bf3abe | 2011-03-11 17:01:07 -0800 | [diff] [blame] | 60 | mFormat(PIXEL_FORMAT_NONE), |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 61 | mGLExtensions(GLExtensions::getInstance()), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 62 | mOpaqueLayer(true), |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 63 | mNeedsDithering(false), |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 64 | mSecure(false), |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 65 | mProtectedByApp(false), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 66 | mFixedSize(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 68 | mCurrentCrop.makeInvalid(); |
| 69 | glGenTextures(1, &mTextureName); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Mathias Agopian | ddc31c3 | 2011-06-12 18:05:53 -0700 | [diff] [blame] | 72 | void Layer::destroy(RefBase const* base) { |
| 73 | mFlinger->destroyLayer(static_cast<LayerBase const*>(base)); |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 76 | void Layer::onFirstRef() |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 77 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 78 | LayerBaseClient::onFirstRef(); |
Mathias Agopian | ddc31c3 | 2011-06-12 18:05:53 -0700 | [diff] [blame] | 79 | setDestroyer(this); |
| 80 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 81 | struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener { |
| 82 | FrameQueuedListener(Layer* layer) : mLayer(layer) { } |
| 83 | private: |
| 84 | wp<Layer> mLayer; |
| 85 | virtual void onFrameAvailable() { |
| 86 | sp<Layer> that(mLayer.promote()); |
| 87 | if (that != 0) { |
| 88 | that->onFrameQueued(); |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this); |
| 93 | mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this)); |
| 94 | mSurfaceTexture->setSynchronousMode(true); |
| 95 | mSurfaceTexture->setBufferCountServer(2); |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 98 | Layer::~Layer() |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 99 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 100 | glDeleteTextures(1, &mTextureName); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 103 | void Layer::onFrameQueued() { |
Jamie Gennis | 3d8063b | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 104 | android_atomic_inc(&mQueuedFrames); |
| 105 | mFlinger->signalEvent(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 108 | // called with SurfaceFlinger::mStateLock as soon as the layer is entered |
| 109 | // in the purgatory list |
| 110 | void Layer::onRemoved() |
| 111 | { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 112 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 113 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 114 | sp<ISurface> Layer::createSurface() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 116 | class BSurface : public BnSurface, public LayerCleaner { |
| 117 | wp<const Layer> mOwner; |
| 118 | virtual sp<ISurfaceTexture> getSurfaceTexture() const { |
| 119 | sp<ISurfaceTexture> res; |
| 120 | sp<const Layer> that( mOwner.promote() ); |
| 121 | if (that != NULL) { |
| 122 | res = that->mSurfaceTexture; |
| 123 | } |
| 124 | return res; |
| 125 | } |
| 126 | public: |
| 127 | BSurface(const sp<SurfaceFlinger>& flinger, |
| 128 | const sp<Layer>& layer) |
| 129 | : LayerCleaner(flinger, layer), mOwner(layer) { } |
| 130 | }; |
| 131 | sp<ISurface> sur(new BSurface(mFlinger, this)); |
Mathias Agopian | a1f47b9 | 2011-02-15 19:01:06 -0800 | [diff] [blame] | 132 | return sur; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 135 | status_t Layer::setBuffers( uint32_t w, uint32_t h, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | PixelFormat format, uint32_t flags) |
| 137 | { |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 138 | // this surfaces pixel format |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | PixelFormatInfo info; |
| 140 | status_t err = getPixelFormatInfo(format, &info); |
| 141 | if (err) return err; |
| 142 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 143 | // the display's pixel format |
| 144 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 145 | uint32_t const maxSurfaceDims = min( |
| 146 | hw.getMaxTextureSize(), hw.getMaxViewportDims()); |
| 147 | |
| 148 | // never allow a surface larger than what our underlying GL implementation |
| 149 | // can handle. |
| 150 | if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { |
| 151 | return BAD_VALUE; |
| 152 | } |
| 153 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 154 | PixelFormatInfo displayInfo; |
| 155 | getPixelFormatInfo(hw.getFormat(), &displayInfo); |
Mathias Agopian | a4b740e | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 156 | const uint32_t hwFlags = hw.getFlags(); |
| 157 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 158 | mFormat = format; |
Mathias Agopian | eff062c | 2010-08-25 14:59:15 -0700 | [diff] [blame] | 159 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 160 | mSecure = (flags & ISurfaceComposer::eSecure) ? true : false; |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 161 | mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 162 | mOpaqueLayer = (flags & ISurfaceComposer::eOpaque); |
| 163 | mCurrentOpacity = getOpacityForFormat(format); |
| 164 | |
| 165 | mSurfaceTexture->setDefaultBufferSize(w, h); |
| 166 | mSurfaceTexture->setDefaultBufferFormat(format); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 167 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 168 | // we use the red index |
| 169 | int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED); |
| 170 | int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED); |
| 171 | mNeedsDithering = layerRedsize > displayRedSize; |
| 172 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | return NO_ERROR; |
| 174 | } |
| 175 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 176 | void Layer::setGeometry(hwc_layer_t* hwcl) |
| 177 | { |
| 178 | hwcl->compositionType = HWC_FRAMEBUFFER; |
| 179 | hwcl->hints = 0; |
| 180 | hwcl->flags = 0; |
| 181 | hwcl->transform = 0; |
| 182 | hwcl->blending = HWC_BLENDING_NONE; |
| 183 | |
| 184 | // we can't do alpha-fade with the hwc HAL |
| 185 | const State& s(drawingState()); |
| 186 | if (s.alpha < 0xFF) { |
| 187 | hwcl->flags = HWC_SKIP_LAYER; |
| 188 | return; |
| 189 | } |
| 190 | |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame^] | 191 | /* |
| 192 | * Transformations are applied in this order: |
| 193 | * 1) buffer orientation/flip/mirror |
| 194 | * 2) state transformation (window manager) |
| 195 | * 3) layer orientation (screen orientation) |
| 196 | * (NOTE: the matrices are multiplied in reverse order) |
| 197 | */ |
| 198 | |
| 199 | const Transform bufferOrientation(mCurrentTransform); |
| 200 | const Transform& stateTransform(s.transform); |
| 201 | const Transform layerOrientation(mOrientation); |
| 202 | |
| 203 | const Transform tr(layerOrientation * stateTransform * bufferOrientation); |
| 204 | |
| 205 | // this gives us only the "orientation" component of the transform |
| 206 | const uint32_t finalTransform = tr.getOrientation(); |
| 207 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 208 | // we can only handle simple transformation |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame^] | 209 | if (finalTransform & Transform::ROT_INVALID) { |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 210 | hwcl->flags = HWC_SKIP_LAYER; |
| 211 | return; |
| 212 | } |
| 213 | |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame^] | 214 | hwcl->transform = finalTransform; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 215 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 216 | if (!isOpaque()) { |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 217 | hwcl->blending = mPremultipliedAlpha ? |
| 218 | HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE; |
| 219 | } |
| 220 | |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame^] | 221 | // scaling is already applied in mTransformedBounds |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 222 | hwcl->displayFrame.left = mTransformedBounds.left; |
| 223 | hwcl->displayFrame.top = mTransformedBounds.top; |
| 224 | hwcl->displayFrame.right = mTransformedBounds.right; |
| 225 | hwcl->displayFrame.bottom = mTransformedBounds.bottom; |
| 226 | |
| 227 | hwcl->visibleRegionScreen.rects = |
| 228 | reinterpret_cast<hwc_rect_t const *>( |
| 229 | visibleRegionScreen.getArray( |
| 230 | &hwcl->visibleRegionScreen.numRects)); |
| 231 | } |
| 232 | |
| 233 | void Layer::setPerFrameData(hwc_layer_t* hwcl) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 234 | const sp<GraphicBuffer>& buffer(mActiveBuffer); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 235 | if (buffer == NULL) { |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 236 | // this can happen if the client never drew into this layer yet, |
| 237 | // or if we ran out of memory. In that case, don't let |
| 238 | // HWC handle it. |
| 239 | hwcl->flags |= HWC_SKIP_LAYER; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 240 | hwcl->handle = NULL; |
| 241 | return; |
| 242 | } |
Louis Huemiller | 0404814 | 2010-12-01 12:29:36 -0800 | [diff] [blame] | 243 | hwcl->handle = buffer->handle; |
Mathias Agopian | f345069 | 2010-12-08 17:40:28 -0800 | [diff] [blame] | 244 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 245 | if (isCropped()) { |
| 246 | hwcl->sourceCrop.left = mCurrentCrop.left; |
| 247 | hwcl->sourceCrop.top = mCurrentCrop.top; |
| 248 | hwcl->sourceCrop.right = mCurrentCrop.right; |
| 249 | hwcl->sourceCrop.bottom = mCurrentCrop.bottom; |
Mathias Agopian | f345069 | 2010-12-08 17:40:28 -0800 | [diff] [blame] | 250 | } else { |
| 251 | hwcl->sourceCrop.left = 0; |
| 252 | hwcl->sourceCrop.top = 0; |
| 253 | hwcl->sourceCrop.right = buffer->width; |
| 254 | hwcl->sourceCrop.bottom = buffer->height; |
| 255 | } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 258 | static inline uint16_t pack565(int r, int g, int b) { |
| 259 | return (r<<11)|(g<<5)|b; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | void Layer::onDraw(const Region& clip) const |
| 262 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 263 | if (CC_UNLIKELY(mActiveBuffer == 0)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | // the texture has not been created yet, this Layer has |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 265 | // in fact never been drawn into. This happens frequently with |
| 266 | // SurfaceView because the WindowManager can't know when the client |
| 267 | // has drawn the first time. |
| 268 | |
| 269 | // If there is nothing under us, we paint the screen in black, otherwise |
| 270 | // we just skip this update. |
| 271 | |
| 272 | // figure out if there is something below us |
| 273 | Region under; |
| 274 | const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ); |
| 275 | const size_t count = drawingLayers.size(); |
| 276 | for (size_t i=0 ; i<count ; ++i) { |
| 277 | const sp<LayerBase>& layer(drawingLayers[i]); |
| 278 | if (layer.get() == static_cast<LayerBase const*>(this)) |
| 279 | break; |
| 280 | under.orSelf(layer->visibleRegionScreen); |
| 281 | } |
| 282 | // if not everything below us is covered, we plug the holes! |
| 283 | Region holes(clip.subtract(under)); |
| 284 | if (!holes.isEmpty()) { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 285 | clearWithOpenGL(holes, 0, 0, 0, 1); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 286 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | return; |
| 288 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 289 | |
| 290 | GLenum target = mSurfaceTexture->getCurrentTextureTarget(); |
| 291 | glBindTexture(target, mTextureName); |
| 292 | if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) { |
| 293 | // TODO: we could be more subtle with isFixedSize() |
| 294 | glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 295 | glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 296 | } else { |
| 297 | glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 298 | glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 299 | } |
| 300 | glEnable(target); |
| 301 | glMatrixMode(GL_TEXTURE); |
| 302 | glLoadMatrixf(mTextureMatrix); |
| 303 | glMatrixMode(GL_MODELVIEW); |
| 304 | |
| 305 | drawWithOpenGL(clip); |
| 306 | |
| 307 | glDisable(target); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 310 | // As documented in libhardware header, formats in the range |
| 311 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 312 | // are known to have no alpha channel |
| 313 | // TODO: move definition for device-specific range into |
| 314 | // hardware.h, instead of using hard-coded values here. |
| 315 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 316 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 317 | bool Layer::getOpacityForFormat(uint32_t format) |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 318 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 319 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 320 | return true; |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 321 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 322 | PixelFormatInfo info; |
| 323 | status_t err = getPixelFormatInfo(PixelFormat(format), &info); |
| 324 | // in case of error (unknown format), we assume no blending |
| 325 | return (err || info.h_alpha <= info.l_alpha); |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 328 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 329 | bool Layer::isOpaque() const |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 330 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 331 | // if we don't have a buffer yet, we're translucent regardless of the |
| 332 | // layer's opaque flag. |
| 333 | if (mActiveBuffer == 0) |
| 334 | return false; |
| 335 | |
| 336 | // if the layer has the opaque flag, then we're always opaque, |
| 337 | // otherwise we use the current buffer's format. |
| 338 | return mOpaqueLayer || mCurrentOpacity; |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 341 | bool Layer::isProtected() const |
| 342 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 343 | const sp<GraphicBuffer>& activeBuffer(mActiveBuffer); |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 344 | return (activeBuffer != 0) && |
| 345 | (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 346 | } |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 347 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | uint32_t Layer::doTransaction(uint32_t flags) |
| 349 | { |
| 350 | const Layer::State& front(drawingState()); |
| 351 | const Layer::State& temp(currentState()); |
| 352 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 353 | const bool sizeChanged = (front.requested_w != temp.requested_w) || |
| 354 | (front.requested_h != temp.requested_h); |
| 355 | |
| 356 | if (sizeChanged) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 357 | // the size changed, we need to ask our client to request a new buffer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 359 | "resize (layer=%p), requested (%dx%d), drawing (%d,%d), " |
| 360 | "fixedSize=%d", |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 361 | this, |
| 362 | int(temp.requested_w), int(temp.requested_h), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 363 | int(front.requested_w), int(front.requested_h), |
| 364 | isFixedSize()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 366 | if (!isFixedSize()) { |
| 367 | // we're being resized and there is a freeze display request, |
| 368 | // acquire a freeze lock, so that the screen stays put |
| 369 | // until we've redrawn at the new size; this is to avoid |
| 370 | // glitches upon orientation changes. |
| 371 | if (mFlinger->hasFreezeRequest()) { |
| 372 | // if the surface is hidden, don't try to acquire the |
| 373 | // freeze lock, since hidden surfaces may never redraw |
| 374 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 375 | mFreezeLock = mFlinger->getFreezeLock(); |
| 376 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 378 | |
| 379 | // this will make sure LayerBase::doTransaction doesn't update |
| 380 | // the drawing state's size |
| 381 | Layer::State& editDraw(mDrawingState); |
| 382 | editDraw.requested_w = temp.requested_w; |
| 383 | editDraw.requested_h = temp.requested_h; |
| 384 | |
| 385 | // record the new size, form this point on, when the client request |
| 386 | // a buffer, it'll get the new size. |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 387 | mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | } |
| 389 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 390 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | if (temp.sequence != front.sequence) { |
| 392 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 393 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 394 | // (it may never redraw, which is fine if it is hidden) |
| 395 | mFreezeLock.clear(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return LayerBase::doTransaction(flags); |
| 400 | } |
| 401 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 402 | bool Layer::isFixedSize() const { |
| 403 | Mutex::Autolock _l(mLock); |
| 404 | return mFixedSize; |
| 405 | } |
| 406 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 407 | void Layer::setFixedSize(bool fixedSize) |
| 408 | { |
| 409 | Mutex::Autolock _l(mLock); |
| 410 | mFixedSize = fixedSize; |
| 411 | } |
| 412 | |
| 413 | bool Layer::isCropped() const { |
| 414 | return !mCurrentCrop.isEmpty(); |
| 415 | } |
| 416 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | // ---------------------------------------------------------------------------- |
| 418 | // pageflip handling... |
| 419 | // ---------------------------------------------------------------------------- |
| 420 | |
| 421 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 422 | { |
Jamie Gennis | 3d8063b | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 423 | if (mQueuedFrames > 0) { |
| 424 | // signal another event if we have more frames pending |
| 425 | if (android_atomic_dec(&mQueuedFrames) > 1) { |
| 426 | mFlinger->signalEvent(); |
| 427 | } |
| 428 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 429 | if (mSurfaceTexture->updateTexImage() < NO_ERROR) { |
| 430 | // something happened! |
| 431 | recomputeVisibleRegions = true; |
| 432 | return; |
| 433 | } |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 434 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 435 | mActiveBuffer = mSurfaceTexture->getCurrentBuffer(); |
| 436 | mSurfaceTexture->getTransformMatrix(mTextureMatrix); |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 437 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 438 | const Rect crop(mSurfaceTexture->getCurrentCrop()); |
| 439 | const uint32_t transform(mSurfaceTexture->getCurrentTransform()); |
| 440 | if ((crop != mCurrentCrop) || (transform != mCurrentTransform)) { |
| 441 | mCurrentCrop = crop; |
| 442 | mCurrentTransform = transform; |
| 443 | mFlinger->invalidateHwcGeometry(); |
| 444 | } |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 445 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 446 | const bool opacity(getOpacityForFormat(mActiveBuffer->format)); |
| 447 | if (opacity != mCurrentOpacity) { |
| 448 | mCurrentOpacity = opacity; |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 449 | recomputeVisibleRegions = true; |
| 450 | } |
| 451 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 452 | const GLenum target(mSurfaceTexture->getCurrentTextureTarget()); |
| 453 | glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 454 | glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 455 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 456 | // update the layer size and release freeze-lock |
| 457 | const Layer::State& front(drawingState()); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 458 | |
| 459 | // FIXME: mPostedDirtyRegion = dirty & bounds |
| 460 | mPostedDirtyRegion.set(front.w, front.h); |
| 461 | |
| 462 | sp<GraphicBuffer> newFrontBuffer(mActiveBuffer); |
Jamie Gennis | 3629d7f | 2011-05-16 16:55:03 -0700 | [diff] [blame] | 463 | if ((newFrontBuffer->getWidth() == front.requested_w && |
| 464 | newFrontBuffer->getHeight() == front.requested_h) || |
| 465 | isFixedSize()) |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 466 | { |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 467 | if ((front.w != front.requested_w) || |
| 468 | (front.h != front.requested_h)) |
| 469 | { |
| 470 | // Here we pretend the transaction happened by updating the |
| 471 | // current and drawing states. Drawing state is only accessed |
| 472 | // in this thread, no need to have it locked |
| 473 | Layer::State& editDraw(mDrawingState); |
| 474 | editDraw.w = editDraw.requested_w; |
| 475 | editDraw.h = editDraw.requested_h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 476 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 477 | // We also need to update the current state so that we don't |
| 478 | // end-up doing too much work during the next transaction. |
| 479 | // NOTE: We actually don't need hold the transaction lock here |
| 480 | // because State::w and State::h are only accessed from |
| 481 | // this thread |
| 482 | Layer::State& editTemp(currentState()); |
| 483 | editTemp.w = editDraw.w; |
| 484 | editTemp.h = editDraw.h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 485 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 486 | // recompute visible region |
| 487 | recomputeVisibleRegions = true; |
| 488 | } |
| 489 | |
| 490 | // we now have the correct size, unfreeze the screen |
| 491 | mFreezeLock.clear(); |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 492 | } |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 493 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void Layer::unlockPageFlip( |
| 497 | const Transform& planeTransform, Region& outDirtyRegion) |
| 498 | { |
| 499 | Region dirtyRegion(mPostedDirtyRegion); |
| 500 | if (!dirtyRegion.isEmpty()) { |
| 501 | mPostedDirtyRegion.clear(); |
| 502 | // The dirty region is given in the layer's coordinate space |
| 503 | // transform the dirty region by the surface's transformation |
| 504 | // and the global transformation. |
| 505 | const Layer::State& s(drawingState()); |
| 506 | const Transform tr(planeTransform * s.transform); |
| 507 | dirtyRegion = tr.transform(dirtyRegion); |
| 508 | |
| 509 | // At this point, the dirty region is in screen space. |
| 510 | // Make sure it's constrained by the visible region (which |
| 511 | // is in screen space as well). |
| 512 | dirtyRegion.andSelf(visibleRegionScreen); |
| 513 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | } |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 515 | if (visibleRegionScreen.isEmpty()) { |
| 516 | // an invisible layer should not hold a freeze-lock |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 517 | // (because it may never be updated and therefore never release it) |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 518 | mFreezeLock.clear(); |
| 519 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 522 | void Layer::dump(String8& result, char* buffer, size_t SIZE) const |
| 523 | { |
| 524 | LayerBaseClient::dump(result, buffer, SIZE); |
| 525 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 526 | sp<const GraphicBuffer> buf0(mActiveBuffer); |
| 527 | uint32_t w0=0, h0=0, s0=0, f0=0; |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 528 | if (buf0 != 0) { |
| 529 | w0 = buf0->getWidth(); |
| 530 | h0 = buf0->getHeight(); |
| 531 | s0 = buf0->getStride(); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 532 | f0 = buf0->format; |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 533 | } |
| 534 | snprintf(buffer, SIZE, |
| 535 | " " |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 536 | "format=%2d, activeBuffer=[%3ux%3u:%3u,%3u]," |
| 537 | " freezeLock=%p, queued-frames=%d\n", |
| 538 | mFormat, w0, h0, s0,f0, |
| 539 | getFreezeLock().get(), mQueuedFrames); |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 540 | |
| 541 | result.append(buffer); |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 542 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 543 | if (mSurfaceTexture != 0) { |
| 544 | mSurfaceTexture->dump(result, " ", buffer, SIZE); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 545 | } |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 548 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 549 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 550 | // TODO: should we do something special if mSecure is set? |
| 551 | if (mProtectedByApp) { |
| 552 | // need a hardware-protected path to external video sink |
| 553 | usage |= GraphicBuffer::USAGE_PROTECTED; |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 554 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 555 | return usage; |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 556 | } |
| 557 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | // --------------------------------------------------------------------------- |
| 559 | |
| 560 | |
| 561 | }; // namespace android |