19ce837365f6cf7dc10632579062e8b22e70c515
[platform/core/uifw/dali-core.git] / dali / integration-api / common / lockless-buffer.h
1 #ifndef __DALI_INTEGRATION_LOCKLESS_BUFFER_H__
2 #define __DALI_INTEGRATION_LOCKLESS_BUFFER_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //     http://floralicense.org/license/
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 // EXTERNAL INCLUDES
21
22 // INTERNAL INCLUDES
23 #include <cstring>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/common/dali-common.h>
26
27 namespace Dali
28 {
29
30 namespace Integration
31 {
32
33 /**
34  * The LocklessBuffer class implements double buffering eligible for multi-(two) threaded use,
35  * where it's possible to read from one thread and write from another
36  * without requiring a mutex lock to avoid performance hit.
37  * It's intended to be used for reading bitmap data in render thread
38  * while still possible to write data in another thread.
39  *
40  * Ideally Write() and Read() calls should be alternating, otherwise written data might be thrown away.
41  *
42  * The buffers are swapped in the reading thread, just before reading begins.
43  * In case the other thread is writing at that moment, buffers are not swapped and previously available data is read.
44  * Similarly if Write() is called before a Read() has finished the previous write buffer is overwritten.
45  */
46 class DALI_IMPORT_API LocklessBuffer
47 {
48
49 public:
50   /**
51    * Constructor.
52    * @param[in] size The size of buffers in bytes.
53    */
54   LocklessBuffer( size_t size );
55
56   /**
57    * Destructor.
58    */
59   ~LocklessBuffer();
60
61   /**
62    * Write data to buffer.
63    * @param[in] src data source
64    * @param[in] size size of data in bytes
65    */
66   void Write( const unsigned char *src, size_t size );
67
68   /**
69    * Try to swap buffers and read data.
70    * @note returned value only valid until Read() is called again or object is destroyed
71    * @return current read buffer contents
72    */
73   const unsigned char* Read();
74
75   /**
76    * @return the buffer size in bytes
77    */
78   unsigned int GetSize() const;
79
80 private:
81   /**
82    * Atomically set state.
83    * We're always writing to one buffer and reading from the other.
84    * Write() sets WRITING bit when started and unsets it when finished.
85    */
86   enum BufferState
87   {
88     R0W1    = 0, ///< Read from buffer 0 write to buffer 1
89     R1W0    = 1, ///< Read from buffer 1 write to buffer 0
90     WRITING = 2, ///< Currently writing to buffer
91     UPDATED = 4, ///< Swapping buffer required; there is new data available
92     WRITE_BUFFER_MASK = 1, ///< indicates which buffer to write to
93     WRITING_MASK      = 2, ///< indicates whether currently writing
94     UPDATED_MASK      = 4  ///< indicates whether new data is available
95   };
96
97 private:
98   LocklessBuffer(); ///< undefined default constructor, need to give size on construction
99   LocklessBuffer( const LocklessBuffer& );            ///< undefined copy constructor
100   LocklessBuffer& operator=( const LocklessBuffer& ); ///< undefined assignment operator
101
102 private:
103   unsigned char* mBuffer[2];     ///< bitmap buffers
104   BufferState volatile mState;   ///< readbuffer number and whether we're currently writing into writebuffer or not
105   size_t mSize;                  ///< size of buffers
106 };
107
108 } // Internal
109
110 } // Dali
111
112 #endif // __DALI_INTEGRATION_LOCKLESS_H__