Home | History | Annotate | Download | only in dvr
      1 #ifndef ANDROID_DVR_DISPLAY_PROTOCOL_H_
      2 #define ANDROID_DVR_DISPLAY_PROTOCOL_H_
      3 
      4 #include <sys/types.h>
      5 
      6 #include <array>
      7 #include <map>
      8 
      9 #include <dvr/dvr_display_types.h>
     10 
     11 #include <dvr/dvr_api.h>
     12 #include <pdx/rpc/buffer_wrapper.h>
     13 #include <pdx/rpc/remote_method.h>
     14 #include <pdx/rpc/serializable.h>
     15 #include <pdx/rpc/variant.h>
     16 #include <private/dvr/bufferhub_rpc.h>
     17 
     18 // RPC protocol definitions for DVR display services (VrFlinger).
     19 
     20 namespace android {
     21 namespace dvr {
     22 namespace display {
     23 
     24 // Native display metrics.
     25 struct Metrics {
     26   // Basic display properties.
     27   uint32_t display_width;
     28   uint32_t display_height;
     29   uint32_t display_x_dpi;
     30   uint32_t display_y_dpi;
     31   uint32_t vsync_period_ns;
     32 
     33   // HMD metrics.
     34   // TODO(eieio): Determine how these fields should be populated. On phones
     35   // these values are determined at runtime by VrCore based on which headset the
     36   // phone is in. On dedicated hardware this needs to come from somewhere else.
     37   // Perhaps these should be moved to a separate structure that is returned by a
     38   // separate runtime call.
     39   uint32_t distorted_width;
     40   uint32_t distorted_height;
     41   uint32_t hmd_ipd_mm;
     42   float inter_lens_distance_m;
     43   std::array<float, 4> left_fov_lrbt;
     44   std::array<float, 4> right_fov_lrbt;
     45 
     46  private:
     47   PDX_SERIALIZABLE_MEMBERS(Metrics, display_width, display_height,
     48                            display_x_dpi, display_y_dpi, vsync_period_ns,
     49                            distorted_width, distorted_height, hmd_ipd_mm,
     50                            inter_lens_distance_m, left_fov_lrbt,
     51                            right_fov_lrbt);
     52 };
     53 
     54 // Serializable base type for enum structs. Enum structs are easier to use than
     55 // enum classes, especially for bitmasks. This base type provides common
     56 // utilities for flags types.
     57 template <typename Integer>
     58 class Flags {
     59  public:
     60   using Base = Flags<Integer>;
     61   using Type = Integer;
     62 
     63   // NOLINTNEXTLINE(google-explicit-constructor)
     64   Flags(const Integer& value) : value_{value} {}
     65   Flags(const Flags&) = default;
     66   Flags& operator=(const Flags&) = default;
     67 
     68   Integer value() const { return value_; }
     69   // NOLINTNEXTLINE(google-explicit-constructor)
     70   operator Integer() const { return value_; }
     71 
     72   bool IsSet(Integer bits) const { return (value_ & bits) == bits; }
     73   bool IsClear(Integer bits) const { return (value_ & bits) == 0; }
     74 
     75   void Set(Integer bits) { value_ |= bits; }
     76   void Clear(Integer bits) { value_ &= ~bits; }
     77 
     78   Integer operator|(Integer bits) const { return value_ | bits; }
     79   Integer operator&(Integer bits) const { return value_ & bits; }
     80 
     81   Flags& operator|=(Integer bits) {
     82     value_ |= bits;
     83     return *this;
     84   }
     85   Flags& operator&=(Integer bits) {
     86     value_ &= bits;
     87     return *this;
     88   }
     89 
     90  private:
     91   Integer value_;
     92 
     93   PDX_SERIALIZABLE_MEMBERS(Flags<Integer>, value_);
     94 };
     95 
     96 // Flags indicating what changed since last update.
     97 struct SurfaceUpdateFlags : public Flags<uint32_t> {
     98   enum : Type {
     99     None = DVR_SURFACE_UPDATE_FLAGS_NONE,
    100     NewSurface = DVR_SURFACE_UPDATE_FLAGS_NEW_SURFACE,
    101     BuffersChanged = DVR_SURFACE_UPDATE_FLAGS_BUFFERS_CHANGED,
    102     VisibilityChanged = DVR_SURFACE_UPDATE_FLAGS_VISIBILITY_CHANGED,
    103     AttributesChanged = DVR_SURFACE_UPDATE_FLAGS_ATTRIBUTES_CHANGED,
    104   };
    105 
    106   SurfaceUpdateFlags() : Base{None} {}
    107   using Base::Base;
    108 };
    109 
    110 // Surface attribute key/value types.
    111 using SurfaceAttributeKey = int32_t;
    112 using SurfaceAttributeValue =
    113     pdx::rpc::Variant<int32_t, int64_t, bool, float, std::array<float, 2>,
    114                       std::array<float, 3>, std::array<float, 4>,
    115                       std::array<float, 8>, std::array<float, 16>>;
    116 
    117 // Defined surface attribute keys.
    118 struct SurfaceAttribute : public Flags<SurfaceAttributeKey> {
    119   enum : Type {
    120     // Keys in the negative integer space are interpreted by VrFlinger for
    121     // direct surfaces.
    122     Direct = DVR_SURFACE_ATTRIBUTE_DIRECT,
    123     ZOrder = DVR_SURFACE_ATTRIBUTE_Z_ORDER,
    124     Visible = DVR_SURFACE_ATTRIBUTE_VISIBLE,
    125 
    126     // Invalid key. May be used to terminate C style lists in public API code.
    127     Invalid = DVR_SURFACE_ATTRIBUTE_INVALID,
    128 
    129     // Positive keys are interpreted by the compositor only.
    130     FirstUserKey = DVR_SURFACE_ATTRIBUTE_FIRST_USER_KEY,
    131   };
    132 
    133   SurfaceAttribute() : Base{Invalid} {}
    134   using Base::Base;
    135 };
    136 
    137 // Collection of surface attribute key/value pairs.
    138 using SurfaceAttributes = std::map<SurfaceAttributeKey, SurfaceAttributeValue>;
    139 
    140 struct SurfaceState {
    141   int32_t surface_id;
    142   int32_t process_id;
    143   int32_t user_id;
    144 
    145   SurfaceAttributes surface_attributes;
    146   SurfaceUpdateFlags update_flags;
    147   std::vector<int32_t> queue_ids;
    148 
    149   // Convenience accessors.
    150   bool GetVisible() const {
    151     bool bool_value = false;
    152     GetAttribute(SurfaceAttribute::Visible, &bool_value,
    153                  ValidTypes<int32_t, int64_t, bool, float>{});
    154     return bool_value;
    155   }
    156 
    157   int GetZOrder() const {
    158     int int_value = 0;
    159     GetAttribute(SurfaceAttribute::ZOrder, &int_value,
    160                  ValidTypes<int32_t, int64_t, float>{});
    161     return int_value;
    162   }
    163 
    164  private:
    165   template <typename... Types>
    166   struct ValidTypes {};
    167 
    168   template <typename ReturnType, typename... Types>
    169   bool GetAttribute(SurfaceAttributeKey key, ReturnType* out_value,
    170                     ValidTypes<Types...>) const {
    171     auto search = surface_attributes.find(key);
    172     if (search != surface_attributes.end())
    173       return pdx::rpc::IfAnyOf<Types...>::Get(&search->second, out_value);
    174     else
    175       return false;
    176   }
    177 
    178   PDX_SERIALIZABLE_MEMBERS(SurfaceState, surface_id, process_id,
    179                            surface_attributes, update_flags, queue_ids);
    180 };
    181 
    182 struct SurfaceInfo {
    183   int surface_id;
    184   bool visible;
    185   int z_order;
    186 
    187  private:
    188   PDX_SERIALIZABLE_MEMBERS(SurfaceInfo, surface_id, visible, z_order);
    189 };
    190 
    191 enum class ConfigFileType : uint32_t {
    192   kLensMetrics,
    193   kDeviceMetrics,
    194   kDeviceConfiguration
    195 };
    196 
    197 struct DisplayProtocol {
    198   // Service path.
    199   static constexpr char kClientPath[] = "system/vr/display/client";
    200 
    201   // Op codes.
    202   enum {
    203     kOpGetMetrics = 0,
    204     kOpGetConfigurationData,
    205     kOpSetupGlobalBuffer,
    206     kOpDeleteGlobalBuffer,
    207     kOpGetGlobalBuffer,
    208     kOpIsVrAppRunning,
    209     kOpCreateSurface,
    210     kOpGetSurfaceInfo,
    211     kOpCreateQueue,
    212     kOpSetAttributes,
    213   };
    214 
    215   // Aliases.
    216   using LocalChannelHandle = pdx::LocalChannelHandle;
    217   using Void = pdx::rpc::Void;
    218 
    219   // Methods.
    220   PDX_REMOTE_METHOD(GetMetrics, kOpGetMetrics, Metrics(Void));
    221   PDX_REMOTE_METHOD(GetConfigurationData, kOpGetConfigurationData,
    222                     std::string(ConfigFileType config_type));
    223   PDX_REMOTE_METHOD(SetupGlobalBuffer, kOpSetupGlobalBuffer,
    224                     LocalNativeBufferHandle(DvrGlobalBufferKey key, size_t size,
    225                                             uint64_t usage));
    226   PDX_REMOTE_METHOD(DeleteGlobalBuffer, kOpDeleteGlobalBuffer,
    227                     void(DvrGlobalBufferKey key));
    228   PDX_REMOTE_METHOD(GetGlobalBuffer, kOpGetGlobalBuffer,
    229                     LocalNativeBufferHandle(DvrGlobalBufferKey key));
    230   PDX_REMOTE_METHOD(IsVrAppRunning, kOpIsVrAppRunning, bool(Void));
    231   PDX_REMOTE_METHOD(CreateSurface, kOpCreateSurface,
    232                     SurfaceInfo(const SurfaceAttributes& attributes));
    233   PDX_REMOTE_METHOD(GetSurfaceInfo, kOpGetSurfaceInfo, SurfaceInfo(Void));
    234   PDX_REMOTE_METHOD(
    235       CreateQueue, kOpCreateQueue,
    236       LocalChannelHandle(const ProducerQueueConfig& producer_config));
    237   PDX_REMOTE_METHOD(SetAttributes, kOpSetAttributes,
    238                     void(const SurfaceAttributes& attributes));
    239 };
    240 
    241 struct DisplayManagerProtocol {
    242   // Service path.
    243   static constexpr char kClientPath[] = "system/vr/display/manager";
    244 
    245   // Op codes.
    246   enum {
    247     kOpGetSurfaceState = 0,
    248     kOpGetSurfaceQueue,
    249   };
    250 
    251   // Aliases.
    252   using LocalChannelHandle = pdx::LocalChannelHandle;
    253   using Void = pdx::rpc::Void;
    254 
    255   // Methods.
    256   PDX_REMOTE_METHOD(GetSurfaceState, kOpGetSurfaceState,
    257                     std::vector<SurfaceState>(Void));
    258   PDX_REMOTE_METHOD(GetSurfaceQueue, kOpGetSurfaceQueue,
    259                     LocalChannelHandle(int surface_id, int queue_id));
    260 };
    261 
    262 struct VSyncSchedInfo {
    263   int64_t vsync_period_ns;
    264   int64_t timestamp_ns;
    265   uint32_t next_vsync_count;
    266 
    267  private:
    268   PDX_SERIALIZABLE_MEMBERS(VSyncSchedInfo, vsync_period_ns, timestamp_ns,
    269                            next_vsync_count);
    270 };
    271 
    272 struct VSyncProtocol {
    273   // Service path.
    274   static constexpr char kClientPath[] = "system/vr/display/vsync";
    275 
    276   // Op codes.
    277   enum {
    278     kOpWait = 0,
    279     kOpAck,
    280     kOpGetLastTimestamp,
    281     kOpGetSchedInfo,
    282     kOpAcknowledge,
    283   };
    284 
    285   // Aliases.
    286   using Void = pdx::rpc::Void;
    287   using Timestamp = int64_t;
    288 
    289   // Methods.
    290   PDX_REMOTE_METHOD(Wait, kOpWait, Timestamp(Void));
    291   PDX_REMOTE_METHOD(GetLastTimestamp, kOpGetLastTimestamp, Timestamp(Void));
    292   PDX_REMOTE_METHOD(GetSchedInfo, kOpGetSchedInfo, VSyncSchedInfo(Void));
    293   PDX_REMOTE_METHOD(Acknowledge, kOpAcknowledge, void(Void));
    294 };
    295 
    296 }  // namespace display
    297 }  // namespace dvr
    298 }  // namespace android
    299 
    300 #endif  // ANDROID_DVR_DISPLAY_PROTOCOL_H_
    301