@@ -10,6 +10,7 @@ namespace RemoteDesktop{
10
10
extern bool GrazyScale;
11
11
}
12
12
namespace INTERNAL {
13
+ // improves speed when memory allocations are kept down because vector resize always does a memset on the unintialized elements
13
14
extern std::vector<std::vector<char >> BufferCache;
14
15
extern std::mutex BufferCacheLock;
15
16
}
@@ -22,23 +23,41 @@ namespace RemoteDesktop{
22
23
Image (const Image& other) = delete ;
23
24
Image () {}
24
25
explicit Image (char * d, int h, int w) :Pixel_Stride(4 ), Height(h), Width(w) {
25
-
26
+ if (!INTERNAL::BufferCache.empty ()){
27
+ std::lock_guard<std::mutex> lock (INTERNAL::BufferCacheLock);
28
+ if (!INTERNAL::BufferCache.empty ()){
29
+ data = std::move (INTERNAL::BufferCache.back ());
30
+ INTERNAL::BufferCache.pop_back ();
31
+ }
32
+ }
26
33
data.resize (Pixel_Stride*Height*Width); memcpy (data.data (), d, Pixel_Stride);
27
34
}
28
35
explicit Image (int h, int w) : Pixel_Stride(4 ), Height(h), Width(w) {
36
+ if (!INTERNAL::BufferCache.empty ()){
37
+ std::lock_guard<std::mutex> lock (INTERNAL::BufferCacheLock);
38
+ if (!INTERNAL::BufferCache.empty ()){
39
+ data = std::move (INTERNAL::BufferCache.back ());
40
+ INTERNAL::BufferCache.pop_back ();
41
+ }
42
+ }
29
43
data.resize (Pixel_Stride*Height*Width);
30
44
}
31
45
Image (Image&& other) :data(std::move(other.data)), Height(std::move(other.Height)), Width(std::move(other.Width)), Compressed(std::move(other.Compressed)){
32
-
46
+
33
47
}
34
48
Image& operator =(Image&& other){
35
-
49
+
36
50
data = std::move (other.data );
37
- Height= std::move (other.Height );
38
- Width= std::move (other.Width );
51
+ Height = std::move (other.Height );
52
+ Width = std::move (other.Width );
39
53
Compressed = std::move (other.Compressed );
40
54
}
41
-
55
+ ~Image (){
56
+ if (data.size () > 100 ){
57
+ std::lock_guard<std::mutex> lock (INTERNAL::BufferCacheLock);
58
+ INTERNAL::BufferCache.emplace_back (std::move (data));
59
+ }
60
+ }
42
61
static Image Create_from_Compressed_Data (char * d, int size_in_bytes, int h, int w);
43
62
void Compress ();
44
63
void Decompress ();
0 commit comments