diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2015-11-24 11:15:15 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2015-11-24 11:15:15 +0300 |
commit | 31cc60e76bfa81783e61a7b01586de9547de0174 (patch) | |
tree | cd5406cef65829caf1fae8e1aecf355034e53549 /source/gameengine/Rasterizer | |
parent | c026aa6d5473ca0d103a8f92e35e341402a571bf (diff) |
BGE: Save screenshots in a different thread
This patch allows the game engine to keep running while performing things like PNG compression and disk I/O.
As an example, my crowd simulation rasterizer saves a screenshot for every frame. This now takes up 13 msec per frame, which was 31 msec before this patch. Effectively, it allows the simulation to save every frame and still run at 60 FPS.
Reviewers: lordloki, moguri, panzergame
Reviewed By: moguri, panzergame
Projects: #game_engine
Differential Revision: https://developer.blender.org/D1507
Diffstat (limited to 'source/gameengine/Rasterizer')
-rw-r--r-- | source/gameengine/Rasterizer/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/gameengine/Rasterizer/RAS_ICanvas.cpp | 128 | ||||
-rw-r--r-- | source/gameengine/Rasterizer/RAS_ICanvas.h | 25 | ||||
-rw-r--r-- | source/gameengine/Rasterizer/SConscript | 1 |
4 files changed, 152 insertions, 4 deletions
diff --git a/source/gameengine/Rasterizer/CMakeLists.txt b/source/gameengine/Rasterizer/CMakeLists.txt index 5bc3f22e327..496a864244b 100644 --- a/source/gameengine/Rasterizer/CMakeLists.txt +++ b/source/gameengine/Rasterizer/CMakeLists.txt @@ -31,6 +31,7 @@ set(INC ../../blender/makesdna ../../blender/blenlib ../../blender/blenkernel + ../../blender/imbuf ../../../intern/container ../../../intern/glew-mx ../../../intern/guardedalloc @@ -53,6 +54,7 @@ set(SRC RAS_Polygon.cpp RAS_TexVert.cpp RAS_texmatrix.cpp + RAS_ICanvas.cpp RAS_2DFilterManager.h RAS_BucketManager.h diff --git a/source/gameengine/Rasterizer/RAS_ICanvas.cpp b/source/gameengine/Rasterizer/RAS_ICanvas.cpp new file mode 100644 index 00000000000..808d257f8f0 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_ICanvas.cpp @@ -0,0 +1,128 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file gameengine/Rasterizer/RAS_ICanvas.cpp + * \ingroup bgerast + */ + +#include "RAS_ICanvas.h" +#include "DNA_scene_types.h" + +#include "BKE_image.h" +#include "BKE_global.h" +#include "BKE_main.h" + +#include "BLI_task.h" +#include "BLI_path_util.h" +#include "BLI_string.h" + +#include "MEM_guardedalloc.h" + +extern "C" { +#include "IMB_imbuf.h" +#include "IMB_imbuf_types.h" +} + + +// Task data for saving screenshots in a different thread. +struct ScreenshotTaskData +{ + unsigned int *dumprect; + int dumpsx; + int dumpsy; + char *path; + ImageFormatData *im_format; +}; + +/** + * Function that actually performs the image compression and saving to disk of a screenshot. + * Run in a separate thread by RAS_ICanvas::save_screenshot(). + * + * @param taskdata Must point to a ScreenshotTaskData object. This function takes ownership + * of all pointers in the ScreenshotTaskData, and frees them. + */ +void save_screenshot_thread_func(TaskPool *__restrict pool, void *taskdata, int threadid); + + +RAS_ICanvas::RAS_ICanvas() +{ + m_taskscheduler = BLI_task_scheduler_create(TASK_SCHEDULER_AUTO_THREADS); + m_taskpool = BLI_task_pool_create(m_taskscheduler, NULL); +} + +RAS_ICanvas::~RAS_ICanvas() +{ + if (m_taskpool) { + BLI_task_pool_work_and_wait(m_taskpool); + BLI_task_pool_free(m_taskpool); + m_taskpool = NULL; + } + + if (m_taskscheduler) { + BLI_task_scheduler_free(m_taskscheduler); + m_taskscheduler = NULL; + } +} + + +void save_screenshot_thread_func(TaskPool *__restrict UNUSED(pool), void *taskdata, int UNUSED(threadid)) +{ + ScreenshotTaskData *task = static_cast<ScreenshotTaskData *>(taskdata); + + /* create and save imbuf */ + ImBuf *ibuf = IMB_allocImBuf(task->dumpsx, task->dumpsy, 24, 0); + ibuf->rect = task->dumprect; + + BKE_imbuf_write_as(ibuf, task->path, task->im_format, false); + + ibuf->rect = NULL; + IMB_freeImBuf(ibuf); + MEM_freeN(task->dumprect); + MEM_freeN(task->path); + MEM_freeN(task->im_format); +} + + +void RAS_ICanvas::save_screenshot(const char *filename, int dumpsx, int dumpsy, unsigned int *dumprect, + ImageFormatData * im_format) +{ + /* create file path */ + char *path = (char *)MEM_mallocN(FILE_MAX, "screenshot-path"); + BLI_strncpy(path, filename, FILE_MAX); + BLI_path_abs(path, G.main->name); + BLI_path_frame(path, m_frame, 0); + m_frame++; + BKE_image_path_ensure_ext_from_imtype(path, im_format->imtype); + + /* Save the actual file in a different thread, so that the + * game engine can keep running at full speed. */ + ScreenshotTaskData *task = (ScreenshotTaskData *)MEM_mallocN(sizeof(ScreenshotTaskData), "screenshot-data"); + task->dumprect = dumprect; + task->dumpsx = dumpsx; + task->dumpsy = dumpsy; + task->path = path; + task->im_format = im_format; + + BLI_task_pool_push(m_taskpool, + save_screenshot_thread_func, + task, + true, // free task data + TASK_PRIORITY_LOW); +} diff --git a/source/gameengine/Rasterizer/RAS_ICanvas.h b/source/gameengine/Rasterizer/RAS_ICanvas.h index 471c2c97fa1..91cc13c8f85 100644 --- a/source/gameengine/Rasterizer/RAS_ICanvas.h +++ b/source/gameengine/Rasterizer/RAS_ICanvas.h @@ -37,6 +37,9 @@ #endif class RAS_Rect; +struct TaskScheduler; +struct TaskPool; +struct ImageFormatData; /** * 2D rendering device context. The connection from 3d rendercontext to 2d surface. @@ -56,10 +59,8 @@ public: MOUSE_NORMAL }; - virtual - ~RAS_ICanvas( - ) { - } + RAS_ICanvas(); + virtual ~RAS_ICanvas(); virtual void @@ -260,7 +261,23 @@ public: protected: RAS_MouseState m_mousestate; + int m_frame; /// frame number for screenshots. + TaskScheduler *m_taskscheduler; + TaskPool *m_taskpool; + /** + * Saves screenshot data to a file. The actual compression and disk I/O is performed in + * a separate thread. + * + * @param filename name of the file, can contain "###" for sequential numbering. A copy of the string + * is made, so the pointer can be freed by the caller. + * @param dumpsx width in pixels. + * @param dumpsy height in pixels. + * @param dumprect pixel data; ownership is passed to this function, which also frees the data. + * @param im_format image format for the file; ownership is passed to this function, which also frees the data. + */ + void save_screenshot(const char *filename, int dumpsx, int dumpsy, unsigned int *dumprect, + ImageFormatData * im_format); #ifdef WITH_CXX_GUARDEDALLOC MEM_CXX_CLASS_ALLOC_FUNCS("GE:RAS_ICanvas") diff --git a/source/gameengine/Rasterizer/SConscript b/source/gameengine/Rasterizer/SConscript index a643f46e39a..a29acda545a 100644 --- a/source/gameengine/Rasterizer/SConscript +++ b/source/gameengine/Rasterizer/SConscript @@ -44,6 +44,7 @@ incs = [ '#source/blender/blenkernel', '#source/blender/gpu', '#source/blender/makesdna', + '#source/blender/imbuf', ] defs = [] |