Layer.cpp revision 2dcb363c6e86b463a9f4e5925746e2f1089aef16
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
22#include <stdlib.h>
23#include <stdint.h>
24#include <sys/types.h>
25#include <math.h>
26
27#include <cutils/compiler.h>
28#include <cutils/native_handle.h>
29#include <cutils/properties.h>
30
31#include <utils/Errors.h>
32#include <utils/Log.h>
33#include <utils/NativeHandle.h>
34#include <utils/StopWatch.h>
35#include <utils/Trace.h>
36
37#include <ui/GraphicBuffer.h>
38#include <ui/PixelFormat.h>
39
40#include <gui/BufferItem.h>
41#include <gui/Surface.h>
42
43#include "clz.h"
44#include "Colorizer.h"
45#include "DisplayDevice.h"
46#include "Layer.h"
47#include "MonitoredProducer.h"
48#include "SurfaceFlinger.h"
49
50#include "DisplayHardware/HWComposer.h"
51
52#include "RenderEngine/RenderEngine.h"
53
54#define DEBUG_RESIZE    0
55
56namespace android {
57
58// ---------------------------------------------------------------------------
59
60int32_t Layer::sSequence = 1;
61
62Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
63        const String8& name, uint32_t w, uint32_t h, uint32_t flags)
64    :   contentDirty(false),
65        sequence(uint32_t(android_atomic_inc(&sSequence))),
66        mFlinger(flinger),
67        mTextureName(-1U),
68        mPremultipliedAlpha(true),
69        mName("unnamed"),
70        mFormat(PIXEL_FORMAT_NONE),
71        mTransactionFlags(0),
72        mPendingStateMutex(),
73        mPendingStates(),
74        mQueuedFrames(0),
75        mSidebandStreamChanged(false),
76        mCurrentTransform(0),
77        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
78        mCurrentOpacity(true),
79        mCurrentFrameNumber(0),
80        mRefreshPending(false),
81        mFrameLatencyNeeded(false),
82        mFiltering(false),
83        mNeedsFiltering(false),
84        mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
85#ifndef USE_HWC2
86        mIsGlesComposition(false),
87#endif
88        mProtectedByApp(false),
89        mHasSurface(false),
90        mClientRef(client),
91        mPotentialCursor(false),
92        mQueueItemLock(),
93        mQueueItemCondition(),
94        mQueueItems(),
95        mLastFrameNumberReceived(0),
96        mUpdateTexImageFailed(false),
97        mAutoRefresh(false)
98{
99#ifdef USE_HWC2
100    ALOGV("Creating Layer %s", name.string());
101#endif
102
103    mCurrentCrop.makeInvalid();
104    mFlinger->getRenderEngine().genTextures(1, &mTextureName);
105    mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
106
107    uint32_t layerFlags = 0;
108    if (flags & ISurfaceComposerClient::eHidden)
109        layerFlags |= layer_state_t::eLayerHidden;
110    if (flags & ISurfaceComposerClient::eOpaque)
111        layerFlags |= layer_state_t::eLayerOpaque;
112    if (flags & ISurfaceComposerClient::eSecure)
113        layerFlags |= layer_state_t::eLayerSecure;
114
115    if (flags & ISurfaceComposerClient::eNonPremultiplied)
116        mPremultipliedAlpha = false;
117
118    mName = name;
119
120    mCurrentState.active.w = w;
121    mCurrentState.active.h = h;
122    mCurrentState.active.transform.set(0, 0);
123    mCurrentState.active.crop.makeInvalid();
124    mCurrentState.active.finalCrop.makeInvalid();
125    mCurrentState.z = 0;
126#ifdef USE_HWC2
127    mCurrentState.alpha = 1.0f;
128#else
129    mCurrentState.alpha = 0xFF;
130#endif
131    mCurrentState.layerStack = 0;
132    mCurrentState.flags = layerFlags;
133    mCurrentState.sequence = 0;
134    mCurrentState.requested = mCurrentState.active;
135
136    // drawing state & current state are identical
137    mDrawingState = mCurrentState;
138
139#ifdef USE_HWC2
140    const auto& hwc = flinger->getHwComposer();
141    const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
142    nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
143#else
144    nsecs_t displayPeriod =
145            flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
146#endif
147    mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
148}
149
150void Layer::onFirstRef() {
151    // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
152    sp<IGraphicBufferProducer> producer;
153    sp<IGraphicBufferConsumer> consumer;
154    BufferQueue::createBufferQueue(&producer, &consumer);
155    mProducer = new MonitoredProducer(producer, mFlinger);
156    mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName);
157    mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
158    mSurfaceFlingerConsumer->setContentsChangedListener(this);
159    mSurfaceFlingerConsumer->setName(mName);
160
161#ifndef TARGET_DISABLE_TRIPLE_BUFFERING
162    mProducer->setMaxDequeuedBufferCount(2);
163#endif
164
165    const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
166    updateTransformHint(hw);
167}
168
169Layer::~Layer() {
170  sp<Client> c(mClientRef.promote());
171    if (c != 0) {
172        c->detachLayer(this);
173    }
174
175    for (auto& point : mRemoteSyncPoints) {
176        point->setTransactionApplied();
177    }
178    mFlinger->deleteTextureAsync(mTextureName);
179    mFrameTracker.logAndResetStats(mName);
180}
181
182// ---------------------------------------------------------------------------
183// callbacks
184// ---------------------------------------------------------------------------
185
186#ifdef USE_HWC2
187void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
188    if (mHwcLayers.empty()) {
189        return;
190    }
191    mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
192}
193#else
194void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
195        HWComposer::HWCLayerInterface* layer) {
196    if (layer) {
197        layer->onDisplayed();
198        mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
199    }
200}
201#endif
202
203void Layer::onFrameAvailable(const BufferItem& item) {
204    // Add this buffer from our internal queue tracker
205    { // Autolock scope
206        Mutex::Autolock lock(mQueueItemLock);
207
208        // Reset the frame number tracker when we receive the first buffer after
209        // a frame number reset
210        if (item.mFrameNumber == 1) {
211            mLastFrameNumberReceived = 0;
212        }
213
214        // Ensure that callbacks are handled in order
215        while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
216            status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
217                    ms2ns(500));
218            if (result != NO_ERROR) {
219                ALOGE("[%s] Timed out waiting on callback", mName.string());
220            }
221        }
222
223        mQueueItems.push_back(item);
224        android_atomic_inc(&mQueuedFrames);
225
226        // Wake up any pending callbacks
227        mLastFrameNumberReceived = item.mFrameNumber;
228        mQueueItemCondition.broadcast();
229    }
230
231    mFlinger->signalLayerUpdate();
232}
233
234void Layer::onFrameReplaced(const BufferItem& item) {
235    { // Autolock scope
236        Mutex::Autolock lock(mQueueItemLock);
237
238        // Ensure that callbacks are handled in order
239        while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
240            status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
241                    ms2ns(500));
242            if (result != NO_ERROR) {
243                ALOGE("[%s] Timed out waiting on callback", mName.string());
244            }
245        }
246
247        if (mQueueItems.empty()) {
248            ALOGE("Can't replace a frame on an empty queue");
249            return;
250        }
251        mQueueItems.editItemAt(0) = item;
252
253        // Wake up any pending callbacks
254        mLastFrameNumberReceived = item.mFrameNumber;
255        mQueueItemCondition.broadcast();
256    }
257}
258
259void Layer::onSidebandStreamChanged() {
260    if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
261        // mSidebandStreamChanged was false
262        mFlinger->signalLayerUpdate();
263    }
264}
265
266// called with SurfaceFlinger::mStateLock from the drawing thread after
267// the layer has been remove from the current state list (and just before
268// it's removed from the drawing state list)
269void Layer::onRemoved() {
270    mSurfaceFlingerConsumer->abandon();
271}
272
273// ---------------------------------------------------------------------------
274// set-up
275// ---------------------------------------------------------------------------
276
277const String8& Layer::getName() const {
278    return mName;
279}
280
281status_t Layer::setBuffers( uint32_t w, uint32_t h,
282                            PixelFormat format, uint32_t flags)
283{
284    uint32_t const maxSurfaceDims = min(
285            mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
286
287    // never allow a surface larger than what our underlying GL implementation
288    // can handle.
289    if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
290        ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
291        return BAD_VALUE;
292    }
293
294    mFormat = format;
295
296    mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
297    mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
298    mCurrentOpacity = getOpacityForFormat(format);
299
300    mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
301    mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
302    mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
303
304    return NO_ERROR;
305}
306
307/*
308 * The layer handle is just a BBinder object passed to the client
309 * (remote process) -- we don't keep any reference on our side such that
310 * the dtor is called when the remote side let go of its reference.
311 *
312 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
313 * this layer when the handle is destroyed.
314 */
315class Layer::Handle : public BBinder, public LayerCleaner {
316    public:
317        Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
318            : LayerCleaner(flinger, layer), owner(layer) {}
319
320        wp<Layer> owner;
321};
322
323sp<IBinder> Layer::getHandle() {
324    Mutex::Autolock _l(mLock);
325
326    LOG_ALWAYS_FATAL_IF(mHasSurface,
327            "Layer::getHandle() has already been called");
328
329    mHasSurface = true;
330
331    return new Handle(mFlinger, this);
332}
333
334sp<IGraphicBufferProducer> Layer::getProducer() const {
335    return mProducer;
336}
337
338// ---------------------------------------------------------------------------
339// h/w composer set-up
340// ---------------------------------------------------------------------------
341
342Rect Layer::getContentCrop() const {
343    // this is the crop rectangle that applies to the buffer
344    // itself (as opposed to the window)
345    Rect crop;
346    if (!mCurrentCrop.isEmpty()) {
347        // if the buffer crop is defined, we use that
348        crop = mCurrentCrop;
349    } else if (mActiveBuffer != NULL) {
350        // otherwise we use the whole buffer
351        crop = mActiveBuffer->getBounds();
352    } else {
353        // if we don't have a buffer yet, we use an empty/invalid crop
354        crop.makeInvalid();
355    }
356    return crop;
357}
358
359static Rect reduce(const Rect& win, const Region& exclude) {
360    if (CC_LIKELY(exclude.isEmpty())) {
361        return win;
362    }
363    if (exclude.isRect()) {
364        return win.reduce(exclude.getBounds());
365    }
366    return Region(win).subtract(exclude).getBounds();
367}
368
369Rect Layer::computeBounds() const {
370    const Layer::State& s(getDrawingState());
371    return computeBounds(s.activeTransparentRegion);
372}
373
374Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
375    const Layer::State& s(getDrawingState());
376    Rect win(s.active.w, s.active.h);
377    if (!s.active.crop.isEmpty()) {
378        win.intersect(s.active.crop, &win);
379    }
380    // subtract the transparent region and snap to the bounds
381    return reduce(win, activeTransparentRegion);
382}
383
384FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
385    // the content crop is the area of the content that gets scaled to the
386    // layer's size.
387    FloatRect crop(getContentCrop());
388
389    // the active.crop is the area of the window that gets cropped, but not
390    // scaled in any ways.
391    const State& s(getDrawingState());
392
393    // apply the projection's clipping to the window crop in
394    // layerstack space, and convert-back to layer space.
395    // if there are no window scaling involved, this operation will map to full
396    // pixels in the buffer.
397    // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
398    // a viewport clipping and a window transform. we should use floating point to fix this.
399
400    Rect activeCrop(s.active.w, s.active.h);
401    if (!s.active.crop.isEmpty()) {
402        activeCrop = s.active.crop;
403    }
404
405    activeCrop = s.active.transform.transform(activeCrop);
406    if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
407        activeCrop.clear();
408    }
409    if (!s.active.finalCrop.isEmpty()) {
410        if(!activeCrop.intersect(s.active.finalCrop, &activeCrop)) {
411            activeCrop.clear();
412        }
413    }
414    activeCrop = s.active.transform.inverse().transform(activeCrop);
415
416    // This needs to be here as transform.transform(Rect) computes the
417    // transformed rect and then takes the bounding box of the result before
418    // returning. This means
419    // transform.inverse().transform(transform.transform(Rect)) != Rect
420    // in which case we need to make sure the final rect is clipped to the
421    // display bounds.
422    if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
423        activeCrop.clear();
424    }
425
426    // subtract the transparent region and snap to the bounds
427    activeCrop = reduce(activeCrop, s.activeTransparentRegion);
428
429    // Transform the window crop to match the buffer coordinate system,
430    // which means using the inverse of the current transform set on the
431    // SurfaceFlingerConsumer.
432    uint32_t invTransform = mCurrentTransform;
433    if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
434        /*
435         * the code below applies the display's inverse transform to the buffer
436         */
437        uint32_t invTransformOrient = hw->getOrientationTransform();
438        // calculate the inverse transform
439        if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
440            invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
441                    NATIVE_WINDOW_TRANSFORM_FLIP_H;
442            // If the transform has been rotated the axis of flip has been swapped
443            // so we need to swap which flip operations we are performing
444            bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
445            bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
446            if (is_h_flipped != is_v_flipped) {
447                invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
448                        NATIVE_WINDOW_TRANSFORM_FLIP_H;
449            }
450        }
451        // and apply to the current transform
452        invTransform = (Transform(invTransform) * Transform(invTransformOrient)).getOrientation();
453    }
454
455    int winWidth = s.active.w;
456    int winHeight = s.active.h;
457    if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
458        // If the activeCrop has been rotate the ends are rotated but not
459        // the space itself so when transforming ends back we can't rely on
460        // a modification of the axes of rotation. To account for this we
461        // need to reorient the inverse rotation in terms of the current
462        // axes of rotation.
463        bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
464        bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
465        if (is_h_flipped == is_v_flipped) {
466            invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
467                    NATIVE_WINDOW_TRANSFORM_FLIP_H;
468        }
469        winWidth = s.active.h;
470        winHeight = s.active.w;
471    }
472    const Rect winCrop = activeCrop.transform(
473            invTransform, s.active.w, s.active.h);
474
475    // below, crop is intersected with winCrop expressed in crop's coordinate space
476    float xScale = crop.getWidth()  / float(winWidth);
477    float yScale = crop.getHeight() / float(winHeight);
478
479    float insetL = winCrop.left                 * xScale;
480    float insetT = winCrop.top                  * yScale;
481    float insetR = (winWidth - winCrop.right )  * xScale;
482    float insetB = (winHeight - winCrop.bottom) * yScale;
483
484    crop.left   += insetL;
485    crop.top    += insetT;
486    crop.right  -= insetR;
487    crop.bottom -= insetB;
488
489    return crop;
490}
491
492#ifdef USE_HWC2
493void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
494#else
495void Layer::setGeometry(
496    const sp<const DisplayDevice>& hw,
497        HWComposer::HWCLayerInterface& layer)
498#endif
499{
500#ifdef USE_HWC2
501    const auto hwcId = displayDevice->getHwcDisplayId();
502    auto& hwcInfo = mHwcLayers[hwcId];
503#else
504    layer.setDefaultState();
505#endif
506
507    // enable this layer
508#ifdef USE_HWC2
509    hwcInfo.forceClientComposition = false;
510
511    if (isSecure() && !displayDevice->isSecure()) {
512        hwcInfo.forceClientComposition = true;
513    }
514
515    auto& hwcLayer = hwcInfo.layer;
516#else
517    layer.setSkip(false);
518
519    if (isSecure() && !hw->isSecure()) {
520        layer.setSkip(true);
521    }
522#endif
523
524    // this gives us only the "orientation" component of the transform
525    const State& s(getDrawingState());
526#ifdef USE_HWC2
527    if (!isOpaque(s) || s.alpha != 1.0f) {
528        auto blendMode = mPremultipliedAlpha ?
529                HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
530        auto error = hwcLayer->setBlendMode(blendMode);
531        ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
532                " %s (%d)", mName.string(), to_string(blendMode).c_str(),
533                to_string(error).c_str(), static_cast<int32_t>(error));
534    }
535#else
536    if (!isOpaque(s) || s.alpha != 0xFF) {
537        layer.setBlending(mPremultipliedAlpha ?
538                HWC_BLENDING_PREMULT :
539                HWC_BLENDING_COVERAGE);
540    }
541#endif
542
543    // apply the layer's transform, followed by the display's global transform
544    // here we're guaranteed that the layer's transform preserves rects
545    Region activeTransparentRegion(s.activeTransparentRegion);
546    if (!s.active.crop.isEmpty()) {
547        Rect activeCrop(s.active.crop);
548        activeCrop = s.active.transform.transform(activeCrop);
549#ifdef USE_HWC2
550        if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
551#else
552        if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
553#endif
554            activeCrop.clear();
555        }
556        activeCrop = s.active.transform.inverse().transform(activeCrop);
557        // This needs to be here as transform.transform(Rect) computes the
558        // transformed rect and then takes the bounding box of the result before
559        // returning. This means
560        // transform.inverse().transform(transform.transform(Rect)) != Rect
561        // in which case we need to make sure the final rect is clipped to the
562        // display bounds.
563        if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
564            activeCrop.clear();
565        }
566        // mark regions outside the crop as transparent
567        activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
568        activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
569                s.active.w, s.active.h));
570        activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
571                activeCrop.left, activeCrop.bottom));
572        activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
573                s.active.w, activeCrop.bottom));
574    }
575    Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
576    if (!s.active.finalCrop.isEmpty()) {
577        if(!frame.intersect(s.active.finalCrop, &frame)) {
578            frame.clear();
579        }
580    }
581#ifdef USE_HWC2
582    if (!frame.intersect(displayDevice->getViewport(), &frame)) {
583        frame.clear();
584    }
585    const Transform& tr(displayDevice->getTransform());
586    Rect transformedFrame = tr.transform(frame);
587    auto error = hwcLayer->setDisplayFrame(transformedFrame);
588    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
589            "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
590            transformedFrame.top, transformedFrame.right,
591            transformedFrame.bottom, to_string(error).c_str(),
592            static_cast<int32_t>(error));
593
594    FloatRect sourceCrop = computeCrop(displayDevice);
595    error = hwcLayer->setSourceCrop(sourceCrop);
596    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
597            "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
598            sourceCrop.left, sourceCrop.top, sourceCrop.right,
599            sourceCrop.bottom, to_string(error).c_str(),
600            static_cast<int32_t>(error));
601
602    error = hwcLayer->setPlaneAlpha(s.alpha);
603    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
604            "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
605            static_cast<int32_t>(error));
606
607    error = hwcLayer->setZOrder(s.z);
608    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
609            mName.string(), s.z, to_string(error).c_str(),
610            static_cast<int32_t>(error));
611#else
612    if (!frame.intersect(hw->getViewport(), &frame)) {
613        frame.clear();
614    }
615    const Transform& tr(hw->getTransform());
616    layer.setFrame(tr.transform(frame));
617    layer.setCrop(computeCrop(hw));
618    layer.setPlaneAlpha(s.alpha);
619#endif
620
621    /*
622     * Transformations are applied in this order:
623     * 1) buffer orientation/flip/mirror
624     * 2) state transformation (window manager)
625     * 3) layer orientation (screen orientation)
626     * (NOTE: the matrices are multiplied in reverse order)
627     */
628
629    const Transform bufferOrientation(mCurrentTransform);
630    Transform transform(tr * s.active.transform * bufferOrientation);
631
632    if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
633        /*
634         * the code below applies the display's inverse transform to the buffer
635         */
636#ifdef USE_HWC2
637        uint32_t invTransform = displayDevice->getOrientationTransform();
638#else
639        uint32_t invTransform = hw->getOrientationTransform();
640#endif
641        uint32_t t_orientation = transform.getOrientation();
642        // calculate the inverse transform
643        if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
644            invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
645                    NATIVE_WINDOW_TRANSFORM_FLIP_H;
646            // If the transform has been rotated the axis of flip has been swapped
647            // so we need to swap which flip operations we are performing
648            bool is_h_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
649            bool is_v_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
650            if (is_h_flipped != is_v_flipped) {
651                t_orientation ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
652                        NATIVE_WINDOW_TRANSFORM_FLIP_H;
653            }
654        }
655        // and apply to the current transform
656        transform = Transform(t_orientation) * Transform(invTransform);
657    }
658
659    // this gives us only the "orientation" component of the transform
660    const uint32_t orientation = transform.getOrientation();
661#ifdef USE_HWC2
662    if (orientation & Transform::ROT_INVALID) {
663        // we can only handle simple transformation
664        hwcInfo.forceClientComposition = true;
665    } else {
666        auto transform = static_cast<HWC2::Transform>(orientation);
667        auto error = hwcLayer->setTransform(transform);
668        ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
669                "%s (%d)", mName.string(), to_string(transform).c_str(),
670                to_string(error).c_str(), static_cast<int32_t>(error));
671    }
672#else
673    if (orientation & Transform::ROT_INVALID) {
674        // we can only handle simple transformation
675        layer.setSkip(true);
676    } else {
677        layer.setTransform(orientation);
678    }
679#endif
680}
681
682#ifdef USE_HWC2
683void Layer::forceClientComposition(int32_t hwcId) {
684    if (mHwcLayers.count(hwcId) == 0) {
685        ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
686        return;
687    }
688
689    mHwcLayers[hwcId].forceClientComposition = true;
690}
691#endif
692
693#ifdef USE_HWC2
694void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
695    // Apply this display's projection's viewport to the visible region
696    // before giving it to the HWC HAL.
697    const Transform& tr = displayDevice->getTransform();
698    const auto& viewport = displayDevice->getViewport();
699    Region visible = tr.transform(visibleRegion.intersect(viewport));
700    auto hwcId = displayDevice->getHwcDisplayId();
701    auto& hwcLayer = mHwcLayers[hwcId].layer;
702    auto error = hwcLayer->setVisibleRegion(visible);
703    if (error != HWC2::Error::None) {
704        ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
705                to_string(error).c_str(), static_cast<int32_t>(error));
706        visible.dump(LOG_TAG);
707    }
708
709    error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
710    if (error != HWC2::Error::None) {
711        ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
712                to_string(error).c_str(), static_cast<int32_t>(error));
713        surfaceDamageRegion.dump(LOG_TAG);
714    }
715
716    auto compositionType = HWC2::Composition::Invalid;
717    if (mSidebandStream.get()) {
718        compositionType = HWC2::Composition::Sideband;
719        auto error = hwcLayer->setSidebandStream(mSidebandStream->handle());
720        if (error != HWC2::Error::None) {
721            ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
722                    mName.string(), mSidebandStream->handle(),
723                    to_string(error).c_str(), static_cast<int32_t>(error));
724            return;
725        }
726    } else {
727        if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr) {
728            compositionType = HWC2::Composition::Client;
729            auto error = hwcLayer->setBuffer(nullptr, Fence::NO_FENCE);
730            if (error != HWC2::Error::None) {
731                ALOGE("[%s] Failed to set null buffer: %s (%d)", mName.string(),
732                        to_string(error).c_str(), static_cast<int32_t>(error));
733                return;
734            }
735        } else {
736            if (mPotentialCursor) {
737                compositionType = HWC2::Composition::Cursor;
738            }
739            auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
740            auto error = hwcLayer->setBuffer(mActiveBuffer->handle,
741                    acquireFence);
742            if (error != HWC2::Error::None) {
743                ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
744                        mActiveBuffer->handle, to_string(error).c_str(),
745                        static_cast<int32_t>(error));
746                return;
747            }
748            // If it's not a cursor, default to device composition
749        }
750    }
751
752    if (mHwcLayers[hwcId].forceClientComposition) {
753        ALOGV("[%s] Forcing Client composition", mName.string());
754        setCompositionType(hwcId, HWC2::Composition::Client);
755    } else if (compositionType != HWC2::Composition::Invalid) {
756        ALOGV("[%s] Requesting %s composition", mName.string(),
757                to_string(compositionType).c_str());
758        setCompositionType(hwcId, compositionType);
759    } else {
760        ALOGV("[%s] Requesting Device composition", mName.string());
761        setCompositionType(hwcId, HWC2::Composition::Device);
762    }
763}
764#else
765void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
766        HWComposer::HWCLayerInterface& layer) {
767    // we have to set the visible region on every frame because
768    // we currently free it during onLayerDisplayed(), which is called
769    // after HWComposer::commit() -- every frame.
770    // Apply this display's projection's viewport to the visible region
771    // before giving it to the HWC HAL.
772    const Transform& tr = hw->getTransform();
773    Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
774    layer.setVisibleRegionScreen(visible);
775    layer.setSurfaceDamage(surfaceDamageRegion);
776    mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
777
778    if (mSidebandStream.get()) {
779        layer.setSidebandStream(mSidebandStream);
780    } else {
781        // NOTE: buffer can be NULL if the client never drew into this
782        // layer yet, or if we ran out of memory
783        layer.setBuffer(mActiveBuffer);
784    }
785}
786#endif
787
788#ifdef USE_HWC2
789void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
790    auto hwcId = displayDevice->getHwcDisplayId();
791    if (mHwcLayers.count(hwcId) == 0 ||
792            getCompositionType(hwcId) != HWC2::Composition::Cursor) {
793        return;
794    }
795
796    // This gives us only the "orientation" component of the transform
797    const State& s(getCurrentState());
798
799    // Apply the layer's transform, followed by the display's global transform
800    // Here we're guaranteed that the layer's transform preserves rects
801    Rect win(s.active.w, s.active.h);
802    if (!s.active.crop.isEmpty()) {
803        win.intersect(s.active.crop, &win);
804    }
805    // Subtract the transparent region and snap to the bounds
806    Rect bounds = reduce(win, s.activeTransparentRegion);
807    Rect frame(s.active.transform.transform(bounds));
808    frame.intersect(displayDevice->getViewport(), &frame);
809    if (!s.active.finalCrop.isEmpty()) {
810        frame.intersect(s.active.finalCrop, &frame);
811    }
812    auto& displayTransform(displayDevice->getTransform());
813    auto position = displayTransform.transform(frame);
814
815    auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
816            position.top);
817    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
818            "to (%d, %d): %s (%d)", mName.string(), position.left,
819            position.top, to_string(error).c_str(),
820            static_cast<int32_t>(error));
821}
822#else
823void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
824        HWComposer::HWCLayerInterface& layer) {
825    int fenceFd = -1;
826
827    // TODO: there is a possible optimization here: we only need to set the
828    // acquire fence the first time a new buffer is acquired on EACH display.
829
830    if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
831        sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
832        if (fence->isValid()) {
833            fenceFd = fence->dup();
834            if (fenceFd == -1) {
835                ALOGW("failed to dup layer fence, skipping sync: %d", errno);
836            }
837        }
838    }
839    layer.setAcquireFenceFd(fenceFd);
840}
841
842Rect Layer::getPosition(
843    const sp<const DisplayDevice>& hw)
844{
845    // this gives us only the "orientation" component of the transform
846    const State& s(getCurrentState());
847
848    // apply the layer's transform, followed by the display's global transform
849    // here we're guaranteed that the layer's transform preserves rects
850    Rect win(s.active.w, s.active.h);
851    if (!s.active.crop.isEmpty()) {
852        win.intersect(s.active.crop, &win);
853    }
854    // subtract the transparent region and snap to the bounds
855    Rect bounds = reduce(win, s.activeTransparentRegion);
856    Rect frame(s.active.transform.transform(bounds));
857    frame.intersect(hw->getViewport(), &frame);
858    if (!s.active.finalCrop.isEmpty()) {
859        frame.intersect(s.active.finalCrop, &frame);
860    }
861    const Transform& tr(hw->getTransform());
862    return Rect(tr.transform(frame));
863}
864#endif
865
866// ---------------------------------------------------------------------------
867// drawing...
868// ---------------------------------------------------------------------------
869
870void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
871    onDraw(hw, clip, false);
872}
873
874void Layer::draw(const sp<const DisplayDevice>& hw,
875        bool useIdentityTransform) const {
876    onDraw(hw, Region(hw->bounds()), useIdentityTransform);
877}
878
879void Layer::draw(const sp<const DisplayDevice>& hw) const {
880    onDraw(hw, Region(hw->bounds()), false);
881}
882
883void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
884        bool useIdentityTransform) const
885{
886    ATRACE_CALL();
887
888    if (CC_UNLIKELY(mActiveBuffer == 0)) {
889        // the texture has not been created yet, this Layer has
890        // in fact never been drawn into. This happens frequently with
891        // SurfaceView because the WindowManager can't know when the client
892        // has drawn the first time.
893
894        // If there is nothing under us, we paint the screen in black, otherwise
895        // we just skip this update.
896
897        // figure out if there is something below us
898        Region under;
899        const SurfaceFlinger::LayerVector& drawingLayers(
900                mFlinger->mDrawingState.layersSortedByZ);
901        const size_t count = drawingLayers.size();
902        for (size_t i=0 ; i<count ; ++i) {
903            const sp<Layer>& layer(drawingLayers[i]);
904            if (layer.get() == static_cast<Layer const*>(this))
905                break;
906            under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
907        }
908        // if not everything below us is covered, we plug the holes!
909        Region holes(clip.subtract(under));
910        if (!holes.isEmpty()) {
911            clearWithOpenGL(hw, holes, 0, 0, 0, 1);
912        }
913        return;
914    }
915
916    // Bind the current buffer to the GL texture, and wait for it to be
917    // ready for us to draw into.
918    status_t err = mSurfaceFlingerConsumer->bindTextureImage();
919    if (err != NO_ERROR) {
920        ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
921        // Go ahead and draw the buffer anyway; no matter what we do the screen
922        // is probably going to have something visibly wrong.
923    }
924
925    bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
926
927    RenderEngine& engine(mFlinger->getRenderEngine());
928
929    if (!blackOutLayer) {
930        // TODO: we could be more subtle with isFixedSize()
931        const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
932
933        // Query the texture matrix given our current filtering mode.
934        float textureMatrix[16];
935        mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
936        mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
937
938        if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
939
940            /*
941             * the code below applies the display's inverse transform to the texture transform
942             */
943
944            // create a 4x4 transform matrix from the display transform flags
945            const mat4 flipH(-1,0,0,0,  0,1,0,0, 0,0,1,0, 1,0,0,1);
946            const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
947            const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
948
949            mat4 tr;
950            uint32_t transform = hw->getOrientationTransform();
951            if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
952                tr = tr * rot90;
953            if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
954                tr = tr * flipH;
955            if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
956                tr = tr * flipV;
957
958            // calculate the inverse
959            tr = inverse(tr);
960
961            // and finally apply it to the original texture matrix
962            const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
963            memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
964        }
965
966        // Set things up for texturing.
967        mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
968        mTexture.setFiltering(useFiltering);
969        mTexture.setMatrix(textureMatrix);
970
971        engine.setupLayerTexturing(mTexture);
972    } else {
973        engine.setupLayerBlackedOut();
974    }
975    drawWithOpenGL(hw, clip, useIdentityTransform);
976    engine.disableTexturing();
977}
978
979
980void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
981        const Region& /* clip */, float red, float green, float blue,
982        float alpha) const
983{
984    RenderEngine& engine(mFlinger->getRenderEngine());
985    computeGeometry(hw, mMesh, false);
986    engine.setupFillWithColor(red, green, blue, alpha);
987    engine.drawMesh(mMesh);
988}
989
990void Layer::clearWithOpenGL(
991        const sp<const DisplayDevice>& hw, const Region& clip) const {
992    clearWithOpenGL(hw, clip, 0,0,0,0);
993}
994
995void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
996        const Region& /* clip */, bool useIdentityTransform) const {
997    const State& s(getDrawingState());
998
999    computeGeometry(hw, mMesh, useIdentityTransform);
1000
1001    /*
1002     * NOTE: the way we compute the texture coordinates here produces
1003     * different results than when we take the HWC path -- in the later case
1004     * the "source crop" is rounded to texel boundaries.
1005     * This can produce significantly different results when the texture
1006     * is scaled by a large amount.
1007     *
1008     * The GL code below is more logical (imho), and the difference with
1009     * HWC is due to a limitation of the HWC API to integers -- a question
1010     * is suspend is whether we should ignore this problem or revert to
1011     * GL composition when a buffer scaling is applied (maybe with some
1012     * minimal value)? Or, we could make GL behave like HWC -- but this feel
1013     * like more of a hack.
1014     */
1015    Rect win(computeBounds());
1016
1017    if (!s.active.finalCrop.isEmpty()) {
1018        win = s.active.transform.transform(win);
1019        if (!win.intersect(s.active.finalCrop, &win)) {
1020            win.clear();
1021        }
1022        win = s.active.transform.inverse().transform(win);
1023        if (!win.intersect(computeBounds(), &win)) {
1024            win.clear();
1025        }
1026    }
1027
1028    float left   = float(win.left)   / float(s.active.w);
1029    float top    = float(win.top)    / float(s.active.h);
1030    float right  = float(win.right)  / float(s.active.w);
1031    float bottom = float(win.bottom) / float(s.active.h);
1032
1033    // TODO: we probably want to generate the texture coords with the mesh
1034    // here we assume that we only have 4 vertices
1035    Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1036    texCoords[0] = vec2(left, 1.0f - top);
1037    texCoords[1] = vec2(left, 1.0f - bottom);
1038    texCoords[2] = vec2(right, 1.0f - bottom);
1039    texCoords[3] = vec2(right, 1.0f - top);
1040
1041    RenderEngine& engine(mFlinger->getRenderEngine());
1042    engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
1043    engine.drawMesh(mMesh);
1044    engine.disableBlending();
1045}
1046
1047#ifdef USE_HWC2
1048void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1049        bool callIntoHwc) {
1050    if (mHwcLayers.count(hwcId) == 0) {
1051        ALOGE("setCompositionType called without a valid HWC layer");
1052        return;
1053    }
1054    auto& hwcInfo = mHwcLayers[hwcId];
1055    auto& hwcLayer = hwcInfo.layer;
1056    ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1057            to_string(type).c_str(), static_cast<int>(callIntoHwc));
1058    if (hwcInfo.compositionType != type) {
1059        ALOGV("    actually setting");
1060        hwcInfo.compositionType = type;
1061        if (callIntoHwc) {
1062            auto error = hwcLayer->setCompositionType(type);
1063            ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1064                    "composition type %s: %s (%d)", mName.string(),
1065                    to_string(type).c_str(), to_string(error).c_str(),
1066                    static_cast<int32_t>(error));
1067        }
1068    }
1069}
1070
1071HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1072    if (mHwcLayers.count(hwcId) == 0) {
1073        ALOGE("getCompositionType called without a valid HWC layer");
1074        return HWC2::Composition::Invalid;
1075    }
1076    return mHwcLayers.at(hwcId).compositionType;
1077}
1078
1079void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1080    if (mHwcLayers.count(hwcId) == 0) {
1081        ALOGE("setClearClientTarget called without a valid HWC layer");
1082        return;
1083    }
1084    mHwcLayers[hwcId].clearClientTarget = clear;
1085}
1086
1087bool Layer::getClearClientTarget(int32_t hwcId) const {
1088    if (mHwcLayers.count(hwcId) == 0) {
1089        ALOGE("getClearClientTarget called without a valid HWC layer");
1090        return false;
1091    }
1092    return mHwcLayers.at(hwcId).clearClientTarget;
1093}
1094#endif
1095
1096uint32_t Layer::getProducerStickyTransform() const {
1097    int producerStickyTransform = 0;
1098    int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1099    if (ret != OK) {
1100        ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1101                strerror(-ret), ret);
1102        return 0;
1103    }
1104    return static_cast<uint32_t>(producerStickyTransform);
1105}
1106
1107uint64_t Layer::getHeadFrameNumber() const {
1108    Mutex::Autolock lock(mQueueItemLock);
1109    if (!mQueueItems.empty()) {
1110        return mQueueItems[0].mFrameNumber;
1111    } else {
1112        return mCurrentFrameNumber;
1113    }
1114}
1115
1116bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1117    if (point->getFrameNumber() <= mCurrentFrameNumber) {
1118        // Don't bother with a SyncPoint, since we've already latched the
1119        // relevant frame
1120        return false;
1121    }
1122
1123    Mutex::Autolock lock(mLocalSyncPointMutex);
1124    mLocalSyncPoints.push_back(point);
1125    return true;
1126}
1127
1128void Layer::setFiltering(bool filtering) {
1129    mFiltering = filtering;
1130}
1131
1132bool Layer::getFiltering() const {
1133    return mFiltering;
1134}
1135
1136// As documented in libhardware header, formats in the range
1137// 0x100 - 0x1FF are specific to the HAL implementation, and
1138// are known to have no alpha channel
1139// TODO: move definition for device-specific range into
1140// hardware.h, instead of using hard-coded values here.
1141#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1142
1143bool Layer::getOpacityForFormat(uint32_t format) {
1144    if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1145        return true;
1146    }
1147    switch (format) {
1148        case HAL_PIXEL_FORMAT_RGBA_8888:
1149        case HAL_PIXEL_FORMAT_BGRA_8888:
1150            return false;
1151    }
1152    // in all other case, we have no blending (also for unknown formats)
1153    return true;
1154}
1155
1156// ----------------------------------------------------------------------------
1157// local state
1158// ----------------------------------------------------------------------------
1159
1160static void boundPoint(vec2* point, const Rect& crop) {
1161    if (point->x < crop.left) {
1162        point->x = crop.left;
1163    }
1164    if (point->x > crop.right) {
1165        point->x = crop.right;
1166    }
1167    if (point->y < crop.top) {
1168        point->y = crop.top;
1169    }
1170    if (point->y > crop.bottom) {
1171        point->y = crop.bottom;
1172    }
1173}
1174
1175void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1176        bool useIdentityTransform) const
1177{
1178    const Layer::State& s(getDrawingState());
1179    const Transform tr(hw->getTransform());
1180    const uint32_t hw_h = hw->getHeight();
1181    Rect win(s.active.w, s.active.h);
1182    if (!s.active.crop.isEmpty()) {
1183        win.intersect(s.active.crop, &win);
1184    }
1185    // subtract the transparent region and snap to the bounds
1186    win = reduce(win, s.activeTransparentRegion);
1187
1188    vec2 lt = vec2(win.left, win.top);
1189    vec2 lb = vec2(win.left, win.bottom);
1190    vec2 rb = vec2(win.right, win.bottom);
1191    vec2 rt = vec2(win.right, win.top);
1192
1193    if (!useIdentityTransform) {
1194        lt = s.active.transform.transform(lt);
1195        lb = s.active.transform.transform(lb);
1196        rb = s.active.transform.transform(rb);
1197        rt = s.active.transform.transform(rt);
1198    }
1199
1200    if (!s.active.finalCrop.isEmpty()) {
1201        boundPoint(&lt, s.active.finalCrop);
1202        boundPoint(&lb, s.active.finalCrop);
1203        boundPoint(&rb, s.active.finalCrop);
1204        boundPoint(&rt, s.active.finalCrop);
1205    }
1206
1207    Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
1208    position[0] = tr.transform(lt);
1209    position[1] = tr.transform(lb);
1210    position[2] = tr.transform(rb);
1211    position[3] = tr.transform(rt);
1212    for (size_t i=0 ; i<4 ; i++) {
1213        position[i].y = hw_h - position[i].y;
1214    }
1215}
1216
1217bool Layer::isOpaque(const Layer::State& s) const
1218{
1219    // if we don't have a buffer yet, we're translucent regardless of the
1220    // layer's opaque flag.
1221    if (mActiveBuffer == 0) {
1222        return false;
1223    }
1224
1225    // if the layer has the opaque flag, then we're always opaque,
1226    // otherwise we use the current buffer's format.
1227    return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
1228}
1229
1230bool Layer::isSecure() const
1231{
1232    const Layer::State& s(mDrawingState);
1233    return (s.flags & layer_state_t::eLayerSecure);
1234}
1235
1236bool Layer::isProtected() const
1237{
1238    const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
1239    return (activeBuffer != 0) &&
1240            (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1241}
1242
1243bool Layer::isFixedSize() const {
1244    return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
1245}
1246
1247bool Layer::isCropped() const {
1248    return !mCurrentCrop.isEmpty();
1249}
1250
1251bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1252    return mNeedsFiltering || hw->needsFiltering();
1253}
1254
1255void Layer::setVisibleRegion(const Region& visibleRegion) {
1256    // always called from main thread
1257    this->visibleRegion = visibleRegion;
1258}
1259
1260void Layer::setCoveredRegion(const Region& coveredRegion) {
1261    // always called from main thread
1262    this->coveredRegion = coveredRegion;
1263}
1264
1265void Layer::setVisibleNonTransparentRegion(const Region&
1266        setVisibleNonTransparentRegion) {
1267    // always called from main thread
1268    this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1269}
1270
1271// ----------------------------------------------------------------------------
1272// transaction
1273// ----------------------------------------------------------------------------
1274
1275void Layer::pushPendingState() {
1276    if (!mCurrentState.modified) {
1277        return;
1278    }
1279
1280    // If this transaction is waiting on the receipt of a frame, generate a sync
1281    // point and send it to the remote layer.
1282    if (mCurrentState.handle != nullptr) {
1283        sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1284        sp<Layer> handleLayer = handle->owner.promote();
1285        if (handleLayer == nullptr) {
1286            ALOGE("[%s] Unable to promote Layer handle", mName.string());
1287            // If we can't promote the layer we are intended to wait on,
1288            // then it is expired or otherwise invalid. Allow this transaction
1289            // to be applied as per normal (no synchronization).
1290            mCurrentState.handle = nullptr;
1291        } else {
1292            auto syncPoint = std::make_shared<SyncPoint>(
1293                    mCurrentState.frameNumber);
1294            if (handleLayer->addSyncPoint(syncPoint)) {
1295                mRemoteSyncPoints.push_back(std::move(syncPoint));
1296            } else {
1297                // We already missed the frame we're supposed to synchronize
1298                // on, so go ahead and apply the state update
1299                mCurrentState.handle = nullptr;
1300            }
1301        }
1302
1303        // Wake us up to check if the frame has been received
1304        setTransactionFlags(eTransactionNeeded);
1305    }
1306    mPendingStates.push_back(mCurrentState);
1307}
1308
1309void Layer::popPendingState() {
1310    auto oldFlags = mCurrentState.flags;
1311    mCurrentState = mPendingStates[0];
1312    mCurrentState.flags = (oldFlags & ~mCurrentState.mask) |
1313            (mCurrentState.flags & mCurrentState.mask);
1314
1315    mPendingStates.removeAt(0);
1316}
1317
1318bool Layer::applyPendingStates() {
1319    bool stateUpdateAvailable = false;
1320    while (!mPendingStates.empty()) {
1321        if (mPendingStates[0].handle != nullptr) {
1322            if (mRemoteSyncPoints.empty()) {
1323                // If we don't have a sync point for this, apply it anyway. It
1324                // will be visually wrong, but it should keep us from getting
1325                // into too much trouble.
1326                ALOGE("[%s] No local sync point found", mName.string());
1327                popPendingState();
1328                stateUpdateAvailable = true;
1329                continue;
1330            }
1331
1332            if (mRemoteSyncPoints.front()->getFrameNumber() !=
1333                    mPendingStates[0].frameNumber) {
1334                ALOGE("[%s] Unexpected sync point frame number found",
1335                        mName.string());
1336
1337                // Signal our end of the sync point and then dispose of it
1338                mRemoteSyncPoints.front()->setTransactionApplied();
1339                mRemoteSyncPoints.pop_front();
1340                continue;
1341            }
1342
1343            if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1344                // Apply the state update
1345                popPendingState();
1346                stateUpdateAvailable = true;
1347
1348                // Signal our end of the sync point and then dispose of it
1349                mRemoteSyncPoints.front()->setTransactionApplied();
1350                mRemoteSyncPoints.pop_front();
1351            } else {
1352                break;
1353            }
1354        } else {
1355            popPendingState();
1356            stateUpdateAvailable = true;
1357        }
1358    }
1359
1360    // If we still have pending updates, wake SurfaceFlinger back up and point
1361    // it at this layer so we can process them
1362    if (!mPendingStates.empty()) {
1363        setTransactionFlags(eTransactionNeeded);
1364        mFlinger->setTransactionFlags(eTraversalNeeded);
1365    }
1366
1367    mCurrentState.modified = false;
1368    return stateUpdateAvailable;
1369}
1370
1371void Layer::notifyAvailableFrames() {
1372    auto headFrameNumber = getHeadFrameNumber();
1373    Mutex::Autolock lock(mLocalSyncPointMutex);
1374    for (auto& point : mLocalSyncPoints) {
1375        if (headFrameNumber >= point->getFrameNumber()) {
1376            point->setFrameAvailable();
1377        }
1378    }
1379}
1380
1381uint32_t Layer::doTransaction(uint32_t flags) {
1382    ATRACE_CALL();
1383
1384    pushPendingState();
1385    if (!applyPendingStates()) {
1386        return 0;
1387    }
1388
1389    const Layer::State& s(getDrawingState());
1390    const Layer::State& c(getCurrentState());
1391
1392    const bool sizeChanged = (c.requested.w != s.requested.w) ||
1393                             (c.requested.h != s.requested.h);
1394
1395    if (sizeChanged) {
1396        // the size changed, we need to ask our client to request a new buffer
1397        ALOGD_IF(DEBUG_RESIZE,
1398                "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1399                "  current={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1400                "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n"
1401                "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1402                "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
1403                this, getName().string(), mCurrentTransform, mCurrentScalingMode,
1404                c.active.w, c.active.h,
1405                c.active.crop.left,
1406                c.active.crop.top,
1407                c.active.crop.right,
1408                c.active.crop.bottom,
1409                c.active.crop.getWidth(),
1410                c.active.crop.getHeight(),
1411                c.requested.w, c.requested.h,
1412                c.requested.crop.left,
1413                c.requested.crop.top,
1414                c.requested.crop.right,
1415                c.requested.crop.bottom,
1416                c.requested.crop.getWidth(),
1417                c.requested.crop.getHeight(),
1418                s.active.w, s.active.h,
1419                s.active.crop.left,
1420                s.active.crop.top,
1421                s.active.crop.right,
1422                s.active.crop.bottom,
1423                s.active.crop.getWidth(),
1424                s.active.crop.getHeight(),
1425                s.requested.w, s.requested.h,
1426                s.requested.crop.left,
1427                s.requested.crop.top,
1428                s.requested.crop.right,
1429                s.requested.crop.bottom,
1430                s.requested.crop.getWidth(),
1431                s.requested.crop.getHeight());
1432
1433        // record the new size, form this point on, when the client request
1434        // a buffer, it'll get the new size.
1435        mSurfaceFlingerConsumer->setDefaultBufferSize(
1436                c.requested.w, c.requested.h);
1437    }
1438
1439    if (!isFixedSize()) {
1440
1441        const bool resizePending = (c.requested.w != c.active.w) ||
1442                                   (c.requested.h != c.active.h);
1443
1444        if (resizePending && mSidebandStream == NULL) {
1445            // don't let Layer::doTransaction update the drawing state
1446            // if we have a pending resize, unless we are in fixed-size mode.
1447            // the drawing state will be updated only once we receive a buffer
1448            // with the correct size.
1449            //
1450            // in particular, we want to make sure the clip (which is part
1451            // of the geometry state) is latched together with the size but is
1452            // latched immediately when no resizing is involved.
1453            //
1454            // If a sideband stream is attached, however, we want to skip this
1455            // optimization so that transactions aren't missed when a buffer
1456            // never arrives
1457
1458            flags |= eDontUpdateGeometryState;
1459        }
1460    }
1461
1462    // always set active to requested, unless we're asked not to
1463    // this is used by Layer, which special cases resizes.
1464    if (flags & eDontUpdateGeometryState)  {
1465    } else {
1466        Layer::State& editCurrentState(getCurrentState());
1467        editCurrentState.active = c.requested;
1468    }
1469
1470    if (s.active != c.active) {
1471        // invalidate and recompute the visible regions if needed
1472        flags |= Layer::eVisibleRegion;
1473    }
1474
1475    if (c.sequence != s.sequence) {
1476        // invalidate and recompute the visible regions if needed
1477        flags |= eVisibleRegion;
1478        this->contentDirty = true;
1479
1480        // we may use linear filtering, if the matrix scales us
1481        const uint8_t type = c.active.transform.getType();
1482        mNeedsFiltering = (!c.active.transform.preserveRects() ||
1483                (type >= Transform::SCALE));
1484    }
1485
1486    // Commit the transaction
1487    commitTransaction();
1488    return flags;
1489}
1490
1491void Layer::commitTransaction() {
1492    mDrawingState = mCurrentState;
1493}
1494
1495uint32_t Layer::getTransactionFlags(uint32_t flags) {
1496    return android_atomic_and(~flags, &mTransactionFlags) & flags;
1497}
1498
1499uint32_t Layer::setTransactionFlags(uint32_t flags) {
1500    return android_atomic_or(flags, &mTransactionFlags);
1501}
1502
1503bool Layer::setPosition(float x, float y) {
1504    if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
1505        return false;
1506    mCurrentState.sequence++;
1507    mCurrentState.requested.transform.set(x, y);
1508    mCurrentState.modified = true;
1509    setTransactionFlags(eTransactionNeeded);
1510    return true;
1511}
1512bool Layer::setLayer(uint32_t z) {
1513    if (mCurrentState.z == z)
1514        return false;
1515    mCurrentState.sequence++;
1516    mCurrentState.z = z;
1517    mCurrentState.modified = true;
1518    setTransactionFlags(eTransactionNeeded);
1519    return true;
1520}
1521bool Layer::setSize(uint32_t w, uint32_t h) {
1522    if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1523        return false;
1524    mCurrentState.requested.w = w;
1525    mCurrentState.requested.h = h;
1526    mCurrentState.modified = true;
1527    setTransactionFlags(eTransactionNeeded);
1528    return true;
1529}
1530#ifdef USE_HWC2
1531bool Layer::setAlpha(float alpha) {
1532#else
1533bool Layer::setAlpha(uint8_t alpha) {
1534#endif
1535    if (mCurrentState.alpha == alpha)
1536        return false;
1537    mCurrentState.sequence++;
1538    mCurrentState.alpha = alpha;
1539    mCurrentState.modified = true;
1540    setTransactionFlags(eTransactionNeeded);
1541    return true;
1542}
1543bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1544    mCurrentState.sequence++;
1545    mCurrentState.requested.transform.set(
1546            matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
1547    mCurrentState.modified = true;
1548    setTransactionFlags(eTransactionNeeded);
1549    return true;
1550}
1551bool Layer::setTransparentRegionHint(const Region& transparent) {
1552    mCurrentState.requestedTransparentRegion = transparent;
1553    mCurrentState.modified = true;
1554    setTransactionFlags(eTransactionNeeded);
1555    return true;
1556}
1557bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1558    const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1559    if (mCurrentState.flags == newFlags)
1560        return false;
1561    mCurrentState.sequence++;
1562    mCurrentState.flags = newFlags;
1563    mCurrentState.mask = mask;
1564    mCurrentState.modified = true;
1565    setTransactionFlags(eTransactionNeeded);
1566    return true;
1567}
1568bool Layer::setCrop(const Rect& crop) {
1569    if (mCurrentState.requested.crop == crop)
1570        return false;
1571    mCurrentState.sequence++;
1572    mCurrentState.requested.crop = crop;
1573    mCurrentState.modified = true;
1574    setTransactionFlags(eTransactionNeeded);
1575    return true;
1576}
1577bool Layer::setFinalCrop(const Rect& crop) {
1578    if (mCurrentState.requested.finalCrop == crop)
1579        return false;
1580    mCurrentState.sequence++;
1581    mCurrentState.requested.finalCrop = crop;
1582    mCurrentState.modified = true;
1583    setTransactionFlags(eTransactionNeeded);
1584    return true;
1585}
1586
1587bool Layer::setLayerStack(uint32_t layerStack) {
1588    if (mCurrentState.layerStack == layerStack)
1589        return false;
1590    mCurrentState.sequence++;
1591    mCurrentState.layerStack = layerStack;
1592    mCurrentState.modified = true;
1593    setTransactionFlags(eTransactionNeeded);
1594    return true;
1595}
1596
1597void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1598        uint64_t frameNumber) {
1599    mCurrentState.handle = handle;
1600    mCurrentState.frameNumber = frameNumber;
1601    // We don't set eTransactionNeeded, because just receiving a deferral
1602    // request without any other state updates shouldn't actually induce a delay
1603    mCurrentState.modified = true;
1604    pushPendingState();
1605    mCurrentState.handle = nullptr;
1606    mCurrentState.frameNumber = 0;
1607    mCurrentState.modified = false;
1608}
1609
1610void Layer::useSurfaceDamage() {
1611    if (mFlinger->mForceFullDamage) {
1612        surfaceDamageRegion = Region::INVALID_REGION;
1613    } else {
1614        surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1615    }
1616}
1617
1618void Layer::useEmptyDamage() {
1619    surfaceDamageRegion.clear();
1620}
1621
1622// ----------------------------------------------------------------------------
1623// pageflip handling...
1624// ----------------------------------------------------------------------------
1625
1626bool Layer::shouldPresentNow(const DispSync& dispSync) const {
1627    if (mSidebandStreamChanged || mAutoRefresh) {
1628        return true;
1629    }
1630
1631    Mutex::Autolock lock(mQueueItemLock);
1632    if (mQueueItems.empty()) {
1633        return false;
1634    }
1635    auto timestamp = mQueueItems[0].mTimestamp;
1636    nsecs_t expectedPresent =
1637            mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
1638
1639    // Ignore timestamps more than a second in the future
1640    bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1641    ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1642            "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1643            expectedPresent);
1644
1645    bool isDue = timestamp < expectedPresent;
1646    return isDue || !isPlausible;
1647}
1648
1649bool Layer::onPreComposition() {
1650    mRefreshPending = false;
1651    return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
1652}
1653
1654void Layer::onPostComposition() {
1655    if (mFrameLatencyNeeded) {
1656        nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
1657        mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1658
1659        sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
1660        if (frameReadyFence->isValid()) {
1661            mFrameTracker.setFrameReadyFence(frameReadyFence);
1662        } else {
1663            // There was no fence for this frame, so assume that it was ready
1664            // to be presented at the desired present time.
1665            mFrameTracker.setFrameReadyTime(desiredPresentTime);
1666        }
1667
1668        const HWComposer& hwc = mFlinger->getHwComposer();
1669#ifdef USE_HWC2
1670        sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1671#else
1672        sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
1673#endif
1674        if (presentFence->isValid()) {
1675            mFrameTracker.setActualPresentFence(presentFence);
1676        } else {
1677            // The HWC doesn't support present fences, so use the refresh
1678            // timestamp instead.
1679            nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1680            mFrameTracker.setActualPresentTime(presentTime);
1681        }
1682
1683        mFrameTracker.advanceFrame();
1684        mFrameLatencyNeeded = false;
1685    }
1686}
1687
1688#ifdef USE_HWC2
1689void Layer::releasePendingBuffer() {
1690    mSurfaceFlingerConsumer->releasePendingBuffer();
1691}
1692#endif
1693
1694bool Layer::isVisible() const {
1695    const Layer::State& s(mDrawingState);
1696#ifdef USE_HWC2
1697    return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1698            && (mActiveBuffer != NULL || mSidebandStream != NULL);
1699#else
1700    return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
1701            && (mActiveBuffer != NULL || mSidebandStream != NULL);
1702#endif
1703}
1704
1705Region Layer::latchBuffer(bool& recomputeVisibleRegions)
1706{
1707    ATRACE_CALL();
1708
1709    if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1710        // mSidebandStreamChanged was true
1711        mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
1712        if (mSidebandStream != NULL) {
1713            setTransactionFlags(eTransactionNeeded);
1714            mFlinger->setTransactionFlags(eTraversalNeeded);
1715        }
1716        recomputeVisibleRegions = true;
1717
1718        const State& s(getDrawingState());
1719        return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
1720    }
1721
1722    Region outDirtyRegion;
1723    if (mQueuedFrames > 0 || mAutoRefresh) {
1724
1725        // if we've already called updateTexImage() without going through
1726        // a composition step, we have to skip this layer at this point
1727        // because we cannot call updateTeximage() without a corresponding
1728        // compositionComplete() call.
1729        // we'll trigger an update in onPreComposition().
1730        if (mRefreshPending) {
1731            return outDirtyRegion;
1732        }
1733
1734        // Capture the old state of the layer for comparisons later
1735        const State& s(getDrawingState());
1736        const bool oldOpacity = isOpaque(s);
1737        sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
1738
1739        struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
1740            Layer::State& front;
1741            Layer::State& current;
1742            bool& recomputeVisibleRegions;
1743            bool stickyTransformSet;
1744            Reject(Layer::State& front, Layer::State& current,
1745                    bool& recomputeVisibleRegions, bool stickySet)
1746                : front(front), current(current),
1747                  recomputeVisibleRegions(recomputeVisibleRegions),
1748                  stickyTransformSet(stickySet) {
1749            }
1750
1751            virtual bool reject(const sp<GraphicBuffer>& buf,
1752                    const BufferItem& item) {
1753                if (buf == NULL) {
1754                    return false;
1755                }
1756
1757                uint32_t bufWidth  = buf->getWidth();
1758                uint32_t bufHeight = buf->getHeight();
1759
1760                // check that we received a buffer of the right size
1761                // (Take the buffer's orientation into account)
1762                if (item.mTransform & Transform::ROT_90) {
1763                    swap(bufWidth, bufHeight);
1764                }
1765
1766                bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
1767                if (front.active != front.requested) {
1768
1769                    if (isFixedSize ||
1770                            (bufWidth == front.requested.w &&
1771                             bufHeight == front.requested.h))
1772                    {
1773                        // Here we pretend the transaction happened by updating the
1774                        // current and drawing states. Drawing state is only accessed
1775                        // in this thread, no need to have it locked
1776                        front.active = front.requested;
1777
1778                        // We also need to update the current state so that
1779                        // we don't end-up overwriting the drawing state with
1780                        // this stale current state during the next transaction
1781                        //
1782                        // NOTE: We don't need to hold the transaction lock here
1783                        // because State::active is only accessed from this thread.
1784                        current.active = front.active;
1785
1786                        // recompute visible region
1787                        recomputeVisibleRegions = true;
1788                    }
1789
1790                    ALOGD_IF(DEBUG_RESIZE,
1791                            "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
1792                            "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1793                            "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
1794                            bufWidth, bufHeight, item.mTransform, item.mScalingMode,
1795                            front.active.w, front.active.h,
1796                            front.active.crop.left,
1797                            front.active.crop.top,
1798                            front.active.crop.right,
1799                            front.active.crop.bottom,
1800                            front.active.crop.getWidth(),
1801                            front.active.crop.getHeight(),
1802                            front.requested.w, front.requested.h,
1803                            front.requested.crop.left,
1804                            front.requested.crop.top,
1805                            front.requested.crop.right,
1806                            front.requested.crop.bottom,
1807                            front.requested.crop.getWidth(),
1808                            front.requested.crop.getHeight());
1809                }
1810
1811                if (!isFixedSize && !stickyTransformSet) {
1812                    if (front.active.w != bufWidth ||
1813                        front.active.h != bufHeight) {
1814                        // reject this buffer
1815                        ALOGE("rejecting buffer: bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1816                                bufWidth, bufHeight, front.active.w, front.active.h);
1817                        return true;
1818                    }
1819                }
1820
1821                // if the transparent region has changed (this test is
1822                // conservative, but that's fine, worst case we're doing
1823                // a bit of extra work), we latch the new one and we
1824                // trigger a visible-region recompute.
1825                if (!front.activeTransparentRegion.isTriviallyEqual(
1826                        front.requestedTransparentRegion)) {
1827                    front.activeTransparentRegion = front.requestedTransparentRegion;
1828
1829                    // We also need to update the current state so that
1830                    // we don't end-up overwriting the drawing state with
1831                    // this stale current state during the next transaction
1832                    //
1833                    // NOTE: We don't need to hold the transaction lock here
1834                    // because State::active is only accessed from this thread.
1835                    current.activeTransparentRegion = front.activeTransparentRegion;
1836
1837                    // recompute visible region
1838                    recomputeVisibleRegions = true;
1839                }
1840
1841                return false;
1842            }
1843        };
1844
1845        Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
1846                getProducerStickyTransform() != 0);
1847
1848
1849        // Check all of our local sync points to ensure that all transactions
1850        // which need to have been applied prior to the frame which is about to
1851        // be latched have signaled
1852
1853        auto headFrameNumber = getHeadFrameNumber();
1854        bool matchingFramesFound = false;
1855        bool allTransactionsApplied = true;
1856        {
1857            Mutex::Autolock lock(mLocalSyncPointMutex);
1858            for (auto& point : mLocalSyncPoints) {
1859                if (point->getFrameNumber() > headFrameNumber) {
1860                    break;
1861                }
1862
1863                matchingFramesFound = true;
1864
1865                if (!point->frameIsAvailable()) {
1866                    // We haven't notified the remote layer that the frame for
1867                    // this point is available yet. Notify it now, and then
1868                    // abort this attempt to latch.
1869                    point->setFrameAvailable();
1870                    allTransactionsApplied = false;
1871                    break;
1872                }
1873
1874                allTransactionsApplied &= point->transactionIsApplied();
1875            }
1876        }
1877
1878        if (matchingFramesFound && !allTransactionsApplied) {
1879            mFlinger->signalLayerUpdate();
1880            return outDirtyRegion;
1881        }
1882
1883        // This boolean is used to make sure that SurfaceFlinger's shadow copy
1884        // of the buffer queue isn't modified when the buffer queue is returning
1885        // BufferItem's that weren't actually queued. This can happen in shared
1886        // buffer mode.
1887        bool queuedBuffer = false;
1888        status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
1889                mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
1890                mLastFrameNumberReceived);
1891        if (updateResult == BufferQueue::PRESENT_LATER) {
1892            // Producer doesn't want buffer to be displayed yet.  Signal a
1893            // layer update so we check again at the next opportunity.
1894            mFlinger->signalLayerUpdate();
1895            return outDirtyRegion;
1896        } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1897            // If the buffer has been rejected, remove it from the shadow queue
1898            // and return early
1899            if (queuedBuffer) {
1900                Mutex::Autolock lock(mQueueItemLock);
1901                mQueueItems.removeAt(0);
1902                android_atomic_dec(&mQueuedFrames);
1903            }
1904            return outDirtyRegion;
1905        } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1906            // This can occur if something goes wrong when trying to create the
1907            // EGLImage for this buffer. If this happens, the buffer has already
1908            // been released, so we need to clean up the queue and bug out
1909            // early.
1910            if (queuedBuffer) {
1911                Mutex::Autolock lock(mQueueItemLock);
1912                mQueueItems.clear();
1913                android_atomic_and(0, &mQueuedFrames);
1914            }
1915
1916            // Once we have hit this state, the shadow queue may no longer
1917            // correctly reflect the incoming BufferQueue's contents, so even if
1918            // updateTexImage starts working, the only safe course of action is
1919            // to continue to ignore updates.
1920            mUpdateTexImageFailed = true;
1921
1922            return outDirtyRegion;
1923        }
1924
1925        if (queuedBuffer) {
1926            // Autolock scope
1927            auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1928
1929            Mutex::Autolock lock(mQueueItemLock);
1930
1931            // Remove any stale buffers that have been dropped during
1932            // updateTexImage
1933            while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1934                mQueueItems.removeAt(0);
1935                android_atomic_dec(&mQueuedFrames);
1936            }
1937
1938            mQueueItems.removeAt(0);
1939        }
1940
1941
1942        // Decrement the queued-frames count.  Signal another event if we
1943        // have more frames pending.
1944        if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
1945                || mAutoRefresh) {
1946            mFlinger->signalLayerUpdate();
1947        }
1948
1949        if (updateResult != NO_ERROR) {
1950            // something happened!
1951            recomputeVisibleRegions = true;
1952            return outDirtyRegion;
1953        }
1954
1955        // update the active buffer
1956        mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
1957        if (mActiveBuffer == NULL) {
1958            // this can only happen if the very first buffer was rejected.
1959            return outDirtyRegion;
1960        }
1961
1962        mRefreshPending = true;
1963        mFrameLatencyNeeded = true;
1964        if (oldActiveBuffer == NULL) {
1965             // the first time we receive a buffer, we need to trigger a
1966             // geometry invalidation.
1967            recomputeVisibleRegions = true;
1968         }
1969
1970        Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
1971        const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
1972        const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
1973        if ((crop != mCurrentCrop) ||
1974            (transform != mCurrentTransform) ||
1975            (scalingMode != mCurrentScalingMode))
1976        {
1977            mCurrentCrop = crop;
1978            mCurrentTransform = transform;
1979            mCurrentScalingMode = scalingMode;
1980            recomputeVisibleRegions = true;
1981        }
1982
1983        if (oldActiveBuffer != NULL) {
1984            uint32_t bufWidth  = mActiveBuffer->getWidth();
1985            uint32_t bufHeight = mActiveBuffer->getHeight();
1986            if (bufWidth != uint32_t(oldActiveBuffer->width) ||
1987                bufHeight != uint32_t(oldActiveBuffer->height)) {
1988                recomputeVisibleRegions = true;
1989            }
1990        }
1991
1992        mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
1993        if (oldOpacity != isOpaque(s)) {
1994            recomputeVisibleRegions = true;
1995        }
1996
1997        mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1998
1999        // Remove any sync points corresponding to the buffer which was just
2000        // latched
2001        {
2002            Mutex::Autolock lock(mLocalSyncPointMutex);
2003            auto point = mLocalSyncPoints.begin();
2004            while (point != mLocalSyncPoints.end()) {
2005                if (!(*point)->frameIsAvailable() ||
2006                        !(*point)->transactionIsApplied()) {
2007                    // This sync point must have been added since we started
2008                    // latching. Don't drop it yet.
2009                    ++point;
2010                    continue;
2011                }
2012
2013                if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2014                    point = mLocalSyncPoints.erase(point);
2015                } else {
2016                    ++point;
2017                }
2018            }
2019        }
2020
2021        // FIXME: postedRegion should be dirty & bounds
2022        Region dirtyRegion(Rect(s.active.w, s.active.h));
2023
2024        // transform the dirty region to window-manager space
2025        outDirtyRegion = (s.active.transform.transform(dirtyRegion));
2026    }
2027    return outDirtyRegion;
2028}
2029
2030uint32_t Layer::getEffectiveUsage(uint32_t usage) const
2031{
2032    // TODO: should we do something special if mSecure is set?
2033    if (mProtectedByApp) {
2034        // need a hardware-protected path to external video sink
2035        usage |= GraphicBuffer::USAGE_PROTECTED;
2036    }
2037    if (mPotentialCursor) {
2038        usage |= GraphicBuffer::USAGE_CURSOR;
2039    }
2040    usage |= GraphicBuffer::USAGE_HW_COMPOSER;
2041    return usage;
2042}
2043
2044void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
2045    uint32_t orientation = 0;
2046    if (!mFlinger->mDebugDisableTransformHint) {
2047        // The transform hint is used to improve performance, but we can
2048        // only have a single transform hint, it cannot
2049        // apply to all displays.
2050        const Transform& planeTransform(hw->getTransform());
2051        orientation = planeTransform.getOrientation();
2052        if (orientation & Transform::ROT_INVALID) {
2053            orientation = 0;
2054        }
2055    }
2056    mSurfaceFlingerConsumer->setTransformHint(orientation);
2057}
2058
2059// ----------------------------------------------------------------------------
2060// debugging
2061// ----------------------------------------------------------------------------
2062
2063void Layer::dump(String8& result, Colorizer& colorizer) const
2064{
2065    const Layer::State& s(getDrawingState());
2066
2067    colorizer.colorize(result, Colorizer::GREEN);
2068    result.appendFormat(
2069            "+ %s %p (%s)\n",
2070            getTypeId(), this, getName().string());
2071    colorizer.reset(result);
2072
2073    s.activeTransparentRegion.dump(result, "transparentRegion");
2074    visibleRegion.dump(result, "visibleRegion");
2075    surfaceDamageRegion.dump(result, "surfaceDamageRegion");
2076    sp<Client> client(mClientRef.promote());
2077
2078    result.appendFormat(            "      "
2079            "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2080            "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
2081            "isOpaque=%1d, invalidate=%1d, "
2082#ifdef USE_HWC2
2083            "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2084#else
2085            "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2086#endif
2087            "      client=%p\n",
2088            s.layerStack, s.z, s.active.transform.tx(), s.active.transform.ty(), s.active.w, s.active.h,
2089            s.active.crop.left, s.active.crop.top,
2090            s.active.crop.right, s.active.crop.bottom,
2091            s.active.finalCrop.left, s.active.finalCrop.top,
2092            s.active.finalCrop.right, s.active.finalCrop.bottom,
2093            isOpaque(s), contentDirty,
2094            s.alpha, s.flags,
2095            s.active.transform[0][0], s.active.transform[0][1],
2096            s.active.transform[1][0], s.active.transform[1][1],
2097            client.get());
2098
2099    sp<const GraphicBuffer> buf0(mActiveBuffer);
2100    uint32_t w0=0, h0=0, s0=0, f0=0;
2101    if (buf0 != 0) {
2102        w0 = buf0->getWidth();
2103        h0 = buf0->getHeight();
2104        s0 = buf0->getStride();
2105        f0 = buf0->format;
2106    }
2107    result.appendFormat(
2108            "      "
2109            "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2110            " queued-frames=%d, mRefreshPending=%d\n",
2111            mFormat, w0, h0, s0,f0,
2112            mQueuedFrames, mRefreshPending);
2113
2114    if (mSurfaceFlingerConsumer != 0) {
2115        mSurfaceFlingerConsumer->dump(result, "            ");
2116    }
2117}
2118
2119void Layer::dumpFrameStats(String8& result) const {
2120    mFrameTracker.dumpStats(result);
2121}
2122
2123void Layer::clearFrameStats() {
2124    mFrameTracker.clearStats();
2125}
2126
2127void Layer::logFrameStats() {
2128    mFrameTracker.logAndResetStats(mName);
2129}
2130
2131void Layer::getFrameStats(FrameStats* outStats) const {
2132    mFrameTracker.getStats(outStats);
2133}
2134
2135void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2136        bool* outIsGlesComposition, nsecs_t* outPostedTime,
2137        sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2138    *outName = mName;
2139    *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2140
2141#ifdef USE_HWC2
2142    *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2143            mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2144            HWC2::Composition::Client : true;
2145#else
2146    *outIsGlesComposition = mIsGlesComposition;
2147#endif
2148    *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2149    *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2150    *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2151}
2152// ---------------------------------------------------------------------------
2153
2154Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2155        const sp<Layer>& layer)
2156    : mFlinger(flinger), mLayer(layer) {
2157}
2158
2159Layer::LayerCleaner::~LayerCleaner() {
2160    // destroy client resources
2161    mFlinger->onLayerDestroyed(mLayer);
2162}
2163
2164// ---------------------------------------------------------------------------
2165}; // namespace android
2166
2167#if defined(__gl_h_)
2168#error "don't include gl/gl.h in this file"
2169#endif
2170
2171#if defined(__gl2_h_)
2172#error "don't include gl2/gl2.h in this file"
2173#endif
2174