surfaceflinger: update for IComposer changes

The new IComposer introduces IComposerClient to manage resources and
replaces most state changing functions by a "command buffer" to batch
calls.

Test: builds and boots
Change-Id: Idd1060f83fab28330e7119ff0f5a5bda5429811a
diff --git a/services/surfaceflinger/DisplayHardware/ComposerHal.h b/services/surfaceflinger/DisplayHardware/ComposerHal.h
index b8f7c20..2246f76 100644
--- a/services/surfaceflinger/DisplayHardware/ComposerHal.h
+++ b/services/surfaceflinger/DisplayHardware/ComposerHal.h
@@ -17,11 +17,16 @@
 #ifndef ANDROID_SF_COMPOSER_HAL_H
 #define ANDROID_SF_COMPOSER_HAL_H
 
+#include <memory>
 #include <string>
+#include <unordered_map>
+#include <utility>
 #include <vector>
 
 #include <android/hardware/graphics/composer/2.1/IComposer.h>
 #include <utils/StrongPointer.h>
+#include <IComposerCommandBuffer.h>
+#include <MessageQueue.h>
 
 namespace android {
 
@@ -36,102 +41,195 @@
 
 using android::hardware::graphics::composer::V2_1::IComposer;
 using android::hardware::graphics::composer::V2_1::IComposerCallback;
+using android::hardware::graphics::composer::V2_1::IComposerClient;
 using android::hardware::graphics::composer::V2_1::Error;
 using android::hardware::graphics::composer::V2_1::Display;
 using android::hardware::graphics::composer::V2_1::Layer;
 using android::hardware::graphics::composer::V2_1::Config;
 
+using android::hardware::graphics::composer::V2_1::CommandWriter;
+using android::hardware::graphics::composer::V2_1::CommandReaderBase;
+
+using android::hardware::kSynchronizedReadWrite;
+using android::hardware::MessageQueue;
+using android::hardware::MQDescriptorSync;
+using android::hardware::hidl_vec;
+using android::hardware::hidl_handle;
+
+class CommandReader : public CommandReaderBase {
+public:
+    ~CommandReader();
+
+    // Parse and execute commands from the command queue.  The commands are
+    // actually return values from the server and will be saved in ReturnData.
+    Error parse();
+
+    // Get and clear saved errors.
+    struct CommandError {
+        uint32_t location;
+        Error error;
+    };
+    std::vector<CommandError> takeErrors();
+
+    bool hasChanges(Display display, uint32_t& numChangedCompositionTypes,
+            uint32_t& numLayerRequestMasks) const;
+
+    // Get and clear saved changed composition types.
+    void takeChangedCompositionTypes(Display display,
+            std::vector<Layer>& layers,
+            std::vector<IComposerClient::Composition>& types);
+
+    // Get and clear saved display requests.
+    void takeDisplayRequests(Display display,
+        uint32_t& displayRequestMask, std::vector<Layer>& layers,
+        std::vector<uint32_t>& layerRequestMasks);
+
+    // Get and clear saved release fences.
+    void takeReleaseFences(Display display, std::vector<Layer>& layers,
+            std::vector<int>& releaseFences);
+
+    // Get and clear saved present fence.
+    void takePresentFence(Display display, int& presentFence);
+
+private:
+    void resetData();
+
+    bool parseSelectDisplay(uint16_t length);
+    bool parseSetError(uint16_t length);
+    bool parseSetChangedCompositionTypes(uint16_t length);
+    bool parseSetDisplayRequests(uint16_t length);
+    bool parseSetPresentFence(uint16_t length);
+    bool parseSetReleaseFences(uint16_t length);
+
+    struct ReturnData {
+        uint32_t displayRequests = 0;
+
+        std::vector<Layer> changedLayers;
+        std::vector<IComposerClient::Composition> compositionTypes;
+
+        std::vector<Layer> requestedLayers;
+        std::vector<uint32_t> requestMasks;
+
+        int presentFence = -1;
+
+        std::vector<Layer> releasedLayers;
+        std::vector<int> releaseFences;
+    };
+
+    std::vector<CommandError> mErrors;
+    std::unordered_map<Display, ReturnData> mReturnData;
+
+    // When SELECT_DISPLAY is parsed, this is updated to point to the
+    // display's return data in mReturnData.  We use it to avoid repeated
+    // map lookups.
+    ReturnData* mCurrentReturnData;
+};
+
 // Composer is a wrapper to IComposer, a proxy to server-side composer.
 class Composer {
 public:
     Composer();
 
-    std::vector<IComposer::Capability> getCapabilities() const;
-    std::string dumpDebugInfo() const;
+    std::vector<IComposer::Capability> getCapabilities();
+    std::string dumpDebugInfo();
 
-    void registerCallback(const sp<IComposerCallback>& callback) const;
+    void registerCallback(const sp<IComposerCallback>& callback);
 
-    uint32_t getMaxVirtualDisplayCount() const;
+    uint32_t getMaxVirtualDisplayCount();
     Error createVirtualDisplay(uint32_t width, uint32_t height,
-            PixelFormat& format, Display& display) const;
-    Error destroyVirtualDisplay(Display display) const;
+            PixelFormat& format, Display& display);
+    Error destroyVirtualDisplay(Display display);
 
-    Error acceptDisplayChanges(Display display) const;
+    Error acceptDisplayChanges(Display display);
 
-    Error createLayer(Display display, Layer& layer) const;
-    Error destroyLayer(Display display, Layer layer) const;
+    Error createLayer(Display display, Layer& layer);
+    Error destroyLayer(Display display, Layer layer);
 
-    Error getActiveConfig(Display display, Config& config) const;
+    Error getActiveConfig(Display display, Config& config);
     Error getChangedCompositionTypes(Display display,
             std::vector<Layer>& layers,
-            std::vector<IComposer::Composition>& types) const;
-    Error getColorModes(Display display, std::vector<ColorMode>& modes) const;
+            std::vector<IComposerClient::Composition>& types);
+    Error getColorModes(Display display, std::vector<ColorMode>& modes);
     Error getDisplayAttribute(Display display, Config config,
-            IComposer::Attribute attribute, int32_t& value) const;
+            IComposerClient::Attribute attribute, int32_t& value);
     Error getDisplayConfigs(Display display,
-            std::vector<Config>& configs) const;
-    Error getDisplayName(Display display, std::string& name) const;
+            std::vector<Config>& configs);
+    Error getDisplayName(Display display, std::string& name);
 
     Error getDisplayRequests(Display display, uint32_t& displayRequestMask,
             std::vector<Layer>& layers,
-            std::vector<uint32_t>& layerRequestMasks) const;
+            std::vector<uint32_t>& layerRequestMasks);
 
