Merge "Delete surfacefinger Layer::latchBuffer code"
diff --git a/libs/gui/Android.bp b/libs/gui/Android.bp
index 9e9799b..9ba85a6 100644
--- a/libs/gui/Android.bp
+++ b/libs/gui/Android.bp
@@ -104,7 +104,7 @@
         "liblog",
     ],
 
-    export_shared_lib_headers: ["libbinder"],
+    export_shared_lib_headers: ["libbinder", "libui"],
 }
 
 subdirs = ["tests"]
diff --git a/libs/gui/tests/Android.bp b/libs/gui/tests/Android.bp
index 3c7958f..092d597 100644
--- a/libs/gui/tests/Android.bp
+++ b/libs/gui/tests/Android.bp
@@ -35,7 +35,6 @@
         "libbinder",
         "libcutils",
         "libgui",
-        "libsync",
         "libui",
         "libutils",
     ],
diff --git a/services/sensorservice/SensorDeviceTreble.cpp b/services/sensorservice/SensorDeviceTreble.cpp
index 66b0f5a..6f946d0 100644
--- a/services/sensorservice/SensorDeviceTreble.cpp
+++ b/services/sensorservice/SensorDeviceTreble.cpp
@@ -27,7 +27,7 @@
 #include "SensorDevice.h"
 #include "SensorService.h"
 
-#include "convert.h"
+#include <sensors/convert.h>
 
 using android::hardware::sensors::V1_0::ISensors;
 using android::hardware::hidl_vec;
diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk
index ba036dd..7135ed3 100644
--- a/services/surfaceflinger/Android.mk
+++ b/services/surfaceflinger/Android.mk
@@ -15,6 +15,7 @@
     GpuService.cpp \
     Layer.cpp \
     LayerDim.cpp \
+    LayerRejecter.cpp \
     MessageQueue.cpp \
     MonitoredProducer.cpp \
     SurfaceFlingerConsumer.cpp \
diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index 2c02c27..5f96f66 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -44,6 +44,7 @@
 #include "Colorizer.h"
 #include "DisplayDevice.h"
 #include "Layer.h"
+#include "LayerRejecter.h"
 #include "MonitoredProducer.h"
 #include "SurfaceFlinger.h"
 
@@ -1786,6 +1787,32 @@
 #endif
 }
 
