1edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
4edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * you may not use this file except in compliance with the License.
6edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
8edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
10edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
171c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#define ATRACE_TAG ATRACE_TAG_GRAPHICS
181c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
19edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdlib.h>
20edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdint.h>
21edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <sys/types.h>
2290ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <math.h>
23edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
24a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian#include <cutils/compiler.h>
25076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <cutils/native_handle.h>
26a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian#include <cutils/properties.h>
27edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
28edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Errors.h>
29edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Log.h>
30edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/StopWatch.h>
311c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#include <utils/Trace.h>
32edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
333330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBuffer.h>
34edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <ui/PixelFormat.h>
359cce325fae8adcf7560a28eef394489f09bad74dMathias Agopian
3690ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/Surface.h>
37edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
38edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "clz.h"
390f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian#include "DisplayDevice.h"
401f7bec634f19c123410a5155c8d282e177c01930Mathias Agopian#include "GLExtensions.h"
41edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "Layer.h"
42edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "SurfaceFlinger.h"
43a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian#include "SurfaceTextureLayer.h"
44edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
451b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian#include "DisplayHardware/HWComposer.h"
461b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian
47edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define DEBUG_RESIZE    0
48edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
49edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
50edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
51edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
52edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
533ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias AgopianLayer::Layer(SurfaceFlinger* flinger, const sp<Client>& client)
543ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    :   LayerBaseClient(flinger, client),
55a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mTextureName(-1U),
56a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mQueuedFrames(0),
57a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mCurrentTransform(0),
58933389f75814bb62e8153528f9cff2cb329b77dfMathias Agopian        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
59a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mCurrentOpacity(true),
604d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian        mRefreshPending(false),
6182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        mFrameLatencyNeeded(false),
6282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        mFrameLatencyOffset(0),
635bf3abefb2745bf0c45b0814cfd44b4682060a6cMathias Agopian        mFormat(PIXEL_FORMAT_NONE),
641f7bec634f19c123410a5155c8d282e177c01930Mathias Agopian        mGLExtensions(GLExtensions::getInstance()),
65a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mOpaqueLayer(true),
66b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian        mSecure(false),
67933389f75814bb62e8153528f9cff2cb329b77dfMathias Agopian        mProtectedByApp(false)
68edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
69a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    mCurrentCrop.makeInvalid();
70a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    glGenTextures(1, &mTextureName);
71e8696a40e09b24b634214684d18526187b316a2fJamie Gennis}
72e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
734297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopianvoid Layer::onLayerDisplayed(const sp<const DisplayDevice>& hw,
74d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        HWComposer::HWCLayerInterface* layer) {
75c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    LayerBaseClient::onLayerDisplayed(hw, layer);
76ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    if (layer) {
77ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        mSurfaceTexture->setReleaseFence(layer->getAndResetReleaseFenceFd());
78ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
79d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian}
80d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian
81a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopianvoid Layer::onFirstRef()
8296f0819f81293076e652792794a961543e6750d7Mathias Agopian{
83a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    LayerBaseClient::onFirstRef();
84ddc31c3e2bc6ffe66695c385d23e8ccc3c6dad06Mathias Agopian
85a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
86a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        FrameQueuedListener(Layer* layer) : mLayer(layer) { }
87a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    private:
88a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        wp<Layer> mLayer;
89a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        virtual void onFrameAvailable() {
90a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            sp<Layer> that(mLayer.promote());
91a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            if (that != 0) {
92a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian                that->onFrameQueued();
93a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            }
94a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        }
95a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    };
96b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
97b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // Creates a custom BufferQueue for SurfaceTexture to use
98b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    sp<BufferQueue> bq = new SurfaceTextureLayer();
99b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mSurfaceTexture = new SurfaceTexture(mTextureName, true,
100a0db308c3d62301ae41b2e2bcf295c4ee4c7b08eMathias Agopian            GL_TEXTURE_EXTERNAL_OES, false, bq);
101b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
102b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
103a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
104a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    mSurfaceTexture->setSynchronousMode(true);
105b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
1067f42a9c47c5a7f40cf02032d286d6bd62f28e650Mathias Agopian#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
1077f42a9c47c5a7f40cf02032d286d6bd62f28e650Mathias Agopian#warning "disabling triple buffering"
10831a353da225af5329735451c761b430d82dfda1bJamie Gennis    mSurfaceTexture->setDefaultMaxBufferCount(2);
1097f42a9c47c5a7f40cf02032d286d6bd62f28e650Mathias Agopian#else
11031a353da225af5329735451c761b430d82dfda1bJamie Gennis    mSurfaceTexture->setDefaultMaxBufferCount(3);
111303d538bb012e82c6b9a98c4930a03455000f761Mathias Agopian#endif
1126905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden
1136905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden    updateTransformHint();
114b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian}
11596f0819f81293076e652792794a961543e6750d7Mathias Agopian
116a67932fe6864ac346e7f78b86df11cf6c5344137Mathias AgopianLayer::~Layer()
117b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian{
118921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    mFlinger->deleteTextureAsync(mTextureName);
11996f0819f81293076e652792794a961543e6750d7Mathias Agopian}
12096f0819f81293076e652792794a961543e6750d7Mathias Agopian
121a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopianvoid Layer::onFrameQueued() {
1223d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis    android_atomic_inc(&mQueuedFrames);
12399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    mFlinger->signalLayerUpdate();
124579b3f88d03d06b897b778bd11818f5104677d1dMathias Agopian}
125579b3f88d03d06b897b778bd11818f5104677d1dMathias Agopian
126d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian// called with SurfaceFlinger::mStateLock as soon as the layer is entered
127d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian// in the purgatory list
128d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopianvoid Layer::onRemoved()
129d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian{
130dbe6486ca151d0eb1950be0aae347f0eb8ed3442Jamie Gennis    mSurfaceTexture->abandon();
13148d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian}
132cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
133a249f2d11249ff37c48119020b797ad437ddef2cJamie Gennisvoid Layer::setName(const String8& name) {
134a249f2d11249ff37c48119020b797ad437ddef2cJamie Gennis    LayerBase::setName(name);
135a249f2d11249ff37c48119020b797ad437ddef2cJamie Gennis    mSurfaceTexture->setName(name);
136a249f2d11249ff37c48119020b797ad437ddef2cJamie Gennis}
137a249f2d11249ff37c48119020b797ad437ddef2cJamie Gennis
138a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopiansp<ISurface> Layer::createSurface()
139edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
140a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    class BSurface : public BnSurface, public LayerCleaner {
141a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        wp<const Layer> mOwner;
142a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        virtual sp<ISurfaceTexture> getSurfaceTexture() const {
143a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            sp<ISurfaceTexture> res;
144a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            sp<const Layer> that( mOwner.promote() );
145a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            if (that != NULL) {
146b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam                res = that->mSurfaceTexture->getBufferQueue();
147a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            }
148a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            return res;
149a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        }
150a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    public:
151a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        BSurface(const sp<SurfaceFlinger>& flinger,
152a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian                const sp<Layer>& layer)
153a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            : LayerCleaner(flinger, layer), mOwner(layer) { }
154a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    };
155a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    sp<ISurface> sur(new BSurface(mFlinger, this));
156a1f47b90ab53af978be45b8bda16c5d084ae66e6Mathias Agopian    return sur;
157edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
158edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
159582270d69db94286a248bd829f1ae6f910d45124Jamie Genniswp<IBinder> Layer::getSurfaceTextureBinder() const
160582270d69db94286a248bd829f1ae6f910d45124Jamie Gennis{
161b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    return mSurfaceTexture->getBufferQueue()->asBinder();
162582270d69db94286a248bd829f1ae6f910d45124Jamie Gennis}
163582270d69db94286a248bd829f1ae6f910d45124Jamie Gennis
164f9d932774e06d5122c48b47d8cabd791783f56d2Mathias Agopianstatus_t Layer::setBuffers( uint32_t w, uint32_t h,
165edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                            PixelFormat format, uint32_t flags)
166edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
167401c257fba770a267f637184b1f532b4e03bed20Mathias Agopian    // this surfaces pixel format
168edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    PixelFormatInfo info;
169edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t err = getPixelFormatInfo(format, &info);
170ff615cc7a1cceedd705b0623b058c54669b29596Mathias Agopian    if (err) {
171ff615cc7a1cceedd705b0623b058c54669b29596Mathias Agopian        ALOGE("unsupported pixelformat %d", format);
172ff615cc7a1cceedd705b0623b058c54669b29596Mathias Agopian        return err;
173ff615cc7a1cceedd705b0623b058c54669b29596Mathias Agopian    }
174edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
175ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian    uint32_t const maxSurfaceDims = min(
176a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
177ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian
178ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian    // never allow a surface larger than what our underlying GL implementation
179ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian    // can handle.
180ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian    if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
181ff615cc7a1cceedd705b0623b058c54669b29596Mathias Agopian        ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
182ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian        return BAD_VALUE;
183ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian    }
184ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian
185cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian    mFormat = format;
186eff062c49e858d0dd94a1e57f6115bc84dba103eMathias Agopian
1873165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian    mSecure = (flags & ISurfaceComposerClient::eSecure) ? true : false;
1883165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian    mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
1893165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian    mOpaqueLayer = (flags & ISurfaceComposerClient::eOpaque);
190a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    mCurrentOpacity = getOpacityForFormat(format);
191a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
192a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    mSurfaceTexture->setDefaultBufferSize(w, h);
193a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    mSurfaceTexture->setDefaultBufferFormat(format);
194b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
195ca99fb8f65f3ea249c56fb6dccefffb54e87696eMathias Agopian
196edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return NO_ERROR;
197edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
198edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
199f15a83f5814219c167f87cb8aaea622fc8493499Jamie GennisRect Layer::computeBufferCrop() const {
200f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    // Start with the SurfaceTexture's buffer crop...
201f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    Rect crop;
202f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    if (!mCurrentCrop.isEmpty()) {
203f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        crop = mCurrentCrop;
204f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    } else  if (mActiveBuffer != NULL){
205f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        crop = Rect(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
206f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    } else {
2074fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        crop.makeInvalid();
2084fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        return crop;
209f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    }
210f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis
211f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    // ... then reduce that in the same proportions as the window crop reduces
212f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    // the window size.
213f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    const State& s(drawingState());
21493ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian    if (!s.active.crop.isEmpty()) {
215f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        // Transform the window crop to match the buffer coordinate system,
216f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        // which means using the inverse of the current transform set on the
217f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        // SurfaceTexture.
218f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        uint32_t invTransform = mCurrentTransform;
21993ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian        int winWidth = s.active.w;
22093ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian        int winHeight = s.active.h;
221f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
222f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis            invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
223f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis                    NATIVE_WINDOW_TRANSFORM_FLIP_H;
22493ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian            winWidth = s.active.h;
22593ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian            winHeight = s.active.w;
226f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        }
22793ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian        Rect winCrop = s.active.crop.transform(invTransform,
22893ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian                s.active.w, s.active.h);
229f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis
230f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        float xScale = float(crop.width()) / float(winWidth);
231f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        float yScale = float(crop.height()) / float(winHeight);
23293ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian        crop.left += int(ceilf(float(winCrop.left) * xScale));
23393ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian        crop.top += int(ceilf(float(winCrop.top) * yScale));
23493ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian        crop.right -= int(ceilf(float(winWidth - winCrop.right) * xScale));
23593ffb86b909005bbee4993fc9053f017466311c7Mathias Agopian        crop.bottom -= int(ceilf(float(winHeight - winCrop.bottom) * yScale));
236f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    }
237f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis
238f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    return crop;
239f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis}
240f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis
2414fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianvoid Layer::setGeometry(
2424297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    const sp<const DisplayDevice>& hw,
2434fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        HWComposer::HWCLayerInterface& layer)
244a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
2454fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    LayerBaseClient::setGeometry(hw, layer);
246a537c0f42e8077baafcbc65844adf1ec8397c040Mathias Agopian
2473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // enable this layer
2483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    layer.setSkip(false);
249a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
250a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    // we can't do alpha-fade with the hwc HAL
251a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    const State& s(drawingState());
252a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (s.alpha < 0xFF) {
2533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        layer.setSkip(true);
254a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
255a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
256dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    if (isSecure() && !hw->isSecure()) {
257dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis        layer.setSkip(true);
258dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    }
259dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis
26029a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    /*
26129a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * Transformations are applied in this order:
26229a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 1) buffer orientation/flip/mirror
26329a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 2) state transformation (window manager)
26429a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 3) layer orientation (screen orientation)
26529a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * (NOTE: the matrices are multiplied in reverse order)
26629a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     */
26729a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian
26829a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    const Transform bufferOrientation(mCurrentTransform);
2694297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    const Transform tr(hw->getTransform() * s.transform * bufferOrientation);
27029a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian
27129a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    // this gives us only the "orientation" component of the transform
27229a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    const uint32_t finalTransform = tr.getOrientation();
27329a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian
274a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    // we can only handle simple transformation
27529a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    if (finalTransform & Transform::ROT_INVALID) {
2763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        layer.setSkip(true);
277a537c0f42e8077baafcbc65844adf1ec8397c040Mathias Agopian    } else {
2783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        layer.setTransform(finalTransform);
279a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
2803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    layer.setCrop(computeBufferCrop());
281a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
282a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
2834297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopianvoid Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
284d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        HWComposer::HWCLayerInterface& layer) {
28571e83e1202dc87afde9c0c31f31c129f3f99263fMathias Agopian    LayerBaseClient::setPerFrameData(hw, layer);
2863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // NOTE: buffer can be NULL if the client never drew into this
2873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // layer yet, or if we ran out of memory
28871e83e1202dc87afde9c0c31f31c129f3f99263fMathias Agopian    layer.setBuffer(mActiveBuffer);
289c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall}
290dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall
2914297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopianvoid Layer::setAcquireFence(const sp<const DisplayDevice>& hw,
292d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        HWComposer::HWCLayerInterface& layer) {
293c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall    int fenceFd = -1;
294d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
295d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // TODO: there is a possible optimization here: we only need to set the
296d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // acquire fence the first time a new buffer is acquired on EACH display.
297d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
298d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    if (layer.getCompositionType() == HWC_OVERLAY) {
299dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        sp<Fence> fence = mSurfaceTexture->getCurrentFence();
300dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        if (fence.get()) {
301c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall            fenceFd = fence->dup();
302dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            if (fenceFd == -1) {
303dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall                ALOGW("failed to dup layer fence, skipping sync: %d", errno);
304dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            }
305dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        }
306dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
307c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall    layer.setAcquireFenceFd(fenceFd);
308c7f3381c3b2945e441747130eae88214435d0819Mathias Agopian}
309c7f3381c3b2945e441747130eae88214435d0819Mathias Agopian
3104297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopianvoid Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
311edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
3121c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
3131c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
314a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    if (CC_UNLIKELY(mActiveBuffer == 0)) {
315edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // the texture has not been created yet, this Layer has
316179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // in fact never been drawn into. This happens frequently with
317179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // SurfaceView because the WindowManager can't know when the client
318179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // has drawn the first time.
319179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian
320179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // If there is nothing under us, we paint the screen in black, otherwise
321179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // we just skip this update.
322179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian
323179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // figure out if there is something below us
324179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        Region under;
325f7ae69d4bd292110da976c8ae766a8ef083d731fMathias Agopian        const SurfaceFlinger::LayerVector& drawingLayers(
326f7ae69d4bd292110da976c8ae766a8ef083d731fMathias Agopian                mFlinger->mDrawingState.layersSortedByZ);
327179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        const size_t count = drawingLayers.size();
328179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        for (size_t i=0 ; i<count ; ++i) {
329179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian            const sp<LayerBase>& layer(drawingLayers[i]);
330179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian            if (layer.get() == static_cast<LayerBase const*>(this))
331179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian                break;
3324297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
333179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        }
334179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // if not everything below us is covered, we plug the holes!
335179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        Region holes(clip.subtract(under));
336179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        if (!holes.isEmpty()) {
3371b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian            clearWithOpenGL(hw, holes, 0, 0, 0, 1);
338179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        }
339edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return;
340edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
341a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
34261e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis    status_t err = mSurfaceTexture->doGLFenceWait();
34361e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis    if (err != OK) {
34461e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis        ALOGE("onDraw: failed waiting for fence: %d", err);
345dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        // Go ahead and draw the buffer anyway; no matter what we do the screen
346dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        // is probably going to have something visibly wrong.
347dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
348dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall
349dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
350dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis
351dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    if (!blackOutLayer) {
352cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        // TODO: we could be more subtle with isFixedSize()
353eba8c688f633f3f3f1b75c2bc64faf799dd2b5f2Mathias Agopian        const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
354cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis
355cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        // Query the texture matrix given our current filtering mode.
356cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        float textureMatrix[16];
357cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        mSurfaceTexture->setFilteringEnabled(useFiltering);
358cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        mSurfaceTexture->getTransformMatrix(textureMatrix);
359cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis
360cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        // Set things up for texturing.
361c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian        glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
362c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian        GLenum filter = GL_NEAREST;
363cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        if (useFiltering) {
364c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian            filter = GL_LINEAR;
3659575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis        }
366c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
367c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
3689575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis        glMatrixMode(GL_TEXTURE);
369cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        glLoadMatrixf(textureMatrix);
3709575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis        glMatrixMode(GL_MODELVIEW);
371c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian        glDisable(GL_TEXTURE_2D);
3724c4163b53e262ff9cc10c819321d4ae03c2d7d46Xavier Ducrohet        glEnable(GL_TEXTURE_EXTERNAL_OES);
373a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    } else {
374c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian        glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
3759575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis        glMatrixMode(GL_TEXTURE);
3769575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis        glLoadIdentity();
3779575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis        glMatrixMode(GL_MODELVIEW);
378c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian        glDisable(GL_TEXTURE_EXTERNAL_OES);
379c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian        glEnable(GL_TEXTURE_2D);
380a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    }
381a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
3821b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian    drawWithOpenGL(hw, clip);
383a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
384c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian    glDisable(GL_TEXTURE_EXTERNAL_OES);
385c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian    glDisable(GL_TEXTURE_2D);
386edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
387edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
388ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// As documented in libhardware header, formats in the range
389ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// 0x100 - 0x1FF are specific to the HAL implementation, and
390ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// are known to have no alpha channel
391ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// TODO: move definition for device-specific range into
392ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// hardware.h, instead of using hard-coded values here.
393ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
394ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold
395a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopianbool Layer::getOpacityForFormat(uint32_t format)
396ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold{
397a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    if (HARDWARE_IS_DEVICE_FORMAT(format)) {
398a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        return true;
399ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold    }
400a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    PixelFormatInfo info;
401a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    status_t err = getPixelFormatInfo(PixelFormat(format), &info);
402a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // in case of error (unknown format), we assume no blending
403a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    return (err || info.h_alpha <= info.l_alpha);
404ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold}
405ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold
406ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold
407a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopianbool Layer::isOpaque() const
408a7f669256f93a593c723f05784ef04d3c7d052bbMathias Agopian{
409a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // if we don't have a buffer yet, we're translucent regardless of the
410a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // layer's opaque flag.
411db5230f4441fa8f120f15bdd6fcfc6e75d9c27d0Jamie Gennis    if (mActiveBuffer == 0) {
412a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        return false;
413db5230f4441fa8f120f15bdd6fcfc6e75d9c27d0Jamie Gennis    }
414a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
415a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // if the layer has the opaque flag, then we're always opaque,
416a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // otherwise we use the current buffer's format.
417a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    return mOpaqueLayer || mCurrentOpacity;
418a7f669256f93a593c723f05784ef04d3c7d052bbMathias Agopian}
419a7f669256f93a593c723f05784ef04d3c7d052bbMathias Agopian
4207a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennisbool Layer::isProtected() const
4217a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennis{
422a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
4237a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennis    return (activeBuffer != 0) &&
4247a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennis            (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
4257a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennis}
426b5b7f260da2c1a2a82e0311e2015d49a82f43667Mathias Agopian
427edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectuint32_t Layer::doTransaction(uint32_t flags)
428edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
4291c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
4301c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
431edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const Layer::State& front(drawingState());
432edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const Layer::State& temp(currentState());
433edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
4344824d40a35333182c2eb3593511b9bcbecd0a943Mathias Agopian    const bool sizeChanged = (temp.requested.w != front.requested.w) ||
4354824d40a35333182c2eb3593511b9bcbecd0a943Mathias Agopian                             (temp.requested.h != front.requested.h);
436a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
437a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian    if (sizeChanged) {
438cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian        // the size changed, we need to ask our client to request a new buffer
4399d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block        ALOGD_IF(DEBUG_RESIZE,
4406905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden                "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
441419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                "  current={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
442419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n"
443419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
444419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
4456905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden                this, (const char*) getName(), mCurrentTransform, mCurrentScalingMode,
446419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                temp.active.w, temp.active.h,
447419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                temp.active.crop.left,
448419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                temp.active.crop.top,
449419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                temp.active.crop.right,
450419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                temp.active.crop.bottom,
451419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                temp.active.crop.getWidth(),
452419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                temp.active.crop.getHeight(),
453419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                temp.requested.w, temp.requested.h,
454b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                temp.requested.crop.left,
455b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                temp.requested.crop.top,
456b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                temp.requested.crop.right,
457b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                temp.requested.crop.bottom,
458b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                temp.requested.crop.getWidth(),
459b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                temp.requested.crop.getHeight(),
460419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.active.w, front.active.h,
461419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.active.crop.left,
462419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.active.crop.top,
463419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.active.crop.right,
464419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.active.crop.bottom,
465419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.active.crop.getWidth(),
466419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.active.crop.getHeight(),
467419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.requested.w, front.requested.h,
468b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                front.requested.crop.left,
469b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                front.requested.crop.top,
470b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                front.requested.crop.right,
471b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                front.requested.crop.bottom,
472b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                front.requested.crop.getWidth(),
473419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                front.requested.crop.getHeight());
474a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
4752a0d5b608447a880beff5149805425f02691442bJamie Gennis        // record the new size, form this point on, when the client request
4762a0d5b608447a880beff5149805425f02691442bJamie Gennis        // a buffer, it'll get the new size.
477b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian        mSurfaceTexture->setDefaultBufferSize(
478b30c415539813b96a831b75d07f3d12aef1aeab7Mathias Agopian                temp.requested.w, temp.requested.h);
479edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
480cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
4810cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian    if (!isFixedSize()) {
4820cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
4830cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian        const bool resizePending = (temp.requested.w != temp.active.w) ||
4840cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian                                   (temp.requested.h != temp.active.h);
4850cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
4860cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian        if (resizePending) {
4870cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // don't let LayerBase::doTransaction update the drawing state
4880cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // if we have a pending resize, unless we are in fixed-size mode.
4890cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // the drawing state will be updated only once we receive a buffer
4900cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // with the correct size.
4910cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            //
4920cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // in particular, we want to make sure the clip (which is part
4930cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // of the geometry state) is latched together with the size but is
4940cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // latched immediately when no resizing is involved.
4950cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
4960cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            flags |= eDontUpdateGeometryState;
4970cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian        }
4980cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian    }
4990cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
500edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return LayerBase::doTransaction(flags);
501edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
502edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
503a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopianbool Layer::isFixedSize() const {
504933389f75814bb62e8153528f9cff2cb329b77dfMathias Agopian    return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
505a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian}
506a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
507a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopianbool Layer::isCropped() const {
508a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    return !mCurrentCrop.isEmpty();
509a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian}
510a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
511edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
512edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// pageflip handling...
513edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
514edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
5154d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopianbool Layer::onPreComposition() {
5164d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian    mRefreshPending = false;
5174d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian    return mQueuedFrames > 0;
51899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
51999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
520d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopianvoid Layer::onPostComposition() {
521d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    if (mFrameLatencyNeeded) {
522d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        const HWComposer& hwc = mFlinger->getHwComposer();
523d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        const size_t offset = mFrameLatencyOffset;
524d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        mFrameStats[offset].timestamp = mSurfaceTexture->getTimestamp();
525d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        mFrameStats[offset].set = systemTime();
526b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mFrameStats[offset].vsync = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
527d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
528d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        mFrameLatencyNeeded = false;
529d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    }
530d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
531d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
532da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool Layer::isVisible() const {
533da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return LayerBaseClient::isVisible() && (mActiveBuffer != NULL);
534da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
535da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
5364fec873a98f7b4380720cd1ad006f74c8cdc73daMathias AgopianRegion Layer::latchBuffer(bool& recomputeVisibleRegions)
537edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
5381c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
5391c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
5404fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    Region outDirtyRegion;
5413d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis    if (mQueuedFrames > 0) {
54299ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
54399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // if we've already called updateTexImage() without going through
54499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // a composition step, we have to skip this layer at this point
54599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // because we cannot call updateTeximage() without a corresponding
54699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // compositionComplete() call.
54799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // we'll trigger an update in onPreComposition().
5484d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian        if (mRefreshPending) {
5494fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            return outDirtyRegion;
55099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        }
55199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
552351a513b12622781de9580b3c96fd0a8578b563bJamie Gennis        // Capture the old state of the layer for comparisons later
553db5230f4441fa8f120f15bdd6fcfc6e75d9c27d0Jamie Gennis        const bool oldOpacity = isOpaque();
554351a513b12622781de9580b3c96fd0a8578b563bJamie Gennis        sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
555db5230f4441fa8f120f15bdd6fcfc6e75d9c27d0Jamie Gennis
5563d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        // signal another event if we have more frames pending
5573d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        if (android_atomic_dec(&mQueuedFrames) > 1) {
55899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian            mFlinger->signalLayerUpdate();
5593d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        }
5603d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis
5612c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian        struct Reject : public SurfaceTexture::BufferRejecter {
5622c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            Layer::State& front;
5632c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            Layer::State& current;
5642c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            bool& recomputeVisibleRegions;
5652c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            Reject(Layer::State& front, Layer::State& current,
5662c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    bool& recomputeVisibleRegions)
5672c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                : front(front), current(current),
5682c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                  recomputeVisibleRegions(recomputeVisibleRegions) {
5692c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            }
5702c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
5712c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            virtual bool reject(const sp<GraphicBuffer>& buf,
5722c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    const BufferQueue::BufferItem& item) {
5732c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                if (buf == NULL) {
5742c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    return false;
5752c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                }
5762c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
5772c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                uint32_t bufWidth  = buf->getWidth();
5782c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                uint32_t bufHeight = buf->getHeight();
5792c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
5802c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                // check that we received a buffer of the right size
5812c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                // (Take the buffer's orientation into account)
5822c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                if (item.mTransform & Transform::ROT_90) {
5832c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    swap(bufWidth, bufHeight);
5842c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                }
5852c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
5862c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
5872c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
5882c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                if (front.active != front.requested) {
5892c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
5902c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    if (isFixedSize ||
5912c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            (bufWidth == front.requested.w &&
5922c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                             bufHeight == front.requested.h))
5932c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    {
5942c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // Here we pretend the transaction happened by updating the
5952c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // current and drawing states. Drawing state is only accessed
5962c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // in this thread, no need to have it locked
5972c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        front.active = front.requested;
5982c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
5992c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // We also need to update the current state so that
6002c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // we don't end-up overwriting the drawing state with
6012c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // this stale current state during the next transaction
6022c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        //
6032c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // NOTE: We don't need to hold the transaction lock here
6042c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // because State::active is only accessed from this thread.
6052c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        current.active = front.active;
6062c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
6072c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // recompute visible region
6082c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        recomputeVisibleRegions = true;
6092c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    }
6102c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
6112c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    ALOGD_IF(DEBUG_RESIZE,
6126905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden                            "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
6132c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
6142c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
6156905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden                            bufWidth, bufHeight, item.mTransform, item.mScalingMode,
6162c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.w, front.active.h,
6172c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.left,
6182c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.top,
6192c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.right,
6202c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.bottom,
6212c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.getWidth(),
6222c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.getHeight(),
6232c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.w, front.requested.h,
6242c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.left,
6252c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.top,
6262c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.right,
6272c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.bottom,
6282c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.getWidth(),
6292c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.getHeight());
6302c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                }
6312c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
6322c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                if (!isFixedSize) {
6332c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    if (front.active.w != bufWidth ||
6342c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        front.active.h != bufHeight) {
6354824d40a35333182c2eb3593511b9bcbecd0a943Mathias Agopian                        // reject this buffer
6362c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        return true;
6372c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    }
6382c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                }
6392c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                return false;
6402c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            }
6412c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian        };
6422c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
6432c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
6442c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian        Reject r(mDrawingState, currentState(), recomputeVisibleRegions);
6452c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
6463941cb240d438bfdebe24920bb2ada86456a0bf9Jamie Gennis        if (mSurfaceTexture->updateTexImage(&r, true) < NO_ERROR) {
647a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            // something happened!
648a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            recomputeVisibleRegions = true;
6494fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            return outDirtyRegion;
650a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        }
651d343e3d5e3177806205b9452b0b43907e28afd9aMathias Agopian
652351a513b12622781de9580b3c96fd0a8578b563bJamie Gennis        // update the active buffer
653351a513b12622781de9580b3c96fd0a8578b563bJamie Gennis        mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
654e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian        if (mActiveBuffer == NULL) {
655e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian            // this can only happen if the very first buffer was rejected.
6564fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            return outDirtyRegion;
657e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian        }
658da9584dc295cc5e6d0b49a97c1e45159249d650bMathias Agopian
6594824d40a35333182c2eb3593511b9bcbecd0a943Mathias Agopian        mRefreshPending = true;
660702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        mFrameLatencyNeeded = true;
661e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian        if (oldActiveBuffer == NULL) {
6622c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian             // the first time we receive a buffer, we need to trigger a
6632c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian             // geometry invalidation.
664ab10c5804c250e7f392c8262f687766edec2e9f4Andy McFadden            recomputeVisibleRegions = true;
6652c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian         }
666702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian
667702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        Rect crop(mSurfaceTexture->getCurrentCrop());
6682c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian        const uint32_t transform(mSurfaceTexture->getCurrentTransform());
6692c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian        const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
670702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        if ((crop != mCurrentCrop) ||
671702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            (transform != mCurrentTransform) ||
672702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            (scalingMode != mCurrentScalingMode))
673702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        {
674702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            mCurrentCrop = crop;
675702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            mCurrentTransform = transform;
676702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            mCurrentScalingMode = scalingMode;
677ab10c5804c250e7f392c8262f687766edec2e9f4Andy McFadden            recomputeVisibleRegions = true;
678702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        }
679702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian
680702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        if (oldActiveBuffer != NULL) {
681e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian            uint32_t bufWidth  = mActiveBuffer->getWidth();
682e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian            uint32_t bufHeight = mActiveBuffer->getHeight();
683702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            if (bufWidth != uint32_t(oldActiveBuffer->width) ||
684702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian                bufHeight != uint32_t(oldActiveBuffer->height)) {
685ab10c5804c250e7f392c8262f687766edec2e9f4Andy McFadden                recomputeVisibleRegions = true;
686702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            }
687702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        }
688702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian
689702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
690702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        if (oldOpacity != isOpaque()) {
691702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            recomputeVisibleRegions = true;
692702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        }
693702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian
694702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
695702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
696edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
6974fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // FIXME: postedRegion should be dirty & bounds
6984fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        const Layer::State& front(drawingState());
6994fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        Region dirtyRegion(Rect(front.active.w, front.active.h));
7001c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
7014fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // transform the dirty region to window-manager space
7024fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        outDirtyRegion = (front.transform.transform(dirtyRegion));
703edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
7044fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    return outDirtyRegion;
705edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
706edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
7071b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopianvoid Layer::dump(String8& result, char* buffer, size_t SIZE) const
7081b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian{
7091b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian    LayerBaseClient::dump(result, buffer, SIZE);
7101b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
711a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    sp<const GraphicBuffer> buf0(mActiveBuffer);
712a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    uint32_t w0=0, h0=0, s0=0, f0=0;
7131b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian    if (buf0 != 0) {
7141b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian        w0 = buf0->getWidth();
7151b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian        h0 = buf0->getHeight();
7161b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian        s0 = buf0->getStride();
717a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        f0 = buf0->format;
7181b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian    }
7191b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian    snprintf(buffer, SIZE,
7201b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian            "      "
721ad795baecccf239621cbffa0249c8e855296cae6Mathias Agopian            "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
7226905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden            " queued-frames=%d, mRefreshPending=%d\n",
723a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            mFormat, w0, h0, s0,f0,
7246905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden            mQueuedFrames, mRefreshPending);
7251b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
7261b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian    result.append(buffer);
727d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian
728a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    if (mSurfaceTexture != 0) {
729a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mSurfaceTexture->dump(result, "            ", buffer, SIZE);
730bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian    }
731d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian}
732d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian
73382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopianvoid Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
73482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian{
73582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    LayerBaseClient::dumpStats(result, buffer, SIZE);
73682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const size_t o = mFrameLatencyOffset;
737b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    const nsecs_t period =
738b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mFlinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
73982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.appendFormat("%lld\n", period);
74082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    for (size_t i=0 ; i<128 ; i++) {
74182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        const size_t index = (o+i) % 128;
74282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        const nsecs_t time_app   = mFrameStats[index].timestamp;
74382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        const nsecs_t time_set   = mFrameStats[index].set;
74482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        const nsecs_t time_vsync = mFrameStats[index].vsync;
74582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        result.appendFormat("%lld\t%lld\t%lld\n",
74682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                time_app,
74782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                time_vsync,
74882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                time_set);
74982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
75082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append("\n");
75182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian}
75282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
75325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopianvoid Layer::clearStats()
75425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian{
75525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    LayerBaseClient::clearStats();
75625e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    memset(mFrameStats, 0, sizeof(mFrameStats));
75725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian}
75825e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
759a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopianuint32_t Layer::getEffectiveUsage(uint32_t usage) const
760b5b7f260da2c1a2a82e0311e2015d49a82f43667Mathias Agopian{
761a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // TODO: should we do something special if mSecure is set?
762a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    if (mProtectedByApp) {
763a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        // need a hardware-protected path to external video sink
764a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        usage |= GraphicBuffer::USAGE_PROTECTED;
765b5b7f260da2c1a2a82e0311e2015d49a82f43667Mathias Agopian    }
7663599bf2c0727bc33e8136f5163eee6f398545e05Jamie Gennis    usage |= GraphicBuffer::USAGE_HW_COMPOSER;
767a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    return usage;
768b5b7f260da2c1a2a82e0311e2015d49a82f43667Mathias Agopian}
769b5b7f260da2c1a2a82e0311e2015d49a82f43667Mathias Agopian
7706905205c8d130b6ea3a813c1b9283492ed183367Andy McFaddenvoid Layer::updateTransformHint() const {
771a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian    uint32_t orientation = 0;
772a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian    if (!mFlinger->mDebugDisableTransformHint) {
7734fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // The transform hint is used to improve performance on the main
7744fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // display -- we can only have a single transform hint, it cannot
7754fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // apply to all displays.
7764fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // This is why we use the default display here. This is not an
7774fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // oversight.
7784297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
7794297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        const Transform& planeTransform(hw->getTransform());
7804fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        orientation = planeTransform.getOrientation();
781a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian        if (orientation & Transform::ROT_INVALID) {
782a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian            orientation = 0;
783a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian        }
784a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian    }
7856905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden    mSurfaceTexture->setTransformHint(orientation);
786a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian}
787a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian
788edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
789edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
790edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
791edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}; // namespace android
792