Layer.cpp revision a537c0f42e8077baafcbc65844adf1ec8397c040
1648a1c137663ef7207684d0d7009dd5518942111Stephen Hines/*
2648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * Copyright (C) 2007 The Android Open Source Project
3648a1c137663ef7207684d0d7009dd5518942111Stephen Hines *
4648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * Licensed under the Apache License, Version 2.0 (the "License");
5648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * you may not use this file except in compliance with the License.
6648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * You may obtain a copy of the License at
7648a1c137663ef7207684d0d7009dd5518942111Stephen Hines *
8648a1c137663ef7207684d0d7009dd5518942111Stephen Hines *      http://www.apache.org/licenses/LICENSE-2.0
9648a1c137663ef7207684d0d7009dd5518942111Stephen Hines *
10648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * Unless required by applicable law or agreed to in writing, software
11648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * distributed under the License is distributed on an "AS IS" BASIS,
12648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * See the License for the specific language governing permissions and
14648a1c137663ef7207684d0d7009dd5518942111Stephen Hines * limitations under the License.
15648a1c137663ef7207684d0d7009dd5518942111Stephen Hines */
16648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
17648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <stdlib.h>
18648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <stdint.h>
19648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <sys/types.h>
20648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
21648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <cutils/compiler.h>
22648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <cutils/native_handle.h>
23648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <cutils/properties.h>
24648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
25648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <utils/Errors.h>
26648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <utils/Log.h>
27648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <utils/StopWatch.h>
28648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
29648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <ui/GraphicBuffer.h>
30648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <ui/PixelFormat.h>
31648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
32648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include <surfaceflinger/Surface.h>
33648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
34648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include "clz.h"
35648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include "DisplayHardware/DisplayHardware.h"
36648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include "DisplayHardware/HWComposer.h"
37648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include "GLExtensions.h"
38648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include "Layer.h"
39648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include "SurfaceFlinger.h"
40648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#include "SurfaceTextureLayer.h"
41648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
42648a1c137663ef7207684d0d7009dd5518942111Stephen Hines#define DEBUG_RESIZE    0
43648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
44648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
45648a1c137663ef7207684d0d7009dd5518942111Stephen Hinesnamespace android {
46648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
47648a1c137663ef7207684d0d7009dd5518942111Stephen Hines// ---------------------------------------------------------------------------
48648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
49648a1c137663ef7207684d0d7009dd5518942111Stephen HinesLayer::Layer(SurfaceFlinger* flinger,
50648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        DisplayID display, const sp<Client>& client)
51648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    :   LayerBaseClient(flinger, display, client),
52648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mTextureName(-1U),
53648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mQueuedFrames(0),
54648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mCurrentTransform(0),
55648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
56648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mCurrentOpacity(true),
57648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mFormat(PIXEL_FORMAT_NONE),
58648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mGLExtensions(GLExtensions::getInstance()),
59648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mOpaqueLayer(true),
60648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mNeedsDithering(false),
61648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mSecure(false),
62648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        mProtectedByApp(false)
63648a1c137663ef7207684d0d7009dd5518942111Stephen Hines{
64648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mCurrentCrop.makeInvalid();
65648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    glGenTextures(1, &mTextureName);
66648a1c137663ef7207684d0d7009dd5518942111Stephen Hines}
67648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
68648a1c137663ef7207684d0d7009dd5518942111Stephen Hinesvoid Layer::destroy(RefBase const* base) {
69648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mFlinger->destroyLayer(static_cast<LayerBase const*>(base));
70648a1c137663ef7207684d0d7009dd5518942111Stephen Hines}
71648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
72648a1c137663ef7207684d0d7009dd5518942111Stephen Hinesvoid Layer::onFirstRef()
73648a1c137663ef7207684d0d7009dd5518942111Stephen Hines{
74648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    LayerBaseClient::onFirstRef();
75648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    setDestroyer(this);
76648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
77648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
78648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        FrameQueuedListener(Layer* layer) : mLayer(layer) { }
79648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    private:
80648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        wp<Layer> mLayer;
81648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        virtual void onFrameAvailable() {
82648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            sp<Layer> that(mLayer.promote());
83648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            if (that != 0) {
84648a1c137663ef7207684d0d7009dd5518942111Stephen Hines                that->onFrameQueued();
85648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            }
86648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        }
87648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    };
88648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
89648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
90648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mSurfaceTexture->setSynchronousMode(true);
91648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mSurfaceTexture->setBufferCountServer(2);
92648a1c137663ef7207684d0d7009dd5518942111Stephen Hines}
93648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
94648a1c137663ef7207684d0d7009dd5518942111Stephen HinesLayer::~Layer()
95648a1c137663ef7207684d0d7009dd5518942111Stephen Hines{
96648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    glDeleteTextures(1, &mTextureName);
97648a1c137663ef7207684d0d7009dd5518942111Stephen Hines}
98648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
99648a1c137663ef7207684d0d7009dd5518942111Stephen Hinesvoid Layer::onFrameQueued() {
100648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    android_atomic_inc(&mQueuedFrames);
101648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mFlinger->signalEvent();
102648a1c137663ef7207684d0d7009dd5518942111Stephen Hines}
103648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
104648a1c137663ef7207684d0d7009dd5518942111Stephen Hines// called with SurfaceFlinger::mStateLock as soon as the layer is entered
105648a1c137663ef7207684d0d7009dd5518942111Stephen Hines// in the purgatory list
106648a1c137663ef7207684d0d7009dd5518942111Stephen Hinesvoid Layer::onRemoved()
107648a1c137663ef7207684d0d7009dd5518942111Stephen Hines{
108648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mSurfaceTexture->abandon();
109648a1c137663ef7207684d0d7009dd5518942111Stephen Hines}
110648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
111648a1c137663ef7207684d0d7009dd5518942111Stephen Hinessp<ISurface> Layer::createSurface()
112648a1c137663ef7207684d0d7009dd5518942111Stephen Hines{
113648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    class BSurface : public BnSurface, public LayerCleaner {
114648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        wp<const Layer> mOwner;
115648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        virtual sp<ISurfaceTexture> getSurfaceTexture() const {
116648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            sp<ISurfaceTexture> res;
117648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            sp<const Layer> that( mOwner.promote() );
118648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            if (that != NULL) {
119648a1c137663ef7207684d0d7009dd5518942111Stephen Hines                res = that->mSurfaceTexture;
120648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            }
121648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            return res;
122648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        }
123648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    public:
124648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        BSurface(const sp<SurfaceFlinger>& flinger,
125648a1c137663ef7207684d0d7009dd5518942111Stephen Hines                const sp<Layer>& layer)
126648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            : LayerCleaner(flinger, layer), mOwner(layer) { }
127648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    };
128648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    sp<ISurface> sur(new BSurface(mFlinger, this));
129648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    return sur;
130648a1c137663ef7207684d0d7009dd5518942111Stephen Hines}
131648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
132648a1c137663ef7207684d0d7009dd5518942111Stephen Hinesstatus_t Layer::setBuffers( uint32_t w, uint32_t h,
133648a1c137663ef7207684d0d7009dd5518942111Stephen Hines                            PixelFormat format, uint32_t flags)
134648a1c137663ef7207684d0d7009dd5518942111Stephen Hines{
135648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    // this surfaces pixel format
136648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    PixelFormatInfo info;
137648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    status_t err = getPixelFormatInfo(format, &info);
138648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    if (err) return err;
139648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
140648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    // the display's pixel format
141648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    const DisplayHardware& hw(graphicPlane(0).displayHardware());
142648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    uint32_t const maxSurfaceDims = min(
143648a1c137663ef7207684d0d7009dd5518942111Stephen Hines            hw.getMaxTextureSize(), hw.getMaxViewportDims());
144648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
145648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    // never allow a surface larger than what our underlying GL implementation
146648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    // can handle.
147648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
148648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        return BAD_VALUE;
149648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    }
150648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
151648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    PixelFormatInfo displayInfo;
152648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    getPixelFormatInfo(hw.getFormat(), &displayInfo);
153648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    const uint32_t hwFlags = hw.getFlags();
154648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
155648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mFormat = format;
156648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
157648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
158648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
159648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
160648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mCurrentOpacity = getOpacityForFormat(format);
161648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
162648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mSurfaceTexture->setDefaultBufferSize(w, h);
163648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mSurfaceTexture->setDefaultBufferFormat(format);
164648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
165648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    // we use the red index
166648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
167648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
168648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    mNeedsDithering = layerRedsize > displayRedSize;
169648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
170648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    return NO_ERROR;
171648a1c137663ef7207684d0d7009dd5518942111Stephen Hines}
172648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
173648a1c137663ef7207684d0d7009dd5518942111Stephen Hinesvoid Layer::setGeometry(hwc_layer_t* hwcl)
174648a1c137663ef7207684d0d7009dd5518942111Stephen Hines{
175648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    LayerBaseClient::setGeometry(hwcl);
176648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
177648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    hwcl->flags &= ~HWC_SKIP_LAYER;
178648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
179648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    // we can't do alpha-fade with the hwc HAL
180648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    const State& s(drawingState());
181648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    if (s.alpha < 0xFF) {
182648a1c137663ef7207684d0d7009dd5518942111Stephen Hines        hwcl->flags = HWC_SKIP_LAYER;
183648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    }
184648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
185648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    /*
186648a1c137663ef7207684d0d7009dd5518942111Stephen Hines     * Transformations are applied in this order:
187648a1c137663ef7207684d0d7009dd5518942111Stephen Hines     * 1) buffer orientation/flip/mirror
188648a1c137663ef7207684d0d7009dd5518942111Stephen Hines     * 2) state transformation (window manager)
189648a1c137663ef7207684d0d7009dd5518942111Stephen Hines     * 3) layer orientation (screen orientation)
190648a1c137663ef7207684d0d7009dd5518942111Stephen Hines     * (NOTE: the matrices are multiplied in reverse order)
191648a1c137663ef7207684d0d7009dd5518942111Stephen Hines     */
192648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
193648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    const Transform bufferOrientation(mCurrentTransform);
194648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    const Transform& stateTransform(s.transform);
195648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    const Transform layerOrientation(mOrientation);
196648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
197648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    const Transform tr(layerOrientation * stateTransform * bufferOrientation);
198648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
199648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    // this gives us only the "orientation" component of the transform
200648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    const uint32_t finalTransform = tr.getOrientation();
201648a1c137663ef7207684d0d7009dd5518942111Stephen Hines
202648a1c137663ef7207684d0d7009dd5518942111Stephen Hines    // we can only handle simple transformation
203    if (finalTransform & Transform::ROT_INVALID) {
204        hwcl->flags = HWC_SKIP_LAYER;
205    } else {
206        hwcl->transform = finalTransform;
207    }
208}
209
210void Layer::setPerFrameData(hwc_layer_t* hwcl) {
211    const sp<GraphicBuffer>& buffer(mActiveBuffer);
212    if (buffer == NULL) {
213        // this can happen if the client never drew into this layer yet,
214        // or if we ran out of memory. In that case, don't let
215        // HWC handle it.
216        hwcl->flags |= HWC_SKIP_LAYER;
217        hwcl->handle = NULL;
218    } else {
219        hwcl->handle = buffer->handle;
220    }
221
222    if (isCropped()) {
223        hwcl->sourceCrop.left   = mCurrentCrop.left;
224        hwcl->sourceCrop.top    = mCurrentCrop.top;
225        hwcl->sourceCrop.right  = mCurrentCrop.right;
226        hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
227    } else {
228        hwcl->sourceCrop.left   = 0;
229        hwcl->sourceCrop.top    = 0;
230        hwcl->sourceCrop.right  = buffer->width;
231        hwcl->sourceCrop.bottom = buffer->height;
232    }
233}
234
235static inline uint16_t pack565(int r, int g, int b) {
236    return (r<<11)|(g<<5)|b;
237}
238void Layer::onDraw(const Region& clip) const
239{
240    if (CC_UNLIKELY(mActiveBuffer == 0)) {
241        // the texture has not been created yet, this Layer has
242        // in fact never been drawn into. This happens frequently with
243        // SurfaceView because the WindowManager can't know when the client
244        // has drawn the first time.
245
246        // If there is nothing under us, we paint the screen in black, otherwise
247        // we just skip this update.
248
249        // figure out if there is something below us
250        Region under;
251        const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
252        const size_t count = drawingLayers.size();
253        for (size_t i=0 ; i<count ; ++i) {
254            const sp<LayerBase>& layer(drawingLayers[i]);
255            if (layer.get() == static_cast<LayerBase const*>(this))
256                break;
257            under.orSelf(layer->visibleRegionScreen);
258        }
259        // if not everything below us is covered, we plug the holes!
260        Region holes(clip.subtract(under));
261        if (!holes.isEmpty()) {
262            clearWithOpenGL(holes, 0, 0, 0, 1);
263        }
264        return;
265    }
266
267    GLenum target = mSurfaceTexture->getCurrentTextureTarget();
268    glBindTexture(target, mTextureName);
269    if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
270        // TODO: we could be more subtle with isFixedSize()
271        glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
272        glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
273    } else {
274        glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
275        glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
276    }
277    glEnable(target);
278    glMatrixMode(GL_TEXTURE);
279    glLoadMatrixf(mTextureMatrix);
280    glMatrixMode(GL_MODELVIEW);
281
282    drawWithOpenGL(clip);
283
284    glDisable(target);
285}
286
287// As documented in libhardware header, formats in the range
288// 0x100 - 0x1FF are specific to the HAL implementation, and
289// are known to have no alpha channel
290// TODO: move definition for device-specific range into
291// hardware.h, instead of using hard-coded values here.
292#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
293
294bool Layer::getOpacityForFormat(uint32_t format)
295{
296    if (HARDWARE_IS_DEVICE_FORMAT(format)) {
297        return true;
298    }
299    PixelFormatInfo info;
300    status_t err = getPixelFormatInfo(PixelFormat(format), &info);
301    // in case of error (unknown format), we assume no blending
302    return (err || info.h_alpha <= info.l_alpha);
303}
304
305
306bool Layer::isOpaque() const
307{
308    // if we don't have a buffer yet, we're translucent regardless of the
309    // layer's opaque flag.
310    if (mActiveBuffer == 0) {
311        return false;
312    }
313
314    // if the layer has the opaque flag, then we're always opaque,
315    // otherwise we use the current buffer's format.
316    return mOpaqueLayer || mCurrentOpacity;
317}
318
319bool Layer::isProtected() const
320{
321    const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
322    return (activeBuffer != 0) &&
323            (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
324}
325
326uint32_t Layer::doTransaction(uint32_t flags)
327{
328    const Layer::State& front(drawingState());
329    const Layer::State& temp(currentState());
330
331    const bool sizeChanged = (front.requested_w != temp.requested_w) ||
332            (front.requested_h != temp.requested_h);
333
334    if (sizeChanged) {
335        // the size changed, we need to ask our client to request a new buffer
336        LOGD_IF(DEBUG_RESIZE,
337                "doTransaction: "
338                "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
339                "scalingMode=%d",
340                this,
341                int(temp.requested_w), int(temp.requested_h),
342                int(front.requested_w), int(front.requested_h),
343                mCurrentScalingMode);
344
345        if (!isFixedSize()) {
346            // we're being resized and there is a freeze display request,
347            // acquire a freeze lock, so that the screen stays put
348            // until we've redrawn at the new size; this is to avoid
349            // glitches upon orientation changes.
350            if (mFlinger->hasFreezeRequest()) {
351                // if the surface is hidden, don't try to acquire the
352                // freeze lock, since hidden surfaces may never redraw
353                if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
354                    mFreezeLock = mFlinger->getFreezeLock();
355                }
356            }
357
358            // this will make sure LayerBase::doTransaction doesn't update
359            // the drawing state's size
360            Layer::State& editDraw(mDrawingState);
361            editDraw.requested_w = temp.requested_w;
362            editDraw.requested_h = temp.requested_h;
363
364            // record the new size, form this point on, when the client request
365            // a buffer, it'll get the new size.
366            mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
367        }
368    }
369
370    if (temp.sequence != front.sequence) {
371        if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
372            // this surface is now hidden, so it shouldn't hold a freeze lock
373            // (it may never redraw, which is fine if it is hidden)
374            mFreezeLock.clear();
375        }
376    }
377
378    return LayerBase::doTransaction(flags);
379}
380
381bool Layer::isFixedSize() const {
382    return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
383}
384
385bool Layer::isCropped() const {
386    return !mCurrentCrop.isEmpty();
387}
388
389// ----------------------------------------------------------------------------
390// pageflip handling...
391// ----------------------------------------------------------------------------
392
393void Layer::lockPageFlip(bool& recomputeVisibleRegions)
394{
395    if (mQueuedFrames > 0) {
396        const bool oldOpacity = isOpaque();
397
398        // signal another event if we have more frames pending
399        if (android_atomic_dec(&mQueuedFrames) > 1) {
400            mFlinger->signalEvent();
401        }
402
403        if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
404            // something happened!
405            recomputeVisibleRegions = true;
406            return;
407        }
408
409        mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
410        mSurfaceTexture->getTransformMatrix(mTextureMatrix);
411
412        const Rect crop(mSurfaceTexture->getCurrentCrop());
413        const uint32_t transform(mSurfaceTexture->getCurrentTransform());
414        const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
415        if ((crop != mCurrentCrop) ||
416            (transform != mCurrentTransform) ||
417            (scalingMode != mCurrentScalingMode))
418        {
419            mCurrentCrop = crop;
420            mCurrentTransform = transform;
421            mCurrentScalingMode = scalingMode;
422            mFlinger->invalidateHwcGeometry();
423        }
424
425        mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
426        if (oldOpacity != isOpaque()) {
427            recomputeVisibleRegions = true;
428        }
429
430        const GLenum target(mSurfaceTexture->getCurrentTextureTarget());
431        glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
432        glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
433
434        // update the layer size and release freeze-lock
435        const Layer::State& front(drawingState());
436
437        // FIXME: mPostedDirtyRegion = dirty & bounds
438        mPostedDirtyRegion.set(front.w, front.h);
439
440
441        if ((front.w != front.requested_w) ||
442            (front.h != front.requested_h))
443        {
444            // check that we received a buffer of the right size
445            // (Take the buffer's orientation into account)
446            sp<GraphicBuffer> newFrontBuffer(mActiveBuffer);
447            uint32_t bufWidth  = newFrontBuffer->getWidth();
448            uint32_t bufHeight = newFrontBuffer->getHeight();
449            if (mCurrentTransform & Transform::ROT_90) {
450                swap(bufWidth, bufHeight);
451            }
452
453            if (isFixedSize() ||
454                    (bufWidth == front.requested_w &&
455                    bufHeight == front.requested_h))
456            {
457                // Here we pretend the transaction happened by updating the
458                // current and drawing states. Drawing state is only accessed
459                // in this thread, no need to have it locked
460                Layer::State& editDraw(mDrawingState);
461                editDraw.w = editDraw.requested_w;
462                editDraw.h = editDraw.requested_h;
463
464                // We also need to update the current state so that we don't
465                // end-up doing too much work during the next transaction.
466                // NOTE: We actually don't need hold the transaction lock here
467                // because State::w and State::h are only accessed from
468                // this thread
469                Layer::State& editTemp(currentState());
470                editTemp.w = editDraw.w;
471                editTemp.h = editDraw.h;
472
473                // recompute visible region
474                recomputeVisibleRegions = true;
475
476                // we now have the correct size, unfreeze the screen
477                mFreezeLock.clear();
478            }
479
480            LOGD_IF(DEBUG_RESIZE,
481                    "lockPageFlip : "
482                    "       (layer=%p), buffer (%ux%u, tr=%02x), "
483                    "requested (%dx%d)",
484                    this,
485                    bufWidth, bufHeight, mCurrentTransform,
486                    front.requested_w, front.requested_h);
487        }
488    }
489}
490
491void Layer::unlockPageFlip(
492        const Transform& planeTransform, Region& outDirtyRegion)
493{
494    Region dirtyRegion(mPostedDirtyRegion);
495    if (!dirtyRegion.isEmpty()) {
496        mPostedDirtyRegion.clear();
497        // The dirty region is given in the layer's coordinate space
498        // transform the dirty region by the surface's transformation
499        // and the global transformation.
500        const Layer::State& s(drawingState());
501        const Transform tr(planeTransform * s.transform);
502        dirtyRegion = tr.transform(dirtyRegion);
503
504        // At this point, the dirty region is in screen space.
505        // Make sure it's constrained by the visible region (which
506        // is in screen space as well).
507        dirtyRegion.andSelf(visibleRegionScreen);
508        outDirtyRegion.orSelf(dirtyRegion);
509    }
510    if (visibleRegionScreen.isEmpty()) {
511        // an invisible layer should not hold a freeze-lock
512        // (because it may never be updated and therefore never release it)
513        mFreezeLock.clear();
514    }
515}
516
517void Layer::dump(String8& result, char* buffer, size_t SIZE) const
518{
519    LayerBaseClient::dump(result, buffer, SIZE);
520
521    sp<const GraphicBuffer> buf0(mActiveBuffer);
522    uint32_t w0=0, h0=0, s0=0, f0=0;
523    if (buf0 != 0) {
524        w0 = buf0->getWidth();
525        h0 = buf0->getHeight();
526        s0 = buf0->getStride();
527        f0 = buf0->format;
528    }
529    snprintf(buffer, SIZE,
530            "      "
531            "format=%2d, activeBuffer=[%3ux%3u:%3u,%3u],"
532            " freezeLock=%p, queued-frames=%d\n",
533            mFormat, w0, h0, s0,f0,
534            getFreezeLock().get(), mQueuedFrames);
535
536    result.append(buffer);
537
538    if (mSurfaceTexture != 0) {
539        mSurfaceTexture->dump(result, "            ", buffer, SIZE);
540    }
541}
542
543uint32_t Layer::getEffectiveUsage(uint32_t usage) const
544{
545    // TODO: should we do something special if mSecure is set?
546    if (mProtectedByApp) {
547        // need a hardware-protected path to external video sink
548        usage |= GraphicBuffer::USAGE_PROTECTED;
549    }
550    return usage;
551}
552
553// ---------------------------------------------------------------------------
554
555
556}; // namespace android
557