+bool Layer::allTransactionsSignaled() {
+    auto headFrameNumber = getHeadFrameNumber();
+    bool matchingFramesFound = false;
+    bool allTransactionsApplied = true;
+    Mutex::Autolock lock(mLocalSyncPointMutex);
+
+    for (auto& point : mLocalSyncPoints) {
+        if (point->getFrameNumber() > headFrameNumber) {
+            break;
+        }
+        matchingFramesFound = true;
+
+        if (!point->frameIsAvailable()) {
+           // We haven't notified the remote layer that the frame for
+           // this point is available yet. Notify it now, and then
+           // abort this attempt to latch.
+           point->setFrameAvailable();
+           allTransactionsApplied = false;
+           break;
+        }
+
+        allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
+    }
+    return !matchingFramesFound || allTransactionsApplied;
+}
+
 Region Layer::latchBuffer(bool& recomputeVisibleRegions)
 {
     ATRACE_CALL();
@@ -1829,163 +1856,7 @@
     const bool oldOpacity = isOpaque(s);
     sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
 
-    struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
-        Layer::State& front;
-        Layer::State& current;
-        bool& recomputeVisibleRegions;
-        bool stickyTransformSet;
-        const char* name;
-        int32_t overrideScalingMode;
-        bool& freezePositionUpdates;
-
-        Reject(Layer::State& front, Layer::State& current,
-                bool& recomputeVisibleRegions, bool stickySet,
-                const char* name,
-                int32_t overrideScalingMode,
-                bool& freezePositionUpdates)
-            : front(front), current(current),
-              recomputeVisibleRegions(recomputeVisibleRegions),
-              stickyTransformSet(stickySet),
-              name(name),
-              overrideScalingMode(overrideScalingMode),
-              freezePositionUpdates(freezePositionUpdates) {
-        }
-
-        virtual bool reject(const sp<GraphicBuffer>& buf,
-                const BufferItem& item) {
-            if (buf == NULL) {
-                return false;
-            }
-
-            uint32_t bufWidth  = buf->getWidth();
-            uint32_t bufHeight = buf->getHeight();
-
-            // check that we received a buffer of the right size
-            // (Take the buffer's orientation into account)
-            if (item.mTransform & Transform::ROT_90) {
-                swap(bufWidth, bufHeight);
-            }
-
-            int actualScalingMode = overrideScalingMode >= 0 ?
-                    overrideScalingMode : item.mScalingMode;
-            bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
-            if (front.active != front.requested) {
-
-                if (isFixedSize ||
-                        (bufWidth == front.requested.w &&
-                         bufHeight == front.requested.h))
-                {
-                    // Here we pretend the transaction happened by updating the
-                    // current and drawing states. Drawing state is only accessed
-                    // in this thread, no need to have it locked
-                    front.active = front.requested;
-
-                    // We also need to update the current state so that
-                    // we don't end-up overwriting the drawing state with
-                    // this stale current state during the next transaction
-                    //
-                    // NOTE: We don't need to hold the transaction lock here
-                    // because State::active is only accessed from this thread.
-                    current.active = front.active;
-                    current.modified = true;
-
-                    // recompute visible region
-                    recomputeVisibleRegions = true;
-                }
-
-                ALOGD_IF(DEBUG_RESIZE,
-                        "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
-                        "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
-                        "            requested={ wh={%4u,%4u} }}\n",
-                        name,
-                        bufWidth, bufHeight, item.mTransform, item.mScalingMode,
-                        front.active.w, front.active.h,
-                        front.crop.left,
-                        front.crop.top,
-                        front.crop.right,
-                        front.crop.bottom,
-                        front.crop.getWidth(),
-                        front.crop.getHeight(),
-                        front.requested.w, front.requested.h);
-            }
-
-            if (!isFixedSize && !stickyTransformSet) {
-                if (front.active.w != bufWidth ||
-                    front.active.h != bufHeight) {
-                    // reject this buffer
-                    ALOGE("[%s] rejecting buffer: "
-                            "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
-                            name, bufWidth, bufHeight, front.active.w, front.active.h);
-                    return true;
-                }
-            }
-
-            // if the transparent region has changed (this test is
-            // conservative, but that's fine, worst case we're doing
-            // a bit of extra work), we latch the new one and we
-            // trigger a visible-region recompute.
-            if (!front.activeTransparentRegion.isTriviallyEqual(
-                    front.requestedTransparentRegion)) {
-                front.activeTransparentRegion = front.requestedTransparentRegion;
-
-                // We also need to update the current state so that
-                // we don't end-up overwriting the drawing state with
-                // this stale current state during the next transaction
-                //
-                // NOTE: We don't need to hold the transaction lock here
-                // because State::active is only accessed from this thread.
-                current.activeTransparentRegion = front.activeTransparentRegion;
-
-                // recompute visible region
-                recomputeVisibleRegions = true;
-            }
-
-            if (front.crop != front.requestedCrop) {
-                front.crop = front.requestedCrop;
-                current.crop = front.requestedCrop;
-                recomputeVisibleRegions = true;
-            }
-            freezePositionUpdates = false;
-
-            return false;
-        }
-    };
-
-    Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
-            getProducerStickyTransform() != 0, mName.string(),
-            mOverrideScalingMode, mFreezePositionUpdates);
-
-
-    // Check all of our local sync points to ensure that all transactions
-    // which need to have been applied prior to the frame which is about to
-    // be latched have signaled
-
-    auto headFrameNumber = getHeadFrameNumber();
-    bool matchingFramesFound = false;
-    bool allTransactionsApplied = true;
-    {
-        Mutex::Autolock lock(mLocalSyncPointMutex);
-        for (auto& point : mLocalSyncPoints) {
-            if (point->getFrameNumber() > headFrameNumber) {
-                break;
-            }
-
-            matchingFramesFound = true;
-
-            if (!point->frameIsAvailable()) {
-                // We haven't notified the remote layer that the frame for
-                // this point is available yet. Notify it now, and then
-                // abort this attempt to latch.
-                point->setFrameAvailable();
-                allTransactionsApplied = false;
-                break;
-            }
-
-            allTransactionsApplied &= point->transactionIsApplied();
-        }
-    }
-
-    if (matchingFramesFound && !allTransactionsApplied) {
+    if (!allTransactionsSignaled()) {
         mFlinger->signalLayerUpdate();
         return outDirtyRegion;
     }
@@ -1995,6 +1866,9 @@
     // BufferItem's that weren't actually queued. This can happen in shared
     // buffer mode.
     bool queuedBuffer = false;
+    LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
+                    getProducerStickyTransform() != 0, mName.string(),
+                    mOverrideScalingMode, mFreezePositionUpdates);
     status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
             mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
             mLastFrameNumberReceived);
