Cache trimmed bounds calculation on DocExporter

The cache of the whole sprite trimmed bounds makes sense only in the
UI preview mode.
This commit is contained in:
David Capello 2019-10-23 15:55:08 -03:00
parent c228b36699
commit d6efddee92
4 changed files with 49 additions and 4 deletions

View File

@ -561,6 +561,7 @@ DocExporter::DocExporter()
: m_docBuf(std::make_shared<doc::ImageBuffer>()) : m_docBuf(std::make_shared<doc::ImageBuffer>())
, m_sampleBuf(std::make_shared<doc::ImageBuffer>()) , m_sampleBuf(std::make_shared<doc::ImageBuffer>())
{ {
m_cache.spriteId = doc::NullId;
reset(); reset();
} }
@ -833,8 +834,23 @@ void DocExporter::captureSamples(Samples& samples,
} }
gfx::Rect spriteBounds = sprite->bounds(); gfx::Rect spriteBounds = sprite->bounds();
if (m_trimSprite) if (m_trimSprite) {
spriteBounds = get_trimmed_bounds(sprite, m_trimByGrid); if (m_cache.spriteId == sprite->id() &&
m_cache.spriteVer == sprite->version() &&
m_cache.trimmedByGrid == m_trimByGrid) {
spriteBounds = m_cache.trimmedBounds;
}
else {
spriteBounds = get_trimmed_bounds(sprite, m_trimByGrid);
// Cache trimmed bounds so we don't have to recalculate them
// in the next iteration/preview.
m_cache.spriteId = sprite->id();
m_cache.spriteVer = sprite->version();
m_cache.trimmedByGrid = m_trimByGrid;
m_cache.trimmedBounds = spriteBounds;
}
}
frame_t outputFrame = 0; frame_t outputFrame = 0;
for (frame_t frame : item.getSelectedFrames()) { for (frame_t frame : item.getSelectedFrames()) {

View File

@ -16,7 +16,9 @@
#include "doc/frame.h" #include "doc/frame.h"
#include "doc/image_buffer.h" #include "doc/image_buffer.h"
#include "doc/object_id.h" #include "doc/object_id.h"
#include "doc/object_version.h"
#include "gfx/fwd.h" #include "gfx/fwd.h"
#include "gfx/rect.h"
#include <iosfwd> #include <iosfwd>
#include <map> #include <map>
@ -167,6 +169,15 @@ namespace app {
doc::ImageBufferPtr m_docBuf; doc::ImageBufferPtr m_docBuf;
doc::ImageBufferPtr m_sampleBuf; doc::ImageBufferPtr m_sampleBuf;
// Trimmed bounds of a specific sprite (to avoid recalculating
// this)
struct Cache {
doc::ObjectId spriteId;
doc::ObjectVersion spriteVer;
gfx::Rect trimmedBounds;
bool trimmedByGrid;
} m_cache;
DISABLE_COPYING(DocExporter); DISABLE_COPYING(DocExporter);
}; };

View File

@ -10,11 +10,10 @@
#include "doc/object_id.h" #include "doc/object_id.h"
#include "doc/object_type.h" #include "doc/object_type.h"
#include "doc/object_version.h"
namespace doc { namespace doc {
typedef uint32_t ObjectVersion;
class Object { class Object {
public: public:
Object(ObjectType type); Object(ObjectType type);

19
src/doc/object_version.h Normal file
View File

@ -0,0 +1,19 @@
// Aseprite Document Library
// Copyright (C) 2019 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef DOC_OBJECT_VERSION_H_INCLUDED
#define DOC_OBJECT_VERSION_H_INCLUDED
#pragma once
#include "base/ints.h"
namespace doc {
typedef uint32_t ObjectVersion;
} // namespace doc
#endif