Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2326 lines
71 KiB

  1. #include "precomp.h"
  2. //
  3. // SBC.CPP
  4. // Send Bitmap Cache
  5. //
  6. // Copyright(c) Microsoft 1997-
  7. //
  8. #define MLZ_FILE_ZONE ZONE_CORE
  9. //
  10. // SBC_HostStarting()
  11. //
  12. BOOL ASHost::SBC_HostStarting(void)
  13. {
  14. BITMAPINFO_ours bitmapInfo;
  15. int i;
  16. BOOL rc = FALSE;
  17. DebugEntry(ASHost::SBC_HostStarting);
  18. if (g_sbcEnabled)
  19. {
  20. //
  21. // We create a DIB section for each tile size which we use during the
  22. // conversion of a bitmap from the native (device) bpp to the protocol
  23. // bpp. We create the DIB sections at the device bpp.
  24. //
  25. ZeroMemory(&bitmapInfo, sizeof(bitmapInfo));
  26. m_pShare->USR_InitDIBitmapHeader((BITMAPINFOHEADER *)&bitmapInfo, g_usrCaptureBPP);
  27. // We only capture at 8 or 24 for NT 5.0, otherwise the screen depth
  28. if ((g_usrCaptureBPP > 8) && (g_usrCaptureBPP != 24))
  29. {
  30. //
  31. // If the device bpp is > 8 (but not 24), we have to set up the DIB
  32. // section to use the same bitmasks as the device. This means
  33. // setting the compression type to BI_BITFIELDS and setting the
  34. // first 3 DWORDS of the bitmap info color table to be the bitmasks
  35. // for R, G and B respectively.
  36. //
  37. // 24bpp does not use bitmasks - it must use
  38. // regular BI_RGB format with 8 bits for each colour.
  39. //
  40. bitmapInfo.bmiHeader.biCompression = BI_BITFIELDS;
  41. ASSERT(g_asbcBitMasks[0]);
  42. ASSERT(g_asbcBitMasks[1]);
  43. ASSERT(g_asbcBitMasks[2]);
  44. bitmapInfo.bmiColors[0] = ((LPTSHR_RGBQUAD)g_asbcBitMasks)[0];
  45. bitmapInfo.bmiColors[1] = ((LPTSHR_RGBQUAD)g_asbcBitMasks)[1];
  46. bitmapInfo.bmiColors[2] = ((LPTSHR_RGBQUAD)g_asbcBitMasks)[2];
  47. }
  48. //
  49. // Initialize m_asbcWorkInfo array which holds the info we use to
  50. // convert from native bpp to protocol bpp.
  51. //
  52. //
  53. // First, intialize all the fields to default values
  54. //
  55. for (i = 0; i < SBC_NUM_TILE_SIZES ; i++)
  56. {
  57. ASSERT(!m_asbcWorkInfo[i].pShuntBuffer);
  58. ASSERT(g_asbcShuntBuffers[i]);
  59. m_asbcWorkInfo[i].pShuntBuffer = g_asbcShuntBuffers[i];
  60. ASSERT(m_asbcWorkInfo[i].mruIndex == 0);
  61. ASSERT(m_asbcWorkInfo[i].workBitmap == 0);
  62. ASSERT(m_asbcWorkInfo[i].pWorkBitmapBits == NULL);
  63. if (i == SBC_MEDIUM_TILE_INDEX)
  64. {
  65. m_asbcWorkInfo[i].tileWidth = MP_MEDIUM_TILE_WIDTH;
  66. m_asbcWorkInfo[i].tileHeight = MP_MEDIUM_TILE_HEIGHT;
  67. }
  68. else
  69. {
  70. m_asbcWorkInfo[i].tileWidth = MP_LARGE_TILE_WIDTH;
  71. m_asbcWorkInfo[i].tileHeight = MP_LARGE_TILE_HEIGHT;
  72. }
  73. bitmapInfo.bmiHeader.biWidth = m_asbcWorkInfo[i].tileWidth;
  74. bitmapInfo.bmiHeader.biHeight = m_asbcWorkInfo[i].tileHeight;
  75. m_asbcWorkInfo[i].workBitmap = CreateDIBSection(NULL,
  76. (BITMAPINFO*)&bitmapInfo,
  77. DIB_RGB_COLORS,
  78. (void **)&(m_asbcWorkInfo[i].pWorkBitmapBits),
  79. NULL, // File mapping object
  80. 0); // Offset into file
  81. // mapping object
  82. if (!m_asbcWorkInfo[i].workBitmap)
  83. {
  84. ERROR_OUT(("Failed to create SBC DIB section %d", i));
  85. DC_QUIT;
  86. }
  87. ASSERT(m_asbcWorkInfo[i].pWorkBitmapBits);
  88. TRACE_OUT(( "Created work DIB section %d, pBits = 0x%08x",
  89. i, m_asbcWorkInfo[i].pWorkBitmapBits));
  90. }
  91. //
  92. // Initialize the fastpath
  93. //
  94. if (!SBCInitFastPath())
  95. {
  96. TRACE_OUT(( "Failed to init fastpath"));
  97. DC_QUIT;
  98. }
  99. if (!SBCInitInternalOrders())
  100. {
  101. ERROR_OUT(( "Failed to init SBC internal order struct"));
  102. DC_QUIT;
  103. }
  104. m_pShare->SBC_RecalcCaps(TRUE);
  105. }
  106. rc = TRUE;
  107. DC_EXIT_POINT:
  108. DebugExitBOOL(ASHost::SBC_HostStarting, rc);
  109. return(rc);
  110. }
  111. //
  112. // ASShare::SBC_HostEnded()
  113. //
  114. void ASHost::SBC_HostEnded(void)
  115. {
  116. int i;
  117. DebugEntry(ASHost::SBC_HostEnded);
  118. if (g_sbcEnabled)
  119. {
  120. //
  121. // Free up the memory associated with sbcOrderInfo.
  122. //
  123. SBCFreeInternalOrders();
  124. SBCInitCacheStructures();
  125. //
  126. // Free our fast path info
  127. //
  128. if (m_sbcFastPath)
  129. {
  130. delete m_sbcFastPath;
  131. m_sbcFastPath = NULL;
  132. }
  133. //
  134. // Clear our cache handles.
  135. //
  136. for (i = 0; i < NUM_BMP_CACHES; i++)
  137. {
  138. if (m_asbcBmpCaches[i].handle != 0)
  139. {
  140. TRACE_OUT(( "Clear cache %d", i));
  141. CH_DestroyCache(m_asbcBmpCaches[i].handle);
  142. BMCFreeCacheData(&m_asbcBmpCaches[i]);
  143. }
  144. }
  145. //
  146. // Free our work DIB sections
  147. //
  148. //
  149. // We just have to delete the DIB sections and reset our variables.
  150. //
  151. for (i = 0 ; i < SBC_NUM_TILE_SIZES ; i++)
  152. {
  153. m_asbcWorkInfo[i].pShuntBuffer = NULL;
  154. if (m_asbcWorkInfo[i].workBitmap != NULL)
  155. {
  156. DeleteBitmap(m_asbcWorkInfo[i].workBitmap);
  157. m_asbcWorkInfo[i].workBitmap = NULL;
  158. m_asbcWorkInfo[i].pWorkBitmapBits = NULL;
  159. }
  160. }
  161. }
  162. DebugExitVOID(ASHost::SBC_HostEnded);
  163. }
  164. //
  165. // SBC_SyncOutgoing()
  166. // Called when we're already hosting and someone new joins the share.
  167. // Resets the OUTGOING bitmap cache for bitblt orders.
  168. //
  169. void ASHost::SBC_SyncOutgoing(void)
  170. {
  171. int i;
  172. DebugEntry(ASHost::SBC_SyncOutgoing);
  173. //
  174. // Only do anything if SBC is enabled
  175. //
  176. if (g_sbcEnabled)
  177. {
  178. //
  179. // Discard all currently cached bitmaps and set the colour table to
  180. // zero so that the next bitmap order which arrives will trigger the
  181. // sending of a new colour table first. Note that if the colour table
  182. // is then full of zeros(!) it will still be OK because the RBC zeros
  183. // out its copy of the colour table when a new host joins the share.
  184. //
  185. TRACE_OUT(( "Clearing all send caches"));
  186. SBCInitCacheStructures();
  187. //
  188. // All we have to do here is to reset our MRU indices for each of the
  189. // shunt buffers. Each of the entries in the shunt buffer will be
  190. // marked as free down in the driver.
  191. //
  192. for (i = 0; i < SBC_NUM_TILE_SIZES; i++)
  193. {
  194. m_asbcWorkInfo[i].mruIndex = 0;
  195. }
  196. }
  197. DebugExitVOID(ASHost::SBC_SyncOutgoing);
  198. }
  199. //
  200. //
  201. // SBC_CopyPrivateOrderData()
  202. //
  203. //
  204. UINT ASHost::SBC_CopyPrivateOrderData
  205. (
  206. LPBYTE pDst,
  207. LPCOM_ORDER pOrder,
  208. UINT freeBytesInBuffer
  209. )
  210. {
  211. UINT orderSize;
  212. LPBYTE pBitmapBits;
  213. DebugEntry(ASHost::SBC_CopyPrivateOrderData);
  214. //
  215. // Copy the order header without the rectangle structure (which we
  216. // do not use).
  217. //
  218. orderSize = sizeof(pOrder->OrderHeader)
  219. - sizeof(pOrder->OrderHeader.rcsDst);
  220. memcpy(pDst, pOrder, orderSize);
  221. //
  222. // Copy the basic order data.
  223. //
  224. memcpy(pDst + orderSize,
  225. pOrder->abOrderData,
  226. pOrder->OrderHeader.cbOrderDataLength);
  227. orderSize += pOrder->OrderHeader.cbOrderDataLength;
  228. if (orderSize > freeBytesInBuffer)
  229. {
  230. ERROR_OUT(( "Overwritten end of buffer. (%u) > (%u)",
  231. orderSize,
  232. freeBytesInBuffer));
  233. }
  234. //
  235. // Set the length field in the order header to be the total amount of
  236. // data we have copied (including the partial header) minus the
  237. // size of a full header. This is horrible! - but is needed because
  238. // the OD2 code looks at the header (which it really should not know
  239. // about) and uses the length field to calculate the total length of
  240. // the order. The OD2 code does not know that we have omitted some
  241. // of the header.
  242. //
  243. ((LPCOM_ORDER)pDst)->OrderHeader.cbOrderDataLength =
  244. (WORD)(orderSize - sizeof(COM_ORDER_HEADER));
  245. //
  246. // Return the total number of bytes that we have copied.
  247. //
  248. DebugExitDWORD(ASHost::SBC_CopyPrivateOrderData, orderSize);
  249. return(orderSize);
  250. }
  251. //
  252. // Name: SBCInitCacheStructures()
  253. //
  254. // Purpose:
  255. //
  256. // Returns:
  257. //
  258. // Params:
  259. //
  260. // Operation:
  261. //
  262. //
  263. void ASHost::SBCInitCacheStructures(void)
  264. {
  265. UINT i;
  266. DebugEntry(ASHost::SBCInitCacheStructures);
  267. ASSERT(g_sbcEnabled);
  268. //
  269. // Reset caches
  270. //
  271. for (i = 0; i < NUM_BMP_CACHES; i++)
  272. {
  273. if (m_asbcBmpCaches[i].handle)
  274. {
  275. CH_ClearCache(m_asbcBmpCaches[i].handle);
  276. }
  277. }
  278. //
  279. // Do any OS specific processing
  280. //
  281. SBC_CacheCleared();
  282. DebugExitVOID(ASHost::SBCInitCacheStructures);
  283. }
  284. //
  285. // SBC_CacheCleared()
  286. //
  287. void ASHost::SBC_CacheCleared(void)
  288. {
  289. int i;
  290. DebugEntry(ASHost::SBC_CacheCleared);
  291. ASSERT(g_sbcEnabled);
  292. ASSERT(m_sbcFastPath);
  293. //
  294. // The cache has been cleared. Reset our fast path.
  295. //
  296. COM_BasedListInit(&m_sbcFastPath->usedList);
  297. COM_BasedListInit(&m_sbcFastPath->freeList);
  298. for (i = 0; i < SBC_FASTPATH_ENTRIES; i++)
  299. {
  300. m_sbcFastPath->entry[i].list.next = 0;
  301. m_sbcFastPath->entry[i].list.prev = 0;
  302. COM_BasedListInsertBefore(&m_sbcFastPath->freeList,
  303. &m_sbcFastPath->entry[i].list);
  304. }
  305. DebugExitVOID(ASHost::SBC_CacheCleared);
  306. }
  307. //
  308. //
  309. // SBCSelectCache(..)
  310. //
  311. // Decides which cache a sub-bitmap from a source bitmap of the specified
  312. // size should go in.
  313. //
  314. // To be cached, the sub-bitmap must:
  315. // have a size, in compressed bytes, which fits in the cache
  316. //
  317. // The R1.1 cache selection is irrespective of the actual memory
  318. // requirement for the cached data. This is wasteful of space, but is
  319. // necessary for R1.1 compatibility. (The R1.1 cache paremeters mean that
  320. // the total cache will be below about 128K in any case)
  321. //
  322. // For R2.0 the cache is selected by this function by comparing the
  323. // post-compress size with the cell area of each of the caches. This gives
  324. // us a much better space usage on both server and client.
  325. //
  326. // Returns:
  327. // TRUE if the sub-bitmap can be cached.
  328. // *pCache is updated with the index of the selected cache.
  329. //
  330. // FALSE if the sub-bitmap cannot be cached.
  331. // *pCache is not updated.
  332. //
  333. //
  334. BOOL ASHost::SBCSelectCache
  335. (
  336. UINT cSize,
  337. UINT * pCache
  338. )
  339. {
  340. BOOL fCacheSelected;
  341. BOOL fSelectedCacheIsFull;
  342. UINT i;
  343. DebugEntry(ASHost::SBCSelectCache);
  344. fCacheSelected = FALSE;
  345. fSelectedCacheIsFull = FALSE;
  346. //
  347. // This loop makes the assumption that cache 0 is the smallest. If
  348. // abmcint.h changes this assumption it will need rewriting.
  349. //
  350. for (i = 0; i < NUM_BMP_CACHES; i++)
  351. {
  352. if (m_asbcBmpCaches[i].cEntries <= 0)
  353. {
  354. //
  355. // No entries in this cache, so skip to the next one
  356. //
  357. continue;
  358. }
  359. //
  360. // R2 bitmap cache - only consider total cell size.
  361. //
  362. // Only consider this cache if
  363. // - we haven't yet found a cache
  364. // OR
  365. // - we have found a cache, but it is full (i.e. will
  366. // require an entry to be ejected) AND this one is not
  367. // full
  368. //
  369. // (Note that a cache is full if freeEntry != NULL)
  370. //
  371. if (!fCacheSelected ||
  372. (fSelectedCacheIsFull &&
  373. ((m_asbcBmpCaches[i].freeEntry == NULL)
  374. || !m_asbcBmpCaches[i].freeEntry->inUse)))
  375. {
  376. if (cSize <= m_asbcBmpCaches[i].cSize)
  377. {
  378. if (fSelectedCacheIsFull)
  379. {
  380. TRACE_OUT(("Using cache %u because cache %u is full",
  381. *pCache, i));
  382. }
  383. *pCache = i;
  384. fCacheSelected = TRUE;
  385. fSelectedCacheIsFull =
  386. ((m_asbcBmpCaches[i].freeEntry != NULL) &&
  387. m_asbcBmpCaches[i].freeEntry->inUse);
  388. if (!fSelectedCacheIsFull)
  389. {
  390. break;
  391. }
  392. }
  393. }
  394. }
  395. DebugExitDWORD(ASHost::SBCSelectCache, fCacheSelected);
  396. return(fCacheSelected);
  397. }
  398. //
  399. // FUNCTION: SBC_RecreateSendCache
  400. //
  401. // DESCRIPTION:
  402. //
  403. // (Re)creates the send bitmap cache with a size suitable for the current
  404. // capabilities.
  405. //
  406. // PARAMETERS:
  407. // cache - index to the cache being recreated
  408. // cOldEntries - the previous max number of entries in the cache
  409. // oldCellSize - the previous cell size
  410. //
  411. // RETURNS: NONE
  412. //
  413. //
  414. void ASHost::SBC_RecreateSendCache
  415. (
  416. UINT cache,
  417. UINT newNumEntries,
  418. UINT newCellSize
  419. )
  420. {
  421. PBMC_DIB_CACHE pCache = &(m_asbcBmpCaches[cache]);
  422. DebugEntry(ASHost::SBC_RecreateSendCache);
  423. //
  424. // Allocate the memory for the new send cache
  425. //
  426. ASSERT((newCellSize != pCache->cCellSize) ||
  427. (newNumEntries != pCache->cEntries));
  428. //
  429. // If the cache already exists then destroy it first
  430. //
  431. if (pCache->handle != 0)
  432. {
  433. TRACE_OUT(( "Destroy SBC cache %d", cache));
  434. CH_DestroyCache(pCache->handle);
  435. pCache->handle = 0;
  436. }
  437. //
  438. // Now reallocate the cache data. This will free any memory previously
  439. // allocated. If the entries/cellsize is zero, it will return success.
  440. //
  441. if (!BMCAllocateCacheData(newNumEntries, newCellSize, cache, pCache))
  442. {
  443. ERROR_OUT(( "Bitmap caching disabled for cache %u", cache));
  444. }
  445. if (pCache->cEntries > 0)
  446. {
  447. //
  448. // Allocate cache handler cache. Note that we force the cache
  449. // handler to leave us with one entry in our hand at all times by
  450. // decrementing its count of entries.
  451. //
  452. if (!CH_CreateCache(&(pCache->handle),
  453. pCache->cEntries - 1,
  454. SBC_NUM_CATEGORIES,
  455. BMC_DIB_NOT_HASHED,
  456. SBCCacheCallback ))
  457. {
  458. ERROR_OUT(( "Could not allocate SBC cache of (%u)",
  459. pCache->cEntries));
  460. pCache->cEntries = 0;
  461. }
  462. }
  463. TRACE_OUT(( "Created new cache: 0x%08x, size %u",
  464. pCache->handle,
  465. pCache->cEntries));
  466. //
  467. // Copy the relevant cache information into the shared memory buffer
  468. //
  469. m_asbcCacheInfo[cache].cEntries = (WORD)pCache->cEntries;
  470. m_asbcCacheInfo[cache].cCellSize = (WORD)pCache->cCellSize;
  471. TRACE_OUT(("SBC cache %d: %d entries of size %d",
  472. cache, m_asbcCacheInfo[cache].cEntries, m_asbcCacheInfo[cache].cCellSize));
  473. DebugExitVOID(ASHost::SBC_RecreateSendCache);
  474. }
  475. //
  476. // SBC_RecalcCaps()
  477. //
  478. // Enumerates all the people in the share and redetermines the size of the
  479. // bitmap cache depending on their and the local receive capabilities.
  480. //
  481. //
  482. // THIS CAN GO AWAY WHEN 2.X COMPAT DOES
  483. //
  484. void ASShare::SBC_RecalcCaps(BOOL fJoiner)
  485. {
  486. SBC_NEW_CAPABILITIES newCapabilities;
  487. UINT newSmallCellSize;
  488. UINT newSmallMaxEntries;
  489. UINT newMediumCellSize;
  490. UINT newMediumMaxEntries;
  491. UINT newLargeCellSize;
  492. UINT newLargeMaxEntries;
  493. PBMC_DIB_CACHE pSmall;
  494. PBMC_DIB_CACHE pMedium;
  495. PBMC_DIB_CACHE pLarge;
  496. BOOL cacheChanged = FALSE;
  497. ASPerson * pasT;
  498. DebugEntry(ASShare::SBC_RecalcCaps);
  499. if (!m_pHost || !g_sbcEnabled)
  500. {
  501. //
  502. // Nothing to do -- we're not hosting, or there is no SBC. Note that
  503. // 2.x always recalculated this stuff when somebody joined AND
  504. // somebody left.
  505. //
  506. DC_QUIT;
  507. }
  508. ValidatePerson(m_pasLocal);
  509. pSmall = &(m_pHost->m_asbcBmpCaches[ID_SMALL_BMP_CACHE]);
  510. pMedium= &(m_pHost->m_asbcBmpCaches[ID_MEDIUM_BMP_CACHE]);
  511. pLarge = &(m_pHost->m_asbcBmpCaches[ID_LARGE_BMP_CACHE]);
  512. //
  513. // Enumerate all the bitmap cache receive capabilities of the parties
  514. // in the share. The usable size of the send bitmap cache is then the
  515. // minimum of all the remote receive caches and the local send cache
  516. // size.
  517. //
  518. //
  519. // Start by setting the size of the local send bitmap cache to the
  520. // local default values.
  521. //
  522. newSmallCellSize = m_pasLocal->cpcCaps.bitmaps.sender.capsSmallCacheCellSize;
  523. newSmallMaxEntries = m_pasLocal->cpcCaps.bitmaps.sender.capsSmallCacheNumEntries;
  524. newMediumCellSize = m_pasLocal->cpcCaps.bitmaps.sender.capsMediumCacheCellSize;
  525. newMediumMaxEntries = m_pasLocal->cpcCaps.bitmaps.sender.capsMediumCacheNumEntries;
  526. newLargeCellSize = m_pasLocal->cpcCaps.bitmaps.sender.capsLargeCacheCellSize;
  527. newLargeMaxEntries = m_pasLocal->cpcCaps.bitmaps.sender.capsLargeCacheNumEntries;
  528. TRACE_OUT(("Recalced SBC caps: Small {%d of %d}, Medium {%d of %d}, Large {%d of %d}",
  529. newSmallMaxEntries, newSmallCellSize,
  530. newMediumMaxEntries, newMediumCellSize,
  531. newLargeMaxEntries, newLargeCellSize));
  532. //
  533. // If we've changed the size, reset the cache before continuing.
  534. //
  535. if ((pSmall->cCellSize != newSmallCellSize) ||
  536. (pSmall->cEntries != newSmallMaxEntries))
  537. {
  538. m_pHost->SBC_RecreateSendCache(ID_SMALL_BMP_CACHE,
  539. newSmallMaxEntries,
  540. newSmallCellSize);
  541. cacheChanged = TRUE;
  542. }
  543. if ((pMedium->cCellSize != newMediumCellSize) ||
  544. (pMedium->cEntries != newMediumMaxEntries))
  545. {
  546. m_pHost->SBC_RecreateSendCache(ID_MEDIUM_BMP_CACHE,
  547. newMediumMaxEntries,
  548. newMediumCellSize);
  549. cacheChanged = TRUE;
  550. }
  551. if ((pLarge->cCellSize != newLargeCellSize) ||
  552. (pLarge->cEntries != newLargeMaxEntries))
  553. {
  554. m_pHost->SBC_RecreateSendCache(ID_LARGE_BMP_CACHE,
  555. newLargeMaxEntries,
  556. newLargeCellSize);
  557. cacheChanged = TRUE;
  558. }
  559. //
  560. // If we had to recreate any of the send caches, make sure that we
  561. // clear the fast path.
  562. //
  563. if (cacheChanged)
  564. {
  565. m_pHost->SBC_CacheCleared();
  566. }
  567. //
  568. // Handle new capabilities
  569. //
  570. //
  571. // Set up the new capabilities structure...
  572. //
  573. newCapabilities.sendingBpp = m_pHost->m_usrSendingBPP;
  574. newCapabilities.cacheInfo = m_pHost->m_asbcCacheInfo;
  575. //
  576. // ... and pass it through to the driver.
  577. //
  578. if (! OSI_FunctionRequest(SBC_ESC_NEW_CAPABILITIES,
  579. (LPOSI_ESCAPE_HEADER)&newCapabilities,
  580. sizeof(newCapabilities)))
  581. {
  582. ERROR_OUT(("SBC_ESC_NEW_CAPABILITIES failed"));
  583. }
  584. DC_EXIT_POINT:
  585. DebugExitVOID(ASShare::SBC_RecalcCaps);
  586. }
  587. //
  588. // FUNCTION: SBCCacheCallback
  589. //
  590. // DESCRIPTION:
  591. //
  592. // Send BMC Cache Manager callback function. Called whenever an entry is
  593. // removed from the cache to allow us to free up the object.
  594. //
  595. // PARAMETERS:
  596. //
  597. // hCache - cache handle
  598. //
  599. // event - the cache event that has occured
  600. //
  601. // iCacheEntry - index of the cache entry that the event is affecting
  602. //
  603. // pData - pointer to the cache data associated with the given cache entry
  604. //
  605. // cbDataSize - size in bytes of the cached data
  606. //
  607. // RETURNS: Nothing
  608. //
  609. //
  610. void SBCCacheCallback
  611. (
  612. ASHost * pHost,
  613. PCHCACHE pCache,
  614. UINT iCacheEntry,
  615. LPBYTE pData
  616. )
  617. {
  618. UINT cache;
  619. DebugEntry(SBCCacheCallback);
  620. //
  621. // Simply release the cache entry for reuse. We must scan for
  622. // the correct cache root
  623. //
  624. for (cache = 0; cache < NUM_BMP_CACHES; cache++)
  625. {
  626. if (pHost->m_asbcBmpCaches[cache].handle == pCache)
  627. {
  628. pHost->m_asbcBmpCaches[cache].freeEntry = (PBMC_DIB_ENTRY)pData;
  629. pHost->m_asbcBmpCaches[cache].freeEntry->inUse = FALSE;
  630. TRACE_OUT(("0x%08x SBC cache entry 0x%08x now free", pCache, pData));
  631. pHost->SBC_CacheEntryRemoved(cache, iCacheEntry);
  632. break;
  633. }
  634. }
  635. DebugExitVOID(SBCCacheCallback);
  636. }
  637. //
  638. //
  639. // SBC_ProcessMemBltOrder()
  640. //
  641. //
  642. BOOL ASHost::SBC_ProcessMemBltOrder
  643. (
  644. LPINT_ORDER pOrder,
  645. LPINT_ORDER * ppNextOrder
  646. )
  647. {
  648. BOOL rc = FALSE;
  649. UINT orderType;
  650. UINT tileId;
  651. UINT tileType;
  652. LPSBC_TILE_DATA pTileData = NULL;
  653. UINT bitmapWidth;
  654. int bitmapHeight;
  655. LPINT_ORDER pBMCOrder = NULL;
  656. UINT colorCacheIndex;
  657. UINT bitsCache;
  658. UINT bitsCacheIndex;
  659. UINT numColors;
  660. LPLONG pXSrc;
  661. LPLONG pYSrc;
  662. BOOL isNewColorTableEntry;
  663. BOOL isNewBitsEntry;
  664. BOOL canFastPath = TRUE;
  665. LPMEMBLT_ORDER pMemBltOrder = (LPMEMBLT_ORDER)&(pOrder->abOrderData);
  666. LPMEM3BLT_ORDER pMem3BltOrder = (LPMEM3BLT_ORDER)pMemBltOrder;
  667. LPMEMBLT_R2_ORDER pMemBltR2Order = (LPMEMBLT_R2_ORDER)pMemBltOrder;
  668. LPMEM3BLT_R2_ORDER pMem3BltR2Order = (LPMEM3BLT_R2_ORDER)pMemBltOrder;
  669. BITMAPINFO_ours sbcBitmapInfo;
  670. DebugEntry(ASHost::SBC_ProcessMemBltOrder);
  671. *ppNextOrder = NULL;
  672. //
  673. // We may already have processed this MEMBLT order and have the color
  674. // table and bitmap bits for it, ready to go across the wire. This
  675. // would happen if the update packager called this function to process
  676. // the MEMBLT, but then didn't have enough room in its current network
  677. // packet to send the color table or the bitmap bits.
  678. //
  679. // So, if we've already processed this order, bail out now.
  680. //
  681. if (m_sbcOrderInfo.pOrder == pOrder)
  682. {
  683. //
  684. // We've got a match ! Do we have valid data for it ? If we don't
  685. // we must have failed last time, so we'll probably fail again (we
  686. // don't do any memory allocation, so it's unlikely that the error
  687. // condition has cleared up). In any case, we should not have been
  688. // called again if we failed last time...
  689. //
  690. if (m_sbcOrderInfo.validData)
  691. {
  692. TRACE_OUT(( "Already have valid data for this MEMBLT"));
  693. rc = TRUE;
  694. }
  695. else
  696. {
  697. WARNING_OUT(( "Have invalid data for this MEMBLT"));
  698. }
  699. DC_QUIT;
  700. }
  701. //
  702. // Re-initialise m_sbcOrderInfo
  703. //
  704. m_sbcOrderInfo.pOrder = pOrder;
  705. m_sbcOrderInfo.validData = FALSE;
  706. m_sbcOrderInfo.sentColorTable = FALSE;
  707. m_sbcOrderInfo.sentBitmapBits = FALSE;
  708. m_sbcOrderInfo.sentMemBlt = FALSE;
  709. //
  710. // Here's on overview of what we do here...
  711. //
  712. // We've been given a MEMBLT order which references an entry in a shunt
  713. // buffer containing the bits for the MEMBLT at the native bpp (the bpp
  714. // of the display). We want to cache the bits and a color table at the
  715. // protocol bpp. So, we
  716. //
  717. // - copy the bits from the shunt buffer into a work DIB section
  718. // - call GetDIBits to get the data from the work DIB section at the
  719. // protocol bpp
  720. // - cache the bits and the color table
  721. // - if we add new cache entries for the bits and / or the color table,
  722. // we fill in m_sbcOrderInfo.pBitmapBits order and / or
  723. // m_sbcOrderInfo.pColorTableInfo to hold the orders to be sent before
  724. // the MEMBLT order.
  725. //
  726. //
  727. // Make sure that we've been given the correct order type. Note that
  728. // we will never be given the R2 versions of the MEMBLT orders.
  729. //
  730. orderType = pMemBltOrder->type;
  731. ASSERT(((orderType == ORD_MEMBLT_TYPE) ||
  732. (orderType == ORD_MEM3BLT_TYPE)));
  733. //
  734. // Get a pointer to the entry in one of the shunt buffers which matches
  735. // this order.
  736. //
  737. if (orderType == ORD_MEMBLT_TYPE)
  738. {
  739. tileId = pMemBltOrder->cacheId;
  740. }
  741. else
  742. {
  743. tileId = pMem3BltOrder->cacheId;
  744. }
  745. if (!SBCGetTileData(tileId, &pTileData, &tileType))
  746. {
  747. ERROR_OUT(( "Failed to find entry for tile %hx in shunt buffer",
  748. tileId));
  749. DC_QUIT;
  750. }
  751. bitmapWidth = pTileData->width;
  752. bitmapHeight = pTileData->height;
  753. //
  754. // Check if we should do any fast path operations on this bitmap
  755. //
  756. if (pTileData->majorCacheInfo == SBC_DONT_FASTPATH)
  757. {
  758. TRACE_OUT(( "Tile %x should not be fastpathed", tileId));
  759. canFastPath = FALSE;
  760. }
  761. //
  762. // Try to find an entry for this bitmap in the fast path (unless the
  763. // bitmap is marked as being non-fastpathable).
  764. //
  765. if (canFastPath && SBCFindInFastPath(pTileData->majorCacheInfo,
  766. pTileData->minorCacheInfo,
  767. pTileData->majorPalette,
  768. pTileData->minorPalette,
  769. pTileData->srcX,
  770. pTileData->srcY,
  771. pTileData->tilingWidth,
  772. pTileData->tilingHeight,
  773. &bitsCache,
  774. &bitsCacheIndex,
  775. &colorCacheIndex))
  776. {
  777. isNewBitsEntry = FALSE;
  778. isNewColorTableEntry = FALSE;
  779. //
  780. // Call the cache handler to get it to update its MRU entry for
  781. // this cache entry
  782. //
  783. CH_TouchCacheEntry(m_asbcBmpCaches[bitsCache].handle, bitsCacheIndex);
  784. }
  785. else
  786. {
  787. //
  788. // There is no entry in the fast path...
  789. //
  790. // Copy the data from the tile in the shunt buffer into the work
  791. // DIB section. Note that this only works correctly because both
  792. // our work DIB and the tile data are "top down" rather than the
  793. // default of "bottom up". i.e the data for the first scanline is
  794. // stored first in memory. If this wasn't the case, we'd have to
  795. // work out an offset into the work DIB to start copying to.
  796. //
  797. memcpy(m_asbcWorkInfo[tileType].pWorkBitmapBits,
  798. pTileData->bitData,
  799. pTileData->bytesUsed);
  800. //
  801. // Now set up the destination for the GetDIBits call. First set up
  802. // a bitmap info header to pass to GetDIBits. Only the header part
  803. // of the structure will be sent across the network - the color
  804. // table is sent via the palette packets.
  805. //
  806. // Note that we set the height in the bitmap info header to be
  807. // negative. This forces a convertion from our "top down" DIB
  808. // format to the default "bottom up" format which we want to cache
  809. // and send over the wire.
  810. //
  811. ZeroMemory(&sbcBitmapInfo, sizeof(sbcBitmapInfo));
  812. m_pShare->USR_InitDIBitmapHeader((BITMAPINFOHEADER *)&sbcBitmapInfo,
  813. m_usrSendingBPP);
  814. sbcBitmapInfo.bmiHeader.biWidth = m_asbcWorkInfo[tileType].tileWidth;
  815. sbcBitmapInfo.bmiHeader.biHeight = -(int)m_asbcWorkInfo[tileType].tileHeight;
  816. //
  817. // OK, we've set up the source and the destination, so now get the
  818. // data at the protocol bpp. We get the bits into the usr general
  819. // bitmap work buffer.
  820. //
  821. if (GetDIBits(m_usrWorkDC,
  822. m_asbcWorkInfo[tileType].workBitmap,
  823. 0,
  824. bitmapHeight,
  825. m_pShare->m_usrPBitmapBuffer,
  826. (BITMAPINFO *)&sbcBitmapInfo,
  827. DIB_RGB_COLORS) != (int)bitmapHeight)
  828. {
  829. ERROR_OUT(( "GetDIBits failed"));
  830. DC_QUIT;
  831. }
  832. TRACE_OUT(( "%d x %d, (fixed %d) -> (%d, %d)",
  833. bitmapWidth,
  834. bitmapHeight,
  835. m_asbcWorkInfo[tileType].tileWidth,
  836. pMemBltOrder->nLeftRect,
  837. pMemBltOrder->nTopRect));
  838. numColors = COLORS_FOR_BPP(m_usrSendingBPP);
  839. //
  840. // There is no color table to cache if there is no color table at
  841. // all, which is the case when sending at 24BPP
  842. //
  843. if (numColors)
  844. {
  845. //
  846. // Cache the color table. If this succeeds, colorCacheIndex will
  847. // be set up to contain the details of the cache entry which the
  848. // data is cached in. In addition, if isNewColorTableEntry is TRUE
  849. // on return, psbcOrders.colorTableOrder will be fully initialized
  850. // and ready to go across the wire.
  851. //
  852. if (!SBCCacheColorTable(m_sbcOrderInfo.pColorTableOrder,
  853. sbcBitmapInfo.bmiColors,
  854. numColors,
  855. &colorCacheIndex,
  856. &isNewColorTableEntry))
  857. {
  858. TRACE_OUT(( "Failed to cache color table"));
  859. DC_QUIT;
  860. }
  861. ASSERT(colorCacheIndex != COLORCACHEINDEX_NONE);
  862. }
  863. else
  864. {
  865. colorCacheIndex = COLORCACHEINDEX_NONE;
  866. isNewColorTableEntry = FALSE;
  867. }
  868. //
  869. // Cache the bits. If this succeeds, bitsCache and bitsCacheIndex
  870. // will be set up to contain the details of the cache entry which
  871. // the data is cached in. In addition, if isNewBitsEntry is TRUE
  872. // on return, psbcOrders.bitmapBitsOrder will be fully initialized
  873. // and ready to go across the wire.
  874. //
  875. // If this fails, the above values will be undefined.
  876. //
  877. if (!SBCCacheBits(m_sbcOrderInfo.pBitmapBitsOrder,
  878. m_sbcOrderInfo.bitmapBitsDataSize,
  879. m_pShare->m_usrPBitmapBuffer,
  880. bitmapWidth,
  881. m_asbcWorkInfo[tileType].tileWidth,
  882. bitmapHeight,
  883. BYTES_IN_BITMAP(m_asbcWorkInfo[tileType].tileWidth,
  884. bitmapHeight,
  885. sbcBitmapInfo.bmiHeader.biBitCount),
  886. &bitsCache,
  887. &bitsCacheIndex,
  888. &isNewBitsEntry))
  889. {
  890. TRACE_OUT(( "Failed to cache bits"));
  891. DC_QUIT;
  892. }
  893. //
  894. // Add the newly cached item to the fast path (unless the bitmap is
  895. // marked as being non-fastpathable).
  896. //
  897. if (canFastPath)
  898. {
  899. SBCAddToFastPath(pTileData->majorCacheInfo,
  900. pTileData->minorCacheInfo,
  901. pTileData->majorPalette,
  902. pTileData->minorPalette,
  903. pTileData->srcX,
  904. pTileData->srcY,
  905. pTileData->tilingWidth,
  906. pTileData->tilingHeight,
  907. bitsCache,
  908. bitsCacheIndex,
  909. colorCacheIndex);
  910. }
  911. }
  912. //
  913. // We've now got valid cache entries for the DIB bits and the color
  914. // table, so we should now fill them into the MEMBLT order.
  915. //
  916. // Set up the source co-ordinates. For R1 protocols, the x-coordinate
  917. // includes the offset which is required to get the right cell within
  918. // the receive bitmap cache. For R2, we set up the cache entry in a
  919. // separate field.
  920. //
  921. if (orderType == ORD_MEMBLT_TYPE)
  922. {
  923. pXSrc = &pMemBltOrder->nXSrc;
  924. pYSrc = &pMemBltOrder->nYSrc;
  925. }
  926. else
  927. {
  928. pXSrc = &pMem3BltOrder->nXSrc;
  929. pYSrc = &pMem3BltOrder->nYSrc;
  930. }
  931. *pXSrc = *pXSrc % pTileData->tilingWidth;
  932. *pYSrc = *pYSrc % pTileData->tilingHeight;
  933. //
  934. // The sub-bitmap and color table are in the cache. Store a cache
  935. // handle and color handle. Also store the cache index for R2
  936. // protocols (see above).
  937. //
  938. if (orderType == ORD_MEMBLT_TYPE)
  939. {
  940. pMemBltOrder->cacheId = MEMBLT_COMBINEHANDLES(colorCacheIndex,
  941. bitsCache);
  942. pMemBltR2Order->type = (TSHR_UINT16)ORD_MEMBLT_R2_TYPE;
  943. pMemBltR2Order->cacheIndex = (TSHR_UINT16)bitsCacheIndex;
  944. TRACE_OUT(( "MEMBLT color %u bitmap %u:%u",
  945. colorCacheIndex,
  946. bitsCache,
  947. bitsCacheIndex));
  948. }
  949. else
  950. {
  951. pMem3BltOrder->cacheId = MEMBLT_COMBINEHANDLES(colorCacheIndex,
  952. bitsCache);
  953. pMem3BltR2Order->type = ORD_MEM3BLT_R2_TYPE;
  954. pMem3BltR2Order->cacheIndex = (TSHR_UINT16)bitsCacheIndex;
  955. TRACE_OUT(( "MEM3BLT color %u bitmap %u:%u",
  956. colorCacheIndex,
  957. bitsCache,
  958. bitsCacheIndex));
  959. }
  960. //
  961. // Must have successfully completed processing the order to get to
  962. // here. Fill in the appropriate info in the m_sbcOrderInfo structure.
  963. // If we got a cache hit on the color table or the bitmap bits then
  964. // we've already sent the data for them.
  965. //
  966. m_sbcOrderInfo.validData = TRUE;
  967. m_sbcOrderInfo.sentColorTable = !isNewColorTableEntry;
  968. m_sbcOrderInfo.sentBitmapBits = !isNewBitsEntry;
  969. rc = TRUE;
  970. DC_EXIT_POINT:
  971. if (rc)
  972. {
  973. //
  974. // We've successfully processed the MEMBLT, so set up a pointer to
  975. // the next order which should be sent by the caller.
  976. //
  977. // Note that if we have already sent these orders, then we return
  978. // a NULL order.
  979. //
  980. if (!m_sbcOrderInfo.sentColorTable)
  981. {
  982. TRACE_OUT(( "Returning color table order"));
  983. *ppNextOrder = m_sbcOrderInfo.pColorTableOrder;
  984. }
  985. else if (!m_sbcOrderInfo.sentBitmapBits)
  986. {
  987. TRACE_OUT(( "Returning bitmap bits order"));
  988. *ppNextOrder = m_sbcOrderInfo.pBitmapBitsOrder;
  989. }
  990. else if (!m_sbcOrderInfo.sentMemBlt)
  991. {
  992. TRACE_OUT(( "Returning MemBlt order"));
  993. *ppNextOrder = pOrder;
  994. }
  995. else
  996. {
  997. TRACE_OUT(( "No order to return"));
  998. rc = FALSE;
  999. }
  1000. }
  1001. //
  1002. // We've finished with the entry in the shunt buffer, so reset the
  1003. // inUse flag to allow the driver to re-use it.
  1004. //
  1005. if (pTileData != NULL)
  1006. {
  1007. pTileData->inUse = FALSE;
  1008. }
  1009. DebugExitBOOL(ASHost::SBC_ProcessMemBltOrder, rc);
  1010. return(rc);
  1011. }
  1012. //
  1013. //
  1014. // SBC_OrderSentNotification()
  1015. //
  1016. //
  1017. void ASHost::SBC_OrderSentNotification(LPINT_ORDER pOrder)
  1018. {
  1019. DebugEntry(ASHost::SBC_OrderSentNotification);
  1020. //
  1021. // pOrder should be a pointer to either our internal bitmap bits order,
  1022. // or our color table order.
  1023. //
  1024. if (pOrder == m_sbcOrderInfo.pBitmapBitsOrder)
  1025. {
  1026. TRACE_OUT(( "Bitmap bits order has been sent"));
  1027. m_sbcOrderInfo.sentBitmapBits = TRUE;
  1028. }
  1029. else if (pOrder == m_sbcOrderInfo.pColorTableOrder)
  1030. {
  1031. TRACE_OUT(( "Color table order has been sent"));
  1032. m_sbcOrderInfo.sentColorTable = TRUE;
  1033. }
  1034. else if (pOrder == m_sbcOrderInfo.pOrder)
  1035. {
  1036. TRACE_OUT(( "Memblt order has been sent"));
  1037. m_sbcOrderInfo.sentMemBlt = TRUE;
  1038. //
  1039. // All parts of the Memblt have been sent now, so reset our pointer
  1040. // to the order. This avoids a problem where
  1041. // SBC_ProcessMemBltOrder is called twice in a row with the same
  1042. // pOrder, but with different data (i.e. consecutive MemBlts
  1043. // ending up in the same point in the order heap). It can happen...
  1044. //
  1045. m_sbcOrderInfo.pOrder = NULL;
  1046. }
  1047. else
  1048. {
  1049. ERROR_OUT(( "Notification for unknown order %#.8lx", pOrder));
  1050. }
  1051. DebugExitVOID(ASHost::SBC_OrderSentNotification);
  1052. }
  1053. //
  1054. //
  1055. // SBC_ProcessInternalOrder()
  1056. //
  1057. //
  1058. void ASHost::SBC_ProcessInternalOrder(LPINT_ORDER pOrder)
  1059. {
  1060. UINT orderType;
  1061. LPINT_COLORTABLE_ORDER_1BPP pColorTableOrder;
  1062. HBITMAP oldBitmap = 0;
  1063. UINT numEntries;
  1064. int i;
  1065. DebugEntry(ASHost::SBC_ProcessInternalOrder);
  1066. //
  1067. // Make sure that we've been given an order type which we recognise.
  1068. // Currently, the only internal order we support is a color table
  1069. // order.
  1070. //
  1071. pColorTableOrder = (LPINT_COLORTABLE_ORDER_1BPP)&(pOrder->abOrderData);
  1072. orderType = pColorTableOrder->header.type;
  1073. ASSERT(orderType == INTORD_COLORTABLE_TYPE);
  1074. //
  1075. // Make sure that the color table order is the same bpp as the work DIB
  1076. // sections.
  1077. //
  1078. ASSERT(pColorTableOrder->header.bpp == g_usrCaptureBPP);
  1079. //
  1080. // All we have to do is to copy the color table from the order into our
  1081. // two work DIB sections. To do that, we have to select the DIB
  1082. // sections into a DC then set the color table for the DC - this sets
  1083. // the color table in the DIB section.
  1084. //
  1085. numEntries = COLORS_FOR_BPP(g_usrCaptureBPP);
  1086. ASSERT(numEntries);
  1087. for (i = 0 ; i < SBC_NUM_TILE_SIZES; i++)
  1088. {
  1089. oldBitmap = SelectBitmap(m_usrWorkDC, m_asbcWorkInfo[i].workBitmap);
  1090. SetDIBColorTable(m_usrWorkDC,
  1091. 0, // First index
  1092. numEntries, // Number of entries
  1093. (RGBQUAD*)pColorTableOrder->colorData);
  1094. }
  1095. if (oldBitmap != NULL)
  1096. {
  1097. SelectBitmap(m_usrWorkDC, oldBitmap);
  1098. }
  1099. DebugExitVOID(ASHost::SBC_ProcessInternalOrder);
  1100. }
  1101. //
  1102. //
  1103. // SBC_PMCacheEntryRemoved()
  1104. //
  1105. //
  1106. void ASHost::SBC_PMCacheEntryRemoved(UINT cacheIndex)
  1107. {
  1108. LPSBC_FASTPATH_ENTRY pEntry;
  1109. LPSBC_FASTPATH_ENTRY pNextEntry;
  1110. DebugEntry(ASHost::SBC_PMCacheEntryRemoved);
  1111. ASSERT(m_sbcFastPath);
  1112. //
  1113. // An entry has been removed from the color cache. We have to remove
  1114. // all entries from the fast path which reference this color table.
  1115. //
  1116. TRACE_OUT(( "Color table cache entry %d removed - removing references",
  1117. cacheIndex));
  1118. pEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListFirst(&m_sbcFastPath->usedList, FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  1119. while (pEntry != NULL)
  1120. {
  1121. pNextEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListNext(&m_sbcFastPath->usedList, pEntry,
  1122. FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  1123. if (pEntry->colorIndex == cacheIndex)
  1124. {
  1125. COM_BasedListRemove(&pEntry->list);
  1126. COM_BasedListInsertAfter(&m_sbcFastPath->freeList, &pEntry->list);
  1127. }
  1128. pEntry = pNextEntry;
  1129. }
  1130. DebugExitVOID(ASHost::SBC_PMCacheEntryRemoved);
  1131. }
  1132. //
  1133. //
  1134. // Name: SBCInitInternalOrders
  1135. //
  1136. // Purpose: Allocate memory for the internal orders used during MEMBLT
  1137. // order processing.
  1138. //
  1139. // Returns: TRUE if initialized OK, FALSE otherwise.
  1140. //
  1141. // Params: None
  1142. //
  1143. // Operation: If successful, this function initializes the following
  1144. //
  1145. // g_Share->sbcOrderInfo
  1146. //
  1147. //
  1148. BOOL ASHost::SBCInitInternalOrders(void)
  1149. {
  1150. BOOL initOK = FALSE;
  1151. UINT orderSize;
  1152. LPINT_ORDER_HEADER pOrderHeader;
  1153. DebugEntry(ASHost::SBCInitInternalOrders);
  1154. //
  1155. // Start with the bitmap bits order. Calculate the number of bytes
  1156. // required to store the bits for the largest bitmap bits order we will
  1157. // ever send. This includes room for the compression header which gets
  1158. // added before the bits if the data is compressed.
  1159. //
  1160. if (g_usrCaptureBPP >= 24)
  1161. {
  1162. // Can possibly send 24bpp TRUE COLOR data
  1163. m_sbcOrderInfo.bitmapBitsDataSize =
  1164. BYTES_IN_BITMAP(MP_LARGE_TILE_WIDTH, MP_LARGE_TILE_HEIGHT, 24)
  1165. + sizeof(CD_HEADER);
  1166. }
  1167. else
  1168. {
  1169. // Can't send 24bpp TRUE color data
  1170. m_sbcOrderInfo.bitmapBitsDataSize =
  1171. BYTES_IN_BITMAP(MP_LARGE_TILE_WIDTH, MP_LARGE_TILE_WIDTH, 8)
  1172. + sizeof(CD_HEADER);
  1173. }
  1174. //
  1175. // Now allocate memory for the bitmap bits order. The size required
  1176. // is:
  1177. // The size of an INT_ORDER_HEADER (this is added in by OA when you
  1178. // call OA_AllocOrderMem)
  1179. // + the size of the largest BMC_BITMAP_BITS_ORDER structure
  1180. // + the number of bytes required for the bitmap bits
  1181. // + contingency for RLE compression overruns !
  1182. //
  1183. orderSize = sizeof(INT_ORDER_HEADER)
  1184. + sizeof(BMC_BITMAP_BITS_ORDER_R2)
  1185. + m_sbcOrderInfo.bitmapBitsDataSize
  1186. + 4;
  1187. TRACE_OUT(( "Allocating %d bytes for SBC bitmap bits order (bits %d)",
  1188. orderSize,
  1189. m_sbcOrderInfo.bitmapBitsDataSize));
  1190. m_sbcOrderInfo.pBitmapBitsOrder = (LPINT_ORDER)new BYTE[orderSize];
  1191. if (!m_sbcOrderInfo.pBitmapBitsOrder)
  1192. {
  1193. ERROR_OUT((
  1194. "Failed to alloc %d bytes for SBC bitmap bits order (bits %d)",
  1195. orderSize,
  1196. m_sbcOrderInfo.bitmapBitsDataSize));
  1197. DC_QUIT;
  1198. }
  1199. //
  1200. // Initialize the INT_ORDER_HEADER - this is normally done in
  1201. // OA_AllocOrderMem(). For the bitmap bits order, we can't fill in the
  1202. // orderLength because it is not a fixed size - this has to be done
  1203. // later when we fill in the bitmap bits. Note that the order length
  1204. // excludes the size of the INT_ORDER_HEADER.
  1205. //
  1206. pOrderHeader = &m_sbcOrderInfo.pBitmapBitsOrder->OrderHeader;
  1207. pOrderHeader->additionalOrderData = 0;
  1208. pOrderHeader->cbAdditionalOrderDataLength = 0;
  1209. //
  1210. // Now the color table order. The size required is:
  1211. // The size of an INT_ORDER_HEADER (this is added in by OA when you
  1212. // call OA_AllocOrderMem)
  1213. // + the size of a BMC_COLOR_TABLE_ORDER structure
  1214. // + the number of bytes required for the color table entries (note
  1215. // that the BMC_COLOR_TABLE_ORDER structure contains the first
  1216. // color table entry, so adjust the number of extra bytes required)
  1217. //
  1218. // Color tables are only for 8bpp and less.
  1219. orderSize = sizeof(INT_ORDER_HEADER)
  1220. + sizeof(BMC_COLOR_TABLE_ORDER)
  1221. + (COLORS_FOR_BPP(8) - 1) * sizeof(TSHR_RGBQUAD);
  1222. TRACE_OUT(( "Allocating %d bytes for SBC color table order", orderSize));
  1223. m_sbcOrderInfo.pColorTableOrder = (LPINT_ORDER)new BYTE[orderSize];
  1224. if (!m_sbcOrderInfo.pColorTableOrder)
  1225. {
  1226. ERROR_OUT(( "Failed to alloc %d bytes for SBC color table order",
  1227. orderSize));
  1228. DC_QUIT;
  1229. }
  1230. pOrderHeader = &m_sbcOrderInfo.pColorTableOrder->OrderHeader;
  1231. pOrderHeader->additionalOrderData = 0;
  1232. pOrderHeader->cbAdditionalOrderDataLength = 0;
  1233. pOrderHeader->Common.cbOrderDataLength = (WORD)(orderSize - sizeof(INT_ORDER_HEADER));
  1234. //
  1235. // Fill in the remaining fields in m_sbcOrderInfo
  1236. //
  1237. m_sbcOrderInfo.pOrder = NULL;
  1238. m_sbcOrderInfo.validData = FALSE;
  1239. m_sbcOrderInfo.sentColorTable = FALSE;
  1240. m_sbcOrderInfo.sentBitmapBits = FALSE;
  1241. m_sbcOrderInfo.sentMemBlt = FALSE;
  1242. //
  1243. // Must be OK to get to here
  1244. //
  1245. initOK = TRUE;
  1246. DC_EXIT_POINT:
  1247. DebugExitDWORD(ASHost::SBCInitInternalOrders, initOK);
  1248. return(initOK);
  1249. }
  1250. //
  1251. //
  1252. // Name: SBCFreeInternalOrders
  1253. //
  1254. // Purpose: Free up the internal orders used by SBC during MEMBLT order
  1255. // processing.
  1256. //
  1257. // Returns: Nothing
  1258. //
  1259. // Params: None
  1260. //
  1261. //
  1262. void ASHost::SBCFreeInternalOrders(void)
  1263. {
  1264. DebugEntry(ASHost::SBCFreeInternalOrders);
  1265. //
  1266. // First free up the memory.
  1267. //
  1268. if (m_sbcOrderInfo.pBitmapBitsOrder)
  1269. {
  1270. delete m_sbcOrderInfo.pBitmapBitsOrder;
  1271. m_sbcOrderInfo.pBitmapBitsOrder = NULL;
  1272. }
  1273. if (m_sbcOrderInfo.pColorTableOrder)
  1274. {
  1275. delete m_sbcOrderInfo.pColorTableOrder;
  1276. m_sbcOrderInfo.pColorTableOrder = NULL;
  1277. }
  1278. //
  1279. // Now reset the remaining fields in m_sbcOrderInfo
  1280. //
  1281. m_sbcOrderInfo.pOrder = NULL;
  1282. m_sbcOrderInfo.validData = FALSE;
  1283. m_sbcOrderInfo.sentColorTable = FALSE;
  1284. m_sbcOrderInfo.sentBitmapBits = FALSE;
  1285. m_sbcOrderInfo.bitmapBitsDataSize = 0;
  1286. DebugExitVOID(ASHost::SBCFreeInternalOrders);
  1287. }
  1288. //
  1289. //
  1290. // Name: SBCInitFastPath
  1291. //
  1292. // Purpose: Initialize the SBC fast path
  1293. //
  1294. // Returns: TRUE if successful, FALSE otherwise
  1295. //
  1296. // Params: None
  1297. //
  1298. //
  1299. BOOL ASHost::SBCInitFastPath(void)
  1300. {
  1301. BOOL rc = FALSE;
  1302. DebugEntry(ASHost::SBCInitFastPath);
  1303. m_sbcFastPath = new SBC_FASTPATH;
  1304. if (!m_sbcFastPath)
  1305. {
  1306. ERROR_OUT(("Failed to alloc m_sbcFastPath"));
  1307. DC_QUIT;
  1308. }
  1309. SET_STAMP(m_sbcFastPath, SBCFASTPATH);
  1310. //
  1311. // Initialize the structure.
  1312. //
  1313. SBC_CacheCleared();
  1314. rc = TRUE;
  1315. DC_EXIT_POINT:
  1316. DebugExitBOOL(ASHost::SBCInitFastPath, rc);
  1317. return(rc);
  1318. }
  1319. //
  1320. //
  1321. // Name: SBCGetTileData
  1322. //
  1323. // Purpose: Given the ID of a tile data entry in one of the SBC shunt
  1324. // buffers, return a pointer to the entry with that ID.
  1325. //
  1326. // Returns: TRUE if the entry is found, FALSE otherwise
  1327. //
  1328. // Params: IN tileId - The ID of the shunt buffer entry to be
  1329. // found.
  1330. // OUT ppTileData - A pointer to the start of the shunt buffer
  1331. // entry (if found)
  1332. // OUT pTileType - The type of shunt buffer entry found. One
  1333. // of:
  1334. // SBC_MEDIUM_TILE
  1335. // SBC_LARGE_TILE
  1336. //
  1337. //
  1338. BOOL ASHost::SBCGetTileData
  1339. (
  1340. UINT tileId,
  1341. LPSBC_TILE_DATA * ppTileData,
  1342. LPUINT pTileType
  1343. )
  1344. {
  1345. BOOL gotTileData = FALSE;
  1346. UINT workTile;
  1347. LPSBC_TILE_DATA pWorkTile;
  1348. DebugEntry(ASHost::SBCGetTileData);
  1349. TRACE_OUT(( "Looking for tile Id %x", tileId));
  1350. //
  1351. // Find out which of the shunt buffers the entry should be in.
  1352. //
  1353. *pTileType = SBC_TILE_TYPE(tileId);
  1354. //
  1355. // We implement the shunt buffers as circular FIFO queues, so in
  1356. // general, we are looking for the entry following the last one which
  1357. // we found. However, this wont always be the case because we do some
  1358. // out of order processing when we do spoiling.
  1359. //
  1360. // So, get the index of the last tile we accessed.
  1361. //
  1362. workTile = m_asbcWorkInfo[*pTileType].mruIndex;
  1363. //
  1364. // OK, so lets go for it ! Start at the tile following the last one we
  1365. // accessed, and loop through the circular buffer until we get a match,
  1366. // or have circled back to the beginning.
  1367. //
  1368. // Note that this has been coded as a "do while" loop, rather than just
  1369. // a "while" loop so that we don't miss mruTile.
  1370. //
  1371. do
  1372. {
  1373. //
  1374. // On to the next tile
  1375. //
  1376. workTile++;
  1377. if (workTile == m_asbcWorkInfo[*pTileType].pShuntBuffer->numEntries)
  1378. {
  1379. workTile = 0;
  1380. }
  1381. pWorkTile = SBCTilePtrFromIndex(m_asbcWorkInfo[*pTileType].pShuntBuffer,
  1382. workTile);
  1383. if (pWorkTile->inUse)
  1384. {
  1385. if (pWorkTile->tileId == tileId)
  1386. {
  1387. //
  1388. // We've got a match.
  1389. //
  1390. TRACE_OUT(( "Matched tile Id %x at index %d",
  1391. tileId,
  1392. workTile));
  1393. *ppTileData = pWorkTile;
  1394. gotTileData = TRUE;
  1395. m_asbcWorkInfo[*pTileType].mruIndex = workTile;
  1396. DC_QUIT;
  1397. }
  1398. }
  1399. }
  1400. while (workTile != m_asbcWorkInfo[*pTileType].mruIndex);
  1401. //
  1402. // If we get to here, we've not found a match.
  1403. //
  1404. TRACE_OUT(( "No match for tile Id %x", tileId));
  1405. DC_EXIT_POINT:
  1406. DebugExitBOOL(ASHost::SBCGetTileData, gotTileData);
  1407. return(gotTileData);
  1408. }
  1409. //
  1410. //
  1411. // Name: SBCCacheColorTable
  1412. //
  1413. // Purpose: Ensure that the given color table is cached.
  1414. //
  1415. // Returns: TRUE if the color table is cached successfully, FALSE
  1416. // otherwise.
  1417. //
  1418. // Params: IN pOrder - A pointer to a color table order to be
  1419. // filled in.
  1420. // IN pColorTable - A pointer to the start of the color table
  1421. // to be cached.
  1422. // IN numColors - The number of colors in the color table.
  1423. // OUT pCacheIndex - The index of the cached color table.
  1424. // OUT pIsNewEntry - TRUE if we added a new cache entry,
  1425. // FALSE if we matched an existing entry.
  1426. //
  1427. // Operation: pOrder is only filled in if *pIsNewEntry is FALSE.
  1428. //
  1429. //
  1430. BOOL ASHost::SBCCacheColorTable
  1431. (
  1432. LPINT_ORDER pOrder,
  1433. LPTSHR_RGBQUAD pColorTable,
  1434. UINT numColors,
  1435. UINT * pCacheIndex,
  1436. LPBOOL pIsNewEntry
  1437. )
  1438. {
  1439. BOOL cachedOK = FALSE;
  1440. UINT cacheIndex;
  1441. PBMC_COLOR_TABLE_ORDER pColorTableOrder;
  1442. DebugEntry(ASHost::SBCCacheColorTable);
  1443. //
  1444. // Call PM to do the caching.
  1445. //
  1446. if (!PM_CacheTxColorTable(&cacheIndex,
  1447. pIsNewEntry,
  1448. numColors,
  1449. pColorTable))
  1450. {
  1451. ERROR_OUT(( "Failed to cache color table"));
  1452. DC_QUIT;
  1453. }
  1454. //
  1455. // If the cache operation resulted in a cache update then we have to
  1456. // fill in the color table order.
  1457. //
  1458. if (*pIsNewEntry)
  1459. {
  1460. //
  1461. // The color table is new so we have to transmit it
  1462. //
  1463. TRACE_OUT(( "New color table"));
  1464. pOrder->OrderHeader.Common.fOrderFlags = OF_PRIVATE;
  1465. pColorTableOrder = (PBMC_COLOR_TABLE_ORDER)(pOrder->abOrderData);
  1466. pColorTableOrder->bmcPacketType = BMC_PT_COLOR_TABLE;
  1467. pColorTableOrder->colorTableSize = (TSHR_UINT16)numColors;
  1468. pColorTableOrder->index = (BYTE)cacheIndex;
  1469. //
  1470. // Copy the new color table into the Order Packet.
  1471. //
  1472. memcpy(pColorTableOrder->data, pColorTable,
  1473. numColors * sizeof(TSHR_RGBQUAD));
  1474. }
  1475. else
  1476. {
  1477. TRACE_OUT(( "Existing color table"));
  1478. }
  1479. //
  1480. // Return the color table index to the caller
  1481. //
  1482. *pCacheIndex = cacheIndex;
  1483. cachedOK = TRUE;
  1484. DC_EXIT_POINT:
  1485. DebugExitBOOL(ASHost::SBCCacheColorTable, cachedOK);
  1486. return(cachedOK);
  1487. }
  1488. //
  1489. //
  1490. // Name: SBCCacheBits
  1491. //
  1492. // Purpose: This function adds the supplied bitmap bits to a bitmap
  1493. // cache. The cache selected depends on the bitmap size, but
  1494. // may be different for R1 and R2. SBCSelectCache handles the
  1495. // determination of the correct cache.
  1496. //
  1497. // Returns: TRUE if the bits have been cached OK, FALSE otherwise
  1498. //
  1499. // Params: IN pOrder - A pointer to a BMC order.
  1500. // IN destBitsSize - The number of bytes available in
  1501. // pOrder to store the bitmap data.
  1502. // IN pDIBits - A pointer to the bits to be cached.
  1503. // IN bitmapWidth - The "in use" width of the bitmap
  1504. // IN fixedBitmapWidth - The actual width of the bitmap
  1505. // IN bitmapHeight - The height of the bitmap
  1506. // IN numBytes - The number of bytes in the bitmap.
  1507. // OUT pCache - The cache that we put the bits into.
  1508. // OUT pCacheIndex - The cache index within *pCache at
  1509. // which we cached the data.
  1510. // OUT pIsNewEntry - TRUE if we added a new cache entry,
  1511. // FALSE if we matched an existing entry.
  1512. //
  1513. // Operation: pOrder is only filled in if *pIsNewEntry is FALSE.
  1514. //
  1515. //
  1516. BOOL ASHost::SBCCacheBits
  1517. (
  1518. LPINT_ORDER pOrder,
  1519. UINT destBitsSize,
  1520. LPBYTE pDIBits,
  1521. UINT bitmapWidth,
  1522. UINT fixedBitmapWidth,
  1523. UINT bitmapHeight,
  1524. UINT numBytes,
  1525. UINT * pCache,
  1526. UINT * pCacheIndex,
  1527. LPBOOL pIsNewEntry
  1528. )
  1529. {
  1530. BOOL cachedOK = FALSE;
  1531. UINT cacheIndex;
  1532. UINT i;
  1533. LPBYTE pCompressed;
  1534. UINT compressedSize;
  1535. BOOL compressed;
  1536. PBMC_DIB_ENTRY pEntry;
  1537. PBMC_DIB_CACHE pCacheHdr;
  1538. PBMC_BITMAP_BITS_ORDER_R2 pBitsOrderR2;
  1539. PBMC_BITMAP_BITS_DATA pBmcData;
  1540. LPBYTE pDestBits;
  1541. DebugEntry(ASHost::SBCCacheBits);
  1542. pBmcData = (PBMC_BITMAP_BITS_DATA)(pOrder->abOrderData);
  1543. pBitsOrderR2 = (PBMC_BITMAP_BITS_ORDER_R2)pBmcData;
  1544. //
  1545. // Get a pointer to where the bitmap data starts in the order. This
  1546. // depends on whether it is an R1 or an R2 bitmap bits order.
  1547. //
  1548. pDestBits = pBitsOrderR2->data;
  1549. //
  1550. // Before we can select a cache entry we need to compress the bits.
  1551. // This therefore mandates a memcpy into the cache entry when we come
  1552. // to add it. The saving in memory by storing the bits compressed
  1553. // makes it all worthwhile.
  1554. //
  1555. // Compress the bitmap data. At this stage we don't know whether the
  1556. // bitmap will compress well or not, so allow cells that are larger
  1557. // than our maximum cell size. The largest we expect to see is 120*120*
  1558. // 24.
  1559. //
  1560. compressedSize = destBitsSize;
  1561. if (m_pShare->BC_CompressBitmap(pDIBits, pDestBits, &compressedSize,
  1562. fixedBitmapWidth, bitmapHeight, m_usrSendingBPP,
  1563. NULL ) &&
  1564. (compressedSize < numBytes))
  1565. {
  1566. TRACE_OUT(( "Compressed bmp data from %u bytes to %u bytes",
  1567. numBytes,
  1568. compressedSize));
  1569. compressed = TRUE;
  1570. pCompressed = pDestBits;
  1571. }
  1572. else
  1573. {
  1574. //
  1575. // The bitmap could not be compressed, or bitmap compression is not
  1576. // enabled. Send the bitmap uncompressed.
  1577. //
  1578. compressed = FALSE;
  1579. compressedSize = numBytes;
  1580. pCompressed = pDIBits;
  1581. }
  1582. //
  1583. // Make sure that the data will fit into the order. Do this after
  1584. // compression since it is possible that the uncompressed data will not
  1585. // fit, but the compressed version will.
  1586. //
  1587. if (compressedSize > destBitsSize)
  1588. {
  1589. WARNING_OUT(( "Data (%d bytes) does not fit into order (%d bytes)",
  1590. compressedSize,
  1591. destBitsSize));
  1592. DC_QUIT;
  1593. }
  1594. //
  1595. // Select the cache based on the compressed size - we pass in the
  1596. // sub-bitmap dimensions for R1 caching; R2 caching just uses the
  1597. // total size of the bits.
  1598. //
  1599. if (!SBCSelectCache(compressedSize + sizeof(BMC_DIB_ENTRY) - 1, pCache))
  1600. {
  1601. TRACE_OUT(( "No cache selected"));
  1602. DC_QUIT;
  1603. }
  1604. else
  1605. {
  1606. TRACE_OUT(( "Selected cache %d", *pCache));
  1607. }
  1608. //
  1609. // Find a free cache entry in our selected cache
  1610. //
  1611. // We arrange that our transmit cache is always one greater than the
  1612. // negotiated cache size so that we should never fail to find a free
  1613. // array entry. Once we have fully populated our Tx cache we will
  1614. // always find the free entry as the one last given back to us by CH.
  1615. // Note the scan to <= sbcTxCache[pmNumTxCacheEntries is NOT a mistake.
  1616. //
  1617. pCacheHdr = &(m_asbcBmpCaches[*pCache]);
  1618. if (pCacheHdr->data == NULL)
  1619. {
  1620. ERROR_OUT(( "Asked to cache when no cache allocated"));
  1621. DC_QUIT;
  1622. }
  1623. //
  1624. // If the cache has returned an entry to us then use that without
  1625. // having to scan. This will be the default mode for adding entries
  1626. // to a fully populated cache.
  1627. //
  1628. if (pCacheHdr->freeEntry != NULL)
  1629. {
  1630. pEntry = pCacheHdr->freeEntry;
  1631. pCacheHdr->freeEntry = NULL;
  1632. TRACE_OUT(( "Cache fully populated - using entry 0x%08x", pEntry));
  1633. }
  1634. else
  1635. {
  1636. //
  1637. // We are in the process of feeding the cache so we need to search
  1638. // for a free entry
  1639. //
  1640. pEntry = (PBMC_DIB_ENTRY)(pCacheHdr->data);
  1641. for (i=0 ; i < pCacheHdr->cEntries ; i++)
  1642. {
  1643. if (!pEntry->inUse)
  1644. {
  1645. break;
  1646. }
  1647. pEntry = (PBMC_DIB_ENTRY)(((LPBYTE)pEntry) + pCacheHdr->cSize);
  1648. }
  1649. //
  1650. // We should never run out of free entries, but cope with it
  1651. //
  1652. if (i == pCacheHdr->cEntries)
  1653. {
  1654. ERROR_OUT(( "All Tx DIB cache entries in use"));
  1655. DC_QUIT;
  1656. }
  1657. }
  1658. //
  1659. // Set up the DIB entry for caching
  1660. //
  1661. pEntry->inUse = TRUE;
  1662. pEntry->cx = (TSHR_UINT16)bitmapWidth;
  1663. pEntry->cxFixed = (TSHR_UINT16)fixedBitmapWidth;
  1664. pEntry->cy = (TSHR_UINT16)bitmapHeight;
  1665. pEntry->bpp = (TSHR_UINT16)m_usrSendingBPP;
  1666. pEntry->cBits = numBytes;
  1667. pEntry->bCompressed = (BYTE)compressed;
  1668. pEntry->cCompressed = compressedSize;
  1669. memcpy(pEntry->bits, pCompressed, compressedSize);
  1670. //
  1671. // Now cache the data
  1672. //
  1673. if (CH_SearchAndCacheData(pCacheHdr->handle,
  1674. (LPBYTE)pEntry,
  1675. sizeof(BMC_DIB_ENTRY) + compressedSize - 1,
  1676. 0,
  1677. &cacheIndex))
  1678. {
  1679. //
  1680. // The sub-bitmap is already in the cache
  1681. //
  1682. *pCacheIndex = cacheIndex;
  1683. TRACE_OUT(( "Bitmap already cached %u:%u cx(%d) cy(%d)",
  1684. *pCache,
  1685. *pCacheIndex,
  1686. bitmapWidth,
  1687. bitmapHeight));
  1688. *pIsNewEntry = FALSE;
  1689. //
  1690. // Free up the entry we just created
  1691. //
  1692. pEntry->inUse = FALSE;
  1693. }
  1694. else
  1695. {
  1696. *pCacheIndex = cacheIndex;
  1697. TRACE_OUT(( "Cache entry at 0x%08x now in use", pEntry));
  1698. TRACE_OUT(( "New cache entry %u:%u cx(%d) cy(%d)",
  1699. *pCache,
  1700. *pCacheIndex,
  1701. bitmapWidth,
  1702. bitmapHeight));
  1703. *pIsNewEntry = TRUE;
  1704. pEntry->iCacheIndex = (TSHR_UINT16)*pCacheIndex;
  1705. }
  1706. //
  1707. // We've got the bits into the cache. If the cache attempt added a
  1708. // cache entry we must fill in the bitmap cache order.
  1709. //
  1710. if (*pIsNewEntry)
  1711. {
  1712. //
  1713. // Fill in the order details.
  1714. //
  1715. // Remember that we have to fill in the order size into the
  1716. // INT_ORDER_HEADER as well as filling in the bitmap bits order
  1717. // header. When doing this, adjust for the number of bitmap bits
  1718. // which are included in the bitmap bits order header.
  1719. //
  1720. pOrder->OrderHeader.Common.fOrderFlags = OF_PRIVATE;
  1721. if (compressed)
  1722. {
  1723. pBmcData->bmcPacketType = BMC_PT_BITMAP_BITS_COMPRESSED;
  1724. }
  1725. else
  1726. {
  1727. pBmcData->bmcPacketType = BMC_PT_BITMAP_BITS_UNCOMPRESSED;
  1728. //
  1729. // The data is not compressed, so copy the uncompressed data
  1730. // into the order. In the case where we compressed the data
  1731. // successfully, we did so directly into the order, so the
  1732. // compressed bits are already there.
  1733. //
  1734. memcpy(pDestBits, pDIBits, compressedSize);
  1735. }
  1736. pBmcData->cacheID = (BYTE)*pCache;
  1737. pBmcData->cxSubBitmapWidth = (TSHR_UINT8)fixedBitmapWidth;
  1738. pBmcData->cySubBitmapHeight = (TSHR_UINT8)bitmapHeight;
  1739. pBmcData->bpp = (TSHR_UINT8)m_usrSendingBPP;
  1740. pBmcData->cbBitmapBits = (TSHR_UINT16)compressedSize;
  1741. //
  1742. // The iCacheEntryR1 field is unused for R2 - we use
  1743. // iCacheEntryR2 instead.
  1744. //
  1745. pBmcData->iCacheEntryR1 = 0;
  1746. pBitsOrderR2->iCacheEntryR2 = (TSHR_UINT16)*pCacheIndex;
  1747. pOrder->OrderHeader.Common.cbOrderDataLength =
  1748. (compressedSize
  1749. + sizeof(BMC_BITMAP_BITS_ORDER_R2)
  1750. - sizeof(pBitsOrderR2->data));
  1751. }
  1752. cachedOK = TRUE;
  1753. DC_EXIT_POINT:
  1754. DebugExitBOOL(ASHost::SBCCacheBits, cachedOK);
  1755. return(cachedOK);
  1756. }
  1757. //
  1758. //
  1759. // Name: SBCAddToFastPath
  1760. //
  1761. // Purpose: Add a bitmap to the fast path
  1762. //
  1763. // Returns: Nothing
  1764. //
  1765. // Params: IN majorInfo - The major caching info passed up from
  1766. // the driver (the bitmap ID)
  1767. // IN minorInfo - The minor caching info passed up from
  1768. // the driver (the bitmap revision number)
  1769. // IN majorPalette - The major palette info passed up from
  1770. // the driver (the XLATEOBJ)
  1771. // IN minorPalette - The minor palette info passed up from
  1772. // the driver (the XLATEOBJ iUniq)
  1773. // IN srcX - The x coord of the source of the Blt
  1774. // IN srcY - The y coord of the source of the Blt
  1775. // IN width - The width of the area being Blted
  1776. // IN height - The height of the area being Blted
  1777. // IN cache - The cache the bits were placed in
  1778. // IN cacheIndex - The index at which the bits were placed
  1779. // in the cache
  1780. // IN colorCacheIndex - The index in the color table cache of
  1781. // the color table associated with the bits
  1782. //
  1783. //
  1784. void ASHost::SBCAddToFastPath
  1785. (
  1786. UINT_PTR majorInfo,
  1787. UINT minorInfo,
  1788. UINT_PTR majorPalette,
  1789. UINT minorPalette,
  1790. int srcX,
  1791. int srcY,
  1792. UINT width,
  1793. UINT height,
  1794. UINT cache,
  1795. UINT cacheIndex,
  1796. UINT colorCacheIndex
  1797. )
  1798. {
  1799. LPSBC_FASTPATH_ENTRY pEntry;
  1800. DebugEntry(ASHost::SBCAddToFastPath);
  1801. //
  1802. // First get a free entry
  1803. //
  1804. pEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListFirst(&m_sbcFastPath->freeList,
  1805. FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  1806. if (pEntry == NULL)
  1807. {
  1808. //
  1809. // There are no entries in the free list, so we have to use the
  1810. // oldest entry in the used list. The used list is stored in MRU
  1811. // order, so we just have to get the last item in the list.
  1812. //
  1813. pEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListLast(&m_sbcFastPath->usedList,
  1814. FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  1815. TRACE_OUT(( "Evicting fast path info for %x %x (%d, %d)",
  1816. pEntry->majorInfo,
  1817. pEntry->minorInfo,
  1818. pEntry->srcX,
  1819. pEntry->srcY));
  1820. }
  1821. //
  1822. // Remove the entry from its current list
  1823. //
  1824. COM_BasedListRemove(&pEntry->list);
  1825. //
  1826. // Now fill in the details
  1827. //
  1828. pEntry->majorInfo = majorInfo;
  1829. pEntry->minorInfo = minorInfo;
  1830. pEntry->majorPalette = majorPalette;
  1831. pEntry->minorPalette = minorPalette;
  1832. pEntry->srcX = srcX;
  1833. pEntry->srcY = srcY;
  1834. pEntry->width = width;
  1835. pEntry->height = height;
  1836. pEntry->cache = (WORD)cache;
  1837. pEntry->cacheIndex = (WORD)cacheIndex;
  1838. pEntry->colorIndex = (WORD)colorCacheIndex;
  1839. //
  1840. // Finally, add the entry to the front of the used list
  1841. //
  1842. TRACE_OUT(( "Adding fast path info for %x %x (%d, %d)",
  1843. pEntry->majorInfo,
  1844. pEntry->minorInfo,
  1845. pEntry->srcX,
  1846. pEntry->srcY));
  1847. COM_BasedListInsertAfter(&m_sbcFastPath->usedList, &pEntry->list);
  1848. DebugExitVOID(ASHost::SBCAddToFastPath);
  1849. }
  1850. //
  1851. //
  1852. // Name: SBCFindInFastPath
  1853. //
  1854. // Purpose: Check to see if a bitmap with the given attributes is in the
  1855. // SBC fast path. If so, return the cache info for the bitmap.
  1856. //
  1857. // Returns: TRUE if the bitmap is in the fast path, FALSE if not.
  1858. //
  1859. // Params: IN majorInfo - The major caching info passed up from
  1860. // the driver (the bitmap ID)
  1861. // IN minorInfo - The minor caching info passed up from
  1862. // the driver (the bitmap revision
  1863. // number)
  1864. // IN majorPalette - The major palette info passed up from
  1865. // the driver (the XLATEOBJ)
  1866. // IN minorPalette - The minor palette info passed up from
  1867. // the driver (the XLATEOBJ iUniq)
  1868. // IN srcX - The x coord of the source of the Blt
  1869. // IN srcY - The y coord of the source of the Blt
  1870. // IN width - The width of the area being Blted
  1871. // IN height - The height of the area being Blted
  1872. // OUT pCache - The cache the bits were placed in
  1873. // OUT pCacheIndex - The index at which the bits were
  1874. // placed in the cache
  1875. // OUT pColorCacheIndex - The index in the color table cache of
  1876. // the color table associated with the
  1877. // bits
  1878. //
  1879. // Operation: The contents of pCache, pCacheIndex and pColorCacheIndex
  1880. // are only valid on return if the function returns TRUE.
  1881. //
  1882. //
  1883. BOOL ASHost::SBCFindInFastPath
  1884. (
  1885. UINT_PTR majorInfo,
  1886. UINT minorInfo,
  1887. UINT_PTR majorPalette,
  1888. UINT minorPalette,
  1889. int srcX,
  1890. int srcY,
  1891. UINT width,
  1892. UINT height,
  1893. UINT * pCache,
  1894. UINT * pCacheIndex,
  1895. UINT * pColorCacheIndex
  1896. )
  1897. {
  1898. BOOL found = FALSE;
  1899. LPSBC_FASTPATH_ENTRY pEntry;
  1900. LPSBC_FASTPATH_ENTRY pNextEntry;
  1901. DebugEntry(ASHost::SBCFindInFastPath);
  1902. //
  1903. // Traverse the in use list looking for a match on the parameters
  1904. // passed in.
  1905. //
  1906. pEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListFirst(&m_sbcFastPath->usedList, FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  1907. while (pEntry != NULL)
  1908. {
  1909. if ((pEntry->majorInfo == majorInfo) &&
  1910. (pEntry->minorInfo == minorInfo) &&
  1911. (pEntry->majorPalette == majorPalette) &&
  1912. (pEntry->minorPalette == minorPalette) &&
  1913. (pEntry->srcX == srcX) &&
  1914. (pEntry->srcY == srcY) &&
  1915. (pEntry->width == width) &&
  1916. (pEntry->height == height))
  1917. {
  1918. //
  1919. // We've found a match - hurrah ! Fill in the return info.
  1920. //
  1921. TRACE_OUT(( "Hit for %x %x (%d, %d) cache %d",
  1922. pEntry->majorInfo,
  1923. pEntry->minorInfo,
  1924. pEntry->srcX,
  1925. pEntry->srcY,
  1926. pEntry->cache,
  1927. pEntry->cacheIndex));
  1928. found = TRUE;
  1929. *pCache = pEntry->cache;
  1930. *pCacheIndex = pEntry->cacheIndex;
  1931. *pColorCacheIndex = pEntry->colorIndex;
  1932. //
  1933. // We order the used list in MRU order, so remove the entry
  1934. // from its current position and add it at the head of the used
  1935. // list.
  1936. //
  1937. COM_BasedListRemove(&pEntry->list);
  1938. COM_BasedListInsertAfter(&m_sbcFastPath->usedList, &pEntry->list);
  1939. //
  1940. // Got a match, so we can break out of the while loop
  1941. //
  1942. break;
  1943. }
  1944. else if ((pEntry->majorInfo == majorInfo) &&
  1945. (pEntry->minorInfo != minorInfo))
  1946. {
  1947. //
  1948. // We have been given a bitmap which we have seen before, but
  1949. // the revision number has changed i.e. the bitmap has been
  1950. // updated (majorInfo identifies the bitmap, and minorInfo
  1951. // identifies the revision number of that bitmap - it is
  1952. // incremented every time the bitmap is changed).
  1953. //
  1954. // We have to remove all entries from the used list which
  1955. // reference this bitmap. We can start from the current
  1956. // position since we know that we can't have an entry for this
  1957. // bitmap earlier in the list, but we have to be careful to get
  1958. // the next entry in the list before removing an entry.
  1959. //
  1960. TRACE_OUT(( "Bitmap %x updated - removing references",
  1961. pEntry->majorInfo));
  1962. pNextEntry = pEntry;
  1963. while (pNextEntry != NULL)
  1964. {
  1965. pEntry = pNextEntry;
  1966. pNextEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListNext(&m_sbcFastPath->usedList,
  1967. pNextEntry, FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  1968. if (pEntry->majorInfo == majorInfo)
  1969. {
  1970. COM_BasedListRemove(&pEntry->list);
  1971. COM_BasedListInsertAfter(&m_sbcFastPath->freeList,
  1972. &pEntry->list);
  1973. }
  1974. }
  1975. //
  1976. // We know we wont find a match, so we can break out of the
  1977. // while loop
  1978. //
  1979. break;
  1980. }
  1981. pEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListNext(&m_sbcFastPath->usedList, pEntry,
  1982. FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  1983. }
  1984. DebugExitBOOL(ASShare::SBCFindInFastPath, found);
  1985. return(found);
  1986. }
  1987. //
  1988. // SBC_CacheEntryRemoved()
  1989. //
  1990. void ASHost::SBC_CacheEntryRemoved
  1991. (
  1992. UINT cache,
  1993. UINT cacheIndex
  1994. )
  1995. {
  1996. LPSBC_FASTPATH_ENTRY pEntry;
  1997. LPSBC_FASTPATH_ENTRY pNextEntry;
  1998. DebugEntry(ASHost::SBC_CacheEntryRemoved);
  1999. ASSERT(m_sbcFastPath);
  2000. //
  2001. // An entry has been removed from the cache. If we have this entry in
  2002. // our fast path, we have to remove it.
  2003. //
  2004. // Just traverse the used list looking for an entry with matching cache
  2005. // and cacheIndex. Note that there may be more than one entry - if the
  2006. // source bitmap has a repeating image, we will get a match on the bits
  2007. // when we cache different areas of the bitmap.
  2008. //
  2009. pNextEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListFirst(&m_sbcFastPath->usedList,
  2010. FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  2011. while (pNextEntry != NULL)
  2012. {
  2013. pEntry = pNextEntry;
  2014. pNextEntry = (LPSBC_FASTPATH_ENTRY)COM_BasedListNext(&m_sbcFastPath->usedList,
  2015. pNextEntry, FIELD_OFFSET(SBC_FASTPATH_ENTRY, list));
  2016. if ((pEntry->cache == cache) && (pEntry->cacheIndex == cacheIndex))
  2017. {
  2018. //
  2019. // Move the entry to the free list
  2020. //
  2021. TRACE_OUT(("Fast path entry %x %x (%d, %d) evicted from cache",
  2022. pEntry->majorInfo,
  2023. pEntry->minorInfo,
  2024. pEntry->srcX,
  2025. pEntry->srcY));
  2026. COM_BasedListRemove(&pEntry->list);
  2027. COM_BasedListInsertAfter(&m_sbcFastPath->freeList,
  2028. &pEntry->list);
  2029. }
  2030. }
  2031. DebugExitVOID(ASHost::SBC_CacheEntryRemoved);
  2032. }