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 | |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 22 | #include <math.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 24 | #include <cutils/compiler.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 25 | #include <cutils/native_handle.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Log.h> |
| 30 | #include <utils/StopWatch.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 31 | #include <utils/Trace.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 33 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <ui/PixelFormat.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 35 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 36 | #include <gui/Surface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | #include "clz.h" |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 39 | #include "DisplayDevice.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 40 | #include "GLExtensions.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | #include "Layer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | #include "SurfaceFlinger.h" |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 43 | #include "SurfaceTextureLayer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 45 | #include "DisplayHardware/HWComposer.h" |
| 46 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | #define DEBUG_RESIZE 0 |
| 48 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 53 | Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client) |
| 54 | : LayerBaseClient(flinger, client), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 55 | mTextureName(-1U), |
| 56 | mQueuedFrames(0), |
| 57 | mCurrentTransform(0), |
Mathias Agopian | 933389f | 2011-07-18 16:15:08 -0700 | [diff] [blame] | 58 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 59 | mCurrentOpacity(true), |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 60 | mRefreshPending(false), |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 61 | mFrameLatencyNeeded(false), |
| 62 | mFrameLatencyOffset(0), |
Mathias Agopian | 5bf3abe | 2011-03-11 17:01:07 -0800 | [diff] [blame] | 63 | mFormat(PIXEL_FORMAT_NONE), |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 64 | mGLExtensions(GLExtensions::getInstance()), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 65 | mOpaqueLayer(true), |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 66 | mSecure(false), |
Mathias Agopian | 933389f | 2011-07-18 16:15:08 -0700 | [diff] [blame] | 67 | mProtectedByApp(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 69 | mCurrentCrop.makeInvalid(); |
| 70 | glGenTextures(1, &mTextureName); |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 73 | void Layer::onLayerDisplayed(const sp<const DisplayDevice>& hw, |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 74 | HWComposer::HWCLayerInterface* layer) { |
Mathias Agopian | c397360 | 2012-08-31 17:51:25 -0700 | [diff] [blame^] | 75 | LayerBaseClient::onLayerDisplayed(hw, layer); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 76 | if (layer) { |
| 77 | mSurfaceTexture->setReleaseFence(layer->getAndResetReleaseFenceFd()); |
| 78 | } |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 81 | void Layer::onFirstRef() |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 82 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 83 | LayerBaseClient::onFirstRef(); |
Mathias Agopian | ddc31c3 | 2011-06-12 18:05:53 -0700 | [diff] [blame] | 84 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 85 | struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener { |
| 86 | FrameQueuedListener(Layer* layer) : mLayer(layer) { } |
| 87 | private: |
| 88 | wp<Layer> mLayer; |
| 89 | virtual void onFrameAvailable() { |
| 90 | sp<Layer> that(mLayer.promote()); |
| 91 | if (that != 0) { |
| 92 | that->onFrameQueued(); |
| 93 | } |
| 94 | } |
| 95 | }; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 96 | |
| 97 | // Creates a custom BufferQueue for SurfaceTexture to use |
| 98 | sp<BufferQueue> bq = new SurfaceTextureLayer(); |
| 99 | mSurfaceTexture = new SurfaceTexture(mTextureName, true, |
Mathias Agopian | a0db308 | 2012-04-23 13:59:36 -0700 | [diff] [blame] | 100 | GL_TEXTURE_EXTERNAL_OES, false, bq); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 101 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 102 | mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0)); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 103 | mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this)); |
| 104 | mSurfaceTexture->setSynchronousMode(true); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 105 | |
Mathias Agopian | 7f42a9c | 2012-04-23 20:00:16 -0700 | [diff] [blame] | 106 | #ifdef TARGET_DISABLE_TRIPLE_BUFFERING |
| 107 | #warning "disabling triple buffering" |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 108 | mSurfaceTexture->setDefaultMaxBufferCount(2); |
Mathias Agopian | 7f42a9c | 2012-04-23 20:00:16 -0700 | [diff] [blame] | 109 | #else |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 110 | mSurfaceTexture->setDefaultMaxBufferCount(3); |
Mathias Agopian | 303d538 | 2012-02-05 01:49:16 -0800 | [diff] [blame] | 111 | #endif |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 114 | Layer::~Layer() |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 115 | { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 116 | mFlinger->deleteTextureAsync(mTextureName); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 119 | void Layer::onFrameQueued() { |
Jamie Gennis | 3d8063b | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 120 | android_atomic_inc(&mQueuedFrames); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 121 | mFlinger->signalLayerUpdate(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 124 | // called with SurfaceFlinger::mStateLock as soon as the layer is entered |
| 125 | // in the purgatory list |
| 126 | void Layer::onRemoved() |
| 127 | { |
Jamie Gennis | dbe6486 | 2011-07-30 14:33:49 -0700 | [diff] [blame] | 128 | mSurfaceTexture->abandon(); |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 129 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 130 | |
Jamie Gennis | a249f2d | 2011-09-16 17:31:54 -0700 | [diff] [blame] | 131 | void Layer::setName(const String8& name) { |
| 132 | LayerBase::setName(name); |
| 133 | mSurfaceTexture->setName(name); |
| 134 | } |
| 135 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 136 | sp<ISurface> Layer::createSurface() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 138 | class BSurface : public BnSurface, public LayerCleaner { |
| 139 | wp<const Layer> mOwner; |
| 140 | virtual sp<ISurfaceTexture> getSurfaceTexture() const { |
| 141 | sp<ISurfaceTexture> res; |
| 142 | sp<const Layer> that( mOwner.promote() ); |
| 143 | if (that != NULL) { |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 144 | res = that->mSurfaceTexture->getBufferQueue(); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 145 | } |
| 146 | return res; |
| 147 | } |
| 148 | public: |
| 149 | BSurface(const sp<SurfaceFlinger>& flinger, |
| 150 | const sp<Layer>& layer) |
| 151 | : LayerCleaner(flinger, layer), mOwner(layer) { } |
| 152 | }; |
| 153 | sp<ISurface> sur(new BSurface(mFlinger, this)); |
Mathias Agopian | a1f47b9 | 2011-02-15 19:01:06 -0800 | [diff] [blame] | 154 | return sur; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 157 | wp<IBinder> Layer::getSurfaceTextureBinder() const |
| 158 | { |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 159 | return mSurfaceTexture->getBufferQueue()->asBinder(); |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 162 | 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] | 163 | PixelFormat format, uint32_t flags) |
| 164 | { |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 165 | // this surfaces pixel format |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | PixelFormatInfo info; |
| 167 | status_t err = getPixelFormatInfo(format, &info); |
Mathias Agopian | ff615cc | 2012-02-24 14:58:36 -0800 | [diff] [blame] | 168 | if (err) { |
| 169 | ALOGE("unsupported pixelformat %d", format); |
| 170 | return err; |
| 171 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 173 | uint32_t const maxSurfaceDims = min( |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 174 | mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims()); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 175 | |
| 176 | // never allow a surface larger than what our underlying GL implementation |
| 177 | // can handle. |
| 178 | if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { |
Mathias Agopian | ff615cc | 2012-02-24 14:58:36 -0800 | [diff] [blame] | 179 | ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h)); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 180 | return BAD_VALUE; |
| 181 | } |
| 182 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 183 | mFormat = format; |
Mathias Agopian | eff062c | 2010-08-25 14:59:15 -0700 | [diff] [blame] | 184 | |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 185 | mSecure = (flags & ISurfaceComposerClient::eSecure) ? true : false; |
| 186 | mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false; |
| 187 | mOpaqueLayer = (flags & ISurfaceComposerClient::eOpaque); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 188 | mCurrentOpacity = getOpacityForFormat(format); |
| 189 | |
| 190 | mSurfaceTexture->setDefaultBufferSize(w, h); |
| 191 | mSurfaceTexture->setDefaultBufferFormat(format); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 192 | mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0)); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 193 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | return NO_ERROR; |
| 195 | } |
| 196 | |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 197 | Rect Layer::computeBufferCrop() const { |
| 198 | // Start with the SurfaceTexture's buffer crop... |
| 199 | Rect crop; |
| 200 | if (!mCurrentCrop.isEmpty()) { |
| 201 | crop = mCurrentCrop; |
| 202 | } else if (mActiveBuffer != NULL){ |
| 203 | crop = Rect(mActiveBuffer->getWidth(), mActiveBuffer->getHeight()); |
| 204 | } else { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 205 | crop.makeInvalid(); |
| 206 | return crop; |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // ... then reduce that in the same proportions as the window crop reduces |
| 210 | // the window size. |
| 211 | const State& s(drawingState()); |
Mathias Agopian | 93ffb86 | 2012-05-16 17:07:49 -0700 | [diff] [blame] | 212 | if (!s.active.crop.isEmpty()) { |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 213 | // Transform the window crop to match the buffer coordinate system, |
| 214 | // which means using the inverse of the current transform set on the |
| 215 | // SurfaceTexture. |
| 216 | uint32_t invTransform = mCurrentTransform; |
Mathias Agopian | 93ffb86 | 2012-05-16 17:07:49 -0700 | [diff] [blame] | 217 | int winWidth = s.active.w; |
| 218 | int winHeight = s.active.h; |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 219 | if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 220 | invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 221 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
Mathias Agopian | 93ffb86 | 2012-05-16 17:07:49 -0700 | [diff] [blame] | 222 | winWidth = s.active.h; |
| 223 | winHeight = s.active.w; |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 224 | } |
Mathias Agopian | 93ffb86 | 2012-05-16 17:07:49 -0700 | [diff] [blame] | 225 | Rect winCrop = s.active.crop.transform(invTransform, |
| 226 | s.active.w, s.active.h); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 227 | |
| 228 | float xScale = float(crop.width()) / float(winWidth); |
| 229 | float yScale = float(crop.height()) / float(winHeight); |
Mathias Agopian | 93ffb86 | 2012-05-16 17:07:49 -0700 | [diff] [blame] | 230 | crop.left += int(ceilf(float(winCrop.left) * xScale)); |
| 231 | crop.top += int(ceilf(float(winCrop.top) * yScale)); |
| 232 | crop.right -= int(ceilf(float(winWidth - winCrop.right) * xScale)); |
| 233 | crop.bottom -= int(ceilf(float(winHeight - winCrop.bottom) * yScale)); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return crop; |
| 237 | } |
| 238 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 239 | void Layer::setGeometry( |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 240 | const sp<const DisplayDevice>& hw, |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 241 | HWComposer::HWCLayerInterface& layer) |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 242 | { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 243 | LayerBaseClient::setGeometry(hw, layer); |
Mathias Agopian | a537c0f | 2011-08-02 15:51:37 -0700 | [diff] [blame] | 244 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 245 | // enable this layer |
| 246 | layer.setSkip(false); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 247 | |
| 248 | // we can't do alpha-fade with the hwc HAL |
| 249 | const State& s(drawingState()); |
| 250 | if (s.alpha < 0xFF) { |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 251 | layer.setSkip(true); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame] | 254 | /* |
| 255 | * Transformations are applied in this order: |
| 256 | * 1) buffer orientation/flip/mirror |
| 257 | * 2) state transformation (window manager) |
| 258 | * 3) layer orientation (screen orientation) |
| 259 | * (NOTE: the matrices are multiplied in reverse order) |
| 260 | */ |
| 261 | |
| 262 | const Transform bufferOrientation(mCurrentTransform); |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 263 | const Transform tr(hw->getTransform() * s.transform * bufferOrientation); |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame] | 264 | |
| 265 | // this gives us only the "orientation" component of the transform |
| 266 | const uint32_t finalTransform = tr.getOrientation(); |
| 267 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 268 | // we can only handle simple transformation |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame] | 269 | if (finalTransform & Transform::ROT_INVALID) { |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 270 | layer.setSkip(true); |
Mathias Agopian | a537c0f | 2011-08-02 15:51:37 -0700 | [diff] [blame] | 271 | } else { |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 272 | layer.setTransform(finalTransform); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 273 | } |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 274 | layer.setCrop(computeBufferCrop()); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 277 | void Layer::setPerFrameData(const sp<const DisplayDevice>& hw, |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 278 | HWComposer::HWCLayerInterface& layer) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 279 | const sp<GraphicBuffer>& buffer(mActiveBuffer); |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 280 | // NOTE: buffer can be NULL if the client never drew into this |
| 281 | // layer yet, or if we ran out of memory |
| 282 | layer.setBuffer(buffer); |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 283 | } |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 284 | |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 285 | void Layer::setAcquireFence(const sp<const DisplayDevice>& hw, |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 286 | HWComposer::HWCLayerInterface& layer) { |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 287 | int fenceFd = -1; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 288 | |
| 289 | // TODO: there is a possible optimization here: we only need to set the |
| 290 | // acquire fence the first time a new buffer is acquired on EACH display. |
| 291 | |
| 292 | if (layer.getCompositionType() == HWC_OVERLAY) { |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 293 | sp<Fence> fence = mSurfaceTexture->getCurrentFence(); |
| 294 | if (fence.get()) { |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 295 | fenceFd = fence->dup(); |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 296 | if (fenceFd == -1) { |
| 297 | ALOGW("failed to dup layer fence, skipping sync: %d", errno); |
| 298 | } |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 299 | } |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 300 | } |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 301 | layer.setAcquireFenceFd(fenceFd); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 304 | void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 306 | ATRACE_CALL(); |
| 307 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 308 | if (CC_UNLIKELY(mActiveBuffer == 0)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | // the texture has not been created yet, this Layer has |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 310 | // in fact never been drawn into. This happens frequently with |
| 311 | // SurfaceView because the WindowManager can't know when the client |
| 312 | // has drawn the first time. |
| 313 | |
| 314 | // If there is nothing under us, we paint the screen in black, otherwise |
| 315 | // we just skip this update. |
| 316 | |
| 317 | // figure out if there is something below us |
| 318 | Region under; |
Mathias Agopian | f7ae69d | 2011-08-23 12:34:29 -0700 | [diff] [blame] | 319 | const SurfaceFlinger::LayerVector& drawingLayers( |
| 320 | mFlinger->mDrawingState.layersSortedByZ); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 321 | const size_t count = drawingLayers.size(); |
| 322 | for (size_t i=0 ; i<count ; ++i) { |
| 323 | const sp<LayerBase>& layer(drawingLayers[i]); |
| 324 | if (layer.get() == static_cast<LayerBase const*>(this)) |
| 325 | break; |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 326 | under.orSelf( hw->getTransform().transform(layer->visibleRegion) ); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 327 | } |
| 328 | // if not everything below us is covered, we plug the holes! |
| 329 | Region holes(clip.subtract(under)); |
| 330 | if (!holes.isEmpty()) { |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 331 | clearWithOpenGL(hw, holes, 0, 0, 0, 1); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 332 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | return; |
| 334 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 335 | |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 336 | // TODO: replace this with a server-side wait |
| 337 | sp<Fence> fence = mSurfaceTexture->getCurrentFence(); |
| 338 | if (fence.get()) { |
| 339 | status_t err = fence->wait(Fence::TIMEOUT_NEVER); |
| 340 | ALOGW_IF(err != OK, "Layer::onDraw: failed waiting for fence: %d", err); |
| 341 | // Go ahead and draw the buffer anyway; no matter what we do the screen |
| 342 | // is probably going to have something visibly wrong. |
| 343 | } |
| 344 | |
Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 345 | if (!isProtected()) { |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 346 | // TODO: we could be more subtle with isFixedSize() |
| 347 | const bool useFiltering = getFiltering() || needsFiltering() || isFixedSize(); |
| 348 | |
| 349 | // Query the texture matrix given our current filtering mode. |
| 350 | float textureMatrix[16]; |
| 351 | mSurfaceTexture->setFilteringEnabled(useFiltering); |
| 352 | mSurfaceTexture->getTransformMatrix(textureMatrix); |
| 353 | |
| 354 | // Set things up for texturing. |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 355 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName); |
| 356 | GLenum filter = GL_NEAREST; |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 357 | if (useFiltering) { |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 358 | filter = GL_LINEAR; |
Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 359 | } |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 360 | glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter); |
| 361 | glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter); |
Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 362 | glMatrixMode(GL_TEXTURE); |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 363 | glLoadMatrixf(textureMatrix); |
Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 364 | glMatrixMode(GL_MODELVIEW); |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 365 | glDisable(GL_TEXTURE_2D); |
Xavier Ducrohet | 4c4163b | 2011-10-21 16:18:48 -0700 | [diff] [blame] | 366 | glEnable(GL_TEXTURE_EXTERNAL_OES); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 367 | } else { |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 368 | glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName()); |
Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 369 | glMatrixMode(GL_TEXTURE); |
| 370 | glLoadIdentity(); |
| 371 | glMatrixMode(GL_MODELVIEW); |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 372 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 373 | glEnable(GL_TEXTURE_2D); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 374 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 375 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 376 | drawWithOpenGL(hw, clip); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 377 | |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 378 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 379 | glDisable(GL_TEXTURE_2D); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 382 | // As documented in libhardware header, formats in the range |
| 383 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 384 | // are known to have no alpha channel |
| 385 | // TODO: move definition for device-specific range into |
| 386 | // hardware.h, instead of using hard-coded values here. |
| 387 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 388 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 389 | bool Layer::getOpacityForFormat(uint32_t format) |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 390 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 391 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 392 | return true; |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 393 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 394 | PixelFormatInfo info; |
| 395 | status_t err = getPixelFormatInfo(PixelFormat(format), &info); |
| 396 | // in case of error (unknown format), we assume no blending |
| 397 | return (err || info.h_alpha <= info.l_alpha); |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 400 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 401 | bool Layer::isOpaque() const |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 402 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 403 | // if we don't have a buffer yet, we're translucent regardless of the |
| 404 | // layer's opaque flag. |
Jamie Gennis | db5230f | 2011-07-28 14:54:07 -0700 | [diff] [blame] | 405 | if (mActiveBuffer == 0) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 406 | return false; |
Jamie Gennis | db5230f | 2011-07-28 14:54:07 -0700 | [diff] [blame] | 407 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 408 | |
| 409 | // if the layer has the opaque flag, then we're always opaque, |
| 410 | // otherwise we use the current buffer's format. |
| 411 | return mOpaqueLayer || mCurrentOpacity; |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 414 | bool Layer::isProtected() const |
| 415 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 416 | const sp<GraphicBuffer>& activeBuffer(mActiveBuffer); |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 417 | return (activeBuffer != 0) && |
| 418 | (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 419 | } |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 420 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | uint32_t Layer::doTransaction(uint32_t flags) |
| 422 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 423 | ATRACE_CALL(); |
| 424 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | const Layer::State& front(drawingState()); |
| 426 | const Layer::State& temp(currentState()); |
| 427 | |
Mathias Agopian | 4824d40 | 2012-06-04 18:16:30 -0700 | [diff] [blame] | 428 | const bool sizeChanged = (temp.requested.w != front.requested.w) || |
| 429 | (temp.requested.h != front.requested.h); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 430 | |
| 431 | if (sizeChanged) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 432 | // the size changed, we need to ask our client to request a new buffer |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 433 | ALOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | 419e196 | 2012-05-23 14:34:07 -0700 | [diff] [blame] | 434 | "doTransaction: geometry (layer=%p), scalingMode=%d\n" |
| 435 | " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" |
| 436 | " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n" |
| 437 | " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" |
| 438 | " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n", |
| 439 | this, mCurrentScalingMode, |
| 440 | temp.active.w, temp.active.h, |
| 441 | temp.active.crop.left, |
| 442 | temp.active.crop.top, |
| 443 | temp.active.crop.right, |
| 444 | temp.active.crop.bottom, |
| 445 | temp.active.crop.getWidth(), |
| 446 | temp.active.crop.getHeight(), |
| 447 | temp.requested.w, temp.requested.h, |
Mathias Agopian | b30c415 | 2012-05-16 18:21:32 -0700 | [diff] [blame] | 448 | temp.requested.crop.left, |
| 449 | temp.requested.crop.top, |
| 450 | temp.requested.crop.right, |
| 451 | temp.requested.crop.bottom, |
| 452 | temp.requested.crop.getWidth(), |
| 453 | temp.requested.crop.getHeight(), |
Mathias Agopian | 419e196 | 2012-05-23 14:34:07 -0700 | [diff] [blame] | 454 | front.active.w, front.active.h, |
| 455 | front.active.crop.left, |
| 456 | front.active.crop.top, |
| 457 | front.active.crop.right, |
| 458 | front.active.crop.bottom, |
| 459 | front.active.crop.getWidth(), |
| 460 | front.active.crop.getHeight(), |
| 461 | front.requested.w, front.requested.h, |
Mathias Agopian | b30c415 | 2012-05-16 18:21:32 -0700 | [diff] [blame] | 462 | front.requested.crop.left, |
| 463 | front.requested.crop.top, |
| 464 | front.requested.crop.right, |
| 465 | front.requested.crop.bottom, |
| 466 | front.requested.crop.getWidth(), |
Mathias Agopian | 419e196 | 2012-05-23 14:34:07 -0700 | [diff] [blame] | 467 | front.requested.crop.getHeight()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | |
Jamie Gennis | 2a0d5b6 | 2011-09-26 16:54:44 -0700 | [diff] [blame] | 469 | // record the new size, form this point on, when the client request |
| 470 | // a buffer, it'll get the new size. |
Mathias Agopian | b30c415 | 2012-05-16 18:21:32 -0700 | [diff] [blame] | 471 | mSurfaceTexture->setDefaultBufferSize( |
| 472 | temp.requested.w, temp.requested.h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 474 | |
Mathias Agopian | 0cd545f | 2012-06-07 14:18:55 -0700 | [diff] [blame] | 475 | if (!isFixedSize()) { |
| 476 | |
| 477 | const bool resizePending = (temp.requested.w != temp.active.w) || |
| 478 | (temp.requested.h != temp.active.h); |
| 479 | |
| 480 | if (resizePending) { |
| 481 | // don't let LayerBase::doTransaction update the drawing state |
| 482 | // if we have a pending resize, unless we are in fixed-size mode. |
| 483 | // the drawing state will be updated only once we receive a buffer |
| 484 | // with the correct size. |
| 485 | // |
| 486 | // in particular, we want to make sure the clip (which is part |
| 487 | // of the geometry state) is latched together with the size but is |
| 488 | // latched immediately when no resizing is involved. |
| 489 | |
| 490 | flags |= eDontUpdateGeometryState; |
| 491 | } |
| 492 | } |
| 493 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | return LayerBase::doTransaction(flags); |
| 495 | } |
| 496 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 497 | bool Layer::isFixedSize() const { |
Mathias Agopian | 933389f | 2011-07-18 16:15:08 -0700 | [diff] [blame] | 498 | return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | bool Layer::isCropped() const { |
| 502 | return !mCurrentCrop.isEmpty(); |
| 503 | } |
| 504 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | // ---------------------------------------------------------------------------- |
| 506 | // pageflip handling... |
| 507 | // ---------------------------------------------------------------------------- |
| 508 | |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 509 | bool Layer::onPreComposition() { |
| 510 | mRefreshPending = false; |
| 511 | return mQueuedFrames > 0; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 514 | void Layer::onPostComposition() { |
| 515 | if (mFrameLatencyNeeded) { |
| 516 | const HWComposer& hwc = mFlinger->getHwComposer(); |
| 517 | const size_t offset = mFrameLatencyOffset; |
| 518 | mFrameStats[offset].timestamp = mSurfaceTexture->getTimestamp(); |
| 519 | mFrameStats[offset].set = systemTime(); |
| 520 | mFrameStats[offset].vsync = hwc.getRefreshTimestamp(); |
| 521 | mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128; |
| 522 | mFrameLatencyNeeded = false; |
| 523 | } |
| 524 | } |
| 525 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 526 | Region Layer::latchBuffer(bool& recomputeVisibleRegions) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 528 | ATRACE_CALL(); |
| 529 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 530 | Region outDirtyRegion; |
Jamie Gennis | 3d8063b | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 531 | if (mQueuedFrames > 0) { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 532 | |
| 533 | // if we've already called updateTexImage() without going through |
| 534 | // a composition step, we have to skip this layer at this point |
| 535 | // because we cannot call updateTeximage() without a corresponding |
| 536 | // compositionComplete() call. |
| 537 | // we'll trigger an update in onPreComposition(). |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 538 | if (mRefreshPending) { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 539 | return outDirtyRegion; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Jamie Gennis | 351a513 | 2011-09-14 18:23:37 -0700 | [diff] [blame] | 542 | // Capture the old state of the layer for comparisons later |
Jamie Gennis | db5230f | 2011-07-28 14:54:07 -0700 | [diff] [blame] | 543 | const bool oldOpacity = isOpaque(); |
Jamie Gennis | 351a513 | 2011-09-14 18:23:37 -0700 | [diff] [blame] | 544 | sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer; |
Jamie Gennis | db5230f | 2011-07-28 14:54:07 -0700 | [diff] [blame] | 545 | |
Jamie Gennis | 3d8063b | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 546 | // signal another event if we have more frames pending |
| 547 | if (android_atomic_dec(&mQueuedFrames) > 1) { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 548 | mFlinger->signalLayerUpdate(); |
Jamie Gennis | 3d8063b | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 551 | struct Reject : public SurfaceTexture::BufferRejecter { |
| 552 | Layer::State& front; |
| 553 | Layer::State& current; |
| 554 | bool& recomputeVisibleRegions; |
| 555 | Reject(Layer::State& front, Layer::State& current, |
| 556 | bool& recomputeVisibleRegions) |
| 557 | : front(front), current(current), |
| 558 | recomputeVisibleRegions(recomputeVisibleRegions) { |
| 559 | } |
| 560 | |
| 561 | virtual bool reject(const sp<GraphicBuffer>& buf, |
| 562 | const BufferQueue::BufferItem& item) { |
| 563 | if (buf == NULL) { |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | uint32_t bufWidth = buf->getWidth(); |
| 568 | uint32_t bufHeight = buf->getHeight(); |
| 569 | |
| 570 | // check that we received a buffer of the right size |
| 571 | // (Take the buffer's orientation into account) |
| 572 | if (item.mTransform & Transform::ROT_90) { |
| 573 | swap(bufWidth, bufHeight); |
| 574 | } |
| 575 | |
| 576 | |
| 577 | bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 578 | if (front.active != front.requested) { |
| 579 | |
| 580 | if (isFixedSize || |
| 581 | (bufWidth == front.requested.w && |
| 582 | bufHeight == front.requested.h)) |
| 583 | { |
| 584 | // Here we pretend the transaction happened by updating the |
| 585 | // current and drawing states. Drawing state is only accessed |
| 586 | // in this thread, no need to have it locked |
| 587 | front.active = front.requested; |
| 588 | |
| 589 | // We also need to update the current state so that |
| 590 | // we don't end-up overwriting the drawing state with |
| 591 | // this stale current state during the next transaction |
| 592 | // |
| 593 | // NOTE: We don't need to hold the transaction lock here |
| 594 | // because State::active is only accessed from this thread. |
| 595 | current.active = front.active; |
| 596 | |
| 597 | // recompute visible region |
| 598 | recomputeVisibleRegions = true; |
| 599 | } |
| 600 | |
| 601 | ALOGD_IF(DEBUG_RESIZE, |
| 602 | "lockPageFlip: (layer=%p), buffer (%ux%u, tr=%02x), scalingMode=%d\n" |
| 603 | " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" |
| 604 | " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n", |
| 605 | this, bufWidth, bufHeight, item.mTransform, item.mScalingMode, |
| 606 | front.active.w, front.active.h, |
| 607 | front.active.crop.left, |
| 608 | front.active.crop.top, |
| 609 | front.active.crop.right, |
| 610 | front.active.crop.bottom, |
| 611 | front.active.crop.getWidth(), |
| 612 | front.active.crop.getHeight(), |
| 613 | front.requested.w, front.requested.h, |
| 614 | front.requested.crop.left, |
| 615 | front.requested.crop.top, |
| 616 | front.requested.crop.right, |
| 617 | front.requested.crop.bottom, |
| 618 | front.requested.crop.getWidth(), |
| 619 | front.requested.crop.getHeight()); |
| 620 | } |
| 621 | |
| 622 | if (!isFixedSize) { |
| 623 | if (front.active.w != bufWidth || |
| 624 | front.active.h != bufHeight) { |
Mathias Agopian | 4824d40 | 2012-06-04 18:16:30 -0700 | [diff] [blame] | 625 | // reject this buffer |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 626 | return true; |
| 627 | } |
| 628 | } |
| 629 | return false; |
| 630 | } |
| 631 | }; |
| 632 | |
| 633 | |
| 634 | Reject r(mDrawingState, currentState(), recomputeVisibleRegions); |
| 635 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 636 | // XXX: not sure if setTransformHint belongs here |
| 637 | // it should only be needed when the main screen orientation changes |
| 638 | mSurfaceTexture->setTransformHint(getTransformHint()); |
| 639 | |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 640 | if (mSurfaceTexture->updateTexImage(&r) < NO_ERROR) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 641 | // something happened! |
| 642 | recomputeVisibleRegions = true; |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 643 | return outDirtyRegion; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 644 | } |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 645 | |
Jamie Gennis | 351a513 | 2011-09-14 18:23:37 -0700 | [diff] [blame] | 646 | // update the active buffer |
| 647 | mActiveBuffer = mSurfaceTexture->getCurrentBuffer(); |
Mathias Agopian | e31564d | 2012-05-29 20:41:03 -0700 | [diff] [blame] | 648 | if (mActiveBuffer == NULL) { |
| 649 | // this can only happen if the very first buffer was rejected. |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 650 | return outDirtyRegion; |
Mathias Agopian | e31564d | 2012-05-29 20:41:03 -0700 | [diff] [blame] | 651 | } |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 652 | |
Mathias Agopian | 4824d40 | 2012-06-04 18:16:30 -0700 | [diff] [blame] | 653 | mRefreshPending = true; |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 654 | mFrameLatencyNeeded = true; |
Mathias Agopian | e31564d | 2012-05-29 20:41:03 -0700 | [diff] [blame] | 655 | if (oldActiveBuffer == NULL) { |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 656 | // the first time we receive a buffer, we need to trigger a |
| 657 | // geometry invalidation. |
| 658 | mFlinger->invalidateHwcGeometry(); |
| 659 | } |
| 660 | |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 661 | Rect crop(mSurfaceTexture->getCurrentCrop()); |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 662 | const uint32_t transform(mSurfaceTexture->getCurrentTransform()); |
| 663 | const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode()); |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 664 | if ((crop != mCurrentCrop) || |
| 665 | (transform != mCurrentTransform) || |
| 666 | (scalingMode != mCurrentScalingMode)) |
| 667 | { |
| 668 | mCurrentCrop = crop; |
| 669 | mCurrentTransform = transform; |
| 670 | mCurrentScalingMode = scalingMode; |
| 671 | mFlinger->invalidateHwcGeometry(); |
| 672 | } |
| 673 | |
| 674 | if (oldActiveBuffer != NULL) { |
Mathias Agopian | e31564d | 2012-05-29 20:41:03 -0700 | [diff] [blame] | 675 | uint32_t bufWidth = mActiveBuffer->getWidth(); |
| 676 | uint32_t bufHeight = mActiveBuffer->getHeight(); |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 677 | if (bufWidth != uint32_t(oldActiveBuffer->width) || |
| 678 | bufHeight != uint32_t(oldActiveBuffer->height)) { |
| 679 | mFlinger->invalidateHwcGeometry(); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format); |
| 684 | if (oldOpacity != isOpaque()) { |
| 685 | recomputeVisibleRegions = true; |
| 686 | } |
| 687 | |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 688 | glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 689 | glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 690 | |
| 691 | // FIXME: postedRegion should be dirty & bounds |
| 692 | const Layer::State& front(drawingState()); |
| 693 | Region dirtyRegion(Rect(front.active.w, front.active.h)); |
| 694 | |
| 695 | // transform the dirty region to window-manager space |
| 696 | outDirtyRegion = (front.transform.transform(dirtyRegion)); |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 697 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 698 | return outDirtyRegion; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 701 | void Layer::dump(String8& result, char* buffer, size_t SIZE) const |
| 702 | { |
| 703 | LayerBaseClient::dump(result, buffer, SIZE); |
| 704 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 705 | sp<const GraphicBuffer> buf0(mActiveBuffer); |
| 706 | uint32_t w0=0, h0=0, s0=0, f0=0; |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 707 | if (buf0 != 0) { |
| 708 | w0 = buf0->getWidth(); |
| 709 | h0 = buf0->getHeight(); |
| 710 | s0 = buf0->getStride(); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 711 | f0 = buf0->format; |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 712 | } |
| 713 | snprintf(buffer, SIZE, |
| 714 | " " |
Mathias Agopian | ad795ba | 2011-08-08 16:02:13 -0700 | [diff] [blame] | 715 | "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X]," |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 716 | " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n", |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 717 | mFormat, w0, h0, s0,f0, |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 718 | getTransformHint(), mQueuedFrames, mRefreshPending); |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 719 | |
| 720 | result.append(buffer); |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 721 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 722 | if (mSurfaceTexture != 0) { |
| 723 | mSurfaceTexture->dump(result, " ", buffer, SIZE); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 724 | } |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 727 | void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const |
| 728 | { |
| 729 | LayerBaseClient::dumpStats(result, buffer, SIZE); |
| 730 | const size_t o = mFrameLatencyOffset; |
Mathias Agopian | 888c822 | 2012-08-04 21:10:38 -0700 | [diff] [blame] | 731 | const nsecs_t period = mFlinger->getHwComposer().getRefreshPeriod(); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 732 | result.appendFormat("%lld\n", period); |
| 733 | for (size_t i=0 ; i<128 ; i++) { |
| 734 | const size_t index = (o+i) % 128; |
| 735 | const nsecs_t time_app = mFrameStats[index].timestamp; |
| 736 | const nsecs_t time_set = mFrameStats[index].set; |
| 737 | const nsecs_t time_vsync = mFrameStats[index].vsync; |
| 738 | result.appendFormat("%lld\t%lld\t%lld\n", |
| 739 | time_app, |
| 740 | time_vsync, |
| 741 | time_set); |
| 742 | } |
| 743 | result.append("\n"); |
| 744 | } |
| 745 | |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 746 | void Layer::clearStats() |
| 747 | { |
| 748 | LayerBaseClient::clearStats(); |
| 749 | memset(mFrameStats, 0, sizeof(mFrameStats)); |
| 750 | } |
| 751 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 752 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 753 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 754 | // TODO: should we do something special if mSecure is set? |
| 755 | if (mProtectedByApp) { |
| 756 | // need a hardware-protected path to external video sink |
| 757 | usage |= GraphicBuffer::USAGE_PROTECTED; |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 758 | } |
Jamie Gennis | 3599bf2 | 2011-08-10 11:48:07 -0700 | [diff] [blame] | 759 | usage |= GraphicBuffer::USAGE_HW_COMPOSER; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 760 | return usage; |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 763 | uint32_t Layer::getTransformHint() const { |
| 764 | uint32_t orientation = 0; |
| 765 | if (!mFlinger->mDebugDisableTransformHint) { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 766 | // The transform hint is used to improve performance on the main |
| 767 | // display -- we can only have a single transform hint, it cannot |
| 768 | // apply to all displays. |
| 769 | // This is why we use the default display here. This is not an |
| 770 | // oversight. |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 771 | sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); |
| 772 | const Transform& planeTransform(hw->getTransform()); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 773 | orientation = planeTransform.getOrientation(); |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 774 | if (orientation & Transform::ROT_INVALID) { |
| 775 | orientation = 0; |
| 776 | } |
| 777 | } |
| 778 | return orientation; |
| 779 | } |
| 780 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | // --------------------------------------------------------------------------- |
| 782 | |
| 783 | |
| 784 | }; // namespace android |