diff --git a/docs/CODING_STYLE.md b/docs/CODING_STYLE.md index 732acec22..34a1c7c18 100644 --- a/docs/CODING_STYLE.md +++ b/docs/CODING_STYLE.md @@ -85,10 +85,10 @@ We are using some C++11 features, mainly: * Use range-based for loops (`for (const auto& item : values) { ... }`) * Use template alias (`template alias = orig;`) * Use non-generic lambda functions -* Use `std::shared_ptr` and `std::unique_ptr` +* Use `std::shared_ptr`, `std::unique_ptr`, or `base::Ref` * Use `base::clamp` (no `std::clamp` yet) * Use `static constexpr T v = ...;` * You can use ``, ``, ``, and `` -* We can use `using T = ...;` instead of `typedef ... T` +* Prefer `using T = ...;` instead of `typedef ... T` * We use gcc 9.2 or clang 9.0 on Linux, so check the features available in https://developer.mozilla.org/en-US/docs/Mozilla/Using_CXX_in_Mozilla_code diff --git a/laf b/laf index 04641545a..7e21fb53f 160000 --- a/laf +++ b/laf @@ -1 +1 @@ -Subproject commit 04641545aab2dc5b26432279dacae2c3a7e781bc +Subproject commit 7e21fb53fb75c5dac11cb0e1adef8eef3e2722a8 diff --git a/src/app/app.cpp b/src/app/app.cpp index 4d22837f0..dfc8b4797 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -373,16 +373,13 @@ void App::run() ResourceFinder rf; rf.includeDataDir(fmt::format("icons/ase{0}.png", size).c_str()); if (rf.findFirst()) { - os::Surface* surf = os::instance()->loadRgbaSurface(rf.filename().c_str()); + os::SurfaceRef surf = os::instance()->loadRgbaSurface(rf.filename().c_str()); if (surf) icons.push_back(surf); } } display->setIcons(icons); - - for (auto surf : icons) - surf->dispose(); } catch (const std::exception&) { // Just ignore the exception, we couldn't change the app icon, no diff --git a/src/app/app_menus.cpp b/src/app/app_menus.cpp index 051f78d37..c2418d849 100644 --- a/src/app/app_menus.cpp +++ b/src/app/app_menus.cpp @@ -317,12 +317,6 @@ AppMenus::AppMenus() [this]{ rebuildRecentList(); }); } -AppMenus::~AppMenus() -{ - if (m_osMenu) - m_osMenu->dispose(); -} - void AppMenus::reload() { MENUS_TRACE("MENUS: AppMenus::reload()"); @@ -554,10 +548,10 @@ bool AppMenus::rebuildRecentList() // Sync native menus if (owner->native() && owner->native()->menuItem) { - os::Menus* menus = os::instance()->menus(); - os::Menu* osMenu = (menus ? menus->createMenu(): nullptr); + auto menus = os::instance()->menus(); + os::MenuRef osMenu = (menus ? menus->makeMenu(): nullptr); if (osMenu) { - createNativeSubmenus(osMenu, menu); + createNativeSubmenus(osMenu.get(), menu); owner->native()->menuItem->setSubmenu(osMenu); } } @@ -817,8 +811,9 @@ void AppMenus::createNativeMenus() if (!menus) // This platform doesn't support native menu items return; - os::Menu* oldOSMenu = m_osMenu; - m_osMenu = menus->createMenu(); + // Save a reference to the old menu to avoid destroying it. + os::MenuRef oldOSMenu = m_osMenu; + m_osMenu = menus->makeMenu(); #ifdef __APPLE__ // Create default macOS app menus (App ... Window) { @@ -854,24 +849,24 @@ void AppMenus::createNativeMenus() os::MenuItemInfo quit(fmt::format("Quit {}", get_app_name()), os::MenuItemInfo::Quit); quit.shortcut = os::Shortcut('q', os::kKeyCmdModifier); - os::Menu* appMenu = menus->createMenu(); - appMenu->addItem(menus->createMenuItem(about)); - appMenu->addItem(menus->createMenuItem(os::MenuItemInfo(os::MenuItemInfo::Separator))); - appMenu->addItem(menus->createMenuItem(preferences)); - appMenu->addItem(menus->createMenuItem(os::MenuItemInfo(os::MenuItemInfo::Separator))); - appMenu->addItem(menus->createMenuItem(hide)); - appMenu->addItem(menus->createMenuItem(os::MenuItemInfo("Hide Others", os::MenuItemInfo::HideOthers))); - appMenu->addItem(menus->createMenuItem(os::MenuItemInfo("Show All", os::MenuItemInfo::ShowAll))); - appMenu->addItem(menus->createMenuItem(os::MenuItemInfo(os::MenuItemInfo::Separator))); - appMenu->addItem(menus->createMenuItem(quit)); + os::MenuRef appMenu = menus->makeMenu(); + appMenu->addItem(menus->makeMenuItem(about)); + appMenu->addItem(menus->makeMenuItem(os::MenuItemInfo(os::MenuItemInfo::Separator))); + appMenu->addItem(menus->makeMenuItem(preferences)); + appMenu->addItem(menus->makeMenuItem(os::MenuItemInfo(os::MenuItemInfo::Separator))); + appMenu->addItem(menus->makeMenuItem(hide)); + appMenu->addItem(menus->makeMenuItem(os::MenuItemInfo("Hide Others", os::MenuItemInfo::HideOthers))); + appMenu->addItem(menus->makeMenuItem(os::MenuItemInfo("Show All", os::MenuItemInfo::ShowAll))); + appMenu->addItem(menus->makeMenuItem(os::MenuItemInfo(os::MenuItemInfo::Separator))); + appMenu->addItem(menus->makeMenuItem(quit)); - os::MenuItem* appItem = menus->createMenuItem(os::MenuItemInfo("App")); + os::MenuItemRef appItem = menus->makeMenuItem(os::MenuItemInfo("App")); appItem->setSubmenu(appMenu); m_osMenu->addItem(appItem); } #endif - createNativeSubmenus(m_osMenu, m_rootMenu.get()); + createNativeSubmenus(m_osMenu.get(), m_rootMenu.get()); #ifdef __APPLE__ { @@ -889,11 +884,11 @@ void AppMenus::createNativeMenus() os::MenuItemInfo minimize("Minimize", os::MenuItemInfo::Minimize); minimize.shortcut = os::Shortcut('m', os::kKeyCmdModifier); - os::Menu* windowMenu = menus->createMenu(); - windowMenu->addItem(menus->createMenuItem(minimize)); - windowMenu->addItem(menus->createMenuItem(os::MenuItemInfo("Zoom", os::MenuItemInfo::Zoom))); + os::MenuRef windowMenu = menus->makeMenu(); + windowMenu->addItem(menus->makeMenuItem(minimize)); + windowMenu->addItem(menus->makeMenuItem(os::MenuItemInfo("Zoom", os::MenuItemInfo::Zoom))); - os::MenuItem* windowItem = menus->createMenuItem(os::MenuItemInfo("Window")); + os::MenuItemRef windowItem = menus->makeMenuItem(os::MenuItemInfo("Window")); windowItem->setSubmenu(windowMenu); // We use helpIndex+1 because the first index in m_osMenu is the @@ -904,10 +899,11 @@ void AppMenus::createNativeMenus() menus->setAppMenu(m_osMenu); if (oldOSMenu) - oldOSMenu->dispose(); + oldOSMenu.reset(); } -void AppMenus::createNativeSubmenus(os::Menu* osMenu, const ui::Menu* uiMenu) +void AppMenus::createNativeSubmenus(os::Menu* osMenu, + const ui::Menu* uiMenu) { os::Menus* menus = os::instance()->menus(); @@ -951,7 +947,7 @@ void AppMenus::createNativeSubmenus(os::Menu* osMenu, const ui::Menu* uiMenu) continue; } - os::MenuItem* osItem = menus->createMenuItem(info); + os::MenuItemRef osItem = menus->makeMenuItem(info); if (osItem) { osMenu->addItem(osItem); if (appMenuItem) { @@ -961,8 +957,8 @@ void AppMenus::createNativeSubmenus(os::Menu* osMenu, const ui::Menu* uiMenu) if (child->type() == ui::kMenuItemWidget && ((ui::MenuItem*)child)->hasSubmenu()) { - os::Menu* osSubmenu = menus->createMenu(); - createNativeSubmenus(osSubmenu, ((ui::MenuItem*)child)->getSubmenu()); + os::MenuRef osSubmenu = menus->makeMenu(); + createNativeSubmenus(osSubmenu.get(), ((ui::MenuItem*)child)->getSubmenu()); osItem->setSubmenu(osSubmenu); } } diff --git a/src/app/app_menus.h b/src/app/app_menus.h index 8991b8e5a..be2da37ee 100644 --- a/src/app/app_menus.h +++ b/src/app/app_menus.h @@ -14,6 +14,7 @@ #include "app/widget_type_mismatch.h" #include "base/disable_copying.h" #include "obs/connection.h" +#include "os/menus.h" #include "ui/base.h" #include "ui/menu.h" @@ -22,11 +23,6 @@ class TiXmlElement; class TiXmlHandle; -namespace os { - class Menu; - class Shortcut; -} - namespace app { class Command; class Params; @@ -41,8 +37,6 @@ namespace app { public: static AppMenus* instance(); - ~AppMenus(); - void reload(); void initTheme(); @@ -83,7 +77,8 @@ namespace app { void syncNativeMenuItemKeyShortcuts(Menu* menu); void updateMenusList(); void createNativeMenus(); - void createNativeSubmenus(os::Menu* osMenu, const ui::Menu* uiMenu); + void createNativeSubmenus(os::Menu* osMenu, + const ui::Menu* uiMenu); #ifdef ENABLE_SCRIPTING void loadScriptsSubmenu(ui::Menu* menu, @@ -118,7 +113,7 @@ namespace app { std::map m_groups; // Native main menu bar (== nullptr if the platform doesn't // support native menus) - os::Menu* m_osMenu; + os::MenuRef m_osMenu; XmlTranslator m_xmlTranslator; }; diff --git a/src/app/cmd/assign_color_profile.cpp b/src/app/cmd/assign_color_profile.cpp index ff6e96223..686ba1ac2 100644 --- a/src/app/cmd/assign_color_profile.cpp +++ b/src/app/cmd/assign_color_profile.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -17,7 +17,7 @@ namespace app { namespace cmd { -AssignColorProfile::AssignColorProfile(doc::Sprite* sprite, const gfx::ColorSpacePtr& cs) +AssignColorProfile::AssignColorProfile(doc::Sprite* sprite, const gfx::ColorSpaceRef& cs) : WithSprite(sprite) , m_oldCS(sprite->colorSpace()) , m_newCS(cs) diff --git a/src/app/cmd/assign_color_profile.h b/src/app/cmd/assign_color_profile.h index 5de5d48db..14af7b8d4 100644 --- a/src/app/cmd/assign_color_profile.h +++ b/src/app/cmd/assign_color_profile.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -18,7 +18,7 @@ namespace cmd { class AssignColorProfile : public Cmd, public WithSprite { public: - AssignColorProfile(doc::Sprite* sprite, const gfx::ColorSpacePtr& cs); + AssignColorProfile(doc::Sprite* sprite, const gfx::ColorSpaceRef& cs); protected: void onExecute() override; @@ -32,8 +32,8 @@ namespace cmd { } private: - gfx::ColorSpacePtr m_oldCS; - gfx::ColorSpacePtr m_newCS; + gfx::ColorSpaceRef m_oldCS; + gfx::ColorSpaceRef m_newCS; }; } // namespace cmd diff --git a/src/app/cmd/convert_color_profile.cpp b/src/app/cmd/convert_color_profile.cpp index 9878376f5..585ba32a3 100644 --- a/src/app/cmd/convert_color_profile.cpp +++ b/src/app/cmd/convert_color_profile.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -24,7 +24,7 @@ namespace app { namespace cmd { static doc::ImageRef convert_image_color_space(const doc::Image* srcImage, - const gfx::ColorSpacePtr& newCS, + const gfx::ColorSpaceRef& newCS, os::ColorSpaceConversion* conversion) { ImageSpec spec = srcImage->spec(); @@ -71,14 +71,14 @@ static doc::ImageRef convert_image_color_space(const doc::Image* srcImage, } void convert_color_profile(doc::Sprite* sprite, - const gfx::ColorSpacePtr& newCS) + const gfx::ColorSpaceRef& newCS) { ASSERT(sprite->colorSpace()); ASSERT(newCS); os::System* system = os::instance(); - auto srcOCS = system->createColorSpace(sprite->colorSpace()); - auto dstOCS = system->createColorSpace(newCS); + auto srcOCS = system->makeColorSpace(sprite->colorSpace()); + auto dstOCS = system->makeColorSpace(newCS); ASSERT(srcOCS); ASSERT(dstOCS); @@ -123,15 +123,15 @@ void convert_color_profile(doc::Sprite* sprite, void convert_color_profile(doc::Image* image, doc::Palette* palette, - const gfx::ColorSpacePtr& oldCS, - const gfx::ColorSpacePtr& newCS) + const gfx::ColorSpaceRef& oldCS, + const gfx::ColorSpaceRef& newCS) { ASSERT(oldCS); ASSERT(newCS); os::System* system = os::instance(); - auto srcOCS = system->createColorSpace(oldCS); - auto dstOCS = system->createColorSpace(newCS); + auto srcOCS = system->makeColorSpace(oldCS); + auto dstOCS = system->makeColorSpace(newCS); ASSERT(srcOCS); ASSERT(dstOCS); @@ -161,7 +161,7 @@ void convert_color_profile(doc::Image* image, } } -ConvertColorProfile::ConvertColorProfile(doc::Sprite* sprite, const gfx::ColorSpacePtr& newCS) +ConvertColorProfile::ConvertColorProfile(doc::Sprite* sprite, const gfx::ColorSpaceRef& newCS) : WithSprite(sprite) { os::System* system = os::instance(); @@ -169,8 +169,8 @@ ConvertColorProfile::ConvertColorProfile(doc::Sprite* sprite, const gfx::ColorSp ASSERT(sprite->colorSpace()); ASSERT(newCS); - auto srcOCS = system->createColorSpace(sprite->colorSpace()); - auto dstOCS = system->createColorSpace(newCS); + auto srcOCS = system->makeColorSpace(sprite->colorSpace()); + auto dstOCS = system->makeColorSpace(newCS); ASSERT(srcOCS); ASSERT(dstOCS); diff --git a/src/app/cmd/convert_color_profile.h b/src/app/cmd/convert_color_profile.h index 1ee6bf5e9..c6c3dce74 100644 --- a/src/app/cmd/convert_color_profile.h +++ b/src/app/cmd/convert_color_profile.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -28,7 +28,7 @@ namespace cmd { class ConvertColorProfile : public Cmd, public WithSprite { public: - ConvertColorProfile(doc::Sprite* sprite, const gfx::ColorSpacePtr& newCS); + ConvertColorProfile(doc::Sprite* sprite, const gfx::ColorSpaceRef& newCS); protected: void onExecute() override; @@ -45,12 +45,12 @@ namespace cmd { // Converts the sprite to the new color profile without undo information. // TODO how to merge this function with cmd::ConvertColorProfile void convert_color_profile(doc::Sprite* sprite, - const gfx::ColorSpacePtr& newCS); + const gfx::ColorSpaceRef& newCS); void convert_color_profile(doc::Image* image, doc::Palette* palette, - const gfx::ColorSpacePtr& oldCS, - const gfx::ColorSpacePtr& newCS); + const gfx::ColorSpaceRef& oldCS, + const gfx::ColorSpaceRef& newCS); } // namespace cmd } // namespace app diff --git a/src/app/color_spaces.cpp b/src/app/color_spaces.cpp index 68e658554..625f1d2df 100644 --- a/src/app/color_spaces.cpp +++ b/src/app/color_spaces.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -33,12 +33,12 @@ void initialize_color_spaces(Preferences& pref) }); } -os::ColorSpacePtr get_screen_color_space() +os::ColorSpaceRef get_screen_color_space() { return os::instance()->defaultDisplay()->colorSpace(); } -os::ColorSpacePtr get_current_color_space() +os::ColorSpaceRef get_current_color_space() { #ifdef ENABLE_UI if (current_editor) @@ -48,14 +48,14 @@ os::ColorSpacePtr get_current_color_space() return get_screen_color_space(); } -gfx::ColorSpacePtr get_working_rgb_space_from_preferences() +gfx::ColorSpaceRef get_working_rgb_space_from_preferences() { if (Preferences::instance().color.manage()) { const std::string name = Preferences::instance().color.workingRgbSpace(); if (name == "sRGB") return gfx::ColorSpace::MakeSRGB(); - std::vector colorSpaces; + std::vector colorSpaces; os::instance()->listColorSpaces(colorSpaces); for (auto& cs : colorSpaces) { if (cs->gfxColorSpace()->name() == name) @@ -78,8 +78,8 @@ ConvertCS::ConvertCS() } } -ConvertCS::ConvertCS(const os::ColorSpacePtr& srcCS, - const os::ColorSpacePtr& dstCS) +ConvertCS::ConvertCS(const os::ColorSpaceRef& srcCS, + const os::ColorSpaceRef& dstCS) { if (g_manage) { m_conversion = os::instance()->convertBetweenColorSpace(srcCS, dstCS); @@ -108,10 +108,10 @@ ConvertCS convert_from_current_to_screen_color_space() return ConvertCS(); } -ConvertCS convert_from_custom_to_srgb(const os::ColorSpacePtr& from) +ConvertCS convert_from_custom_to_srgb(const os::ColorSpaceRef& from) { return ConvertCS(from, - os::instance()->createColorSpace(gfx::ColorSpace::MakeSRGB())); + os::instance()->makeColorSpace(gfx::ColorSpace::MakeSRGB())); } } // namespace app diff --git a/src/app/color_spaces.h b/src/app/color_spaces.h index d25cf8009..99337153c 100644 --- a/src/app/color_spaces.h +++ b/src/app/color_spaces.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (c) 2018-2020 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -12,8 +12,6 @@ #include "gfx/color_space.h" #include "os/color_space.h" -#include - namespace doc { class Sprite; } @@ -23,27 +21,27 @@ namespace app { void initialize_color_spaces(Preferences& pref); - os::ColorSpacePtr get_screen_color_space(); + os::ColorSpaceRef get_screen_color_space(); // Returns the color space of the current document. - os::ColorSpacePtr get_current_color_space(); + os::ColorSpaceRef get_current_color_space(); - gfx::ColorSpacePtr get_working_rgb_space_from_preferences(); + gfx::ColorSpaceRef get_working_rgb_space_from_preferences(); class ConvertCS { public: ConvertCS(); - ConvertCS(const os::ColorSpacePtr& srcCS, - const os::ColorSpacePtr& dstCS); + ConvertCS(const os::ColorSpaceRef& srcCS, + const os::ColorSpaceRef& dstCS); ConvertCS(ConvertCS&&); ConvertCS& operator=(const ConvertCS&) = delete; gfx::Color operator()(const gfx::Color c); private: - std::unique_ptr m_conversion; + os::Ref m_conversion; }; ConvertCS convert_from_current_to_screen_color_space(); - ConvertCS convert_from_custom_to_srgb(const os::ColorSpacePtr& from); + ConvertCS convert_from_custom_to_srgb(const os::ColorSpaceRef& from); } // namespace app diff --git a/src/app/commands/cmd_fullscreen_preview.cpp b/src/app/commands/cmd_fullscreen_preview.cpp index fbbbb6d77..2797f04a5 100644 --- a/src/app/commands/cmd_fullscreen_preview.cpp +++ b/src/app/commands/cmd_fullscreen_preview.cpp @@ -26,7 +26,6 @@ #include "doc/palette.h" #include "doc/primitives.h" #include "doc/sprite.h" -#include "os/scoped_handle.h" #include "os/surface.h" #include "os/system.h" @@ -53,7 +52,7 @@ public: , m_proj(editor->projection()) , m_index_bg_color(-1) , m_doublebuf(Image::create(IMAGE_RGB, ui::display_w(), ui::display_h())) - , m_doublesur(os::instance()->createRgbaSurface(ui::display_w(), ui::display_h())) { + , m_doublesur(os::instance()->makeRgbaSurface(ui::display_w(), ui::display_h())) { // Do not use DocWriter (do not lock the document) because we // will call other sub-commands (e.g. previous frame, next frame, // etc.). @@ -232,8 +231,8 @@ protected: } convert_image_to_surface(m_doublebuf.get(), m_pal, - m_doublesur, 0, 0, 0, 0, m_doublebuf->width(), m_doublebuf->height()); - g->blit(m_doublesur, 0, 0, 0, 0, m_doublesur->width(), m_doublesur->height()); + m_doublesur.get(), 0, 0, 0, 0, m_doublebuf->width(), m_doublebuf->height()); + g->blit(m_doublesur.get(), 0, 0, 0, 0, m_doublesur->width(), m_doublesur->height()); } private: @@ -249,7 +248,7 @@ private: int m_index_bg_color; std::unique_ptr m_render; std::unique_ptr m_doublebuf; - os::ScopedHandle m_doublesur; + os::SurfaceRef m_doublesur; filters::TiledMode m_tiled; }; diff --git a/src/app/commands/cmd_options.cpp b/src/app/commands/cmd_options.cpp index 3923d930c..1ded3a332 100644 --- a/src/app/commands/cmd_options.cpp +++ b/src/app/commands/cmd_options.cpp @@ -99,13 +99,13 @@ class OptionsWindow : public app::gen::Options { class ColorSpaceItem : public ListItem { public: - ColorSpaceItem(const os::ColorSpacePtr& cs) + ColorSpaceItem(const os::ColorSpaceRef& cs) : ListItem(cs->gfxColorSpace()->name()), m_cs(cs) { } - os::ColorSpacePtr cs() const { return m_cs; } + os::ColorSpaceRef cs() const { return m_cs; } private: - os::ColorSpacePtr m_cs; + os::ColorSpaceRef m_cs; }; class ThemeItem : public ListItem { @@ -1609,7 +1609,7 @@ private: std::string m_restoreThisTheme; int m_restoreScreenScaling; int m_restoreUIScaling; - std::vector m_colorSpaces; + std::vector m_colorSpaces; std::string m_templateTextForDisplayCS; RgbMapAlgorithmSelector m_rgbmapAlgorithmSelector; }; diff --git a/src/app/commands/cmd_sprite_properties.cpp b/src/app/commands/cmd_sprite_properties.cpp index 21587c2d7..975d16f5d 100644 --- a/src/app/commands/cmd_sprite_properties.cpp +++ b/src/app/commands/cmd_sprite_properties.cpp @@ -63,7 +63,7 @@ void SpritePropertiesCommand::onExecute(Context* context) ColorButton* color_button = nullptr; // List of available color profiles - std::vector colorSpaces; + std::vector colorSpaces; os::instance()->listColorSpaces(colorSpaces); // Load the window widget @@ -152,7 +152,7 @@ void SpritePropertiesCommand::onExecute(Context* context) ++i; } if (selectedColorProfile < 0) { - colorSpaces.push_back(os::instance()->createColorSpace(sprite->colorSpace())); + colorSpaces.push_back(os::instance()->makeColorSpace(sprite->colorSpace())); selectedColorProfile = colorSpaces.size()-1; } diff --git a/src/app/commands/screenshot.cpp b/src/app/commands/screenshot.cpp index d8bbcbe11..010c82c69 100644 --- a/src/app/commands/screenshot.cpp +++ b/src/app/commands/screenshot.cpp @@ -70,7 +70,7 @@ void ScreenshotCommand::onExecute(Context* ctx) rf.includeDesktopDir(""); os::Display* display = ui::Manager::getDefault()->getDisplay(); - os::Surface* surface = display->getSurface(); + os::Surface* surface = display->surface(); std::string fn; if (params().save()) { diff --git a/src/app/crash/read_document.cpp b/src/app/crash/read_document.cpp index eb7316f44..c312c06e4 100644 --- a/src/app/crash/read_document.cpp +++ b/src/app/crash/read_document.cpp @@ -344,7 +344,7 @@ private: // Read color space if (!s.eof()) { - gfx::ColorSpacePtr colorSpace = readColorSpace(s); + gfx::ColorSpaceRef colorSpace = readColorSpace(s); if (colorSpace) spr->setColorSpace(colorSpace); } @@ -359,7 +359,7 @@ private: return spr.release(); } - gfx::ColorSpacePtr readColorSpace(std::ifstream& s) { + gfx::ColorSpaceRef readColorSpace(std::ifstream& s) { const gfx::ColorSpace::Type type = (gfx::ColorSpace::Type)read16(s); const gfx::ColorSpace::Flag flags = (gfx::ColorSpace::Flag)read16(s); const double gamma = fixmath::fixtof(read32(s)); @@ -375,7 +375,7 @@ private: s.read((char*)&buf[0], n); std::string name = read_string(s); - auto colorSpace = std::make_shared( + auto colorSpace = base::make_ref( type, flags, gamma, std::move(buf)); colorSpace->setName(name); return colorSpace; diff --git a/src/app/crash/write_document.cpp b/src/app/crash/write_document.cpp index b58b1dd39..3778a9038 100644 --- a/src/app/crash/write_document.cpp +++ b/src/app/crash/write_document.cpp @@ -185,7 +185,7 @@ private: return true; } - bool writeColorSpace(std::ofstream& s, const gfx::ColorSpacePtr& colorSpace) { + bool writeColorSpace(std::ofstream& s, const gfx::ColorSpaceRef& colorSpace) { write16(s, colorSpace->type()); write16(s, colorSpace->flags()); write32(s, fixmath::ftofix(colorSpace->gamma())); diff --git a/src/app/doc.cpp b/src/app/doc.cpp index 31b4c0e94..ecfd714bf 100644 --- a/src/app/doc.cpp +++ b/src/app/doc.cpp @@ -594,7 +594,7 @@ void Doc::updateOSColorSpace(bool appWideSignal) { auto system = os::instance(); if (system) { - m_osColorSpace = system->createColorSpace(sprite()->colorSpace()); + m_osColorSpace = system->makeColorSpace(sprite()->colorSpace()); if (!m_osColorSpace && system->defaultDisplay()) m_osColorSpace = system->defaultDisplay()->colorSpace(); } diff --git a/src/app/doc.h b/src/app/doc.h index c5a3ea210..90afcd20c 100644 --- a/src/app/doc.h +++ b/src/app/doc.h @@ -99,7 +99,7 @@ namespace app { color_t bgColor() const; color_t bgColor(Layer* layer) const; - os::ColorSpacePtr osColorSpace() const { return m_osColorSpace; } + os::ColorSpaceRef osColorSpace() const { return m_osColorSpace; } ////////////////////////////////////////////////////////////////////// // Notifications @@ -259,7 +259,7 @@ namespace app { gfx::Point m_lastDrawingPoint; // Last used color space to render a sprite. - os::ColorSpacePtr m_osColorSpace; + os::ColorSpaceRef m_osColorSpace; DISABLE_COPYING(Doc); }; diff --git a/src/app/doc_exporter.cpp b/src/app/doc_exporter.cpp index dd7374da6..0b898894b 100644 --- a/src/app/doc_exporter.cpp +++ b/src/app/doc_exporter.cpp @@ -1090,7 +1090,7 @@ Doc* DocExporter::createEmptyTexture(const Samples& samples, ColorMode colorMode = ColorMode::INDEXED; Palette* palette = nullptr; int maxColors = 256; - gfx::ColorSpacePtr colorSpace; + gfx::ColorSpaceRef colorSpace; color_t transparentColor = 0; for (const auto& sample : samples) { diff --git a/src/app/file/ase_format.cpp b/src/app/file/ase_format.cpp index 5ad37a036..22559f366 100644 --- a/src/app/file/ase_format.cpp +++ b/src/app/file/ase_format.cpp @@ -868,7 +868,7 @@ static void ase_file_write_color_profile(FILE* f, dio::AsepriteFrameHeader* frame_header, const doc::Sprite* sprite) { - const gfx::ColorSpacePtr& cs = sprite->colorSpace(); + const gfx::ColorSpaceRef& cs = sprite->colorSpace(); if (!cs) // No color return; diff --git a/src/app/file/file.cpp b/src/app/file/file.cpp index a251a2ef2..43ccb1a3e 100644 --- a/src/app/file/file.cpp +++ b/src/app/file/file.cpp @@ -914,7 +914,7 @@ void FileOp::postLoad() } // What to do with the sprite color profile? - gfx::ColorSpacePtr spriteCS = sprite->colorSpace(); + gfx::ColorSpaceRef spriteCS = sprite->colorSpace(); app::gen::ColorProfileBehavior behavior = app::gen::ColorProfileBehavior::DISABLE; diff --git a/src/app/file/file_op_config.h b/src/app/file/file_op_config.h index 398e034cc..d8f67ee8e 100644 --- a/src/app/file/file_op_config.h +++ b/src/app/file/file_op_config.h @@ -25,7 +25,7 @@ namespace app { // profile or without a color profile. app::gen::ColorProfileBehavior filesWithProfile = app::gen::ColorProfileBehavior::EMBEDDED; app::gen::ColorProfileBehavior missingProfile = app::gen::ColorProfileBehavior::ASSIGN; - gfx::ColorSpacePtr workingCS = gfx::ColorSpace::MakeSRGB(); + gfx::ColorSpaceRef workingCS = gfx::ColorSpace::MakeSRGB(); // True if we should render each frame to save it with the new // blend mode.h diff --git a/src/app/file/jpeg_format.cpp b/src/app/file/jpeg_format.cpp index cc0ae9a15..4abde1853 100644 --- a/src/app/file/jpeg_format.cpp +++ b/src/app/file/jpeg_format.cpp @@ -69,7 +69,7 @@ class JpegFormat : public FileFormat { } bool onLoad(FileOp* fop) override; - gfx::ColorSpacePtr loadColorSpace(FileOp* fop, jpeg_decompress_struct* dinfo); + gfx::ColorSpaceRef loadColorSpace(FileOp* fop, jpeg_decompress_struct* dinfo); #ifdef ENABLE_SAVE bool onSave(FileOp* fop) override; void saveColorSpace(FileOp* fop, jpeg_compress_struct* cinfo, @@ -256,7 +256,7 @@ bool JpegFormat::onLoad(FileOp* fop) } // Read color space - gfx::ColorSpacePtr colorSpace = loadColorSpace(fop, &dinfo); + gfx::ColorSpaceRef colorSpace = loadColorSpace(fop, &dinfo); if (colorSpace) fop->setEmbeddedColorProfile(); else { // sRGB is the default JPG color space. @@ -282,7 +282,7 @@ bool JpegFormat::onLoad(FileOp* fop) // in two steps: // (1) Discover all ICC profile markers and verify that they are numbered properly. // (2) Copy the data from each marker into a contiguous ICC profile. -gfx::ColorSpacePtr JpegFormat::loadColorSpace(FileOp* fop, jpeg_decompress_struct* dinfo) +gfx::ColorSpaceRef JpegFormat::loadColorSpace(FileOp* fop, jpeg_decompress_struct* dinfo) { // Note that 256 will be enough storage space since each markerIndex is stored in 8-bits. jpeg_marker_struct* markerSequence[256]; diff --git a/src/app/file/png_format.cpp b/src/app/file/png_format.cpp index b86b5dc15..0b0a9ecf8 100644 --- a/src/app/file/png_format.cpp +++ b/src/app/file/png_format.cpp @@ -59,7 +59,7 @@ class PngFormat : public FileFormat { } bool onLoad(FileOp* fop) override; - gfx::ColorSpacePtr loadColorSpace(png_structp png, png_infop info); + gfx::ColorSpaceRef loadColorSpace(png_structp png, png_infop info); #ifdef ENABLE_SAVE bool onSave(FileOp* fop) override; void saveColorSpace(png_structp png, png_infop info, const gfx::ColorSpace* colorSpace); @@ -457,7 +457,7 @@ bool PngFormat::onLoad(FileOp* fop) // // Code to read color spaces from png files from Skia (SkPngCodec.cpp) // by Google Inc. -gfx::ColorSpacePtr PngFormat::loadColorSpace(png_structp png_ptr, png_infop info_ptr) +gfx::ColorSpaceRef PngFormat::loadColorSpace(png_structp png_ptr, png_infop info_ptr) { // First check for an ICC profile png_bytep profile; diff --git a/src/app/file_selector.cpp b/src/app/file_selector.cpp index e32753d0d..d7e8f0119 100644 --- a/src/app/file_selector.cpp +++ b/src/app/file_selector.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -31,8 +32,8 @@ bool show_file_selector( if (Preferences::instance().experimental.useNativeFileDialog() && os::instance()->nativeDialogs()) { - os::FileDialog* dlg = - os::instance()->nativeDialogs()->createFileDialog(); + os::FileDialogRef dlg = + os::instance()->nativeDialogs()->makeFileDialog(); if (dlg) { dlg->setTitle(title); @@ -65,7 +66,6 @@ bool show_file_selector( else output.push_back(dlg->fileName()); } - dlg->dispose(); return res; } } diff --git a/src/app/file_system.cpp b/src/app/file_system.cpp index 39e60b3ce..2b4eb95c9 100644 --- a/src/app/file_system.cpp +++ b/src/app/file_system.cpp @@ -56,7 +56,7 @@ namespace app { namespace { class FileItem; -typedef std::map FileItemMap; +using FileItemMap = std::map; // the root of the file-system FileItem* rootitem = nullptr; @@ -121,8 +121,8 @@ public: m_thumbnailProgress = progress; } - os::Surface* getThumbnail() override; - void setThumbnail(os::Surface* thumbnail) override; + os::SurfaceRef getThumbnail() override; + void setThumbnail(const os::SurfaceRef& thumbnail) override; // Calls "delete this" void deleteItem() { @@ -586,16 +586,21 @@ bool FileItem::hasExtension(const base::paths& extensions) return base::has_file_extension(m_filename, extensions); } -os::Surface* FileItem::getThumbnail() +os::SurfaceRef FileItem::getThumbnail() { - return m_thumbnail; + os::SurfaceRef ref(m_thumbnail.load()); + if (ref) + ref->ref(); // base::Ref(T*) doesn't add an extra reference + return ref; } -void FileItem::setThumbnail(os::Surface* thumbnail) +void FileItem::setThumbnail(const os::SurfaceRef& newThumbnail) { - auto old = m_thumbnail.exchange(thumbnail); + if (newThumbnail) + newThumbnail->ref(); + auto old = m_thumbnail.exchange(newThumbnail.get()); if (old) - old->dispose(); + old->unref(); } FileItem::FileItem(FileItem* parent) @@ -621,8 +626,7 @@ FileItem::~FileItem() { FS_TRACE("FS: Destroying FileItem() with parent %p\n", m_parent); - if (auto ptr = m_thumbnail.load()) - ptr->dispose(); + m_thumbnail.exchange(nullptr); #ifdef _WIN32 if (m_fullpidl && m_fullpidl != m_pidl) { diff --git a/src/app/file_system.h b/src/app/file_system.h index 133b714c9..5650ac4f3 100644 --- a/src/app/file_system.h +++ b/src/app/file_system.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -12,19 +12,15 @@ #include "base/mutex.h" #include "base/paths.h" #include "obs/signal.h" +#include "os/surface.h" #include #include -namespace os { - class Surface; -} - namespace app { class IFileItem; - - typedef std::vector FileItemList; + using FileItemList = std::vector; class FileSystemModule { static FileSystemModule* m_instance; @@ -91,8 +87,8 @@ namespace app { virtual double getThumbnailProgress() = 0; virtual void setThumbnailProgress(double progress) = 0; - virtual os::Surface* getThumbnail() = 0; - virtual void setThumbnail(os::Surface* thumbnail) = 0; + virtual os::SurfaceRef getThumbnail() = 0; + virtual void setThumbnail(const os::SurfaceRef& thumbnail) = 0; }; } // namespace app diff --git a/src/app/modules/gui.cpp b/src/app/modules/gui.cpp index ca0040bbb..4c134cd63 100644 --- a/src/app/modules/gui.cpp +++ b/src/app/modules/gui.cpp @@ -96,9 +96,9 @@ protected: void saveLayout(Widget* widget, const std::string& str) override; }; -static os::Display* main_display = NULL; -static CustomizedGuiManager* manager = NULL; -static Theme* gui_theme = NULL; +static os::DisplayRef main_display = nullptr; +static CustomizedGuiManager* manager = nullptr; +static Theme* gui_theme = nullptr; static ui::Timer* defered_invalid_timer = nullptr; static gfx::Region defered_invalid_region; @@ -124,7 +124,7 @@ static bool create_main_display(bool gpuAccel, try { if (w > 0 && h > 0) { - main_display = os::instance()->createDisplay( + main_display = os::instance()->makeDisplay( w, h, (scale == 0 ? 2: base::clamp(scale, 1, 4))); } } @@ -136,7 +136,7 @@ static bool create_main_display(bool gpuAccel, for (int c=0; try_resolutions[c].width; ++c) { try { main_display = - os::instance()->createDisplay( + os::instance()->makeDisplay( try_resolutions[c].width, try_resolutions[c].height, (scale == 0 ? try_resolutions[c].scale: scale)); @@ -193,7 +193,7 @@ int init_module_gui() // Create the default-manager manager = new CustomizedGuiManager(); - manager->setDisplay(main_display); + manager->setDisplay(main_display.get()); // Setup the GUI theme for all widgets gui_theme = new SkinTheme; @@ -221,7 +221,8 @@ void exit_module_gui() ui::set_theme(nullptr, ui::guiscale()); delete gui_theme; - main_display->dispose(); + // This should be the last unref() of the display to delete it. + main_display.reset(); } void update_displays_color_profile_from_preferences() @@ -240,13 +241,13 @@ void update_displays_color_profile_from_preferences() break; case gen::WindowColorProfile::SRGB: system->setDisplaysColorSpace( - system->createColorSpace(gfx::ColorSpace::MakeSRGB())); + system->makeColorSpace(gfx::ColorSpace::MakeSRGB())); break; case gen::WindowColorProfile::SPECIFIC: { std::string name = Preferences::instance().color.windowProfileName(); - std::vector colorSpaces; + std::vector colorSpaces; system->listColorSpaces(colorSpaces); for (auto& cs : colorSpaces) { diff --git a/src/app/script/image_spec_class.cpp b/src/app/script/image_spec_class.cpp index 2d76249a2..4c607cd95 100644 --- a/src/app/script/image_spec_class.cpp +++ b/src/app/script/image_spec_class.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (c) 2018-2019 Igara Studio S.A. +// Copyright (c) 2018-2020 Igara Studio S.A. // Copyright (C) 2018 David Capello // // This program is distributed under the terms of @@ -115,7 +115,7 @@ int ImageSpec_set_colorSpace(lua_State* L) { auto spec = get_obj(L, 1); auto cs = get_obj(L, 2); - spec->setColorSpace(std::make_shared(*cs)); + spec->setColorSpace(base::make_ref(*cs)); return 0; } diff --git a/src/app/script/sprite_class.cpp b/src/app/script/sprite_class.cpp index f8e241815..9343f23d5 100644 --- a/src/app/script/sprite_class.cpp +++ b/src/app/script/sprite_class.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2015-2018 David Capello // // This program is distributed under the terms of @@ -294,7 +294,7 @@ int Sprite_assignColorSpace(lua_State* L) auto cs = get_obj(L, 2); Tx tx; tx(new cmd::AssignColorProfile( - sprite, std::make_shared(*cs))); + sprite, base::make_ref(*cs))); tx.commit(); return 1; } @@ -305,7 +305,7 @@ int Sprite_convertColorSpace(lua_State* L) auto cs = get_obj(L, 2); Tx tx; tx(new cmd::ConvertColorProfile( - sprite, std::make_shared(*cs))); + sprite, base::make_ref(*cs))); tx.commit(); return 1; } diff --git a/src/app/thumbnail_generator.cpp b/src/app/thumbnail_generator.cpp index 2f4e40066..d4677d59a 100644 --- a/src/app/thumbnail_generator.cpp +++ b/src/app/thumbnail_generator.cpp @@ -158,13 +158,13 @@ private: // Set the thumbnail of the file-item. if (thumbnailImage) { - os::Surface* thumbnail = - os::instance()->createRgbaSurface( + os::SurfaceRef thumbnail = + os::instance()->makeRgbaSurface( thumbnailImage->width(), thumbnailImage->height()); convert_image_to_surface( - thumbnailImage.get(), palette.get(), thumbnail, + thumbnailImage.get(), palette.get(), thumbnail.get(), 0, 0, 0, 0, thumbnailImage->width(), thumbnailImage->height()); { diff --git a/src/app/thumbnails.cpp b/src/app/thumbnails.cpp index 17f406cdf..0c469121e 100644 --- a/src/app/thumbnails.cpp +++ b/src/app/thumbnails.cpp @@ -22,8 +22,8 @@ namespace app { namespace thumb { -os::Surface* get_cel_thumbnail(const doc::Cel* cel, - const gfx::Size& fitInSize) +os::SurfaceRef get_cel_thumbnail(const doc::Cel* cel, + const gfx::Size& fitInSize) { gfx::Size newSize; @@ -57,11 +57,11 @@ os::Surface* get_cel_thumbnail(const doc::Cel* cel, gfx::Clip(gfx::Rect(gfx::Point(0, 0), newSize)), 255, doc::BlendMode::NORMAL); - if (os::Surface* thumbnail = os::instance()->createRgbaSurface( + if (os::SurfaceRef thumbnail = os::instance()->makeRgbaSurface( thumbnailImage->width(), thumbnailImage->height())) { convert_image_to_surface( - thumbnailImage.get(), palette, thumbnail, + thumbnailImage.get(), palette, thumbnail.get(), 0, 0, 0, 0, thumbnailImage->width(), thumbnailImage->height()); return thumbnail; } diff --git a/src/app/thumbnails.h b/src/app/thumbnails.h index a735aa014..17adc5b72 100644 --- a/src/app/thumbnails.h +++ b/src/app/thumbnails.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2016 Carlo Caputo // // This program is distributed under the terms of @@ -10,6 +10,7 @@ #pragma once #include "gfx/size.h" +#include "os/surface.h" namespace doc { class Cel; @@ -22,8 +23,8 @@ namespace os { namespace app { namespace thumb { - os::Surface* get_cel_thumbnail(const doc::Cel* cel, - const gfx::Size& fitInSize); + os::SurfaceRef get_cel_thumbnail(const doc::Cel* cel, + const gfx::Size& fitInSize); } // thumb } // app diff --git a/src/app/ui/app_menuitem.cpp b/src/app/ui/app_menuitem.cpp index abe1ef6e7..52f8964e6 100644 --- a/src/app/ui/app_menuitem.cpp +++ b/src/app/ui/app_menuitem.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This program is distributed under the terms of @@ -25,8 +25,7 @@ #include "ui/widget.h" #include -#include -#include +#include namespace app { @@ -47,18 +46,6 @@ AppMenuItem::AppMenuItem(const std::string& text, { } -AppMenuItem::~AppMenuItem() -{ - if (m_native) { - // Do not call disposeNative(), the native handle will be disposed - // when the main menu (app menu) is disposed. - - // TODO improve handling of these kind of pointer from laf-os library - - delete m_native; - } -} - void AppMenuItem::setKey(const KeyPtr& key) { m_key = key; @@ -68,23 +55,14 @@ void AppMenuItem::setKey(const KeyPtr& key) void AppMenuItem::setNative(const Native& native) { if (!m_native) - m_native = new Native(native); - else { - // Do not call disposeNative(), the native handle will be disposed - // when the main menu (app menu) is disposed. - + m_native.reset(new Native(native)); + else *m_native = native; - } } void AppMenuItem::disposeNative() { -#if 0 // TODO fix this and the whole handling of native menu items from laf-os - if (m_native->menuItem) { - m_native->menuItem->dispose(); - m_native->menuItem = nullptr; - } -#endif + m_native.reset(); } void AppMenuItem::syncNativeMenuItemKeyShortcut() diff --git a/src/app/ui/app_menuitem.h b/src/app/ui/app_menuitem.h index d62fa3a9c..f31cb67bb 100644 --- a/src/app/ui/app_menuitem.h +++ b/src/app/ui/app_menuitem.h @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This program is distributed under the terms of @@ -10,12 +11,11 @@ #include "app/commands/params.h" #include "app/ui/key.h" +#include "os/menus.h" #include "os/shortcut.h" #include "ui/menu.h" -namespace os { - class MenuItem; -} +#include namespace app { class Command; @@ -28,7 +28,7 @@ namespace app { class AppMenuItem : public ui::MenuItem { public: struct Native { - os::MenuItem* menuItem = nullptr; + os::MenuItemRef menuItem = nullptr; os::Shortcut shortcut; app::KeyContext keyContext = app::KeyContext::Any; }; @@ -36,7 +36,6 @@ namespace app { AppMenuItem(const std::string& text, Command* command = nullptr, const Params& params = Params()); - ~AppMenuItem(); KeyPtr key() { return m_key; } void setKey(const KeyPtr& key); @@ -47,7 +46,7 @@ namespace app { Command* getCommand() { return m_command; } const Params& getParams() const { return m_params; } - Native* native() { return m_native; } + Native* native() const { return m_native.get(); } void setNative(const Native& native); void disposeNative(); void syncNativeMenuItemKeyShortcut(); @@ -65,7 +64,7 @@ namespace app { Command* m_command; Params m_params; bool m_isRecentFileItem; - Native* m_native; + std::unique_ptr m_native; static Params s_contextParams; }; diff --git a/src/app/ui/brush_popup.cpp b/src/app/ui/brush_popup.cpp index 5761b888a..63d78df54 100644 --- a/src/app/ui/brush_popup.cpp +++ b/src/app/ui/brush_popup.cpp @@ -443,7 +443,7 @@ void BrushPopup::onBrushChanges() } // static -os::Surface* BrushPopup::createSurfaceForBrush(const BrushRef& origBrush) +os::SurfaceRef BrushPopup::createSurfaceForBrush(const BrushRef& origBrush) { Image* image = nullptr; BrushRef brush = origBrush; @@ -455,7 +455,7 @@ os::Surface* BrushPopup::createSurfaceForBrush(const BrushRef& origBrush) image = brush->image(); } - os::Surface* surface = os::instance()->createRgbaSurface( + os::SurfaceRef surface = os::instance()->makeRgbaSurface( std::min(10, image ? image->width(): 4), std::min(10, image ? image->height(): 4)); @@ -468,7 +468,7 @@ os::Surface* BrushPopup::createSurfaceForBrush(const BrushRef& origBrush) } convert_image_to_surface( - image, palette, surface, + image, palette, surface.get(), 0, 0, 0, 0, image->width(), image->height()); if (image->pixelFormat() == IMAGE_BITMAP) diff --git a/src/app/ui/brush_popup.h b/src/app/ui/brush_popup.h index 11f6e522a..a5dc1af77 100644 --- a/src/app/ui/brush_popup.h +++ b/src/app/ui/brush_popup.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2015 David Capello // // This program is distributed under the terms of @@ -23,7 +23,7 @@ namespace app { void setBrush(doc::Brush* brush); void regenerate(const gfx::Rect& box); - static os::Surface* createSurfaceForBrush(const doc::BrushRef& brush); + static os::SurfaceRef createSurfaceForBrush(const doc::BrushRef& brush); private: void onStandardBrush(); diff --git a/src/app/ui/button_set.cpp b/src/app/ui/button_set.cpp index e05e43eb2..169c31140 100644 --- a/src/app/ui/button_set.cpp +++ b/src/app/ui/button_set.cpp @@ -159,7 +159,7 @@ void ButtonSet::Item::onPaint(ui::PaintEvent& ev) } if (hasText()) { - g->setFont(font()); + g->setFont(AddRef(font())); g->drawUIText(text(), fg, gfx::ColorNone, textRc.origin(), 0); } } diff --git a/src/app/ui/color_selector.cpp b/src/app/ui/color_selector.cpp index 90eb7d2ae..6a7f08c33 100644 --- a/src/app/ui/color_selector.cpp +++ b/src/app/ui/color_selector.cpp @@ -94,10 +94,8 @@ public: } m_paintingThread.join(); - if (m_canvas) { - m_canvas->dispose(); - m_canvas = nullptr; - } + if (m_canvas) + m_canvas.reset(); } } @@ -113,18 +111,16 @@ public: std::unique_lock lock(m_mutex); stopCurrentPainting(lock); - auto oldCanvas = m_canvas; - m_canvas = os::instance()->createSurface(w, h, activeCS); + os::SurfaceRef oldCanvas = m_canvas; + m_canvas = os::instance()->makeSurface(w, h, activeCS); os::Paint paint; paint.color(bgColor); paint.style(os::Paint::Fill); m_canvas->drawRect(gfx::Rect(0, 0, w, h), paint); - if (oldCanvas) { - m_canvas->drawSurface(oldCanvas, 0, 0); - oldCanvas->dispose(); - } + if (oldCanvas) + m_canvas->drawSurface(oldCanvas.get(), 0, 0); } - return m_canvas; + return m_canvas.get(); } void startBgPainting(ColorSelector* colorSelector, @@ -182,7 +178,7 @@ private: { lock.unlock(); colorSel->onPaintSurfaceInBgThread( - m_canvas, + m_canvas.get(), m_mainBounds, m_bottomBarBounds, m_alphaBarBounds, @@ -211,7 +207,7 @@ private: std::mutex m_mutex; std::condition_variable m_paintingCV; std::condition_variable m_waitStopCV; - os::Surface* m_canvas; + os::SurfaceRef m_canvas; ColorSelector* m_colorSelector; ui::Manager* m_manager; gfx::Rect m_mainBounds; diff --git a/src/app/ui/dithering_selector.cpp b/src/app/ui/dithering_selector.cpp index 85e1980ac..d68e43aa7 100644 --- a/src/app/ui/dithering_selector.cpp +++ b/src/app/ui/dithering_selector.cpp @@ -80,11 +80,10 @@ private: // Reuse the preview in case that the palette is exactly the same if (palette->id() == m_palId && palette->getModifications() == m_palMods) - return m_preview; + return m_preview.get(); // In other case regenerate the preview for the current palette - m_preview->dispose(); - m_preview = nullptr; + m_preview.reset(); } const int w = 128, h = 16; @@ -111,13 +110,13 @@ private: m_dithering, nullptr, palette, true, -1, nullptr); } - m_preview = os::instance()->createRgbaSurface(w, h); - convert_image_to_surface(image2.get(), palette, m_preview, + m_preview = os::instance()->makeRgbaSurface(w, h); + convert_image_to_surface(image2.get(), palette, m_preview.get(), 0, 0, 0, 0, w, h); m_palId = palette->id(); m_palMods = palette->getModifications(); - return m_preview; + return m_preview.get(); } void onSizeHint(SizeHintEvent& ev) override { @@ -162,7 +161,7 @@ private: bool m_matrixOnly; render::Dithering m_dithering; - os::Surface* m_preview; + os::SurfaceRef m_preview; doc::ObjectId m_palId; int m_palMods; }; diff --git a/src/app/ui/draggable_widget.h b/src/app/ui/draggable_widget.h index 0a8e2fca9..ebcdd6461 100644 --- a/src/app/ui/draggable_widget.h +++ b/src/app/ui/draggable_widget.h @@ -138,10 +138,10 @@ private: gfx::Size sz = getFloatingOverlaySize(); sz.w = std::max(1, sz.w); sz.h = std::max(1, sz.h); - os::Surface* surface = os::instance()->createRgbaSurface(sz.w, sz.h); + os::SurfaceRef surface = os::instance()->makeRgbaSurface(sz.w, sz.h); { - os::SurfaceLock lock(surface); + os::SurfaceLock lock(surface.get()); os::Paint paint; paint.color(gfx::rgba(0, 0, 0, 0)); paint.style(os::Paint::Fill); @@ -149,17 +149,18 @@ private: } { ui::Graphics g(surface, 0, 0); - g.setFont(this->font()); + g.setFont(AddRef(this->font())); drawFloatingOverlay(g); } - m_floatingOverlay.reset(new ui::Overlay(surface, gfx::Point(), - ui::Overlay::MouseZOrder-1)); - ui::OverlayManager::instance()->addOverlay(m_floatingOverlay.get()); + m_floatingOverlay = base::make_ref( + surface, gfx::Point(), + (ui::Overlay::ZOrder)(ui::Overlay::MouseZOrder-1)); + ui::OverlayManager::instance()->addOverlay(m_floatingOverlay); } void destroyFloatingOverlay() { - ui::OverlayManager::instance()->removeOverlay(m_floatingOverlay.get()); + ui::OverlayManager::instance()->removeOverlay(m_floatingOverlay); m_floatingOverlay.reset(); m_isDragging = false; } @@ -213,7 +214,7 @@ private: // Overlay used to show the floating widget (this overlay floats // next to the mouse cursor). - std::unique_ptr m_floatingOverlay; + ui::OverlayRef m_floatingOverlay; // Relative mouse position between the widget and the overlay. gfx::Point m_floatingOffset; diff --git a/src/app/ui/editor/editor.cpp b/src/app/ui/editor/editor.cpp index 31736a55e..e6eaaf4bb 100644 --- a/src/app/ui/editor/editor.cpp +++ b/src/app/ui/editor/editor.cpp @@ -674,7 +674,7 @@ void Editor::drawOneSpriteUnclippedRect(ui::Graphics* g, const gfx::Rect& sprite if (rendered) { // Convert the render to a os::Surface - static os::Surface* tmp = nullptr; // TODO move this to other centralized place + static os::SurfaceRef tmp = nullptr; // TODO move this to other centralized place if (!tmp || tmp->width() < rc2.w || @@ -682,10 +682,7 @@ void Editor::drawOneSpriteUnclippedRect(ui::Graphics* g, const gfx::Rect& sprite tmp->colorSpace() != m_document->osColorSpace()) { const int maxw = std::max(rc2.w, tmp ? tmp->width(): 0); const int maxh = std::max(rc2.h, tmp ? tmp->height(): 0); - if (tmp) - tmp->dispose(); - - tmp = os::instance()->createSurface( + tmp = os::instance()->makeSurface( maxw, maxh, m_document->osColorSpace()); } @@ -707,13 +704,13 @@ void Editor::drawOneSpriteUnclippedRect(ui::Graphics* g, const gfx::Rect& sprite } convert_image_to_surface(rendered.get(), m_sprite->palette(m_frame), - tmp, 0, 0, 0, 0, rc2.w, rc2.h); + tmp.get(), 0, 0, 0, 0, rc2.w, rc2.h); if (newEngine) { - g->drawSurface(tmp, gfx::Rect(0, 0, rc2.w, rc2.h), dest); + g->drawSurface(tmp.get(), gfx::Rect(0, 0, rc2.w, rc2.h), dest); } else { - g->blit(tmp, 0, 0, dest.x, dest.y, dest.w, dest.h); + g->blit(tmp.get(), 0, 0, dest.x, dest.y, dest.w, dest.h); } } } diff --git a/src/app/ui/file_list.cpp b/src/app/ui/file_list.cpp index 6c33a2488..9f0b562ed 100644 --- a/src/app/ui/file_list.cpp +++ b/src/app/ui/file_list.cpp @@ -455,8 +455,8 @@ void FileList::onPaint(ui::PaintEvent& ev) g->drawRect(gfx::rgba(0, 0, 0, 64), tbounds); tbounds.shrink(1); - os::Surface* thumbnail = m_selected->getThumbnail(); - g->drawRgbaSurface(thumbnail, + os::SurfaceRef thumbnail = m_selected->getThumbnail(); + g->drawRgbaSurface(thumbnail.get(), gfx::Rect(0, 0, thumbnail->width(), thumbnail->height()), tbounds); } @@ -498,10 +498,10 @@ void FileList::paintItem(ui::Graphics* g, IFileItem* fi, const int i) gfx::Rect textBounds = info.text; // Folder icon or thumbnail - os::Surface* thumbnail = nullptr; + os::SurfaceRef thumbnail = nullptr; if (fi->isFolder()) { if (isListView()) { - thumbnail = theme->parts.folderIconSmall()->bitmap(0); + thumbnail = theme->parts.folderIconSmall()->bitmapRef(0); tbounds = textBounds; tbounds.x += 2*guiscale(); tbounds.w = tbounds.h; @@ -510,8 +510,8 @@ void FileList::paintItem(ui::Graphics* g, IFileItem* fi, const int i) else { thumbnail = (m_zoom < 4.0 ? - theme->parts.folderIconMedium()->bitmap(0): - theme->parts.folderIconBig()->bitmap(0)); + theme->parts.folderIconMedium()->bitmapRef(0): + theme->parts.folderIconBig()->bitmapRef(0)); } } else { @@ -552,7 +552,7 @@ void FileList::paintItem(ui::Graphics* g, IFileItem* fi, const int i) tbounds.shrink(1); } - g->drawRgbaSurface(thumbnail, + g->drawRgbaSurface(thumbnail.get(), gfx::Rect(0, 0, thumbnail->width(), thumbnail->height()), tbounds); } @@ -574,7 +574,7 @@ gfx::Rect FileList::mainThumbnailBounds() if (!m_selected) return result; - os::Surface* thumbnail = m_selected->getThumbnail(); + os::SurfaceRef thumbnail = m_selected->getThumbnail(); if (!thumbnail) return result; diff --git a/src/app/ui/font_popup.cpp b/src/app/ui/font_popup.cpp index 96105bfcc..4bdf44457 100644 --- a/src/app/ui/font_popup.cpp +++ b/src/app/ui/font_popup.cpp @@ -71,15 +71,14 @@ private: if (m_image) { Graphics* g = ev.graphics(); - os::Surface* sur = os::instance()->createRgbaSurface(m_image->width(), - m_image->height()); + os::SurfaceRef sur = os::instance()->makeRgbaSurface(m_image->width(), + m_image->height()); convert_image_to_surface( - m_image.get(), nullptr, sur, + m_image.get(), nullptr, sur.get(), 0, 0, 0, 0, m_image->width(), m_image->height()); - g->drawRgbaSurface(sur, textWidth()+4, 0); - sur->dispose(); + g->drawRgbaSurface(sur.get(), textWidth()+4, 0); } } diff --git a/src/app/ui/palette_view.cpp b/src/app/ui/palette_view.cpp index 67ec5a205..1c3f6946f 100644 --- a/src/app/ui/palette_view.cpp +++ b/src/app/ui/palette_view.cpp @@ -591,7 +591,7 @@ void PaletteView::onPaint(ui::PaintEvent& ev) gfx::Color neg = color_utils::blackandwhite_neg(negColor); os::Font* minifont = theme->getMiniFont(); const std::string text = base::convert_to(k); - g->setFont(minifont); + g->setFont(AddRef(minifont)); g->drawText(text, neg, gfx::ColorNone, gfx::Point(box2.x + box2.w/2 - minifont->textLength(text)/2, box2.y + box2.h/2 - minifont->height()/2)); diff --git a/src/app/ui/palettes_listbox.cpp b/src/app/ui/palettes_listbox.cpp index ca222efbf..e7ae354cc 100644 --- a/src/app/ui/palettes_listbox.cpp +++ b/src/app/ui/palettes_listbox.cpp @@ -149,7 +149,7 @@ void PalettesListBox::onResourceChange(Resource* resource) void PalettesListBox::onPaintResource(Graphics* g, gfx::Rect& bounds, Resource* resource) { doc::Palette* palette = static_cast(resource)->palette(); - auto tick = SkinTheme::instance()->parts.checkSelected()->bitmap(0); + os::Surface* tick = SkinTheme::instance()->parts.checkSelected()->bitmap(0); // Draw tick (to say "this palette matches the active sprite // palette"). diff --git a/src/app/ui/search_entry.cpp b/src/app/ui/search_entry.cpp index 6b93cfbac..1d6655376 100644 --- a/src/app/ui/search_entry.cpp +++ b/src/app/ui/search_entry.cpp @@ -55,7 +55,7 @@ void SearchEntry::onPaint(ui::PaintEvent& ev) SkinTheme* theme = static_cast(this->theme()); theme->paintEntry(ev); - auto icon = theme->parts.iconSearch()->bitmap(0); + os::Surface* icon = theme->parts.iconSearch()->bitmap(0); Rect bounds = clientBounds(); ev.graphics()->drawColoredRgbaSurface( icon, theme->colors.text(), diff --git a/src/app/ui/skin/font_data.cpp b/src/app/ui/skin/font_data.cpp index bf351502e..ac7ecd94b 100644 --- a/src/app/ui/skin/font_data.cpp +++ b/src/app/ui/skin/font_data.cpp @@ -28,29 +28,7 @@ FontData::FontData(os::FontType type) { } -FontData::~FontData() -{ -#if _DEBUG - static std::set deletedFonts; -#endif - - // Destroy all fonts - for (auto& it : m_fonts) { - os::Font* font = it.second; - if (font) { -#if _DEBUG // Check that there are not double-cached fonts - auto it2 = deletedFonts.find(font); - ASSERT(it2 == deletedFonts.end()); - deletedFonts.insert(font); -#endif - // Don't delete font->fallback() as it's already m_fonts - font->dispose(); - } - } - m_fonts.clear(); -} - -os::Font* FontData::getFont(int size) +os::FontRef FontData::getFont(int size) { if (m_type == os::FontType::SpriteSheet) size = 1; // Same size always @@ -61,7 +39,7 @@ os::Font* FontData::getFont(int size) if (it != m_fonts.end()) return it->second; - os::Font* font = nullptr; + os::FontRef font = nullptr; switch (m_type) { case os::FontType::SpriteSheet: @@ -76,9 +54,9 @@ os::Font* FontData::getFont(int size) } if (m_fallback) { - os::Font* fallback = m_fallback->getFont(m_fallbackSize); + os::FontRef fallback = m_fallback->getFont(m_fallbackSize); if (font) - font->setFallback(fallback); + font->setFallback(fallback.get()); else return fallback; // Don't double-cache the fallback font } diff --git a/src/app/ui/skin/font_data.h b/src/app/ui/skin/font_data.h index ba4e954e5..3980e7f8a 100644 --- a/src/app/ui/skin/font_data.h +++ b/src/app/ui/skin/font_data.h @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2017 David Capello // // This program is distributed under the terms of @@ -19,7 +20,6 @@ namespace skin { class FontData { public: FontData(os::FontType type); - ~FontData(); void setFilename(const std::string& filename) { m_filename = filename; } void setAntialias(bool antialias) { m_antialias = antialias; } @@ -28,13 +28,13 @@ namespace skin { m_fallbackSize = fallbackSize; } - os::Font* getFont(int size); + os::FontRef getFont(int size); private: os::FontType m_type; std::string m_filename; bool m_antialias; - std::map m_fonts; // key=font size, value=real font + std::map m_fonts; // key=font size, value=real font FontData* m_fallback; int m_fallbackSize; diff --git a/src/app/ui/skin/skin_part.cpp b/src/app/ui/skin/skin_part.cpp index 887531c6f..c1e567ab5 100644 --- a/src/app/ui/skin/skin_part.cpp +++ b/src/app/ui/skin/skin_part.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This program is distributed under the terms of @@ -26,15 +27,10 @@ SkinPart::~SkinPart() void SkinPart::clear() { - for (auto& bitmap : m_bitmaps) { - if (bitmap) { - bitmap->dispose(); - bitmap = nullptr; - } - } + m_bitmaps.clear(); } -void SkinPart::setBitmap(std::size_t index, os::Surface* bitmap) +void SkinPart::setBitmap(std::size_t index, const os::SurfaceRef& bitmap) { if (index >= m_bitmaps.size()) m_bitmaps.resize(index+1, nullptr); diff --git a/src/app/ui/skin/skin_part.h b/src/app/ui/skin/skin_part.h index 483369251..d9bd20b90 100644 --- a/src/app/ui/skin/skin_part.h +++ b/src/app/ui/skin/skin_part.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This program is distributed under the terms of @@ -11,6 +11,7 @@ #include "gfx/rect.h" #include "gfx/size.h" +#include "os/surface_list.h" #include #include @@ -24,7 +25,7 @@ namespace app { class SkinPart { public: - typedef std::vector Bitmaps; + using Bitmaps = os::SurfaceList; SkinPart(); ~SkinPart(); @@ -35,12 +36,18 @@ namespace app { void clear(); // It doesn't destroy the previous bitmap in the given "index". - void setBitmap(std::size_t index, os::Surface* bitmap); + void setBitmap(std::size_t index, const os::SurfaceRef& bitmap); void setSpriteBounds(const gfx::Rect& bounds); void setSlicesBounds(const gfx::Rect& bounds); os::Surface* bitmap(std::size_t index) const { - return (index < m_bitmaps.size() ? m_bitmaps[index]: nullptr); + return (index < m_bitmaps.size() ? const_cast(this)->m_bitmaps[index].get(): nullptr); + } + + os::SurfaceRef bitmapRef(std::size_t index) { + return (index < m_bitmaps.size() ? + const_cast(this)->m_bitmaps[index]: + nullptr); } os::Surface* bitmapNW() const { return bitmap(0); } diff --git a/src/app/ui/skin/skin_theme.cpp b/src/app/ui/skin/skin_theme.cpp index 21ee60721..157ff9578 100644 --- a/src/app/ui/skin/skin_theme.cpp +++ b/src/app/ui/skin/skin_theme.cpp @@ -76,7 +76,7 @@ struct app::skin::SkinTheme::BackwardCompatibility { const int h = font->height(); style.setId(theme->styles.slider()->id()); - style.setFont(font); + style.setFont(AddRef(font)); auto part = theme->parts.sliderEmpty(); style.setBorder( @@ -236,9 +236,7 @@ SkinTheme::~SkinTheme() for (auto& it : m_cursors) delete it.second; // Delete cursor - if (m_sheet) - m_sheet->dispose(); - + m_sheet.reset(); m_parts_by_id.clear(); // Destroy fonts @@ -320,7 +318,7 @@ void SkinTheme::loadSheet() { // Load the skin sheet std::string sheet_filename(base::join_path(m_path, "sheet.png")); - os::Surface* newSheet = nullptr; + os::SurfaceRef newSheet; try { newSheet = os::instance()->loadRgbaSurface(sheet_filename.c_str()); } @@ -332,10 +330,8 @@ void SkinTheme::loadSheet() throw base::Exception("Error loading %s file", sheet_filename.c_str()); // Replace the sprite sheet - if (m_sheet) { - m_sheet->dispose(); - m_sheet = nullptr; - } + if (m_sheet) + m_sheet.reset(); m_sheet = newSheet; if (m_sheet) m_sheet->applyScale(guiscale()); @@ -395,7 +391,7 @@ void SkinTheme::loadXml(BackwardCompatibility* backward) if (sizeStr) size = std::strtol(sizeStr, nullptr, 10); - os::Font* font = fontData->getFont(size); + os::FontRef font = fontData->getFont(size); m_themeFonts[idStr] = font; if (id == "default") @@ -474,7 +470,7 @@ void SkinTheme::loadXml(BackwardCompatibility* backward) if (w > 0 && h > 0) { part->setSpriteBounds(gfx::Rect(x, y, w, h)); - part->setBitmap(0, sliceSheet(part->bitmap(0), gfx::Rect(x, y, w, h))); + part->setBitmap(0, sliceSheet(part->bitmapRef(0), gfx::Rect(x, y, w, h))); } else if (xmlPart->Attribute("w1")) { // 3x3-1 part (NW, N, NE, E, SE, S, SW, W) int w1 = scale*strtol(xmlPart->Attribute("w1"), nullptr, 10); @@ -487,14 +483,14 @@ void SkinTheme::loadXml(BackwardCompatibility* backward) part->setSpriteBounds(gfx::Rect(x, y, w1+w2+w3, h1+h2+h3)); part->setSlicesBounds(gfx::Rect(w1, h1, w2, h2)); - part->setBitmap(0, sliceSheet(part->bitmap(0), gfx::Rect(x, y, w1, h1))); // NW - part->setBitmap(1, sliceSheet(part->bitmap(1), gfx::Rect(x+w1, y, w2, h1))); // N - part->setBitmap(2, sliceSheet(part->bitmap(2), gfx::Rect(x+w1+w2, y, w3, h1))); // NE - part->setBitmap(3, sliceSheet(part->bitmap(3), gfx::Rect(x+w1+w2, y+h1, w3, h2))); // E - part->setBitmap(4, sliceSheet(part->bitmap(4), gfx::Rect(x+w1+w2, y+h1+h2, w3, h3))); // SE - part->setBitmap(5, sliceSheet(part->bitmap(5), gfx::Rect(x+w1, y+h1+h2, w2, h3))); // S - part->setBitmap(6, sliceSheet(part->bitmap(6), gfx::Rect(x, y+h1+h2, w1, h3))); // SW - part->setBitmap(7, sliceSheet(part->bitmap(7), gfx::Rect(x, y+h1, w1, h2))); // W + part->setBitmap(0, sliceSheet(part->bitmapRef(0), gfx::Rect(x, y, w1, h1))); // NW + part->setBitmap(1, sliceSheet(part->bitmapRef(1), gfx::Rect(x+w1, y, w2, h1))); // N + part->setBitmap(2, sliceSheet(part->bitmapRef(2), gfx::Rect(x+w1+w2, y, w3, h1))); // NE + part->setBitmap(3, sliceSheet(part->bitmapRef(3), gfx::Rect(x+w1+w2, y+h1, w3, h2))); // E + part->setBitmap(4, sliceSheet(part->bitmapRef(4), gfx::Rect(x+w1+w2, y+h1+h2, w3, h3))); // SE + part->setBitmap(5, sliceSheet(part->bitmapRef(5), gfx::Rect(x+w1, y+h1+h2, w2, h3))); // S + part->setBitmap(6, sliceSheet(part->bitmapRef(6), gfx::Rect(x, y+h1+h2, w1, h3))); // SW + part->setBitmap(7, sliceSheet(part->bitmapRef(7), gfx::Rect(x, y+h1, w1, h2))); // W } // Is it a mouse cursor? @@ -511,10 +507,8 @@ void SkinTheme::loadXml(BackwardCompatibility* backward) it->second = nullptr; } - // TODO share the Surface with the SkinPart - os::Surface* slice = sliceSheet(nullptr, gfx::Rect(x, y, w, h)); - Cursor* cursor = - new Cursor(slice, gfx::Point(focusx, focusy)); + os::SurfaceRef slice = sliceSheet(nullptr, gfx::Rect(x, y, w, h)); + Cursor* cursor = new Cursor(slice, gfx::Point(focusx, focusy)); m_cursors[cursorName] = cursor; for (int c=0; cAttribute("font"); if (fontId) { - os::Font* font = m_themeFonts[fontId]; + os::FontRef font = m_themeFonts[fontId]; style->setFont(font); } } @@ -706,7 +700,7 @@ void SkinTheme::loadXml(BackwardCompatibility* backward) SkinPartPtr part = it->second; if (part) { if (layer.type() == ui::Style::Layer::Type::kIcon) - layer.setIcon(part->bitmap(0)); + layer.setIcon(AddRef(part->bitmap(0))); else { layer.setSpriteSheet(m_sheet); layer.setSpriteBounds(part->spriteBounds()); @@ -743,21 +737,20 @@ void SkinTheme::loadXml(BackwardCompatibility* backward) ThemeFile::updateInternals(); } -os::Surface* SkinTheme::sliceSheet(os::Surface* sur, const gfx::Rect& bounds) +os::SurfaceRef SkinTheme::sliceSheet(os::SurfaceRef sur, const gfx::Rect& bounds) { if (sur && (sur->width() != bounds.w || sur->height() != bounds.h)) { - sur->dispose(); sur = nullptr; } if (!bounds.isEmpty()) { if (!sur) - sur = os::instance()->createRgbaSurface(bounds.w, bounds.h); + sur = os::instance()->makeRgbaSurface(bounds.w, bounds.h); - os::SurfaceLock lockSrc(m_sheet); - os::SurfaceLock lockDst(sur); - m_sheet->blitTo(sur, bounds.x, bounds.y, 0, 0, bounds.w, bounds.h); + os::SurfaceLock lockSrc(m_sheet.get()); + os::SurfaceLock lockDst(sur.get()); + m_sheet->blitTo(sur.get(), bounds.x, bounds.y, 0, 0, bounds.w, bounds.h); } else { ASSERT(!sur); @@ -1450,7 +1443,7 @@ void SkinTheme::drawText(Graphics* g, const char* t, if (t || widget->hasText()) { Rect textrc; - g->setFont(widget->font()); + g->setFont(AddRef(widget->font())); if (!t) t = widget->text().c_str(); @@ -1601,7 +1594,7 @@ void SkinTheme::drawRect(Graphics* g, const Rect& rc, void SkinTheme::drawRect(ui::Graphics* g, const gfx::Rect& rc, SkinPart* skinPart, const bool drawCenter) { - Theme::drawSlices(g, m_sheet, rc, + Theme::drawSlices(g, m_sheet.get(), rc, skinPart->spriteBounds(), skinPart->slicesBounds(), gfx::ColorNone, diff --git a/src/app/ui/skin/skin_theme.h b/src/app/ui/skin/skin_theme.h index 83ef78f5d..ea25e1645 100644 --- a/src/app/ui/skin/skin_theme.h +++ b/src/app/ui/skin/skin_theme.h @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This program is distributed under the terms of @@ -26,10 +27,6 @@ namespace ui { class Graphics; } -namespace os { - class Surface; -} - namespace app { namespace skin { @@ -51,9 +48,9 @@ namespace app { int preferredScreenScaling() { return m_preferredScreenScaling; } int preferredUIScaling() { return m_preferredUIScaling; } - os::Font* getDefaultFont() const override { return m_defaultFont; } + os::Font* getDefaultFont() const override { return m_defaultFont.get(); } os::Font* getWidgetFont(const ui::Widget* widget) const override; - os::Font* getMiniFont() const { return m_miniFont; } + os::Font* getMiniFont() const { return m_miniFont.get(); } ui::Cursor* getStandardCursor(ui::CursorType type) override; void initWidget(ui::Widget* widget) override; @@ -138,7 +135,7 @@ namespace app { void loadSheet(); void loadXml(BackwardCompatibility* backward); - os::Surface* sliceSheet(os::Surface* sur, const gfx::Rect& bounds); + os::SurfaceRef sliceSheet(os::SurfaceRef sur, const gfx::Rect& bounds); gfx::Color getWidgetBgColor(ui::Widget* widget); void drawText(ui::Graphics* g, const char* t, @@ -153,7 +150,7 @@ namespace app { std::string findThemePath(const std::string& themeId) const; std::string m_path; - os::Surface* m_sheet; + os::SurfaceRef m_sheet; std::map m_parts_by_id; std::map m_colors_by_id; std::map m_dimensions_by_id; @@ -161,9 +158,9 @@ namespace app { std::vector m_standardCursors; std::map m_styles; std::map m_fonts; - std::map m_themeFonts; - os::Font* m_defaultFont; - os::Font* m_miniFont; + std::map m_themeFonts; + os::FontRef m_defaultFont; + os::FontRef m_miniFont; int m_preferredScreenScaling; int m_preferredUIScaling; }; diff --git a/src/app/ui/tabs.cpp b/src/app/ui/tabs.cpp index 1e1a1cc39..26e793b9d 100644 --- a/src/app/ui/tabs.cpp +++ b/src/app/ui/tabs.cpp @@ -931,12 +931,12 @@ void Tabs::createFloatingOverlay(Tab* tab) { ASSERT(!m_floatingOverlay); - os::Surface* surface = os::instance()->createRgbaSurface( + os::SurfaceRef surface = os::instance()->makeRgbaSurface( tab->width, m_tabsHeight); // Fill the surface with pink color { - os::SurfaceLock lock(surface); + os::SurfaceLock lock(surface.get()); os::Paint paint; paint.color(gfx::rgba(0, 0, 0, 0)); paint.style(os::Paint::Fill); @@ -944,12 +944,14 @@ void Tabs::createFloatingOverlay(Tab* tab) } { Graphics g(surface, 0, 0); - g.setFont(font()); + g.setFont(AddRef(font())); drawTab(&g, g.getClipBounds(), tab, 0, true, true); } - m_floatingOverlay.reset(new Overlay(surface, gfx::Point(), Overlay::MouseZOrder-1)); - OverlayManager::instance()->addOverlay(m_floatingOverlay.get()); + m_floatingOverlay = base::make_ref( + surface, gfx::Point(), + (ui::Overlay::ZOrder)(Overlay::MouseZOrder-1)); + OverlayManager::instance()->addOverlay(m_floatingOverlay); } void Tabs::destroyFloatingTab() @@ -974,7 +976,7 @@ void Tabs::destroyFloatingTab() void Tabs::destroyFloatingOverlay() { if (m_floatingOverlay) { - OverlayManager::instance()->removeOverlay(m_floatingOverlay.get()); + OverlayManager::instance()->removeOverlay(m_floatingOverlay); m_floatingOverlay.reset(); } } diff --git a/src/app/ui/tabs.h b/src/app/ui/tabs.h index 871a55af4..4ca1bec5c 100644 --- a/src/app/ui/tabs.h +++ b/src/app/ui/tabs.h @@ -9,6 +9,7 @@ #define APP_UI_TABS_H_INCLUDED #pragma once +#include "base/ref.h" #include "ui/animated_widget.h" #include "ui/timer.h" #include "ui/widget.h" @@ -286,7 +287,7 @@ namespace app { // (this overlay floats next to the mouse cursor). It's destroyed // and recreated every time the tab is put inside or outside the // Tabs widget. - std::unique_ptr m_floatingOverlay; + base::Ref m_floatingOverlay; // Relative mouse position inside the m_dragTab (used to adjust // the m_floatingOverlay precisely). diff --git a/src/app/ui/timeline/timeline.cpp b/src/app/ui/timeline/timeline.cpp index 16e253ec8..f11fe6837 100644 --- a/src/app/ui/timeline/timeline.cpp +++ b/src/app/ui/timeline/timeline.cpp @@ -2247,14 +2247,13 @@ void Timeline::drawCel(ui::Graphics* g, layer_t layerIndex, frame_t frame, Cel* skinTheme()->calcBorder(this, style)); if (!thumb_bounds.isEmpty()) { - if (os::Surface* surface = thumb::get_cel_thumbnail(cel, thumb_bounds.size())) { + if (os::SurfaceRef surface = thumb::get_cel_thumbnail(cel, thumb_bounds.size())) { const int t = base::clamp(thumb_bounds.w/8, 4, 16); draw_checked_grid(g, thumb_bounds, gfx::Size(t, t), docPref()); - g->drawRgbaSurface(surface, + g->drawRgbaSurface(surface.get(), thumb_bounds.center().x-surface->width()/2, thumb_bounds.center().y-surface->height()/2); - surface->dispose(); } } } @@ -2337,14 +2336,13 @@ void Timeline::drawCelOverlay(ui::Graphics* g) gfx::Rect rc = m_sprite->bounds().fitIn( gfx::Rect(m_thumbnailsOverlayBounds).shrink(1)); - if (os::Surface* surface = thumb::get_cel_thumbnail(cel, rc.size())) { + if (os::SurfaceRef surface = thumb::get_cel_thumbnail(cel, rc.size())) { draw_checked_grid(g, rc, gfx::Size(8, 8)*ui::guiscale(), docPref()); - g->drawRgbaSurface(surface, + g->drawRgbaSurface(surface.get(), rc.center().x-surface->width()/2, rc.center().y-surface->height()/2); g->drawRect(gfx::rgba(0, 0, 0, 128), m_thumbnailsOverlayBounds); - surface->dispose(); } } diff --git a/src/app/widget_loader.cpp b/src/app/widget_loader.cpp index 3a3ccf5a2..59ead315d 100644 --- a/src/app/widget_loader.cpp +++ b/src/app/widget_loader.cpp @@ -495,8 +495,8 @@ Widget* WidgetLoader::convertXmlElementToWidget(const TiXmlElement* elem, Widget throw base::Exception("File %s not found", file); try { - os::Surface* sur = os::instance()->loadRgbaSurface(rf.filename().c_str()); - widget = new ImageView(sur, 0, true); + os::SurfaceRef sur = os::instance()->loadRgbaSurface(rf.filename().c_str()); + widget = new ImageView(sur, 0); } catch (...) { throw base::Exception("Error loading %s file", file); diff --git a/src/dio/LICENSE.txt b/src/dio/LICENSE.txt index a3236b421..a4da267a8 100644 --- a/src/dio/LICENSE.txt +++ b/src/dio/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (c) 2018 Igara Studio S.A. +Copyright (c) 2018-2020 Igara Studio S.A. Copyright (c) 2016-2018 David Capello Permission is hereby granted, free of charge, to any person obtaining diff --git a/src/dio/aseprite_decoder.cpp b/src/dio/aseprite_decoder.cpp index fceaa0a60..d8b3b8c34 100644 --- a/src/dio/aseprite_decoder.cpp +++ b/src/dio/aseprite_decoder.cpp @@ -1,5 +1,5 @@ // Aseprite Document IO Library -// Copyright (c) 2018-2019 Igara Studio S.A. +// Copyright (c) 2018-2020 Igara Studio S.A. // Copyright (c) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -757,7 +757,7 @@ void AsepriteDecoder::readColorProfile(doc::Sprite* sprite) readPadding(8); // Without color space, like old Aseprite versions - gfx::ColorSpacePtr cs(nullptr); + gfx::ColorSpaceRef cs(nullptr); switch (type) { diff --git a/src/doc/image.h b/src/doc/image.h index c95587f99..ff1ed89b6 100644 --- a/src/doc/image.h +++ b/src/doc/image.h @@ -1,5 +1,5 @@ // Aseprite Document Library -// Copyright (c) 2018 Igara Studio S.A. +// Copyright (c) 2018-2020 Igara Studio S.A. // Copyright (c) 2001-2016 David Capello // // This file is released under the terms of the MIT license. @@ -52,7 +52,7 @@ namespace doc { gfx::Rect bounds() const { return m_spec.bounds(); } color_t maskColor() const { return m_spec.maskColor(); } void setMaskColor(color_t c) { m_spec.setMaskColor(c); } - void setColorSpace(const gfx::ColorSpacePtr& cs) { m_spec.setColorSpace(cs); } + void setColorSpace(const gfx::ColorSpaceRef& cs) { m_spec.setColorSpace(cs); } virtual int getMemSize() const override; int getRowStrideSize() const; diff --git a/src/doc/image_spec.h b/src/doc/image_spec.h index c25be4082..a6a14bcb9 100644 --- a/src/doc/image_spec.h +++ b/src/doc/image_spec.h @@ -1,5 +1,5 @@ // Aseprite Document Library -// Copyright (c) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (c) 2016 David Capello // // This file is released under the terms of the MIT license. @@ -24,7 +24,7 @@ namespace doc { const int width, const int height, const color_t maskColor = 0, - const gfx::ColorSpacePtr& colorSpace = gfx::ColorSpace::MakeNone()) + const gfx::ColorSpaceRef& colorSpace = gfx::ColorSpace::MakeNone()) : m_colorMode(colorMode), m_size(width, height), m_maskColor(maskColor), @@ -38,7 +38,7 @@ namespace doc { int height() const { return m_size.h; } const gfx::Size& size() const { return m_size; } gfx::Rect bounds() const { return gfx::Rect(m_size); } - const gfx::ColorSpacePtr& colorSpace() const { return m_colorSpace; } + const gfx::ColorSpaceRef& colorSpace() const { return m_colorSpace; } // The transparent color for colored images (0 by default) or just 0 for RGBA and Grayscale color_t maskColor() const { return m_maskColor; } @@ -47,7 +47,7 @@ namespace doc { void setWidth(const int width) { m_size.w = width; } void setHeight(const int height) { m_size.h = height; } void setMaskColor(const color_t color) { m_maskColor = color; } - void setColorSpace(const gfx::ColorSpacePtr& cs) { m_colorSpace = cs; } + void setColorSpace(const gfx::ColorSpaceRef& cs) { m_colorSpace = cs; } void setSize(const int width, const int height) { @@ -74,7 +74,7 @@ namespace doc { ColorMode m_colorMode; gfx::Size m_size; color_t m_maskColor; - gfx::ColorSpacePtr m_colorSpace; + gfx::ColorSpaceRef m_colorSpace; }; } // namespace doc diff --git a/src/doc/sprite.cpp b/src/doc/sprite.cpp index b1572d3d1..bd8d5318e 100644 --- a/src/doc/sprite.cpp +++ b/src/doc/sprite.cpp @@ -169,7 +169,7 @@ void Sprite::setSize(int width, int height) m_spec.setSize(width, height); } -void Sprite::setColorSpace(const gfx::ColorSpacePtr& colorSpace) +void Sprite::setColorSpace(const gfx::ColorSpaceRef& colorSpace) { m_spec.setColorSpace(colorSpace); for (auto cel : uniqueCels()) diff --git a/src/doc/sprite.h b/src/doc/sprite.h index 2e63dee54..bb63df90e 100644 --- a/src/doc/sprite.h +++ b/src/doc/sprite.h @@ -84,12 +84,12 @@ namespace doc { gfx::Rect bounds() const { return m_spec.bounds(); } int width() const { return m_spec.width(); } int height() const { return m_spec.height(); } - const gfx::ColorSpacePtr& colorSpace() const { return m_spec.colorSpace(); } + const gfx::ColorSpaceRef& colorSpace() const { return m_spec.colorSpace(); } void setPixelFormat(PixelFormat format); void setPixelRatio(const PixelRatio& pixelRatio); void setSize(int width, int height); - void setColorSpace(const gfx::ColorSpacePtr& colorSpace); + void setColorSpace(const gfx::ColorSpaceRef& colorSpace); // Returns true if the sprite has a background layer and it's visible bool isOpaque() const; diff --git a/src/main/main.cpp b/src/main/main.cpp index e93966186..42a2a298a 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -19,7 +19,6 @@ #include "base/memory_dump.h" #include "base/system_console.h" #include "os/error.h" -#include "os/scoped_handle.h" #include "os/system.h" #include @@ -67,7 +66,7 @@ int app_main(int argc, char* argv[]) MemLeak memleak; base::SystemConsole systemConsole; app::AppOptions options(argc, const_cast(argv)); - os::ScopedHandle system(os::create_system()); + os::SystemRef system(os::make_system()); app::App app; // Change the memory dump filename to save on disk (.dmp diff --git a/src/ui/cursor.cpp b/src/ui/cursor.cpp index 46c641380..908636434 100644 --- a/src/ui/cursor.cpp +++ b/src/ui/cursor.cpp @@ -1,4 +1,5 @@ // Aseprite UI Library +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. @@ -15,16 +16,11 @@ namespace ui { -Cursor::Cursor(os::Surface* surface, const gfx::Point& focus) +Cursor::Cursor(const os::SurfaceRef& surface, const gfx::Point& focus) : m_surface(surface) , m_focus(focus) { ASSERT(m_surface != nullptr); } -Cursor::~Cursor() -{ - m_surface->dispose(); -} - } // namespace ui diff --git a/src/ui/cursor.h b/src/ui/cursor.h index 3d9b35f7a..e22434eb8 100644 --- a/src/ui/cursor.h +++ b/src/ui/cursor.h @@ -1,4 +1,5 @@ // Aseprite UI Library +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This file is released under the terms of the MIT license. @@ -9,6 +10,7 @@ #pragma once #include "gfx/point.h" +#include "os/surface.h" namespace os { class Surface; } @@ -16,15 +18,13 @@ namespace ui { class Cursor { public: - // The surface is disposed in ~Cursor. - Cursor(os::Surface* surface, const gfx::Point& focus); - ~Cursor(); + Cursor(const os::SurfaceRef& surface, const gfx::Point& focus); - os::Surface* getSurface() const { return m_surface; } + const os::SurfaceRef& getSurface() const { return m_surface; } const gfx::Point& getFocus() const { return m_focus; } private: - os::Surface* m_surface; + os::SurfaceRef m_surface; gfx::Point m_focus; }; diff --git a/src/ui/graphics.cpp b/src/ui/graphics.cpp index 920011c1f..fccb68d91 100644 --- a/src/ui/graphics.cpp +++ b/src/ui/graphics.cpp @@ -30,10 +30,11 @@ #include #include +#include namespace ui { -Graphics::Graphics(os::Surface* surface, int dx, int dy) +Graphics::Graphics(const os::SurfaceRef& surface, int dx, int dy) : m_surface(surface) , m_dx(dx) , m_dy(dy) @@ -44,7 +45,7 @@ Graphics::~Graphics() { // If we were drawing in the screen surface, we mark these regions // as dirty for the final flip. - if (m_surface == os::instance()->defaultDisplay()->getSurface()) + if (m_surface == os::instance()->defaultDisplay()->surface()) Manager::getDefault()->dirtyRect(m_dirtyBounds); } @@ -132,7 +133,7 @@ void Graphics::setDrawMode(DrawMode mode, int param, gfx::Color Graphics::getPixel(int x, int y) { - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); return m_surface->getPixel(m_dx+x, m_dy+y); } @@ -140,7 +141,7 @@ void Graphics::putPixel(gfx::Color color, int x, int y) { dirty(gfx::Rect(m_dx+x, m_dy+y, 1, 1)); - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); m_surface->putPixel(color, m_dx+x, m_dy+y); } @@ -148,7 +149,7 @@ void Graphics::drawHLine(gfx::Color color, int x, int y, int w) { dirty(gfx::Rect(m_dx+x, m_dy+y, w, 1)); - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); os::Paint paint; paint.color(color); m_surface->drawRect(gfx::Rect(m_dx+x, m_dy+y, w, 1), paint); @@ -158,7 +159,7 @@ void Graphics::drawVLine(gfx::Color color, int x, int y, int h) { dirty(gfx::Rect(m_dx+x, m_dy+y, 1, h)); - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); os::Paint paint; paint.color(color); m_surface->drawRect(gfx::Rect(m_dx+x, m_dy+y, 1, h), paint); @@ -170,7 +171,7 @@ void Graphics::drawLine(gfx::Color color, const gfx::Point& _a, const gfx::Point gfx::Point b(m_dx+_b.x, m_dy+_b.y); dirty(gfx::Rect(a, b)); - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); os::Paint paint; paint.color(color); m_surface->drawLine(a, b, paint); @@ -178,7 +179,7 @@ void Graphics::drawLine(gfx::Color color, const gfx::Point& _a, const gfx::Point void Graphics::drawPath(gfx::Path& path, const Paint& paint) { - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); auto m = matrix(); save(); @@ -197,7 +198,7 @@ void Graphics::drawRect(gfx::Color color, const gfx::Rect& rcOrig) rc.offset(m_dx, m_dy); dirty(rc); - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); os::Paint paint; paint.color(color); paint.style(os::Paint::Stroke); @@ -210,7 +211,7 @@ void Graphics::fillRect(gfx::Color color, const gfx::Rect& rcOrig) rc.offset(m_dx, m_dy); dirty(rc); - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); os::Paint paint; paint.color(color); paint.style(os::Paint::Fill); @@ -240,7 +241,7 @@ void Graphics::drawSurface(os::Surface* surface, int x, int y) dirty(gfx::Rect(m_dx+x, m_dy+y, surface->width(), surface->height())); os::SurfaceLock lockSrc(surface); - os::SurfaceLock lockDst(m_surface); + os::SurfaceLock lockDst(m_surface.get()); m_surface->drawSurface(surface, m_dx+x, m_dy+y); } @@ -252,7 +253,7 @@ void Graphics::drawSurface(os::Surface* surface, dstRect.w, dstRect.h)); os::SurfaceLock lockSrc(surface); - os::SurfaceLock lockDst(m_surface); + os::SurfaceLock lockDst(m_surface.get()); m_surface->drawSurface( surface, srcRect, @@ -264,7 +265,7 @@ void Graphics::drawRgbaSurface(os::Surface* surface, int x, int y) dirty(gfx::Rect(m_dx+x, m_dy+y, surface->width(), surface->height())); os::SurfaceLock lockSrc(surface); - os::SurfaceLock lockDst(m_surface); + os::SurfaceLock lockDst(m_surface.get()); m_surface->drawRgbaSurface(surface, m_dx+x, m_dy+y); } @@ -273,7 +274,7 @@ void Graphics::drawRgbaSurface(os::Surface* surface, int srcx, int srcy, int dst dirty(gfx::Rect(m_dx+dstx, m_dy+dsty, w, h)); os::SurfaceLock lockSrc(surface); - os::SurfaceLock lockDst(m_surface); + os::SurfaceLock lockDst(m_surface.get()); m_surface->drawRgbaSurface(surface, srcx, srcy, m_dx+dstx, m_dy+dsty, w, h); } @@ -285,7 +286,7 @@ void Graphics::drawRgbaSurface(os::Surface* surface, dstRect.w, dstRect.h)); os::SurfaceLock lockSrc(surface); - os::SurfaceLock lockDst(m_surface); + os::SurfaceLock lockDst(m_surface.get()); m_surface->drawRgbaSurface( surface, srcRect, @@ -297,7 +298,7 @@ void Graphics::drawColoredRgbaSurface(os::Surface* surface, gfx::Color color, in dirty(gfx::Rect(m_dx+x, m_dy+y, surface->width(), surface->height())); os::SurfaceLock lockSrc(surface); - os::SurfaceLock lockDst(m_surface); + os::SurfaceLock lockDst(m_surface.get()); m_surface->drawColoredRgbaSurface(surface, color, gfx::ColorNone, gfx::Clip(m_dx+x, m_dy+y, 0, 0, surface->width(), surface->height())); } @@ -308,7 +309,7 @@ void Graphics::drawColoredRgbaSurface(os::Surface* surface, gfx::Color color, dirty(gfx::Rect(m_dx+dstx, m_dy+dsty, w, h)); os::SurfaceLock lockSrc(surface); - os::SurfaceLock lockDst(m_surface); + os::SurfaceLock lockDst(m_surface.get()); m_surface->drawColoredRgbaSurface(surface, color, gfx::ColorNone, gfx::Clip(m_dx+dstx, m_dy+dsty, srcx, srcy, w, h)); } @@ -323,7 +324,7 @@ void Graphics::drawSurfaceNine(os::Surface* surface, dirty(displacedDst); os::SurfaceLock lockSrc(surface); - os::SurfaceLock lockDst(m_surface); + os::SurfaceLock lockDst(m_surface.get()); m_surface->drawSurfaceNine(surface, src, center, displacedDst, paint); } @@ -332,11 +333,11 @@ void Graphics::blit(os::Surface* srcSurface, int srcx, int srcy, int dstx, int d dirty(gfx::Rect(m_dx+dstx, m_dy+dsty, w, h)); os::SurfaceLock lockSrc(srcSurface); - os::SurfaceLock lockDst(m_surface); - srcSurface->blitTo(m_surface, srcx, srcy, m_dx+dstx, m_dy+dsty, w, h); + os::SurfaceLock lockDst(m_surface.get()); + srcSurface->blitTo(m_surface.get(), srcx, srcy, m_dx+dstx, m_dy+dsty, w, h); } -void Graphics::setFont(os::Font* font) +void Graphics::setFont(const os::FontRef& font) { m_font = font; } @@ -349,9 +350,9 @@ void Graphics::drawText(base::utf8_const_iterator it, { gfx::Point pt(m_dx+origPt.x, m_dy+origPt.y); - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); gfx::Rect textBounds = - os::draw_text(m_surface, m_font, it, end, fg, bg, pt.x, pt.y, delegate); + os::draw_text(m_surface.get(), m_font.get(), it, end, fg, bg, pt.x, pt.y, delegate); dirty(gfx::Rect(pt.x, pt.y, textBounds.w, textBounds.h)); } @@ -431,12 +432,12 @@ private: void Graphics::drawUIText(const std::string& str, gfx::Color fg, gfx::Color bg, const gfx::Point& pt, const int mnemonic) { - os::SurfaceLock lock(m_surface); + os::SurfaceLock lock(m_surface.get()); int x = m_dx+pt.x; int y = m_dy+pt.y; - DrawUITextDelegate delegate(m_surface, m_font, mnemonic); - os::draw_text(m_surface, m_font, + DrawUITextDelegate delegate(m_surface.get(), m_font.get(), mnemonic); + os::draw_text(m_surface.get(), m_font.get(), base::utf8_const_iterator(str.begin()), base::utf8_const_iterator(str.end()), fg, bg, x, y, &delegate); @@ -453,7 +454,7 @@ void Graphics::drawAlignedUIText(const std::string& str, gfx::Color fg, gfx::Col gfx::Size Graphics::measureUIText(const std::string& str) { return gfx::Size( - Graphics::measureUITextLength(str, m_font), + Graphics::measureUITextLength(str, m_font.get()), m_font->height()); } @@ -611,9 +612,9 @@ void Graphics::dirty(const gfx::Rect& bounds) // ScreenGraphics ScreenGraphics::ScreenGraphics() - : Graphics(os::instance()->defaultDisplay()->getSurface(), 0, 0) + : Graphics(AddRef(os::instance()->defaultDisplay()->surface()), 0, 0) { - setFont(get_theme()->getDefaultFont()); + setFont(AddRef(get_theme()->getDefaultFont())); } ScreenGraphics::~ScreenGraphics() diff --git a/src/ui/graphics.h b/src/ui/graphics.h index 0aa03c603..d0db15fb9 100644 --- a/src/ui/graphics.h +++ b/src/ui/graphics.h @@ -15,7 +15,9 @@ #include "gfx/point.h" #include "gfx/rect.h" #include "gfx/size.h" +#include "os/font.h" #include "os/paint.h" +#include "os/surface.h" #include #include @@ -28,8 +30,6 @@ namespace gfx { namespace os { class DrawTextDelegate; - class Font; - class Surface; } namespace ui { @@ -44,13 +44,13 @@ namespace ui { Checked, }; - Graphics(os::Surface* surface, int dx, int dy); + Graphics(const os::SurfaceRef& surface, int dx, int dy); ~Graphics(); int width() const; int height() const; - os::Surface* getInternalSurface() { return m_surface; } + os::Surface* getInternalSurface() { return m_surface.get(); } int getInternalDeltaX() { return m_dx; } int getInternalDeltaY() { return m_dy; } @@ -108,8 +108,8 @@ namespace ui { // FONT & TEXT // ====================================================================== - os::Font* font() { return m_font; } - void setFont(os::Font* font); + os::Font* font() { return m_font.get(); } + void setFont(const os::FontRef& font); void drawText(base::utf8_const_iterator it, const base::utf8_const_iterator& end, @@ -127,11 +127,11 @@ namespace ui { gfx::Size doUIStringAlgorithm(const std::string& str, gfx::Color fg, gfx::Color bg, const gfx::Rect& rc, int align, bool draw); void dirty(const gfx::Rect& bounds); - os::Surface* m_surface; + os::SurfaceRef m_surface; int m_dx; int m_dy; gfx::Rect m_clipBounds; - os::Font* m_font; + os::FontRef m_font; gfx::Rect m_dirtyBounds; }; diff --git a/src/ui/image_view.cpp b/src/ui/image_view.cpp index 11e5992fa..7618d9248 100644 --- a/src/ui/image_view.cpp +++ b/src/ui/image_view.cpp @@ -21,20 +21,13 @@ namespace ui { -ImageView::ImageView(os::Surface* sur, int align, bool dispose) +ImageView::ImageView(const os::SurfaceRef& sur, int align) : Widget(kImageViewWidget) , m_sur(sur) - , m_disposeSurface(dispose) { setAlign(align); } -ImageView::~ImageView() -{ - if (m_disposeSurface) - delete m_sur; -} - void ImageView::onSizeHint(SizeHintEvent& ev) { gfx::Rect box; @@ -57,7 +50,7 @@ void ImageView::onPaint(PaintEvent& ev) m_sur->width(), m_sur->height()); g->fillRect(bgColor(), bounds); - g->drawRgbaSurface(m_sur, icon.x, icon.y); + g->drawRgbaSurface(m_sur.get(), icon.x, icon.y); } } // namespace ui diff --git a/src/ui/image_view.h b/src/ui/image_view.h index 0f7a42a05..63d1cc7bd 100644 --- a/src/ui/image_view.h +++ b/src/ui/image_view.h @@ -8,6 +8,7 @@ #define UI_IMAGE_VIEW_H_INCLUDED #pragma once +#include "os/ref.h" #include "ui/widget.h" namespace os { @@ -18,16 +19,14 @@ namespace ui { class ImageView : public Widget { public: - ImageView(os::Surface* sur, int align, bool disposeSurface); - ~ImageView(); + ImageView(const os::Ref& sur, int align); protected: void onSizeHint(SizeHintEvent& ev) override; void onPaint(PaintEvent& ev) override; private: - os::Surface* m_sur; - bool m_disposeSurface; + os::Ref m_sur; }; } // namespace ui diff --git a/src/ui/manager.cpp b/src/ui/manager.cpp index d728050a7..8caa9d0f6 100644 --- a/src/ui/manager.cpp +++ b/src/ui/manager.cpp @@ -1491,7 +1491,7 @@ bool Manager::sendMessageToWidget(Message* msg, Widget* widget) // Restore overlays in the region that we're going to paint. OverlayManager::instance()->restoreOverlappedAreas(paintMsg->rect()); - os::Surface* surface = m_display->getSurface(); + os::Surface* surface = m_display->surface(); surface->saveClip(); if (surface->clipRect(paintMsg->rect())) { diff --git a/src/ui/move_region.cpp b/src/ui/move_region.cpp index 9ce9292a7..cf6972814 100644 --- a/src/ui/move_region.cpp +++ b/src/ui/move_region.cpp @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -34,7 +34,7 @@ void move_region(Manager* manager, const Region& region, int dx, int dy) bounds |= gfx::Rect(bounds).offset(dx, dy); overlays->restoreOverlappedAreas(bounds); - os::Surface* surface = display->getSurface(); + os::Surface* surface = display->surface(); os::SurfaceLock lock(surface); // Fast path, move one rectangle. diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index 7b91e3f00..18be98fb4 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -17,7 +17,9 @@ namespace ui { -Overlay::Overlay(os::Surface* overlaySurface, const gfx::Point& pos, ZOrder zorder) +Overlay::Overlay(const os::SurfaceRef& overlaySurface, + const gfx::Point& pos, + ZOrder zorder) : m_surface(overlaySurface) , m_overlap(nullptr) , m_captured(nullptr) @@ -36,16 +38,16 @@ Overlay::~Overlay() manager->invalidateRect(gfx::Rect(m_pos.x, m_pos.y, m_surface->width(), m_surface->height())); - m_surface->dispose(); + m_surface.reset(); } if (m_overlap) - m_overlap->dispose(); + m_overlap.reset(); } -os::Surface* Overlay::setSurface(os::Surface* newSurface) +os::SurfaceRef Overlay::setSurface(const os::SurfaceRef& newSurface) { - os::Surface* oldSurface = m_surface; + os::SurfaceRef oldSurface = m_surface; m_surface = newSurface; return oldSurface; } @@ -64,8 +66,8 @@ void Overlay::drawOverlay() !m_captured) return; - os::SurfaceLock lock(m_surface); - m_captured->drawRgbaSurface(m_surface, m_pos.x, m_pos.y); + os::SurfaceLock lock(m_surface.get()); + m_captured->drawRgbaSurface(m_surface.get(), m_pos.x, m_pos.y); Manager::getDefault()->dirtyRect( gfx::Rect(m_pos.x, m_pos.y, @@ -89,13 +91,13 @@ void Overlay::captureOverlappedArea(os::Surface* screen) if (!m_overlap) { // Use the same color space for the overlay as in the screen - m_overlap = os::instance()->createSurface(m_surface->width(), - m_surface->height(), - screen->colorSpace()); + m_overlap = os::instance()->makeSurface(m_surface->width(), + m_surface->height(), + screen->colorSpace()); } - os::SurfaceLock lock(m_overlap); - screen->blitTo(m_overlap, m_pos.x, m_pos.y, 0, 0, + os::SurfaceLock lock(m_overlap.get()); + screen->blitTo(m_overlap.get(), m_pos.x, m_pos.y, 0, 0, m_overlap->width(), m_overlap->height()); m_captured = screen; @@ -112,7 +114,7 @@ void Overlay::restoreOverlappedArea(const gfx::Rect& restoreBounds) !restoreBounds.intersects(bounds())) return; - os::SurfaceLock lock(m_overlap); + os::SurfaceLock lock(m_overlap.get()); m_overlap->blitTo(m_captured, 0, 0, m_pos.x, m_pos.y, m_overlap->width(), m_overlap->height()); diff --git a/src/ui/overlay.h b/src/ui/overlay.h index afd14710b..79ed040eb 100644 --- a/src/ui/overlay.h +++ b/src/ui/overlay.h @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. @@ -9,8 +9,10 @@ #define UI_OVERLAY_H_INCLUDED #pragma once +#include "base/ref.h" #include "gfx/fwd.h" #include "gfx/point.h" +#include "os/surface.h" #include "ui/base.h" namespace os { @@ -19,17 +21,22 @@ namespace os { namespace ui { - class Overlay { + class Overlay; + using OverlayRef = base::Ref; + + class Overlay : public base::RefCountT { public: - typedef int ZOrder; + enum ZOrder { + NormalZOrder = 0, + MouseZOrder = 5000 + }; - static const ZOrder NormalZOrder = 0; - static const ZOrder MouseZOrder = 5000; - - Overlay(os::Surface* overlaySurface, const gfx::Point& pos, ZOrder zorder = 0); + Overlay(const os::SurfaceRef& overlaySurface, + const gfx::Point& pos, + ZOrder zorder = NormalZOrder); ~Overlay(); - os::Surface* setSurface(os::Surface* newSurface); + os::SurfaceRef setSurface(const os::SurfaceRef& newSurface); const gfx::Point& position() const { return m_pos; } gfx::Rect bounds() const; @@ -45,8 +52,8 @@ namespace ui { } private: - os::Surface* m_surface; - os::Surface* m_overlap; + os::SurfaceRef m_surface; + os::SurfaceRef m_overlap; // Surface where we captured the overlapped (m_overlap) // region. It's nullptr if the overlay wasn't drawn yet. diff --git a/src/ui/overlay_manager.cpp b/src/ui/overlay_manager.cpp index 132741d3c..bc87fc186 100644 --- a/src/ui/overlay_manager.cpp +++ b/src/ui/overlay_manager.cpp @@ -20,8 +20,8 @@ namespace ui { -static bool less_than(Overlay* x, Overlay* y) { - return *x < *y; +static bool zorder_less_than(const OverlayRef& a, const OverlayRef& b) { + return *a < *b; } OverlayManager* OverlayManager::m_singleton = nullptr; @@ -46,13 +46,13 @@ OverlayManager::~OverlayManager() { } -void OverlayManager::addOverlay(Overlay* overlay) +void OverlayManager::addOverlay(const OverlayRef& overlay) { - iterator it = std::lower_bound(begin(), end(), overlay, less_than); + iterator it = std::lower_bound(begin(), end(), overlay, zorder_less_than); m_overlays.insert(it, overlay); } -void OverlayManager::removeOverlay(Overlay* overlay) +void OverlayManager::removeOverlay(const OverlayRef& overlay) { if (overlay) overlay->restoreOverlappedArea(gfx::Rect()); @@ -73,7 +73,7 @@ void OverlayManager::restoreOverlappedAreas(const gfx::Rect& restoreBounds) if (!manager) return; - for (Overlay* overlay : *this) + for (auto& overlay : *this) overlay->restoreOverlappedArea(restoreBounds); } @@ -86,13 +86,13 @@ void OverlayManager::drawOverlays() if (!manager) return; - os::Surface* displaySurface = manager->getDisplay()->getSurface(); + os::Surface* displaySurface = manager->getDisplay()->surface(); os::SurfaceLock lock(displaySurface); - for (Overlay* overlay : *this) + for (auto& overlay : *this) overlay->captureOverlappedArea(displaySurface); - for (Overlay* overlay : *this) + for (auto& overlay : *this) overlay->drawOverlay(); } diff --git a/src/ui/overlay_manager.h b/src/ui/overlay_manager.h index bfeb73781..463754572 100644 --- a/src/ui/overlay_manager.h +++ b/src/ui/overlay_manager.h @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2015 David Capello // // This file is released under the terms of the MIT license. @@ -9,16 +9,17 @@ #define UI_OVERLAY_MANAGER_H_INCLUDED #pragma once +#include "base/ref.h" #include "gfx/rect.h" #include "ui/base.h" +#include "ui/overlay.h" + #include namespace os { class Surface; } namespace ui { - class Overlay; - class OverlayManager { friend class UISystem; // So it can call destroyInstance() from ~UISystem static OverlayManager* m_singleton; @@ -29,8 +30,8 @@ namespace ui { public: static OverlayManager* instance(); - void addOverlay(Overlay* overlay); - void removeOverlay(Overlay* overlay); + void addOverlay(const OverlayRef& overlay); + void removeOverlay(const OverlayRef& overlay); void drawOverlays(); void restoreOverlappedAreas(const gfx::Rect& bounds); @@ -38,7 +39,7 @@ namespace ui { private: static void destroyInstance(); - typedef std::vector OverlayList; + typedef std::vector OverlayList; typedef OverlayList::iterator iterator; iterator begin() { return m_overlays.begin(); } diff --git a/src/ui/style.cpp b/src/ui/style.cpp index 83abe1ce9..583922579 100644 --- a/src/ui/style.cpp +++ b/src/ui/style.cpp @@ -1,4 +1,5 @@ // Aseprite UI Library +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2017 David Capello // // This file is released under the terms of the MIT license. @@ -10,6 +11,8 @@ #include "ui/style.h" +#include "os/font.h" + namespace ui { // static @@ -29,6 +32,11 @@ Style::Style(const Style* base) m_layers = base->layers(); } +void Style::setFont(const os::Ref& font) +{ + m_font = font; +} + void Style::addLayer(const Layer& layer) { int i, j = int(m_layers.size()); diff --git a/src/ui/style.h b/src/ui/style.h index 980a0e71d..441488089 100644 --- a/src/ui/style.h +++ b/src/ui/style.h @@ -1,4 +1,5 @@ // Aseprite UI Library +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2017 David Capello // // This file is released under the terms of the MIT license. @@ -13,6 +14,8 @@ #include "gfx/point.h" #include "gfx/rect.h" #include "gfx/size.h" +#include "os/font.h" +#include "os/surface.h" #include "ui/base.h" #include @@ -62,8 +65,8 @@ namespace ui { int align() const { return m_align; } gfx::Color color() const { return m_color; } - os::Surface* icon() const { return m_icon; } - os::Surface* spriteSheet() const { return m_spriteSheet; } + os::Surface* icon() const { return m_icon.get(); } + os::Surface* spriteSheet() const { return m_spriteSheet.get(); } const gfx::Rect& spriteBounds() const { return m_spriteBounds; } const gfx::Rect& slicesBounds() const { return m_slicesBounds; } const gfx::Point& offset() const { return m_offset; } @@ -72,8 +75,8 @@ namespace ui { void setFlags(const int flags) { m_flags = flags; } void setAlign(const int align) { m_align = align; } void setColor(gfx::Color color) { m_color = color; } - void setIcon(os::Surface* icon) { m_icon = icon; } - void setSpriteSheet(os::Surface* spriteSheet) { m_spriteSheet = spriteSheet; } + void setIcon(const os::SurfaceRef& icon) { m_icon = icon; } + void setSpriteSheet(const os::SurfaceRef& spriteSheet) { m_spriteSheet = spriteSheet; } void setSpriteBounds(const gfx::Rect& bounds) { m_spriteBounds = bounds; } void setSlicesBounds(const gfx::Rect& bounds) { m_slicesBounds = bounds; } void setOffset(const gfx::Point& offset) { m_offset = offset; } @@ -83,8 +86,8 @@ namespace ui { int m_flags; int m_align; gfx::Color m_color; - os::Surface* m_icon; - os::Surface* m_spriteSheet; + os::SurfaceRef m_icon; + os::SurfaceRef m_spriteSheet; gfx::Rect m_spriteBounds; gfx::Rect m_slicesBounds; gfx::Point m_offset; @@ -100,7 +103,7 @@ namespace ui { const gfx::Border& margin() const { return m_margin; } const gfx::Border& border() const { return m_border; } const gfx::Border& padding() const { return m_padding; } - os::Font* font() const { return m_font; } + os::Font* font() const { return m_font.get(); } const Layers& layers() const { return m_layers; } Layers& layers() { return m_layers; } @@ -108,7 +111,7 @@ namespace ui { void setMargin(const gfx::Border& value) { m_margin = value; } void setBorder(const gfx::Border& value) { m_border = value; } void setPadding(const gfx::Border& value) { m_padding = value; } - void setFont(os::Font* font) { m_font = font; } + void setFont(const os::FontRef& font); void addLayer(const Layer& layer); private: @@ -118,7 +121,7 @@ namespace ui { gfx::Border m_margin; gfx::Border m_border; gfx::Border m_padding; - os::Font* m_font; + os::FontRef m_font; }; } // namespace ui diff --git a/src/ui/system.cpp b/src/ui/system.cpp index 2aa0da5f9..ffb6137ae 100644 --- a/src/ui/system.cpp +++ b/src/ui/system.cpp @@ -41,7 +41,7 @@ static CursorType mouse_cursor_type = kOutsideDisplay; static const Cursor* mouse_cursor_custom = nullptr; static const Cursor* mouse_cursor = nullptr; static os::Display* mouse_display = nullptr; -static Overlay* mouse_cursor_overlay = nullptr; +static OverlayRef mouse_cursor_overlay = nullptr; static bool use_native_mouse_cursor = true; static bool support_native_custom_cursor = false; @@ -58,7 +58,7 @@ static void update_mouse_overlay(const Cursor* cursor) if (mouse_cursor && mouse_scares == 0) { if (!mouse_cursor_overlay) { - mouse_cursor_overlay = new Overlay( + mouse_cursor_overlay = base::make_ref( mouse_cursor->getSurface(), get_mouse_position(), Overlay::MouseZOrder); @@ -73,8 +73,7 @@ static void update_mouse_overlay(const Cursor* cursor) else if (mouse_cursor_overlay) { OverlayManager::instance()->removeOverlay(mouse_cursor_overlay); mouse_cursor_overlay->setSurface(nullptr); - delete mouse_cursor_overlay; - mouse_cursor_overlay = nullptr; + mouse_cursor_overlay.reset(); } } @@ -88,7 +87,7 @@ static bool update_custom_native_cursor(const Cursor* cursor) if (cursor) { result = mouse_display->setNativeMouseCursor( // The surface is already scaled by guiscale() - cursor->getSurface(), + cursor->getSurface().get(), cursor->getFocus(), // We scale the cursor by the os::Display scale mouse_display->scale() * mouse_cursor_scale); diff --git a/src/ui/theme.cpp b/src/ui/theme.cpp index c5a1c4754..f447942c1 100644 --- a/src/ui/theme.cpp +++ b/src/ui/theme.cpp @@ -408,9 +408,9 @@ void Theme::paintLayer(Graphics* g, case Style::Layer::Type::kText: if (layer.color() != gfx::ColorNone) { - os::Font* oldFont = g->font(); + os::FontRef oldFont = AddRef(g->font()); if (style->font()) - g->setFont(style->font()); + g->setFont(AddRef(style->font())); if (layer.align() & WORDWRAP) { gfx::Rect textBounds = rc; @@ -546,7 +546,8 @@ void Theme::measureLayer(const Widget* widget, case Style::Layer::Type::kText: if (layer.color() != gfx::ColorNone) { - os::Font* font = (style->font() ? style->font(): widget->font()); + os::Font* font = (style->font() ? style->font(): + widget->font()); gfx::Size textSize(Graphics::measureUITextLength(widget->text(), font), font->height()); diff --git a/src/ui/widget.cpp b/src/ui/widget.cpp index 333f1d736..5a7ecc069 100644 --- a/src/ui/widget.cpp +++ b/src/ui/widget.cpp @@ -160,8 +160,8 @@ void Widget::setTextQuiet(const std::string& text) os::Font* Widget::font() const { if (!m_font && m_theme) - m_font = m_theme->getWidgetFont(this); - return m_font; + m_font = AddRef(m_theme->getWidgetFont(this)); + return m_font.get(); } void Widget::setBgColor(gfx::Color color) @@ -198,7 +198,7 @@ void Widget::setStyle(Style* style) m_border = m_theme->calcBorder(this, style); m_bgColor = m_theme->calcBgColor(this, style); if (style->font()) - m_font = style->font(); + m_font = AddRef(style->font()); } // =============================================================== @@ -1050,10 +1050,10 @@ void Widget::paint(Graphics* graphics, region.createIntersection(region, drawRegion); Graphics graphics2( - graphics->getInternalSurface(), + base::AddRef(graphics->getInternalSurface()), widget->bounds().x, widget->bounds().y); - graphics2.setFont(widget->font()); + graphics2.setFont(AddRef(widget->font())); for (Region::const_iterator it = region.begin(), @@ -1142,48 +1142,49 @@ void Widget::invalidateRegion(const Region& region) class DeleteGraphicsAndSurface { public: - DeleteGraphicsAndSurface(const gfx::Rect& clip, os::Surface* surface) + DeleteGraphicsAndSurface(const gfx::Rect& clip, + const os::SurfaceRef& surface) : m_pt(clip.origin()), m_surface(surface) { } void operator()(Graphics* graphics) { { - os::Surface* dst = os::instance()->defaultDisplay()->getSurface(); - os::SurfaceLock lockSrc(m_surface); + os::Surface* dst = os::instance()->defaultDisplay()->surface(); + os::SurfaceLock lockSrc(m_surface.get()); os::SurfaceLock lockDst(dst); m_surface->blitTo( dst, 0, 0, m_pt.x, m_pt.y, m_surface->width(), m_surface->height()); } - m_surface->dispose(); + m_surface.reset(); delete graphics; } private: gfx::Point m_pt; - os::Surface* m_surface; + os::SurfaceRef m_surface; }; GraphicsPtr Widget::getGraphics(const gfx::Rect& clip) { GraphicsPtr graphics; - os::Surface* surface; - os::Surface* defaultSurface = os::instance()->defaultDisplay()->getSurface(); + os::SurfaceRef surface; + os::Surface* defaultSurface = os::instance()->defaultDisplay()->surface(); // In case of double-buffering, we need to create the temporary // buffer only if the default surface is the screen. if (isDoubleBuffered() && defaultSurface->isDirectToScreen()) { - surface = os::instance()->createSurface(clip.w, clip.h); + surface = os::instance()->makeSurface(clip.w, clip.h); graphics.reset(new Graphics(surface, -clip.x, -clip.y), - DeleteGraphicsAndSurface(clip, surface)); + DeleteGraphicsAndSurface(clip, surface)); } // In other case, we can draw directly onto the screen. else { - surface = defaultSurface; + surface = AddRef(defaultSurface); graphics.reset(new Graphics(surface, bounds().x, bounds().y)); } - graphics->setFont(font()); + graphics->setFont(AddRef(font())); return graphics; } diff --git a/src/ui/widget.h b/src/ui/widget.h index 3b04a3c69..eadd36d42 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -16,6 +16,7 @@ #include "gfx/region.h" #include "gfx/size.h" #include "obs/signal.h" +#include "os/font.h" #include "ui/base.h" #include "ui/component.h" #include "ui/graphics.h" @@ -26,10 +27,6 @@ #define ASSERT_VALID_WIDGET(widget) ASSERT((widget) != nullptr) -namespace os { - class Font; -} - namespace ui { class InitThemeEvent; @@ -407,7 +404,7 @@ namespace ui { Theme* m_theme; // Widget's theme Style* m_style; std::string m_text; // Widget text - mutable os::Font* m_font; // Cached font returned by the theme + mutable os::FontRef m_font; // Cached font returned by the theme gfx::Color m_bgColor; // Background color gfx::Rect m_bounds; gfx::Region m_updateRegion; // Region to be redrawed.