diff --git a/services/surfaceflinger/Layer.h b/services/surfaceflinger/Layer.h
index efb0fb8..64b049c 100644
--- a/services/surfaceflinger/Layer.h
+++ b/services/surfaceflinger/Layer.h
@@ -558,6 +558,11 @@
 
     // -----------------------------------------------------------------------
 
+    // Check all of the local sync points to ensure that all transactions
+    // which need to have been applied prior to the frame which is about to
+    // be latched have signaled
+    bool allTransactionsSignaled();
+
     // constants
     sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
     sp<IGraphicBufferProducer> mProducer;
diff --git a/services/surfaceflinger/LayerRejecter.cpp b/services/surfaceflinger/LayerRejecter.cpp
new file mode 100644
index 0000000..2bc0605
--- /dev/null
+++ b/services/surfaceflinger/LayerRejecter.cpp
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LayerRejecter.h"
+
+#include "clz.h"
+
+#define DEBUG_RESIZE 0
+
+namespace android {
+
+LayerRejecter::LayerRejecter(Layer::State& front,
+                             Layer::State& current,
+                             bool& recomputeVisibleRegions,
+                             bool stickySet,
+                             const char* name,
+                             int32_t overrideScalingMode,
+                             bool& freezePositionUpdates)
+  : mFront(front),
+    mCurrent(current),
+    mRecomputeVisibleRegions(recomputeVisibleRegions),
+    mStickyTransformSet(stickySet),
+    mName(name),
+    mOverrideScalingMode(overrideScalingMode),
+    mFreezePositionUpdates(freezePositionUpdates) {}
+
+bool LayerRejecter::reject(const sp<GraphicBuffer>& buf, const BufferItem& item) {
+    if (buf == NULL) {
+        return false;
+    }
+
+    uint32_t bufWidth = buf->getWidth();
+    uint32_t bufHeight = buf->getHeight();
+
+    // check that we received a buffer of the right size
+    // (Take the buffer's orientation into account)
+    if (item.mTransform & Transform::ROT_90) {
+        swap(bufWidth, bufHeight);
+    }
+
+    int actualScalingMode = mOverrideScalingMode >= 0 ? mOverrideScalingMode : item.mScalingMode;
+    bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
+    if (mFront.active != mFront.requested) {
+        if (isFixedSize || (bufWidth == mFront.requested.w && bufHeight == mFront.requested.h)) {
+            // Here we pretend the transaction happened by updating the
+            // current and drawing states. Drawing state is only accessed
+            // in this thread, no need to have it locked
+            mFront.active = mFront.requested;
+
+            // We also need to update the current state so that
+            // we don't end-up overwriting the drawing state with
+            // this stale current state during the next transaction
+            //
+            // NOTE: We don't need to hold the transaction lock here
+            // because State::active is only accessed from this thread.
+            mCurrent.active = mFront.active;
+            mCurrent.modified = true;
+
+            // recompute visible region
+            mRecomputeVisibleRegions = true;
+        }
+
+        ALOGD_IF(DEBUG_RESIZE,
+                 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
+                 "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) "
+                 "}\n"
+                 "            requested={ wh={%4u,%4u} }}\n",
+                 mName, bufWidth, bufHeight, item.mTransform, item.mScalingMode, mFront.active.w,
+                 mFront.active.h, mFront.crop.left, mFront.crop.top, mFront.crop.right,
+                 mFront.crop.bottom, mFront.crop.getWidth(), mFront.crop.getHeight(),
+                 mFront.requested.w, mFront.requested.h);
+    }
+
+    if (!isFixedSize && !mStickyTransformSet) {
+        if (mFront.active.w != bufWidth || mFront.active.h != bufHeight) {
+            // reject this buffer
+            ALOGE("[%s] rejecting buffer: "
+                  "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
+                  mName, bufWidth, bufHeight, mFront.active.w, mFront.active.h);
+            return true;
+        }
+    }
+
+    // if the transparent region has changed (this test is
+    // conservative, but that's fine, worst case we're doing
+    // a bit of extra work), we latch the new one and we
+    // trigger a visible-region recompute.
+    if (!mFront.activeTransparentRegion.isTriviallyEqual(mFront.requestedTransparentRegion)) {
+        mFront.activeTransparentRegion = mFront.requestedTransparentRegion;
+
+        // We also need to update the current state so that
+        // we don't end-up overwriting the drawing state with
+        // this stale current state during the next transaction
+        //
+        // NOTE: We don't need to hold the transaction lock here
+        // because State::active is only accessed from this thread.
+        mCurrent.activeTransparentRegion = mFront.activeTransparentRegion;
+
+        // recompute visible region
+        mRecomputeVisibleRegions = true;
+    }
+
+    if (mFront.crop != mFront.requestedCrop) {
+        mFront.crop = mFront.requestedCrop;
+        mCurrent.crop = mFront.requestedCrop;
+        mRecomputeVisibleRegions = true;
+    }
+    mFreezePositionUpdates = false;
+
+    return false;
+}
+
+}  // namespace android
\ No newline at end of file
diff --git a/services/surfaceflinger/LayerRejecter.h b/services/surfaceflinger/LayerRejecter.h
new file mode 100644
index 0000000..c2a9483
--- /dev/null
+++ b/services/surfaceflinger/LayerRejecter.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_LAYER_REJECTER_H
+#define ANDROID_LAYER_REJECTER_H
+
+#include "Layer.h"
+#include "SurfaceFlingerConsumer.h"
+
+namespace android {
+    class LayerRejecter : public SurfaceFlingerConsumer::BufferRejecter {
+    public:
+        LayerRejecter(Layer::State &front,
+                      Layer::State &current,
+                      bool &recomputeVisibleRegions,
+                      bool stickySet,
+                      const char *name,
+                      int32_t overrideScalingMode,
+                      bool &freezePositionUpdates);
+
+        virtual bool reject(const sp<GraphicBuffer> &buf, const BufferItem &item);
+
+    private:
+        Layer::State &mFront;
+        Layer::State &mCurrent;
+        bool &mRecomputeVisibleRegions;
+        bool mStickyTransformSet;
+        const char *mName;
+        int32_t mOverrideScalingMode;
+        bool &mFreezePositionUpdates;
+    };
+}  // namespace android
+
+#endif  // ANDROID_LAYER_REJECTER_H
\ No newline at end of file