From da409fb8d615c3e1aac6505827026843aee25324 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Sun, 21 Oct 2018 23:36:26 -0700 Subject: ui: Manage loaded resources with smart pointers. Test: Run recovery_unit_test on marlin. Test: `Run graphics test` on marlin. Change-Id: I8239c3d9fb288f80ee11f615402768ff8ef8ecd0 --- screen_ui.h | 85 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 41 insertions(+), 44 deletions(-) (limited to 'screen_ui.h') diff --git a/screen_ui.h b/screen_ui.h index 2d0d97d8d..ff245a2fb 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -162,32 +162,31 @@ class TextMenu : public Menu { int char_height_; }; -// This class uses GRSurfaces* as the menu header and items. +// This class uses GRSurface's as the menu header and items. class GraphicMenu : public Menu { public: // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial - // selection to |initial_selection|. - GraphicMenu(GRSurface* graphic_headers, const std::vector& graphic_items, + // selection to |initial_selection|. |headers| and |items| will be made local copies. + GraphicMenu(const GRSurface* graphic_headers, const std::vector& graphic_items, size_t initial_selection, const DrawInterface& draw_funcs); int Select(int sel) override; int DrawHeader(int x, int y) const override; int DrawItems(int x, int y, int screen_width, bool long_press) const override; - // Checks if all the header and items are valid GRSurfaces; and that they can fit in the area + // Checks if all the header and items are valid GRSurface's; and that they can fit in the area // defined by |max_width| and |max_height|. - static bool Validate(size_t max_width, size_t max_height, GRSurface* graphic_headers, - const std::vector& graphic_items); + static bool Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers, + const std::vector& graphic_items); // Returns true if |surface| fits on the screen with a vertical offset |y|. static bool ValidateGraphicSurface(size_t max_width, size_t max_height, int y, const GRSurface* surface); private: - // Pointers to the menu headers and items in graphic icons. This class does not have the ownership - // of the these objects. - GRSurface* graphic_headers_; - std::vector graphic_items_; + // Menu headers and items in graphic icons. These are the copies owned by the class instance. + std::unique_ptr graphic_headers_; + std::vector> graphic_items_; }; // Implementation of RecoveryUI appropriate for devices with a screen @@ -243,6 +242,7 @@ class ScreenRecoveryUI : public RecoveryUI, public DrawInterface { protected: static constexpr int kMenuIndent = 4; + // The margin that we don't want to use for showing texts (e.g. round screen, or screen with // rounded corners). const int margin_width_; @@ -261,8 +261,8 @@ class ScreenRecoveryUI : public RecoveryUI, public DrawInterface { // Creates a GraphicMenu with |graphic_header| and |graphic_items|. If the GraphicMenu isn't // valid or it doesn't fit on the screen; falls back to create a TextMenu instead. If succeeds, // returns a unique pointer to the created menu; otherwise returns nullptr. - virtual std::unique_ptr CreateMenu(GRSurface* graphic_header, - const std::vector& graphic_items, + virtual std::unique_ptr CreateMenu(const GRSurface* graphic_header, + const std::vector& graphic_items, const std::vector& text_headers, const std::vector& text_items, size_t initial_selection) const; @@ -288,8 +288,8 @@ class ScreenRecoveryUI : public RecoveryUI, public DrawInterface { virtual void update_screen_locked(); virtual void update_progress_locked(); - GRSurface* GetCurrentFrame() const; - GRSurface* GetCurrentText() const; + const GRSurface* GetCurrentFrame() const; + const GRSurface* GetCurrentText() const; void ProgressThreadLoop(); @@ -299,8 +299,8 @@ class ScreenRecoveryUI : public RecoveryUI, public DrawInterface { void ClearText(); void LoadAnimation(); - void LoadBitmap(const char* filename, GRSurface** surface); - void LoadLocalizedBitmap(const char* filename, GRSurface** surface); + std::unique_ptr LoadBitmap(const std::string& filename); + std::unique_ptr LoadLocalizedBitmap(const std::string& filename); int PixelsFromDp(int dp) const; virtual int GetAnimationBaseline() const; @@ -324,30 +324,34 @@ class ScreenRecoveryUI : public RecoveryUI, public DrawInterface { int DrawTextLines(int x, int y, const std::vector& lines) const override; int DrawWrappedTextLines(int x, int y, const std::vector& lines) const override; - Icon currentIcon; - // The layout to use. int layout_; - GRSurface* error_icon; - - GRSurface* erasing_text; - GRSurface* error_text; - GRSurface* installing_text; - GRSurface* no_command_text; - - // Graphs for the wipe data menu - GRSurface* wipe_data_menu_header_text_; - GRSurface* try_again_text_; - GRSurface* factory_data_reset_text_; - - GRSurface** introFrames; - GRSurface** loopFrames; - - GRSurface* progressBarEmpty; - GRSurface* progressBarFill; - GRSurface* stageMarkerEmpty; - GRSurface* stageMarkerFill; + // The images that contain localized texts. + std::unique_ptr erasing_text_; + std::unique_ptr error_text_; + std::unique_ptr installing_text_; + std::unique_ptr no_command_text_; + + // Localized text images for the wipe data menu. + std::unique_ptr wipe_data_menu_header_text_; + std::unique_ptr try_again_text_; + std::unique_ptr factory_data_reset_text_; + + // current_icon_ points to one of the frames in intro_frames_ or loop_frames_, indexed by + // current_frame_, or error_icon_. + Icon current_icon_; + std::unique_ptr error_icon_; + std::vector> intro_frames_; + std::vector> loop_frames_; + size_t current_frame_; + bool intro_done_; + + // progress_bar and stage_marker images. + std::unique_ptr progress_bar_empty_; + std::unique_ptr progress_bar_fill_; + std::unique_ptr stage_marker_empty_; + std::unique_ptr stage_marker_fill_; ProgressType progressBarType; @@ -377,13 +381,6 @@ class ScreenRecoveryUI : public RecoveryUI, public DrawInterface { std::thread progress_thread_; std::atomic progress_thread_stopped_{ false }; - // Number of intro frames and loop frames in the animation. - size_t intro_frames; - size_t loop_frames; - - size_t current_frame; - bool intro_done; - int stage, max_stage; int char_width_; -- cgit v1.2.3