Refactor DeviceInfo in HWUI

Remove the need for both Vulkan and EGL managers to initialize
it. Also remove unused code paths.

Test: hwui_unit_tests
Change-Id: I33ad881468eddbf91ec63207f0d82bed8d97f5ad
diff --git a/libs/hwui/DeviceInfo.cpp b/libs/hwui/DeviceInfo.cpp
index b5b87d5..21fbbdc 100644
--- a/libs/hwui/DeviceInfo.cpp
+++ b/libs/hwui/DeviceInfo.cpp
@@ -26,8 +26,6 @@
 
 #include <log/log.h>
 
-#include <GLES2/gl2.h>
-
 namespace android {
 namespace uirenderer {
 
@@ -46,39 +44,12 @@
         1920,   // viewportH
 };
 
-static DeviceInfo* sDeviceInfo = nullptr;
-static std::once_flag sInitializedFlag;
-
 const DeviceInfo* DeviceInfo::get() {
-    LOG_ALWAYS_FATAL_IF(!sDeviceInfo, "DeviceInfo not yet initialized.");
-    return sDeviceInfo;
+        static DeviceInfo sDeviceInfo;
+        return &sDeviceInfo;
 }
 
-void DeviceInfo::initialize() {
-    std::call_once(sInitializedFlag, []() {
-        sDeviceInfo = new DeviceInfo();
-        sDeviceInfo->load();
-    });
-}
-
-void DeviceInfo::initialize(int maxTextureSize) {
-    std::call_once(sInitializedFlag, [maxTextureSize]() {
-        sDeviceInfo = new DeviceInfo();
-        sDeviceInfo->mDisplayInfo = DeviceInfo::queryDisplayInfo();
-        sDeviceInfo->mMaxTextureSize = maxTextureSize;
-        sDeviceInfo->queryCompositionPreference(&sDeviceInfo->mTargetDataSpace,
-                                                &sDeviceInfo->mTargetPixelFormat);
-    });
-}
-
-void DeviceInfo::load() {
-    mDisplayInfo = queryDisplayInfo();
-    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
-    sDeviceInfo->queryCompositionPreference(&sDeviceInfo->mTargetDataSpace,
-                                            &sDeviceInfo->mTargetPixelFormat);
-}
-
-DisplayInfo DeviceInfo::queryDisplayInfo() {
+DisplayInfo QueryDisplayInfo() {
     if (Properties::isolatedProcess) {
         return sDummyDisplay;
     }
@@ -90,17 +61,36 @@
     return displayInfo;
 }
 
-void DeviceInfo::queryCompositionPreference(ui::Dataspace* dataSpace,
-                                            ui::PixelFormat* pixelFormat) {
+void QueryCompositionPreference(ui::Dataspace* dataSpace,
+                                ui::PixelFormat* pixelFormat) {
     if (Properties::isolatedProcess) {
         *dataSpace = ui::Dataspace::V0_SRGB;
         *pixelFormat = ui::PixelFormat::RGBA_8888;
     }
 
     status_t status =
-        SurfaceComposerClient::getCompositionPreference(dataSpace, pixelFormat);
+            SurfaceComposerClient::getCompositionPreference(dataSpace, pixelFormat);
     LOG_ALWAYS_FATAL_IF(status, "Failed to get composition preference, error %d", status);
 }
 
+DeviceInfo::DeviceInfo() {
+#if HWUI_NULL_GPU
+        mMaxTextureSize = NULL_GPU_MAX_TEXTURE_SIZE;
+#else
+        mMaxTextureSize = -1;
+#endif
+    mDisplayInfo = QueryDisplayInfo();
+    QueryCompositionPreference(&mTargetDataSpace, &mTargetPixelFormat);
+}
+
+int DeviceInfo::maxTextureSize() const {
+    LOG_ALWAYS_FATAL_IF(mMaxTextureSize < 0, "MaxTextureSize has not been initialized yet.");
+    return mMaxTextureSize;
+}
+
+void DeviceInfo::setMaxTextureSize(int maxTextureSize) {
+    const_cast<DeviceInfo*>(DeviceInfo::get())->mMaxTextureSize = maxTextureSize;
+}
+
 } /* namespace uirenderer */
 } /* namespace android */
diff --git a/libs/hwui/DeviceInfo.h b/libs/hwui/DeviceInfo.h
index 416af17..1d747741 100644
--- a/libs/hwui/DeviceInfo.h
+++ b/libs/hwui/DeviceInfo.h
@@ -19,51 +19,38 @@
 #include <ui/DisplayInfo.h>
 #include <ui/GraphicTypes.h>
 
-#include "Extensions.h"
 #include "utils/Macros.h"
 
 namespace android {
 namespace uirenderer {
 
+namespace renderthread {
+    class RenderThread;
+}
+
 class DeviceInfo {
     PREVENT_COPY_AND_ASSIGN(DeviceInfo);
 
 public:
-    // returns nullptr if DeviceInfo is not initialized yet
-    // Note this does not have a memory fence so it's up to the caller
-    // to use one if required. Normally this should not be necessary
     static const DeviceInfo* get();
 
-    // only call this after GL has been initialized, or at any point if compiled
-    // with HWUI_NULL_GPU
-    static void initialize();
-    static void initialize(int maxTextureSize);
+    // this value is only valid after the GPU has been initialized and there is a valid graphics
+    // context or if you are using the HWUI_NULL_GPU
+    int maxTextureSize() const;
 
-    int maxTextureSize() const { return mMaxTextureSize; }
     ui::Dataspace getTargetDataSpace() const { return mTargetDataSpace; }
     ui::PixelFormat getTargetPixelFormat() const { return mTargetPixelFormat; }
     const DisplayInfo& displayInfo() const { return mDisplayInfo; }
-    const Extensions& extensions() const { return mExtensions; }
-
-    static uint32_t multiplyByResolution(uint32_t in) {
-        auto di = DeviceInfo::get()->displayInfo();
-        return di.w * di.h * in;
-    }
-
-    static DisplayInfo queryDisplayInfo();
 
 private:
-    static void queryCompositionPreference(ui::Dataspace* dataSpace,
-                                           ui::PixelFormat* pixelFormat);
+    friend class renderthread::RenderThread;
+    static void setMaxTextureSize(int maxTextureSize);
 
-    DeviceInfo() {}
-    ~DeviceInfo() {}
-
-    void load();
+    DeviceInfo();
 
     int mMaxTextureSize;
     DisplayInfo mDisplayInfo;
-    Extensions mExtensions;
+
     // TODO(lpy) Replace below with android_ prefix types.
     ui::Dataspace mTargetDataSpace;
     ui::PixelFormat mTargetPixelFormat;
diff --git a/libs/hwui/Extensions.h b/libs/hwui/Extensions.h
deleted file mode 100644
index e90f40c1..0000000
--- a/libs/hwui/Extensions.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright (C) 2010 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_HWUI_EXTENSIONS_H
-#define ANDROID_HWUI_EXTENSIONS_H
-
-namespace android {
-namespace uirenderer {
-
-///////////////////////////////////////////////////////////////////////////////
-// Classes
-///////////////////////////////////////////////////////////////////////////////
-
-class Extensions {
-public:
-    Extensions() {}
-
-    inline bool hasNPot() const { return false; }
-    inline bool hasFramebufferFetch() const { return false; }
-    inline bool hasDiscardFramebuffer() const { return false; }
-    inline bool hasDebugMarker() const { return false; }
-    inline bool has1BitStencil() const { return false; }
-    inline bool has4BitStencil() const { return false; }
-    inline bool hasUnpackRowLength() const { return mVersionMajor >= 3; }
-    inline bool hasPixelBufferObjects() const { return mVersionMajor >= 3; }
-    inline bool hasOcclusionQueries() const { return mVersionMajor >= 3; }
-    inline bool hasFloatTextures() const { return mVersionMajor >= 3; }
-    inline bool hasRenderableFloatTextures() const {
-        return (mVersionMajor >= 3 && mVersionMinor >= 2);
-    }
-    inline bool hasSRGB() const { return false; }
-    inline bool hasSRGBWriteControl() const { return hasSRGB() && false; }
-    inline bool hasLinearBlending() const { return hasSRGB() && false; }
-
-    inline int getMajorGlVersion() const { return mVersionMajor; }
-    inline int getMinorGlVersion() const { return mVersionMinor; }
-
-private:
-    int mVersionMajor = 2;
-    int mVersionMinor = 0;
-};  // class Extensions
-
-};  // namespace uirenderer
-};  // namespace android
-
-#endif  // ANDROID_HWUI_EXTENSIONS_H
diff --git a/libs/hwui/debug/wrap_gles.h b/libs/hwui/debug/wrap_gles.h
index 27a29aa..3da6e80 100644
--- a/libs/hwui/debug/wrap_gles.h
+++ b/libs/hwui/debug/wrap_gles.h
@@ -28,6 +28,9 @@
 #include <GLES3/gl31.h>
 #include <GLES3/gl32.h>
 
+// constant used by the NULL GPU implementation as well as HWUI's unit tests
+constexpr int NULL_GPU_MAX_TEXTURE_SIZE = 2048;
+
 // Generate stubs that route all the calls to our function table
 #include "gles_redefine.h"
 
diff --git a/libs/hwui/renderthread/EglManager.cpp b/libs/hwui/renderthread/EglManager.cpp
index 675df41..0cb23e5 100644
--- a/libs/hwui/renderthread/EglManager.cpp
+++ b/libs/hwui/renderthread/EglManager.cpp
@@ -23,7 +23,6 @@
 #include "utils/Color.h"
 #include "utils/StringUtils.h"
 
-#include "DeviceInfo.h"
 #include "Frame.h"
 #include "Properties.h"
 
@@ -127,7 +126,6 @@
     createContext();
     createPBufferSurface();
     makeCurrent(mPBufferSurface, nullptr, /* force */ true);
-    DeviceInfo::initialize();
 
     mSurfaceColorGamut = DataSpaceToColorGamut(
         static_cast<android_dataspace>(DeviceInfo::get()->getTargetDataSpace()));
diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp
index 7258a0a..a5dcc72 100644
--- a/libs/hwui/renderthread/RenderThread.cpp
+++ b/libs/hwui/renderthread/RenderThread.cpp
@@ -162,7 +162,7 @@
 }
 
 void RenderThread::initThreadLocals() {
-    mDisplayInfo = DeviceInfo::queryDisplayInfo();
+    mDisplayInfo = DeviceInfo::get()->displayInfo();
     nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
     mTimeLord.setFrameInterval(frameIntervalNanos);
     initializeDisplayEventReceiver();
@@ -246,6 +246,9 @@
         mGrContext->releaseResourcesAndAbandonContext();
     }
     mGrContext = std::move(context);
+    if (mGrContext) {
+        DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
+    }
 }
 
 int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
diff --git a/libs/hwui/renderthread/VulkanManager.cpp b/libs/hwui/renderthread/VulkanManager.cpp
index 038e13c..4881172 100644
--- a/libs/hwui/renderthread/VulkanManager.cpp
+++ b/libs/hwui/renderthread/VulkanManager.cpp
@@ -16,7 +16,6 @@
 
 #include "VulkanManager.h"
 
-#include "DeviceInfo.h"
 #include "Properties.h"
 #include "RenderThread.h"
 #include "renderstate/RenderState.h"
@@ -399,8 +398,6 @@
 
     free_features_extensions_structs(features);
 
-    DeviceInfo::initialize(mRenderThread.getGrContext()->maxRenderTargetSize());
-
     if (Properties::enablePartialUpdates && Properties::useBufferAge) {
         mSwapBehavior = SwapBehavior::BufferAge;
     }
diff --git a/libs/hwui/tests/common/TestUtils.h b/libs/hwui/tests/common/TestUtils.h
index 0e6582c..c35f512 100644
--- a/libs/hwui/tests/common/TestUtils.h
+++ b/libs/hwui/tests/common/TestUtils.h
@@ -16,7 +16,6 @@
 
 #pragma once
 
-#include <DeviceInfo.h>
 #include <DisplayList.h>
 #include <Matrix.h>
 #include <Properties.h>
@@ -179,12 +178,6 @@
     static sp<RenderNode> createNode(
             int left, int top, int right, int bottom,
             std::function<void(RenderProperties& props, Canvas& canvas)> setup) {
-#if HWUI_NULL_GPU
-        // if RenderNodes are being sync'd/used, device info will be needed, since
-        // DeviceInfo::maxTextureSize() affects layer property
-        DeviceInfo::initialize();
-#endif
-
         sp<RenderNode> node = new RenderNode();
         RenderProperties& props = node->mutateStagingProperties();
         props.setLeftTopRightBottom(left, top, right, bottom);
@@ -202,12 +195,6 @@
     static sp<RenderNode> createNode(
             int left, int top, int right, int bottom,
             std::function<void(RenderProperties& props, RecordingCanvasType& canvas)> setup) {
-#if HWUI_NULL_GPU
-        // if RenderNodes are being sync'd/used, device info will be needed, since
-        // DeviceInfo::maxTextureSize() affects layer property
-        DeviceInfo::initialize();
-#endif
-
         sp<RenderNode> node = new RenderNode();
         RenderProperties& props = node->mutateStagingProperties();
         props.setLeftTopRightBottom(left, top, right, bottom);
@@ -233,11 +220,6 @@
             std::function<void(RenderProperties& props, skiapipeline::SkiaRecordingCanvas& canvas)>
                     setup,
             const char* name = nullptr, skiapipeline::SkiaDisplayList* displayList = nullptr) {
-#if HWUI_NULL_GPU
-        // if RenderNodes are being sync'd/used, device info will be needed, since
-        // DeviceInfo::maxTextureSize() affects layer property
-        DeviceInfo::initialize();
-#endif
         sp<RenderNode> node = new RenderNode();
         if (name) {
             node->setName(name);
diff --git a/libs/hwui/tests/unit/RenderPropertiesTests.cpp b/libs/hwui/tests/unit/RenderPropertiesTests.cpp
index 85655fc..3e8e057 100644
--- a/libs/hwui/tests/unit/RenderPropertiesTests.cpp
+++ b/libs/hwui/tests/unit/RenderPropertiesTests.cpp
@@ -22,8 +22,6 @@
 using namespace android::uirenderer;
 
 TEST(RenderProperties, layerValidity) {
-    DeviceInfo::initialize();
-
     const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
     ASSERT_LE(2048, maxTextureSize);
     ASSERT_GT(100000, maxTextureSize);