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