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