1 | /** @file
|
---|
2 | *
|
---|
3 | * VBoxGuest -- VirtualBox Win 2000/XP guest display driver
|
---|
4 | *
|
---|
5 | * VRDP bitmap cache.
|
---|
6 | *
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
11 | *
|
---|
12 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | * available from http://www.virtualbox.org. This file is free software;
|
---|
14 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | * General Public License as published by the Free Software Foundation,
|
---|
16 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
17 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
18 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 | *
|
---|
20 | * If you received this file as part of a commercial VirtualBox
|
---|
21 | * distribution, then only the terms of your commercial VirtualBox
|
---|
22 | * license agreement apply instead of the previous paragraph.
|
---|
23 | *
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "driver.h"
|
---|
27 | #include "vrdpbmp.h"
|
---|
28 | #include <iprt/crc64.h>
|
---|
29 |
|
---|
30 | /*
|
---|
31 | * Cache has a fixed number of preallocated entries. Entries are linked in the MRU
|
---|
32 | * list. The list contains both used and free entries. Free entries are at the end.
|
---|
33 | * The most recently used entry is in the head.
|
---|
34 | *
|
---|
35 | * The purpose of the cache is to answer whether the bitmap was already encountered
|
---|
36 | * before.
|
---|
37 | *
|
---|
38 | * No serialization because the code is executed under vboxHwBuffer* semaphore.
|
---|
39 | */
|
---|
40 |
|
---|
41 | static uint64_t surfHash (const SURFOBJ *pso, uint32_t cbLine)
|
---|
42 | {
|
---|
43 | uint64_t u64CRC = RTCrc64Start ();
|
---|
44 |
|
---|
45 | uint32_t h = pso->sizlBitmap.cy;
|
---|
46 | uint8_t *pu8 = (uint8_t *)pso->pvScan0;
|
---|
47 |
|
---|
48 | while (h > 0)
|
---|
49 | {
|
---|
50 | u64CRC = RTCrc64Process (u64CRC, pu8, cbLine);
|
---|
51 | pu8 += pso->lDelta;
|
---|
52 | h--;
|
---|
53 | }
|
---|
54 |
|
---|
55 | u64CRC = RTCrc64Finish (u64CRC);
|
---|
56 |
|
---|
57 | return u64CRC;
|
---|
58 | } /* Hash function end. */
|
---|
59 |
|
---|
60 |
|
---|
61 | static BOOL bcComputeHash (const SURFOBJ *pso, VRDPBCHASH *phash)
|
---|
62 | {
|
---|
63 | uint32_t cbLine;
|
---|
64 |
|
---|
65 | int bytesPerPixel = format2BytesPerPixel (pso);
|
---|
66 |
|
---|
67 | if (bytesPerPixel == 0)
|
---|
68 | {
|
---|
69 | return FALSE;
|
---|
70 | }
|
---|
71 |
|
---|
72 | phash->cx = (uint16_t)pso->sizlBitmap.cx;
|
---|
73 | phash->cy = (uint16_t)pso->sizlBitmap.cy;
|
---|
74 | phash->bytesPerPixel = bytesPerPixel;
|
---|
75 |
|
---|
76 | cbLine = pso->sizlBitmap.cx * bytesPerPixel;
|
---|
77 | phash->hash64 = surfHash (pso, cbLine);
|
---|
78 |
|
---|
79 | memset (phash->padding, 0, sizeof (phash->padding));
|
---|
80 |
|
---|
81 | return TRUE;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* Meves an entry to the head of MRU list. */
|
---|
85 | static void bcMoveToHead (VRDPBC *pCache, VRDPBCENTRY *pEntry)
|
---|
86 | {
|
---|
87 | if (pEntry->prev)
|
---|
88 | {
|
---|
89 | /* The entry is not yet in the head. Exclude from list. */
|
---|
90 | pEntry->prev->next = pEntry->next;
|
---|
91 |
|
---|
92 | if (pEntry->next)
|
---|
93 | {
|
---|
94 | pEntry->next->prev = pEntry->prev;
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | pCache->tail = pEntry->prev;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* Insert the entry at the head of MRU list. */
|
---|
102 | pEntry->prev = NULL;
|
---|
103 | pEntry->next = pCache->head;
|
---|
104 |
|
---|
105 | VBVA_ASSERT(pCache->head);
|
---|
106 |
|
---|
107 | pCache->head->prev = pEntry;
|
---|
108 | pCache->head = pEntry;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* Returns TRUE if the hash already presents in the cache.
|
---|
113 | * Moves the found entry to the head of MRU list.
|
---|
114 | */
|
---|
115 | static BOOL bcFindHash (VRDPBC *pCache, const VRDPBCHASH *phash)
|
---|
116 | {
|
---|
117 | /* Search the MRU list. */
|
---|
118 | VRDPBCENTRY *pEntry = pCache->head;
|
---|
119 |
|
---|
120 | while (pEntry && pEntry->fUsed)
|
---|
121 | {
|
---|
122 | if (memcmp (&pEntry->hash, phash, sizeof (VRDPBCHASH)) == 0)
|
---|
123 | {
|
---|
124 | /* Found the entry. Move it to the head of MRU list. */
|
---|
125 | bcMoveToHead (pCache, pEntry);
|
---|
126 |
|
---|
127 | return TRUE;
|
---|
128 | }
|
---|
129 |
|
---|
130 | pEntry = pEntry->next;
|
---|
131 | }
|
---|
132 |
|
---|
133 | return FALSE;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /* Returns TRUE is a entry was also deleted to nake room for new entry. */
|
---|
137 | static BOOL bcInsertHash (VRDPBC *pCache, const VRDPBCHASH *phash, VRDPBCHASH *phashDeleted)
|
---|
138 | {
|
---|
139 | BOOL bRc = FALSE;
|
---|
140 |
|
---|
141 | /* Get the free entry to be used. Try tail, that should be */
|
---|
142 | VRDPBCENTRY *pEntry = pCache->tail;
|
---|
143 |
|
---|
144 | if (pEntry->fUsed)
|
---|
145 | {
|
---|
146 | /* The cache is full. Remove the tail. */
|
---|
147 | memcpy (phashDeleted, &pEntry->hash, sizeof (VRDPBCHASH));
|
---|
148 | bRc = TRUE;
|
---|
149 | }
|
---|
150 |
|
---|
151 | bcMoveToHead (pCache, pEntry);
|
---|
152 |
|
---|
153 | memcpy (&pEntry->hash, phash, sizeof (VRDPBCHASH));
|
---|
154 | pEntry->fUsed = TRUE;
|
---|
155 |
|
---|
156 | return bRc;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Public functions.
|
---|
161 | */
|
---|
162 |
|
---|
163 | /* Find out whether the surface already in the cache.
|
---|
164 | * Insert in the cache if not.
|
---|
165 | */
|
---|
166 | int vrdpbmpCacheSurface (VRDPBC *pCache, const SURFOBJ *pso, VRDPBCHASH *phash, VRDPBCHASH *phashDeleted)
|
---|
167 | {
|
---|
168 | int rc;
|
---|
169 |
|
---|
170 | VRDPBCHASH hash;
|
---|
171 |
|
---|
172 | BOOL bResult = bcComputeHash (pso, &hash);
|
---|
173 |
|
---|
174 | if (!bResult)
|
---|
175 | {
|
---|
176 | DISPDBG((1, "MEMBLT: vrdpbmpCacheSurface: could not compute hash.\n"));
|
---|
177 | return VRDPBMP_RC_NOT_CACHED;
|
---|
178 | }
|
---|
179 |
|
---|
180 | bResult = bcFindHash (pCache, &hash);
|
---|
181 |
|
---|
182 | *phash = hash;
|
---|
183 |
|
---|
184 | if (bResult)
|
---|
185 | {
|
---|
186 | return VRDPBMP_RC_ALREADY_CACHED;
|
---|
187 | }
|
---|
188 |
|
---|
189 | rc = VRDPBMP_RC_CACHED;
|
---|
190 |
|
---|
191 | bResult = bcInsertHash (pCache, &hash, phashDeleted);
|
---|
192 |
|
---|
193 | if (bResult)
|
---|
194 | {
|
---|
195 | rc |= VRDPBMP_RC_F_DELETED;
|
---|
196 | }
|
---|
197 |
|
---|
198 | return rc;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* Setup the initial state of the cache. */
|
---|
202 | void vrdpbmpReset (VRDPBC *pCache)
|
---|
203 | {
|
---|
204 | int i;
|
---|
205 |
|
---|
206 | VBVA_ASSERT(sizeof (VRDPBCHASH) == sizeof (VRDPBITMAPHASH));
|
---|
207 |
|
---|
208 | /* Reinitialize the cache structure. */
|
---|
209 | memset (pCache, 0, sizeof (VRDPBC));
|
---|
210 |
|
---|
211 | pCache->head = &pCache->aEntries[0];
|
---|
212 | pCache->tail = &pCache->aEntries[ELEMENTS(pCache->aEntries) - 1];
|
---|
213 |
|
---|
214 | for (i = 0; i < ELEMENTS(pCache->aEntries); i++)
|
---|
215 | {
|
---|
216 | VRDPBCENTRY *pEntry = &pCache->aEntries[i];
|
---|
217 |
|
---|
218 | if (pEntry != pCache->tail)
|
---|
219 | {
|
---|
220 | pEntry->next = &pCache->aEntries[i + 1];
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (pEntry != pCache->head)
|
---|
224 | {
|
---|
225 | pEntry->prev = &pCache->aEntries[i - 1];
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|