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-2007 Sun Microsystems, Inc.
|
---|
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 (GPL) as published by the Free Software
|
---|
16 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 | *
|
---|
20 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
21 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
22 | * additional information or have any questions.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "driver.h"
|
---|
26 | #include "vrdpbmp.h"
|
---|
27 | #include <iprt/crc64.h>
|
---|
28 | #include <VBox/VRDPOrders.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 | VRDPBCENTRY *pEntry;
|
---|
141 |
|
---|
142 | DISPDBG((1, "insert hash cache %p, tail %p.\n", pCache, pCache->tail));
|
---|
143 |
|
---|
144 | /* Get the free entry to be used. Try tail, that should be */
|
---|
145 | pEntry = pCache->tail;
|
---|
146 |
|
---|
147 | if (pEntry == NULL)
|
---|
148 | {
|
---|
149 | return bRc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (pEntry->fUsed)
|
---|
153 | {
|
---|
154 | /* The cache is full. Remove the tail. */
|
---|
155 | memcpy (phashDeleted, &pEntry->hash, sizeof (VRDPBCHASH));
|
---|
156 | bRc = TRUE;
|
---|
157 | }
|
---|
158 |
|
---|
159 | bcMoveToHead (pCache, pEntry);
|
---|
160 |
|
---|
161 | memcpy (&pEntry->hash, phash, sizeof (VRDPBCHASH));
|
---|
162 | pEntry->fUsed = TRUE;
|
---|
163 |
|
---|
164 | return bRc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Public functions.
|
---|
169 | */
|
---|
170 |
|
---|
171 | /* Find out whether the surface already in the cache.
|
---|
172 | * Insert in the cache if not.
|
---|
173 | */
|
---|
174 | int vrdpbmpCacheSurface (VRDPBC *pCache, const SURFOBJ *pso, VRDPBCHASH *phash, VRDPBCHASH *phashDeleted)
|
---|
175 | {
|
---|
176 | int rc;
|
---|
177 |
|
---|
178 | VRDPBCHASH hash;
|
---|
179 |
|
---|
180 | BOOL bResult = bcComputeHash (pso, &hash);
|
---|
181 |
|
---|
182 | DISPDBG((1, "vrdpbmpCacheSurface: compute hash %d.\n", bResult));
|
---|
183 | if (!bResult)
|
---|
184 | {
|
---|
185 | DISPDBG((1, "MEMBLT: vrdpbmpCacheSurface: could not compute hash.\n"));
|
---|
186 | return VRDPBMP_RC_NOT_CACHED;
|
---|
187 | }
|
---|
188 |
|
---|
189 | bResult = bcFindHash (pCache, &hash);
|
---|
190 |
|
---|
191 | DISPDBG((1, "vrdpbmpCacheSurface: find hash %d.\n", bResult));
|
---|
192 | *phash = hash;
|
---|
193 |
|
---|
194 | if (bResult)
|
---|
195 | {
|
---|
196 | return VRDPBMP_RC_ALREADY_CACHED;
|
---|
197 | }
|
---|
198 |
|
---|
199 | rc = VRDPBMP_RC_CACHED;
|
---|
200 |
|
---|
201 | bResult = bcInsertHash (pCache, &hash, phashDeleted);
|
---|
202 |
|
---|
203 | DISPDBG((1, "vrdpbmpCacheSurface: insert hash %d.\n", bResult));
|
---|
204 | if (bResult)
|
---|
205 | {
|
---|
206 | rc |= VRDPBMP_RC_F_DELETED;
|
---|
207 | }
|
---|
208 |
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* Setup the initial state of the cache. */
|
---|
213 | void vrdpbmpReset (VRDPBC *pCache)
|
---|
214 | {
|
---|
215 | int i;
|
---|
216 |
|
---|
217 | VBVA_ASSERT(sizeof (VRDPBCHASH) == sizeof (VRDPBITMAPHASH));
|
---|
218 |
|
---|
219 | /* Reinitialize the cache structure. */
|
---|
220 | memset (pCache, 0, sizeof (VRDPBC));
|
---|
221 |
|
---|
222 | pCache->head = &pCache->aEntries[0];
|
---|
223 | pCache->tail = &pCache->aEntries[RT_ELEMENTS(pCache->aEntries) - 1];
|
---|
224 |
|
---|
225 | for (i = 0; i < RT_ELEMENTS(pCache->aEntries); i++)
|
---|
226 | {
|
---|
227 | VRDPBCENTRY *pEntry = &pCache->aEntries[i];
|
---|
228 |
|
---|
229 | if (pEntry != pCache->tail)
|
---|
230 | {
|
---|
231 | pEntry->next = &pCache->aEntries[i + 1];
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (pEntry != pCache->head)
|
---|
235 | {
|
---|
236 | pEntry->prev = &pCache->aEntries[i - 1];
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|