Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_MemoryManager.h « intern « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 506bf2a8c240325512a563f4fa881e1df1a2360e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright 2011, Blender Foundation.
 *
 * 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.
 *
 * Contributor: 
 *		Jeroen Bakker 
 *		Monique Dewanchand
 */

#ifndef _COM_MemoryManager_h_
#define _COM_MemoryManager_h_

#include "COM_MemoryBuffer.h"
#include "COM_MemoryProxy.h"
#include "COM_ExecutionGroup.h"
#include "COM_MemoryManagerState.h"

/**
  * @page memorymanager The Memory Manager
  * The compositor has its own MemoryManager. The goal of the MemoryManager is to manage the memory allocated by chunks.
  * During execution new chunks will be created [MemoryManager.allocateMemoryBuffer] When calculation is finished the MemoryBuffer will get the state [MemoryBufferState.COM_MB_AVAILABLE].
  * From now on other ExecutionGroup and NodeOperations may read from the MemoryBuffer.
  * The MemoryManager also has the capability to save MemoryBuffer's to disk in order to free some memory.
  *
  * @section S_MEM Memory manager
  * The memory manager synchronize and optimize data across devices.
  * Only one NodeOperation running on a device is able to write to a MemoryBuffer. This MemoryBuffer is only allocated on the main-device memory (CPU).
  * The MemoryBuffer.state will be [MemoryBufferState.COM_MB_ALLOCATED]. As soon as the chunk has been executed the state changes to [MemoryBufferState.COM_MB_AVAILABLE]. This MemoryBuffer can now be used as inputBuffer of ExecutionGroup's.
  * When needed the MemoryBuffer will be stored to a file. This will save memory that can be used by other tiles.
  * @subsection S_MEM_1 Step one
  * When a chunk of an ExecutionGroup is being executed by a device, the MemoryBuffer is allocated on the CPU.
  * <pre>
  * Allocation of the output MemoryBuffer
  *  +----------------------------------------+
  *  | Main device (CPU)                      |
  *  | +----------------+   +--------------+  |
  *  | | ExecutionGroup |   | MemoryBuffer |  |
  *  | |                |   | Chunk a      |  |
  *  | +----------------+   +--------------+  |
  *  |                                        |
  *  +----------------------------------------+
  * </pre>
  * @see MemoryManager.allocateMemoryBuffer
  *
  * @subsection S_MEM_2 Step two
  * The Device will execute the ExecutionGroup. This differs per type of Device. CPUDevice will call the NodeOperation.executeRegion method of the outputnode of the ExecutionGroup.
  * The [NodeOperation.executeRegion] writes the result to the allocated MemoryBuffer. When finished the state of the MemoryBuffer will be set to [MemoryBufferState.COM_MB_AVAILABLE].
  * <pre>
  * Execute a chunk and store result to the MemoryBuffer
  *  +----------------------------------------+
  *  | Main device (CPU)                      |
  *  | +----------------+   +--------------+  |
  *  | | ExecutionGroup |   | MemoryBuffer |  |
  *  | |                |   | Chunk a      |  |
  *  | +----------------+   +--------------+  |
  *  |         |                 ^            |
  *  | +----------------+        |            |
  *  | | NodeOperation  |--------+            |
  *  | |                |     Write result    |
  *  | +----------------+                     |
  *  |                                        |
  *  +----------------------------------------+
  * </pre>
  * @subsection S_MEM_3 Step 3
  * Other Chunks that depend on the MemoryBuffer can now use it.
  * When a MemoryBuffer is being used its number of users are increased. When a 'user' is finished the number of users are decreased, If a MemoryBuffer has no users, the system can decide to store the data to disk and free some memory.
  * @see MemoryBuffer.numberOfUsers
  * @see MemoryBuffer.saveToDisk
  *
  * @subsection S_MEM_CON Temporarily MemoryBuffers
  * Nodes like blur nodes can depend on multiple MemoryBuffer of the same MemoryProxy. These multiple buffers will be consolidated temporarily to a new MemoryBuffer.
  * When execution is finished this temporarily memory buffer is deallicated.
  * <pre>
  *  Original MemoryBuffer's  Temporarily
  *  +-------+ +-------+      MemoryBuffer
  *  | MB A  | | MB B  |      +-------+-------+
  *  +-------+ +-------+      | MB A  | MB B  |
  *                       ==> +-------+-------+
  *  +-------+ +-------+      | MB C  | MB D  |
  *  | MB C  | | MB D  |      +-------+-------+
  *  +-------+ +-------+
  * </pre>
  * @see ExecutionGroup.constructConsolidatedMemoryBuffer constructs the temporarily MemoryBuffer
  * @see MemoryBuffer.state state is MemoryManagerState.COM_MB_TEMPORARILY
  * @see ExecutionGroup.finalizeChunkExecution deallocate the temporarily MemoryBuffer
  * @note this MemoryBuffer is not managed by the MemoryManager
  */

/**
  * @brief the memory manager for the compostor
  * @ingroup Memory
  */
class MemoryManager {
private:
	/**
	  * @brief retrieve the state of a certain MemoryProxy;
	  * @param memoryProxy the MemoryProxy to retrieve the state from
	  */
	static MemoryManagerState* getState(MemoryProxy* memoryProxy);
public:
	/**
	  * @brief allocate a memory buffer
	  * @param memoryProxy the MemoryProxy to get a chunk from
	   * @param chunkNumber number of the chunk to receive
	   * @param rect size + position of the chunk
	  */
	static MemoryBuffer* allocateMemoryBuffer(MemoryProxy *memoryProxy, unsigned int chunkNumber, rcti* rect);

	/**
	  * @brief get a memory buffer
	  * @param memoryProxy the MemoryProxy to get a chunk from
	  * @param chunkNumber number of the chunk to receive
	  * @param addUser must we add a user to the chunk.
	  */
	static MemoryBuffer* getMemoryBuffer(MemoryProxy *memoryProxy, unsigned int chunkNumber);

	/**
	  * @brief add a MemoryProxy to the scope of the memory manager
	  * @param memoryProxy the MemoryProxy to add
	  */
	static void addMemoryProxy(MemoryProxy *memoryProxy);

	/**
	  * @brief clear the memory manager
	  */
	static void clear();

	/**
	  * @brief initialize the memory manager.
	  */
	static void initialize();
};
#endif