Snap for 6435660 from 579d26b9d8b2f45b8daab206eb765e823115d5be to sdk-release

Change-Id: Ibdbc0d6d23cb27dafb2adca3dd13bfe5fac7eefd
diff --git a/android-emu/android/base/ring_buffer.c b/android-emu/android/base/ring_buffer.c
index 1ad122e..527d2ea 100644
--- a/android-emu/android/base/ring_buffer.c
+++ b/android-emu/android/base/ring_buffer.c
@@ -415,26 +415,12 @@
 #endif
 }
 
-static uint64_t ring_buffer_curr_us() {
-    uint64_t res;
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    res = tv.tv_sec * 1000000ULL + tv.tv_usec;
-    return res;
-}
-
-static const uint32_t yield_backoff_us = 1000;
-static const uint32_t sleep_backoff_us = 2000;
-
 bool ring_buffer_wait_write(
     const struct ring_buffer* r,
     const struct ring_buffer_view* v,
     uint32_t bytes,
     uint64_t timeout_us) {
 
-    uint64_t start_us = ring_buffer_curr_us();
-    uint64_t curr_wait_us;
-
     bool can_write =
         v ? ring_buffer_view_can_write(r, v, bytes) :
             ring_buffer_can_write(r, bytes);
@@ -475,7 +461,6 @@
     struct ring_buffer_view* v,
     uint32_t bytes) {
 
-    uint32_t step_shift = 0;
     uint32_t available = v ? (v->size >> 1) : (RING_BUFFER_SIZE >> 1);
     uint32_t res = available < bytes ? available : bytes;
 
diff --git a/system/egl/egl.cpp b/system/egl/egl.cpp
index 5940b79..aaa763a 100644
--- a/system/egl/egl.cpp
+++ b/system/egl/egl.cpp
@@ -120,7 +120,7 @@
 #endif //LOG_EGL_ERRORS
 
 #define VALIDATE_CONFIG(cfg,ret) \
-    if(((intptr_t)(cfg)<0)||((intptr_t)(cfg)>s_display.getNumConfigs())) { \
+    if (!s_display.isValidConfig(cfg)) { \
         RETURN_ERROR(ret,EGL_BAD_CONFIG); \
     }
 
@@ -408,7 +408,7 @@
     setNativeHeight(nativeHeight);
 
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
-    rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)config,
+    rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)s_display.getIndexOfConfig(config),
             getWidth(), getHeight());
 
     if (!rcSurface) {
@@ -668,7 +668,7 @@
 {
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
 
-    rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)config,
+    rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)s_display.getIndexOfConfig(config),
             getWidth(), getHeight());
     if (!rcSurface) {
         ALOGE("rcCreateWindowSurface returned 0");
@@ -945,7 +945,7 @@
 
     EGLint i;
     for (i = 0 ; i < numConfigs && i < config_size ; i++) {
-        *configs++ = (EGLConfig)(uintptr_t)i;
+        *configs++ = (EGLConfig)(uintptr_t)s_display.getConfigAtIndex(i);
     }
     *num_config = i;
     return EGL_TRUE;
@@ -1006,7 +1006,8 @@
     if (configs!=NULL) {
         EGLint i=0;
         for (i=0;i<(*num_config);i++) {
-             *((uintptr_t*)configs+i) = *((uint32_t*)tempConfigs+i);
+            EGLConfig guestConfig = s_display.getConfigAtIndex(*((uint32_t*)tempConfigs+i));
+            configs[i] = guestConfig;
         }
     }
 
@@ -1614,7 +1615,7 @@
     if (majorVersion == 3 && minorVersion == 2) {
         rcMajorVersion = 4;
     }
-    uint32_t rcContext = rcEnc->rcCreateContext(rcEnc, (uintptr_t)config, rcShareCtx, rcMajorVersion);
+    uint32_t rcContext = rcEnc->rcCreateContext(rcEnc, (uintptr_t)s_display.getIndexOfConfig(config), rcShareCtx, rcMajorVersion);
     if (!rcContext) {
         ALOGE("rcCreateContext returned 0");
         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
diff --git a/system/egl/eglDisplay.cpp b/system/egl/eglDisplay.cpp
index 2e18e11..b3f14da 100644
--- a/system/egl/eglDisplay.cpp
+++ b/system/egl/eglDisplay.cpp
@@ -171,7 +171,10 @@
 
         uint32_t nInts = m_numConfigAttribs * (m_numConfigs + 1);
         EGLint tmp_buf[nInts];
+        uint32_t configCount = nInts - m_numConfigAttribs;
+
         m_configs = new EGLint[nInts-m_numConfigAttribs];
+
         if (!m_configs) {
             pthread_mutex_unlock(&m_lock);
             return false;
@@ -204,7 +207,7 @@
 void eglDisplay::processConfigs()
 {
     for (intptr_t i=0; i<m_numConfigs; i++) {
-        EGLConfig config = (EGLConfig)i;
+        EGLConfig config = getConfigAtIndex(i);
         PixelFormat format;
         if (getConfigNativePixelFormat(config, &format)) {
             setConfigAttrib(config, EGL_NATIVE_VISUAL_ID, format);
@@ -443,13 +446,29 @@
         ALOGE("[%s] Bad attribute idx\n", __FUNCTION__);
         return EGL_FALSE;
     }
-    *value = *(m_configs + (intptr_t)config*m_numConfigAttribs + attribIdx);
+    *value = *(m_configs + (intptr_t)(getIndexOfConfig(config))*m_numConfigAttribs + attribIdx);
     return EGL_TRUE;
 }
 
 #define EGL_COLOR_COMPONENT_TYPE_EXT 0x3339
 #define EGL_COLOR_COMPONENT_TYPE_FIXED_EXT 0x333A
 
+EGLConfig eglDisplay::getConfigAtIndex(uint32_t index) const {
+    uintptr_t asPtr = (uintptr_t)index;
+    return (EGLConfig)(asPtr + 1);
+}
+
+uint32_t eglDisplay::getIndexOfConfig(EGLConfig config) const {
+    uintptr_t asInteger = (uintptr_t)config;
+    return (uint32_t)(asInteger - 1);
+}
+
+bool eglDisplay::isValidConfig(EGLConfig cfg) const {
+    uint32_t index = getIndexOfConfig(cfg);
+    intptr_t asInt = (intptr_t)index;
+    return !(asInt < 0 || asInt > m_numConfigs);
+}
+
 EGLBoolean eglDisplay::getConfigAttrib(EGLConfig config, EGLint attrib, EGLint * value)
 {
     if (attrib == EGL_FRAMEBUFFER_TARGET_ANDROID) {
@@ -484,10 +503,10 @@
 void eglDisplay::dumpConfig(EGLConfig config)
 {
     EGLint value = 0;
-    DBG("^^^^^^^^^^ dumpConfig %d ^^^^^^^^^^^^^^^^^^", (int)config);
+    DBG("^^^^^^^^^^ dumpConfig %p ^^^^^^^^^^^^^^^^^^", config);
     for (int i=0; i<m_numConfigAttribs; i++) {
         getAttribValue(config, i, &value);
-        DBG("{%d}[%d] %d\n", (int)config, i, value);
+        DBG("Config %p: {%u}[%d] %d\n", config, getIndexOfConfig(config), i, value);
     }
 }
 
@@ -501,7 +520,7 @@
         ALOGE("[%s] Bad attribute idx\n", __FUNCTION__);
         return EGL_FALSE;
     }
-    *(m_configs + (intptr_t)config*m_numConfigAttribs + attribIdx) = value;
+    *(m_configs + (intptr_t)(getIndexOfConfig(config))*m_numConfigAttribs + attribIdx) = value;
     return EGL_TRUE;
 }
 
diff --git a/system/egl/eglDisplay.h b/system/egl/eglDisplay.h
index 2394da1..951075d 100644
--- a/system/egl/eglDisplay.h
+++ b/system/egl/eglDisplay.h
@@ -56,6 +56,11 @@
     const EGLClient_glesInterface *gles2_iface() const { return m_gles2_iface; }
 
     int     getNumConfigs(){ return m_numConfigs; }
+
+    EGLConfig getConfigAtIndex(uint32_t index) const;
+    uint32_t getIndexOfConfig(EGLConfig config) const;
+    bool isValidConfig(EGLConfig cfg) const;
+
     EGLBoolean  getConfigAttrib(EGLConfig config, EGLint attrib, EGLint * value);
     EGLBoolean  setConfigAttrib(EGLConfig config, EGLint attrib, EGLint value);
     EGLBoolean getConfigGLPixelFormat(EGLConfig config, GLenum * format);
diff --git a/system/hwc2/Android.mk b/system/hwc2/Android.mk
index 698de73..24a8a58 100644
--- a/system/hwc2/Android.mk
+++ b/system/hwc2/Android.mk
@@ -32,6 +32,7 @@
 emulator_hwcomposer_cflags += \
     -DLOG_TAG=\"hwc2\" \
     -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION) \
+    -DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION
 
 emulator_hwcomposer_c_includes += \
     system/core/libsync \
@@ -45,15 +46,14 @@
 emulator_hwcomposer_relative_path := hw
 
 emulator_hwcomposer2_src_files := \
-    EmuHWC2.cpp \
-    MiniFence.cpp
+    EmuHWC2.cpp
 
 include $(CLEAR_VARS)
 
 LOCAL_VENDOR_MODULE := true
 LOCAL_SHARED_LIBRARIES := $(emulator_hwcomposer_shared_libraries)
 LOCAL_SHARED_LIBRARIES += libOpenglSystemCommon lib_renderControl_enc
-LOCAL_SHARED_LIBRARIES += libcbmanager
+LOCAL_SHARED_LIBRARIES += libui
 LOCAL_SRC_FILES := $(emulator_hwcomposer2_src_files)
 LOCAL_C_INCLUDES := $(emulator_hwcomposer_c_includes)
 LOCAL_MODULE_RELATIVE_PATH := $(emulator_hwcomposer_relative_path)
diff --git a/system/hwc2/EmuHWC2.cpp b/system/hwc2/EmuHWC2.cpp
index eaddcd0..bac4eac 100644
--- a/system/hwc2/EmuHWC2.cpp
+++ b/system/hwc2/EmuHWC2.cpp
@@ -27,6 +27,8 @@
 
 #include <EGL/egl.h>
 #include <EGL/eglext.h>
+#include <ui/GraphicBuffer.h>
+#include <ui/GraphicBufferAllocator.h>
 
 #include "../egl/goldfish_sync.h"
 
@@ -390,17 +392,27 @@
 }
 
 const native_handle_t* EmuHWC2::allocateDisplayColorBuffer() {
-    typedef CbManager::BufferUsage BufferUsage;
+    const uint32_t layerCount = 1;
+    const uint64_t graphicBufferId = 0; // not used
 
-    return mCbManager.allocateBuffer(
-        mDisplayWidth,
-        mDisplayHeight,
-        CbManager::PixelFormat::RGBA_8888,
-        (BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_RENDER_TARGET));
+    buffer_handle_t h;
+    uint32_t stride;
+
+    if (GraphicBufferAllocator::get().allocate(
+        mDisplayWidth, mDisplayHeight,
+        PIXEL_FORMAT_RGBA_8888,
+        layerCount,
+        (GraphicBuffer::USAGE_HW_COMPOSER | GraphicBuffer::USAGE_HW_RENDER),
+        &h, &stride,
+        graphicBufferId, "EmuHWC2") == OK) {
+        return static_cast<const native_handle_t*>(h);
+    } else {
+        return nullptr;
+    }
 }
 
 void EmuHWC2::freeDisplayColorBuffer(const native_handle_t* h) {
-    mCbManager.freeBuffer(h);
+    GraphicBufferAllocator::get().free(h);
 }
 
 // Display functions
diff --git a/system/hwc2/EmuHWC2.h b/system/hwc2/EmuHWC2.h
index 8cb71c1..1ff3394 100644
--- a/system/hwc2/EmuHWC2.h
+++ b/system/hwc2/EmuHWC2.h
@@ -24,8 +24,10 @@
 #undef HWC2_USE_CPP11
 #include <utils/Thread.h>
 
+#include <android-base/unique_fd.h>
 #include <atomic>
 #include <map>
+#include <memory>
 #include <mutex>
 #include <numeric>
 #include <sstream>
@@ -36,8 +38,6 @@
 
 #include <cutils/native_handle.h>
 
-#include "cbmanager.h"
-#include "MiniFence.h"
 #include "HostConnection.h"
 
 namespace android {
@@ -126,17 +126,19 @@
     // layer. This class is a container for these two.
     class FencedBuffer {
         public:
-            FencedBuffer() : mBuffer(nullptr), mFence(MiniFence::NO_FENCE) {}
+            FencedBuffer() : mBuffer(nullptr) {}
 
             void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; }
-            void setFence(int fenceFd) { mFence = new MiniFence(fenceFd); }
+            void setFence(int fenceFd) {
+                mFence = std::make_shared<base::unique_fd>(fenceFd);
+            }
 
             buffer_handle_t getBuffer() const { return mBuffer; }
-            int getFence() const { return mFence->dup(); }
+            int getFence() const { return mFence ? dup(mFence->get()) : -1; }
 
         private:
             buffer_handle_t mBuffer;
-            sp<MiniFence> mFence;
+            std::shared_ptr<base::unique_fd> mFence;
     };
 
     typedef struct compose_layer {
@@ -467,7 +469,6 @@
     const native_handle_t* allocateDisplayColorBuffer();
     void freeDisplayColorBuffer(const native_handle_t* h);
 
-    CbManager mCbManager;
     std::unordered_set<HWC2::Capability> mCapabilities;
 
     // These are potentially accessed from multiple threads, and are protected
diff --git a/system/hwc2/MiniFence.cpp b/system/hwc2/MiniFence.cpp
deleted file mode 100644
index ecfb063..0000000
--- a/system/hwc2/MiniFence.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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/system/hwc2/MiniFence.h b/system/hwc2/MiniFence.h
deleted file mode 100644
index 31c9536..0000000
--- a/system/hwc2/MiniFence.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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 EMU_HWC2_MINIFENCE_H
-#define EMU_HWC2_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 //EMU_HWC2_MINIFENCE_H