Refactor laf-os memory handling

This commit is contained in:
David Capello 2020-07-07 19:06:48 -03:00
parent b7f1555eb3
commit db4504e816
83 changed files with 445 additions and 510 deletions

View File

@ -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<typename T> alias = orig<T>;`)
* 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 `<atomic>`, `<thread>`, `<mutex>`, and `<condition_variable>`
* 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

2
laf

@ -1 +1 @@
Subproject commit 04641545aab2dc5b26432279dacae2c3a7e781bc
Subproject commit 7e21fb53fb75c5dac11cb0e1adef8eef3e2722a8

View File

@ -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

View File

@ -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);
}
}

View File

@ -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<std::string, GroupInfo> 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;
};

View File

@ -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)

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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<os::ColorSpacePtr> colorSpaces;
std::vector<os::ColorSpaceRef> 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

View File

@ -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 <memory>
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<os::ColorSpaceConversion> m_conversion;
os::Ref<os::ColorSpaceConversion> 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

View File

@ -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<Image> m_render;
std::unique_ptr<Image> m_doublebuf;
os::ScopedHandle<os::Surface> m_doublesur;
os::SurfaceRef m_doublesur;
filters::TiledMode m_tiled;
};

View File

@ -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<os::ColorSpacePtr> m_colorSpaces;
std::vector<os::ColorSpaceRef> m_colorSpaces;
std::string m_templateTextForDisplayCS;
RgbMapAlgorithmSelector m_rgbmapAlgorithmSelector;
};

View File

@ -63,7 +63,7 @@ void SpritePropertiesCommand::onExecute(Context* context)
ColorButton* color_button = nullptr;
// List of available color profiles
std::vector<os::ColorSpacePtr> colorSpaces;
std::vector<os::ColorSpaceRef> 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;
}

View File

@ -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()) {

View File

@ -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<gfx::ColorSpace>(
auto colorSpace = base::make_ref<gfx::ColorSpace>(
type, flags, gamma, std::move(buf));
colorSpace->setName(name);
return colorSpace;

View File

@ -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()));

View File

@ -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();
}

View File

@ -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);
};

View File

@ -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) {

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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];

View File

@ -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;

View File

@ -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;
}
}

View File

@ -56,7 +56,7 @@ namespace app {
namespace {
class FileItem;
typedef std::map<std::string, FileItem*> FileItemMap;
using FileItemMap = std::map<std::string, FileItem*>;
// 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) {

View File

@ -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 <string>
#include <vector>
namespace os {
class Surface;
}
namespace app {
class IFileItem;
typedef std::vector<IFileItem*> FileItemList;
using FileItemList = std::vector<IFileItem*>;
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

View File

@ -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<os::ColorSpacePtr> colorSpaces;
std::vector<os::ColorSpaceRef> colorSpaces;
system->listColorSpaces(colorSpaces);
for (auto& cs : colorSpaces) {

View File

@ -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<doc::ImageSpec>(L, 1);
auto cs = get_obj<gfx::ColorSpace>(L, 2);
spec->setColorSpace(std::make_shared<gfx::ColorSpace>(*cs));
spec->setColorSpace(base::make_ref<gfx::ColorSpace>(*cs));
return 0;
}

View File

@ -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<gfx::ColorSpace>(L, 2);
Tx tx;
tx(new cmd::AssignColorProfile(
sprite, std::make_shared<gfx::ColorSpace>(*cs)));
sprite, base::make_ref<gfx::ColorSpace>(*cs)));
tx.commit();
return 1;
}
@ -305,7 +305,7 @@ int Sprite_convertColorSpace(lua_State* L)
auto cs = get_obj<gfx::ColorSpace>(L, 2);
Tx tx;
tx(new cmd::ConvertColorProfile(
sprite, std::make_shared<gfx::ColorSpace>(*cs)));
sprite, base::make_ref<gfx::ColorSpace>(*cs)));
tx.commit();
return 1;
}

View File

@ -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());
{

View File

@ -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;
}

View File

@ -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

View File

@ -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 <cstdarg>
#include <cstdio>
#include <cstring>
#include <string>
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()

View File

@ -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 <memory>
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<Native> m_native;
static Params s_contextParams;
};

View File

@ -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)

View File

@ -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();

View File

@ -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);
}
}

View File

@ -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<std::mutex> 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;

View File

@ -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;
};

View File

@ -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<ui::Overlay>(
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<ui::Overlay> m_floatingOverlay;
ui::OverlayRef m_floatingOverlay;
// Relative mouse position between the widget and the overlay.
gfx::Point m_floatingOffset;

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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<std::string>(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));

View File

@ -149,7 +149,7 @@ void PalettesListBox::onResourceChange(Resource* resource)
void PalettesListBox::onPaintResource(Graphics* g, gfx::Rect& bounds, Resource* resource)
{
doc::Palette* palette = static_cast<PaletteResource*>(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").

View File

@ -55,7 +55,7 @@ void SearchEntry::onPaint(ui::PaintEvent& ev)
SkinTheme* theme = static_cast<SkinTheme*>(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(),

View File

@ -28,29 +28,7 @@ FontData::FontData(os::FontType type)
{
}
FontData::~FontData()
{
#if _DEBUG
static std::set<os::Font*> 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
}

View File

@ -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<int, os::Font*> m_fonts; // key=font size, value=real font
std::map<int, os::FontRef> m_fonts; // key=font size, value=real font
FontData* m_fallback;
int m_fallbackSize;

View File

@ -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);

View File

@ -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 <memory>
#include <vector>
@ -24,7 +25,7 @@ namespace app {
class SkinPart {
public:
typedef std::vector<os::Surface*> 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<SkinPart*>(this)->m_bitmaps[index].get(): nullptr);
}
os::SurfaceRef bitmapRef(std::size_t index) {
return (index < m_bitmaps.size() ?
const_cast<SkinPart*>(this)->m_bitmaps[index]:
nullptr);
}
os::Surface* bitmapNW() const { return bitmap(0); }

View File

@ -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; c<kCursorTypes; ++c) {
@ -612,7 +606,7 @@ void SkinTheme::loadXml(BackwardCompatibility* backward)
{
const char* fontId = xmlStyle->Attribute("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<SkinTheme>::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,

View File

@ -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<std::string, SkinPartPtr> m_parts_by_id;
std::map<std::string, gfx::Color> m_colors_by_id;
std::map<std::string, int> m_dimensions_by_id;
@ -161,9 +158,9 @@ namespace app {
std::vector<ui::Cursor*> m_standardCursors;
std::map<std::string, ui::Style*> m_styles;
std::map<std::string, FontData*> m_fonts;
std::map<std::string, os::Font*> m_themeFonts;
os::Font* m_defaultFont;
os::Font* m_miniFont;
std::map<std::string, os::FontRef> m_themeFonts;
os::FontRef m_defaultFont;
os::FontRef m_miniFont;
int m_preferredScreenScaling;
int m_preferredUIScaling;
};

View File

@ -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<ui::Overlay>(
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();
}
}

View File

@ -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<ui::Overlay> m_floatingOverlay;
base::Ref<ui::Overlay> m_floatingOverlay;
// Relative mouse position inside the m_dragTab (used to adjust
// the m_floatingOverlay precisely).

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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

View File

@ -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) {

View File

@ -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;

View File

@ -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

View File

@ -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())

View File

@ -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;

View File

@ -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 <clocale>
@ -67,7 +66,7 @@ int app_main(int argc, char* argv[])
MemLeak memleak;
base::SystemConsole systemConsole;
app::AppOptions options(argc, const_cast<const char**>(argv));
os::ScopedHandle<os::System> system(os::create_system());
os::SystemRef system(os::make_system());
app::App app;
// Change the memory dump filename to save on disk (.dmp

View File

@ -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

View File

@ -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;
};

View File

@ -30,10 +30,11 @@
#include <algorithm>
#include <cctype>
#include <string>
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()

View File

@ -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 <memory>
#include <string>
@ -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;
};

View File

@ -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

View File

@ -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<os::Surface>& sur, int align);
protected:
void onSizeHint(SizeHintEvent& ev) override;
void onPaint(PaintEvent& ev) override;
private:
os::Surface* m_sur;
bool m_disposeSurface;
os::Ref<os::Surface> m_sur;
};
} // namespace ui

View File

@ -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())) {

View File

@ -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.

View File

@ -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());

View File

@ -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<Overlay>;
class Overlay : public base::RefCountT<Overlay> {
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.

View File

@ -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();
}

View File

@ -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 <vector>
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<Overlay*> OverlayList;
typedef std::vector<OverlayRef> OverlayList;
typedef OverlayList::iterator iterator;
iterator begin() { return m_overlays.begin(); }

View File

@ -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<os::Font>& font)
{
m_font = font;
}
void Style::addLayer(const Layer& layer)
{
int i, j = int(m_layers.size());

View File

@ -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 <string>
@ -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

View File

@ -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<Overlay>(
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);

View File

@ -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());

View File

@ -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;
}

View File

@ -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.