Add MiniFence to drop HWC2on1Adapter libui dep

This class will soon become a library used by devices with no support
for HWC2. They will link against libhwc2on1adapter. Drivers should have
no depedencies on libui.

Test:Manual
Change-Id: Iabd2aa89fc3b737a999632a16c4f6c30464787c4
diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk
index afaccd2..a317ea2 100644
--- a/services/surfaceflinger/Android.mk
+++ b/services/surfaceflinger/Android.mk
@@ -26,6 +26,7 @@
     DisplayHardware/FramebufferSurface.cpp \
     DisplayHardware/HWC2.cpp \
     DisplayHardware/HWC2On1Adapter.cpp \
+    DisplayHardware/MiniFence.cpp \
     DisplayHardware/PowerHAL.cpp \
     DisplayHardware/VirtualDisplaySurface.cpp \
     Effects/Daltonizer.cpp \
diff --git a/services/surfaceflinger/DisplayHardware/HWC2On1Adapter.cpp b/services/surfaceflinger/DisplayHardware/HWC2On1Adapter.cpp
index 4187890..13bf0b5 100644
--- a/services/surfaceflinger/DisplayHardware/HWC2On1Adapter.cpp
+++ b/services/surfaceflinger/DisplayHardware/HWC2On1Adapter.cpp
@@ -724,7 +724,7 @@
         }
 
         auto releaseFence = layer->getReleaseFence();
-        if (releaseFence != Fence::NO_FENCE) {
+        if (releaseFence != MiniFence::NO_FENCE) {
             if (outputsNonNull) {
                 outLayers[numWritten] = layer->getId();
                 outFences[numWritten] = releaseFence->dup();
@@ -2003,7 +2003,7 @@
     mReleaseFence.add(fenceFd);
 }
 
-const sp<Fence>& HWC2On1Adapter::Layer::getReleaseFence() const {
+const sp<MiniFence>& HWC2On1Adapter::Layer::getReleaseFence() const {
     return mReleaseFence.get();
 }
 
diff --git a/services/surfaceflinger/DisplayHardware/HWC2On1Adapter.h b/services/surfaceflinger/DisplayHardware/HWC2On1Adapter.h
index 408bc41..a1d2c88 100644
--- a/services/surfaceflinger/DisplayHardware/HWC2On1Adapter.h
+++ b/services/surfaceflinger/DisplayHardware/HWC2On1Adapter.h
@@ -23,7 +23,7 @@
 #undef HWC2_INCLUDE_STRINGIFICATION
 #undef HWC2_USE_CPP11
 
-#include <ui/Fence.h>
+#include "MiniFence.h"
 
 #include <atomic>
 #include <map>
@@ -155,35 +155,35 @@
     class DeferredFence {
         public:
             DeferredFence()
-              : mFences({Fence::NO_FENCE, Fence::NO_FENCE}) {}
+              : mFences({MiniFence::NO_FENCE, MiniFence::NO_FENCE}) {}
 
             void add(int32_t fenceFd) {
-                mFences.emplace(new Fence(fenceFd));
+                mFences.emplace(new MiniFence(fenceFd));
                 mFences.pop();
             }
 
-            const sp<Fence>& get() const {
+            const sp<MiniFence>& get() const {
                 return mFences.front();
             }
 
         private:
             // There are always two fences in this queue.
-            std::queue<sp<Fence>> mFences;
+            std::queue<sp<MiniFence>> mFences;
     };
 
     class FencedBuffer {
         public:
-            FencedBuffer() : mBuffer(nullptr), mFence(Fence::NO_FENCE) {}
+            FencedBuffer() : mBuffer(nullptr), mFence(MiniFence::NO_FENCE) {}
 
             void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; }
-            void setFence(int fenceFd) { mFence = new Fence(fenceFd); }
+            void setFence(int fenceFd) { mFence = new MiniFence(fenceFd); }
 
             buffer_handle_t getBuffer() const { return mBuffer; }
             int getFence() const { return mFence->dup(); }
 
         private:
             buffer_handle_t mBuffer;
-            sp<Fence> mFence;
+            sp<MiniFence> mFence;
     };
 
     class Display {
@@ -552,7 +552,7 @@
             uint32_t getZ() const { return mZ; }
 
             void addReleaseFence(int fenceFd);
-            const sp<Fence>& getReleaseFence() const;
+            const sp<MiniFence>& getReleaseFence() const;
 
             void setHwc1Id(size_t id) { mHwc1Id = id; }
             size_t getHwc1Id() const { return mHwc1Id; }
diff --git a/services/surfaceflinger/DisplayHardware/MiniFence.cpp b/services/surfaceflinger/DisplayHardware/MiniFence.cpp
new file mode 100644
index 0000000..ecfb063
--- /dev/null
+++ b/services/surfaceflinger/DisplayHardware/MiniFence.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2017 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 "MiniFence.h"
+
+#include <unistd.h>
+
+namespace android {
+
+const sp<MiniFence> MiniFence::NO_FENCE = sp<MiniFence>(new MiniFence);
+
+MiniFence::MiniFence() :
+    mFenceFd(-1) {
+}
+
+MiniFence::MiniFence(int fenceFd) :
+    mFenceFd(fenceFd) {
+}
+
+MiniFence::~MiniFence() {
+    if (mFenceFd != -1) {
+        close(mFenceFd);
+    }
+}
+
+int MiniFence::dup() const {
+    return ::dup(mFenceFd);
+}
+}
diff --git a/services/surfaceflinger/DisplayHardware/MiniFence.h b/services/surfaceflinger/DisplayHardware/MiniFence.h
new file mode 100644
index 0000000..75de764
--- /dev/null
+++ b/services/surfaceflinger/DisplayHardware/MiniFence.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2017 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 MINIFENCE_H
+#define MINIFENCE_H
+
+#include <utils/RefBase.h>
+
+namespace android {
+
+/* MiniFence is a minimal re-implementation of Fence from libui. It exists to
+ * avoid linking the HWC2on1Adapter to libui and satisfy Treble requirements.
+ */
+class MiniFence : public LightRefBase<MiniFence> {
+public:
+    static const sp<MiniFence> NO_FENCE;
+
+    // Construct a new MiniFence object with an invalid file descriptor.
+    MiniFence();
+
+    // Construct a new MiniFence object to manage a given fence file descriptor.
+    // When the new MiniFence object is destructed the file descriptor will be
+    // closed.
+    explicit MiniFence(int fenceFd);
+
+    // Not copyable or movable.
+    MiniFence(const MiniFence& rhs) = delete;
+    MiniFence& operator=(const MiniFence& rhs) = delete;
+    MiniFence(MiniFence&& rhs) = delete;
+    MiniFence& operator=(MiniFence&& rhs) = delete;
+
+    // Return a duplicate of the fence file descriptor. The caller is
+    // responsible for closing the returned file descriptor. On error, -1 will
+    // be returned and errno will indicate the problem.
+    int dup() const;
+
+private:
+    // Only allow instantiation using ref counting.
+    friend class LightRefBase<MiniFence>;
+    ~MiniFence();
+
+    int mFenceFd;
+
+};
+}
+#endif //MINIFENCE_H