-    Error getDisplayType(Display display, IComposer::DisplayType& type) const;
-    Error getDozeSupport(Display display, bool& support) const;
+    Error getDisplayType(Display display, IComposerClient::DisplayType& type);
+    Error getDozeSupport(Display display, bool& support);
     Error getHdrCapabilities(Display display, std::vector<Hdr>& types,
             float& maxLuminance, float& maxAverageLuminance,
-            float& minLuminance) const;
+            float& minLuminance);
 
     Error getReleaseFences(Display display, std::vector<Layer>& layers,
-            std::vector<int>& releaseFences) const;
+            std::vector<int>& releaseFences);
 
-    Error presentDisplay(Display display, int& presentFence) const;
+    Error presentDisplay(Display display, int& presentFence);
 
-    Error setActiveConfig(Display display, Config config) const;
+    Error setActiveConfig(Display display, Config config);
     Error setClientTarget(Display display, const native_handle_t* target,
             int acquireFence, Dataspace dataspace,
-            const std::vector<IComposer::Rect>& damage) const;
-    Error setColorMode(Display display, ColorMode mode) const;
+            const std::vector<IComposerClient::Rect>& damage);
+    Error setColorMode(Display display, ColorMode mode);
     Error setColorTransform(Display display, const float* matrix,
-            ColorTransform hint) const;
+            ColorTransform hint);
     Error setOutputBuffer(Display display, const native_handle_t* buffer,
-            int releaseFence) const;
-    Error setPowerMode(Display display, IComposer::PowerMode mode) const;
-    Error setVsyncEnabled(Display display, IComposer::Vsync enabled) const;
+            int releaseFence);
+    Error setPowerMode(Display display, IComposerClient::PowerMode mode);
+    Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled);
+
+    Error setClientTargetSlotCount(Display display);
 
     Error validateDisplay(Display display, uint32_t& numTypes,
-            uint32_t& numRequests) const;
+            uint32_t& numRequests);
 
     Error setCursorPosition(Display display, Layer layer,
-            int32_t x, int32_t y) const;
+            int32_t x, int32_t y);
     Error setLayerBuffer(Display display, Layer layer,
-            const native_handle_t* buffer, int acquireFence) const;
+            const native_handle_t* buffer, int acquireFence);
     Error setLayerSurfaceDamage(Display display, Layer layer,
-            const std::vector<IComposer::Rect>& damage) const;
+            const std::vector<IComposerClient::Rect>& damage);
     Error setLayerBlendMode(Display display, Layer layer,
-            IComposer::BlendMode mode) const;
+            IComposerClient::BlendMode mode);
     Error setLayerColor(Display display, Layer layer,
-            const IComposer::Color& color) const;
+            const IComposerClient::Color& color);
     Error setLayerCompositionType(Display display, Layer layer,
-            IComposer::Composition type) const;
+            IComposerClient::Composition type);
     Error setLayerDataspace(Display display, Layer layer,
-            Dataspace dataspace) const;
+            Dataspace dataspace);
     Error setLayerDisplayFrame(Display display, Layer layer,
-            const IComposer::Rect& frame) const;
+            const IComposerClient::Rect& frame);
     Error setLayerPlaneAlpha(Display display, Layer layer,
-            float alpha) const;
+            float alpha);
     Error setLayerSidebandStream(Display display, Layer layer,
-            const native_handle_t* stream) const;
+            const native_handle_t* stream);
     Error setLayerSourceCrop(Display display, Layer layer,
-            const IComposer::FRect& crop) const;
+            const IComposerClient::FRect& crop);
     Error setLayerTransform(Display display, Layer layer,
-            Transform transform) const;
+            Transform transform);
     Error setLayerVisibleRegion(Display display, Layer layer,
-            const std::vector<IComposer::Rect>& visible) const;
-    Error setLayerZOrder(Display display, Layer layer, uint32_t z) const;
+            const std::vector<IComposerClient::Rect>& visible);
+    Error setLayerZOrder(Display display, Layer layer, uint32_t z);
 
 private:
-    sp<IComposer> mService;
+    // Many public functions above simply write a command into the command
+    // queue to batch the calls.  validateDisplay and presentDisplay will call
+    // this function to execute the command queue.
+    Error execute();
+
+    sp<IComposer> mComposer;
+    sp<IComposerClient> mClient;
+
+    // 64KiB minus a small space for metadata such as read/write pointers
+    static constexpr size_t kWriterInitialSize =
+        64 * 1024 / sizeof(uint32_t) - 16;
+    CommandWriter mWriter;
+    CommandReader mReader;
 };
 
 } // namespace Hwc2