Leaked source code of windows server 2003
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.

1151 lines
35 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: heap.c
  3. *
  4. * This module contains the routines for a 2-d heap. It is used primarily
  5. * for allocating space for device-format-bitmaps in off-screen memory.
  6. *
  7. * Off-screen bitmaps are a big deal on NT because:
  8. *
  9. * 1) It reduces the working set. Any bitmap stored in off-screen
  10. * memory is a bitmap that isn't taking up space in main memory.
  11. *
  12. * 2) There is a speed win by using the accelerator hardware for
  13. * drawing, in place of NT's GDI code. NT's GDI is written entirely
  14. * in 'C++' and perhaps isn't as fast as it could be.
  15. *
  16. * 3) It leads naturally to nifty tricks that can take advantage of
  17. * the hardware, such as MaskBlt support and cheap double buffering
  18. * for OpenGL.
  19. *
  20. * The heap algorithm employed herein attempts to solve an unsolvable
  21. * problem: the problem of keeping arbitrary sized bitmaps as packed as
  22. * possible in a 2-d space, when the bitmaps can come and go at random.
  23. *
  24. * This problem is due entirely to the nature of the hardware for which this
  25. * driver is written: the hardware treats everything as 2-d quantities. If
  26. * the hardware bitmap pitch could be changed so that the bitmaps could be
  27. * packed linearly in memory, the problem would be infinitely easier (it is
  28. * much easier to track the memory, and the accelerator can be used to re-pack
  29. * the heap to avoid segmentation).
  30. *
  31. * If your hardware can treat bitmaps as one dimensional quantities (as can
  32. * the XGA and ATI), by all means please implement a new off-screen heap.
  33. *
  34. * When the heap gets full, old allocations will automatically be punted
  35. * from off-screen and copied to DIBs, which we'll let GDI draw on.
  36. *
  37. * Note that this heap manages reverse-L shape off-screen memory
  38. * configurations (where the scan pitch is longer than the visible screen,
  39. * such as happens at 800x600 when the scan length must be a multiple of
  40. * 1024).
  41. *
  42. * NOTE: All heap operations must be done under some sort of synchronization,
  43. * whether it's controlled by GDI or explicitly by the driver. All
  44. * the routines in this module assume that they have exclusive access
  45. * to the heap data structures; multiple threads partying in here at
  46. * the same time would be a Bad Thing. (By default, GDI does NOT
  47. * synchronize drawing on device-created bitmaps.)
  48. *
  49. * Copyright (c) 1993-1994 Microsoft Corporation
  50. \**************************************************************************/
  51. #include "precomp.h"
  52. #define OH_ALLOC_SIZE 4000 // Do all memory allocations in 4k chunks
  53. #define OH_QUANTUM 8 // The minimum dimension of an allocation
  54. #define CXCY_SENTINEL 0x7fffffff // The sentinel at the end of the available
  55. // list has this very large 'cxcy' value
  56. // This macro results in the available list being maintained with a
  57. // cx-major, cy-minor sort:
  58. #define CXCY(cx, cy) (((cx) << 16) | (cy))
  59. /******************************Public*Routine******************************\
  60. * OH* pohNewNode
  61. *
  62. * Allocates a basic memory unit in which we'll pack our data structures.
  63. *
  64. * Since we'll have a lot of OH nodes, most of which we will be
  65. * occasionally traversing, we do our own memory allocation scheme to
  66. * keep them densely packed in memory.
  67. *
  68. * It would be the worst possible thing for the working set to simply
  69. * call EngAllocMem(sizeof(OH)) every time we needed a new node. There
  70. * would be no locality; OH nodes would get scattered throughout memory,
  71. * and as we traversed the available list for one of our allocations,
  72. * it would be far more likely that we would hit a hard page fault.
  73. \**************************************************************************/
  74. OH* pohNewNode(
  75. PDEV* ppdev)
  76. {
  77. LONG i;
  78. LONG cOhs;
  79. OHALLOC* poha;
  80. OH* poh;
  81. if (ppdev->heap.pohFreeList == NULL)
  82. {
  83. // We zero-init to initialize all the OH flags, and to help in
  84. // debugging (we can afford to do this since we'll be doing this
  85. // very infrequently):
  86. poha = EngAllocMem(FL_ZERO_MEMORY, OH_ALLOC_SIZE, ALLOC_TAG);
  87. if (poha == NULL)
  88. return(NULL);
  89. // Insert this OHALLOC at the begining of the OHALLOC chain:
  90. poha->pohaNext = ppdev->heap.pohaChain;
  91. ppdev->heap.pohaChain = poha;
  92. // This has a '+ 1' because OHALLOC includes an extra OH in its
  93. // structure declaration:
  94. cOhs = (OH_ALLOC_SIZE - sizeof(OHALLOC)) / sizeof(OH) + 1;
  95. // The big OHALLOC allocation is simply a container for a bunch of
  96. // OH data structures in an array. The new OH data structures are
  97. // linked together and added to the OH free list:
  98. poh = &poha->aoh[0];
  99. for (i = cOhs - 1; i != 0; i--)
  100. {
  101. poh->pohNext = poh + 1;
  102. poh = poh + 1;
  103. }
  104. poh->pohNext = NULL;
  105. ppdev->heap.pohFreeList = &poha->aoh[0];
  106. }
  107. poh = ppdev->heap.pohFreeList;
  108. ppdev->heap.pohFreeList = poh->pohNext;
  109. return(poh);
  110. }
  111. /******************************Public*Routine******************************\
  112. * VOID vOhFreeNode
  113. *
  114. * Frees our basic data structure allocation unit by adding it to a free
  115. * list.
  116. *
  117. \**************************************************************************/
  118. VOID vOhFreeNode(
  119. PDEV* ppdev,
  120. OH* poh)
  121. {
  122. if (poh == NULL)
  123. return;
  124. poh->pohNext = ppdev->heap.pohFreeList;
  125. ppdev->heap.pohFreeList = poh;
  126. poh->ofl = 0;
  127. }
  128. /******************************Public*Routine******************************\
  129. * OH* pohFree
  130. *
  131. * Frees an off-screen heap allocation. The free space will be combined
  132. * with any adjacent free spaces to avoid segmentation of the 2-d heap.
  133. *
  134. * Note: A key idea here is that the data structure for the upper-left-
  135. * most node must be kept at the same physical CPU memory so that
  136. * adjacency links are kept correctly (when two free spaces are
  137. * merged, the lower or right node can be freed).
  138. *
  139. \**************************************************************************/
  140. OH* pohFree(
  141. PDEV* ppdev,
  142. OH* poh)
  143. {
  144. ULONG cxcy;
  145. OH* pohBeside;
  146. OH* pohNext;
  147. OH* pohPrev;
  148. if (poh == NULL)
  149. return(NULL);
  150. DISPDBG((1, "Freeing %li x %li at (%li, %li)",
  151. poh->cx, poh->cy, poh->x, poh->y));
  152. #if DEBUG_HEAP
  153. {
  154. RECTL rclBitmap;
  155. RBRUSH_COLOR rbc;
  156. LONG xOffset;
  157. LONG yOffset;
  158. rclBitmap.left = poh->x;
  159. rclBitmap.top = poh->y;
  160. rclBitmap.right = poh->x + poh->cx;
  161. rclBitmap.bottom = poh->y + poh->cy;
  162. xOffset = ppdev->xOffset;
  163. yOffset = ppdev->yOffset;
  164. ppdev->xOffset = 0;
  165. ppdev->yOffset = 0;
  166. ppdev->pfnFillSolid(ppdev, 1, &rclBitmap, LOGICAL_0, LOGICAL_0, rbc,
  167. NULL);
  168. ppdev->xOffset = xOffset;
  169. ppdev->yOffset = yOffset;
  170. }
  171. #endif
  172. // Update the uniqueness to show that space has been freed, so that
  173. // we may decide to see if some DIBs can be moved back into off-screen
  174. // memory:
  175. ppdev->iHeapUniq++;
  176. MergeLoop:
  177. ASSERTDD(!(poh->ofl & OFL_PERMANENT), "Can't free permanents for now");
  178. // Try merging with the right sibling:
  179. pohBeside = poh->pohRight;
  180. if ((pohBeside->ofl & OFL_AVAILABLE) &&
  181. (pohBeside->cy == poh->cy) &&
  182. (pohBeside->pohUp == poh->pohUp) &&
  183. (pohBeside->pohDown == poh->pohDown) &&
  184. (pohBeside->pohRight->pohLeft != pohBeside))
  185. {
  186. // Add the right rectangle to ours:
  187. poh->cx += pohBeside->cx;
  188. poh->pohRight = pohBeside->pohRight;
  189. // Remove 'pohBeside' from the ??? list and free it:
  190. pohBeside->pohNext->pohPrev = pohBeside->pohPrev;
  191. pohBeside->pohPrev->pohNext = pohBeside->pohNext;
  192. vOhFreeNode(ppdev, pohBeside);
  193. goto MergeLoop;
  194. }
  195. // Try merging with the lower sibling:
  196. pohBeside = poh->pohDown;
  197. if ((pohBeside->ofl & OFL_AVAILABLE) &&
  198. (pohBeside->cx == poh->cx) &&
  199. (pohBeside->pohLeft == poh->pohLeft) &&
  200. (pohBeside->pohRight == poh->pohRight) &&
  201. (pohBeside->pohDown->pohUp != pohBeside))
  202. {
  203. poh->cy += pohBeside->cy;
  204. poh->pohDown = pohBeside->pohDown;
  205. pohBeside->pohNext->pohPrev = pohBeside->pohPrev;
  206. pohBeside->pohPrev->pohNext = pohBeside->pohNext;
  207. vOhFreeNode(ppdev, pohBeside);
  208. goto MergeLoop;
  209. }
  210. // Try merging with the left sibling:
  211. pohBeside = poh->pohLeft;
  212. if ((pohBeside->ofl & OFL_AVAILABLE) &&
  213. (pohBeside->cy == poh->cy) &&
  214. (pohBeside->pohUp == poh->pohUp) &&
  215. (pohBeside->pohDown == poh->pohDown) &&
  216. (pohBeside->pohRight == poh) &&
  217. (poh->pohRight->pohLeft != poh))
  218. {
  219. // We add our rectangle to the one to the left:
  220. pohBeside->cx += poh->cx;
  221. pohBeside->pohRight = poh->pohRight;
  222. // Remove 'poh' from the ??? list and free it:
  223. poh->pohNext->pohPrev = poh->pohPrev;
  224. poh->pohPrev->pohNext = poh->pohNext;
  225. vOhFreeNode(ppdev, poh);
  226. poh = pohBeside;
  227. goto MergeLoop;
  228. }
  229. // Try merging with the upper sibling:
  230. pohBeside = poh->pohUp;
  231. if ((pohBeside->ofl & OFL_AVAILABLE) &&
  232. (pohBeside->cx == poh->cx) &&
  233. (pohBeside->pohLeft == poh->pohLeft) &&
  234. (pohBeside->pohRight == poh->pohRight) &&
  235. (pohBeside->pohDown == poh) &&
  236. (poh->pohDown->pohUp != poh))
  237. {
  238. pohBeside->cy += poh->cy;
  239. pohBeside->pohDown = poh->pohDown;
  240. poh->pohNext->pohPrev = poh->pohPrev;
  241. poh->pohPrev->pohNext = poh->pohNext;
  242. vOhFreeNode(ppdev, poh);
  243. poh = pohBeside;
  244. goto MergeLoop;
  245. }
  246. // Remove the node from the ???list if it was in use (we wouldn't
  247. // want to do this for a OFL_PERMANENT node that had been freed):
  248. poh->pohNext->pohPrev = poh->pohPrev;
  249. poh->pohPrev->pohNext = poh->pohNext;
  250. cxcy = CXCY(poh->cx, poh->cy);
  251. // Insert the node into the available list:
  252. pohNext = ppdev->heap.ohAvailable.pohNext;
  253. while (pohNext->cxcy < cxcy)
  254. {
  255. pohNext = pohNext->pohNext;
  256. }
  257. pohPrev = pohNext->pohPrev;
  258. pohPrev->pohNext = poh;
  259. pohNext->pohPrev = poh;
  260. poh->pohPrev = pohPrev;
  261. poh->pohNext = pohNext;
  262. poh->ofl = OFL_AVAILABLE;
  263. poh->cxcy = cxcy;
  264. // Return the node pointer for the new and improved available rectangle:
  265. return(poh);
  266. }
  267. /******************************Public*Routine******************************\
  268. * OH* pohAllocate
  269. *
  270. * Allocates space for an off-screen rectangle. It will attempt to find
  271. * the smallest available free rectangle, and will allocate the block out
  272. * of its upper-left corner. The remaining two rectangles will be placed
  273. * on the available free space list.
  274. *
  275. * If the rectangle would have been large enough to fit into off-screen
  276. * memory, but there is not enough available free space, we will boot
  277. * bitmaps out of off-screen and into DIBs until there is enough room.
  278. *
  279. \**************************************************************************/
  280. OH* pohAllocate(
  281. PDEV* ppdev,
  282. LONG cxThis, // Width of rectangle to be allocated
  283. LONG cyThis, // Height of rectangle to be allocated
  284. FLOH floh) // Allocation flags
  285. {
  286. ULONG cxcyThis; // Width and height search key
  287. OH* pohThis; // Points to found available rectangle we'll use
  288. ULONG cxcy; // Temporary versions
  289. OH* pohNext;
  290. OH* pohPrev;
  291. LONG cxRem;
  292. LONG cyRem;
  293. OH* pohBelow;
  294. LONG cxBelow;
  295. LONG cyBelow;
  296. OH* pohBeside;
  297. LONG cxBeside;
  298. LONG cyBeside;
  299. DISPDBG((1, "Allocating %li x %li...", cxThis, cyThis));
  300. ASSERTDD((cxThis > 0) && (cyThis > 0), "Illegal allocation size");
  301. // Increase the width to get the proper alignment (thus ensuring that all
  302. // allocations will be properly aligned):
  303. cxThis = (cxThis + (HEAP_X_ALIGNMENT - 1)) & ~(HEAP_X_ALIGNMENT - 1);
  304. // We can't succeed if the requested rectangle is larger than the
  305. // largest possible available rectangle:
  306. if ((cxThis > ppdev->heap.cxMax) || (cyThis > ppdev->heap.cyMax))
  307. return(NULL);
  308. // Find the first available rectangle the same size or larger than
  309. // the requested one:
  310. cxcyThis = CXCY(cxThis, cyThis);
  311. pohThis = ppdev->heap.ohAvailable.pohNext;
  312. while (pohThis->cxcy < cxcyThis)
  313. {
  314. pohThis = pohThis->pohNext;
  315. }
  316. while (pohThis->cy < cyThis)
  317. {
  318. pohThis = pohThis->pohNext;
  319. }
  320. if (pohThis->cxcy == CXCY_SENTINEL)
  321. {
  322. // There was no space large enough...
  323. if (floh & FLOH_ONLY_IF_ROOM)
  324. return(NULL);
  325. // We couldn't find an available rectangle that was big enough
  326. // to fit our request. So throw things out of the heap until we
  327. // have room:
  328. do {
  329. pohThis = ppdev->heap.ohDfb.pohPrev; // Least-recently blitted
  330. ASSERTDD(pohThis != &ppdev->heap.ohDfb, "Ran out of in-use entries");
  331. // We can safely exit here if we have to:
  332. pohThis = pohMoveOffscreenDfbToDib(ppdev, pohThis);
  333. if (pohThis == NULL)
  334. return(NULL);
  335. } while ((pohThis->cx < cxThis) || (pohThis->cy < cyThis));
  336. }
  337. // We've now found an available rectangle that is the same size or
  338. // bigger than our requested rectangle. We're going to use the
  339. // upper-left corner of our found rectangle, and divide the unused
  340. // remainder into two rectangles which will go on the available
  341. // list.
  342. // Compute the width of the unused rectangle to the right, and the
  343. // height of the unused rectangle below:
  344. cyRem = pohThis->cy - cyThis;
  345. cxRem = pohThis->cx - cxThis;
  346. // Given finite area, we wish to find the two rectangles that are
  347. // most square -- i.e., the arrangement that gives two rectangles
  348. // with the least perimiter:
  349. cyBelow = cyRem;
  350. cxBeside = cxRem;
  351. if (cxRem <= cyRem)
  352. {
  353. cxBelow = cxThis + cxRem;
  354. cyBeside = cyThis;
  355. }
  356. else
  357. {
  358. cxBelow = cxThis;
  359. cyBeside = cyThis + cyRem;
  360. }
  361. // We only make new available rectangles of the unused right and bottom
  362. // portions if they're greater in dimension than OH_QUANTUM (it hardly
  363. // makes sense to do the book-work to keep around a 2-pixel wide
  364. // available space, for example):
  365. pohBeside = NULL;
  366. if (cxBeside >= OH_QUANTUM)
  367. {
  368. pohBeside = pohNewNode(ppdev);
  369. if (pohBeside == NULL)
  370. return(NULL);
  371. }
  372. pohBelow = NULL;
  373. if (cyBelow >= OH_QUANTUM)
  374. {
  375. pohBelow = pohNewNode(ppdev);
  376. if (pohBelow == NULL)
  377. {
  378. vOhFreeNode(ppdev, pohBeside);
  379. return(NULL);
  380. }
  381. // Insert this rectangle into the available list (which is
  382. // sorted on ascending cxcy):
  383. cxcy = CXCY(cxBelow, cyBelow);
  384. pohNext = ppdev->heap.ohAvailable.pohNext;
  385. while (pohNext->cxcy < cxcy)
  386. {
  387. pohNext = pohNext->pohNext;
  388. }
  389. pohPrev = pohNext->pohPrev;
  390. pohPrev->pohNext = pohBelow;
  391. pohNext->pohPrev = pohBelow;
  392. pohBelow->pohPrev = pohPrev;
  393. pohBelow->pohNext = pohNext;
  394. // Now update the adjacency information:
  395. pohBelow->pohLeft = pohThis->pohLeft;
  396. pohBelow->pohUp = pohThis;
  397. pohBelow->pohRight = pohThis->pohRight;
  398. pohBelow->pohDown = pohThis->pohDown;
  399. // Update the rest of the new node information:
  400. pohBelow->cxcy = cxcy;
  401. pohBelow->ofl = OFL_AVAILABLE;
  402. pohBelow->x = pohThis->x;
  403. pohBelow->y = pohThis->y + cyThis;
  404. pohBelow->cx = cxBelow;
  405. pohBelow->cy = cyBelow;
  406. // Modify the current node to reflect the changes we've made:
  407. pohThis->cy = cyThis;
  408. }
  409. if (cxBeside >= OH_QUANTUM)
  410. {
  411. // Insert this rectangle into the available list (which is
  412. // sorted on ascending cxcy):
  413. cxcy = CXCY(cxBeside, cyBeside);
  414. pohNext = ppdev->heap.ohAvailable.pohNext;
  415. while (pohNext->cxcy < cxcy)
  416. {
  417. pohNext = pohNext->pohNext;
  418. }
  419. pohPrev = pohNext->pohPrev;
  420. pohPrev->pohNext = pohBeside;
  421. pohNext->pohPrev = pohBeside;
  422. pohBeside->pohPrev = pohPrev;
  423. pohBeside->pohNext = pohNext;
  424. // Now update the adjacency information:
  425. pohBeside->pohUp = pohThis->pohUp;
  426. pohBeside->pohLeft = pohThis;
  427. pohBeside->pohDown = pohThis->pohDown;
  428. pohBeside->pohRight = pohThis->pohRight;
  429. // Update the rest of the new node information:
  430. pohBeside->cxcy = cxcy;
  431. pohBeside->ofl = OFL_AVAILABLE;
  432. pohBeside->x = pohThis->x + cxThis;
  433. pohBeside->y = pohThis->y;
  434. pohBeside->cx = cxBeside;
  435. pohBeside->cy = cyBeside;
  436. // Modify the current node to reflect the changes we've made:
  437. pohThis->cx = cxThis;
  438. }
  439. if (pohBelow != NULL)
  440. {
  441. pohThis->pohDown = pohBelow;
  442. if ((pohBeside != NULL) && (cyBeside == pohThis->cy))
  443. pohBeside->pohDown = pohBelow;
  444. }
  445. if (pohBeside != NULL)
  446. {
  447. pohThis->pohRight = pohBeside;
  448. if ((pohBelow != NULL) && (cxBelow == pohThis->cx))
  449. pohBelow->pohRight = pohBeside;
  450. }
  451. pohThis->ofl = OFL_INUSE;
  452. pohThis->cxcy = CXCY(pohThis->cx, pohThis->cy);
  453. pohThis->pdsurf = NULL; // Caller is responsible for
  454. // setting this field
  455. // Remove this from the available list:
  456. pohThis->pohPrev->pohNext = pohThis->pohNext;
  457. pohThis->pohNext->pohPrev = pohThis->pohPrev;
  458. // Now insert this at the head of the DFB list:
  459. pohThis->pohNext = ppdev->heap.ohDfb.pohNext;
  460. pohThis->pohPrev = &ppdev->heap.ohDfb;
  461. ppdev->heap.ohDfb.pohNext->pohPrev = pohThis;
  462. ppdev->heap.ohDfb.pohNext = pohThis;
  463. DISPDBG((1, " Allocated at (%li, %li)", pohThis->x, pohThis->y));
  464. return(pohThis);
  465. }
  466. /******************************Public*Routine******************************\
  467. * VOID vCalculateMaxmimum
  468. *
  469. * Traverses the list of in-use and available rectangles to find the one
  470. * with the maximal area.
  471. *
  472. \**************************************************************************/
  473. VOID vCalculateMaximum(
  474. PDEV* ppdev)
  475. {
  476. OH* poh;
  477. OH* pohSentinel;
  478. LONG lArea;
  479. LONG lMaxArea;
  480. LONG cxMax;
  481. LONG cyMax;
  482. LONG i;
  483. lMaxArea = 0;
  484. cxMax = 0;
  485. cyMax = 0;
  486. // First time through, loop through the list of available rectangles:
  487. pohSentinel = &ppdev->heap.ohAvailable;
  488. for (i = 2; i != 0; i--)
  489. {
  490. for (poh = pohSentinel->pohNext; poh != pohSentinel; poh = poh->pohNext)
  491. {
  492. ASSERTDD(!(poh->ofl & OFL_PERMANENT),
  493. "Permanent in available/DFB chain?");
  494. // We don't have worry about this multiply overflowing
  495. // because we are dealing in physical screen coordinates,
  496. // which will probably never be more than 15 bits:
  497. lArea = poh->cx * poh->cy;
  498. if (lArea > lMaxArea)
  499. {
  500. cxMax = poh->cx;
  501. cyMax = poh->cy;
  502. lMaxArea = lArea;
  503. }
  504. }
  505. // Second time through, loop through the list of in-use rectangles:
  506. pohSentinel = &ppdev->heap.ohDfb;
  507. }
  508. // All that we are interested in is the dimensions of the rectangle
  509. // that has the largest possible available area (and remember that
  510. // there might not be any possible available area):
  511. ppdev->heap.cxMax = cxMax;
  512. ppdev->heap.cyMax = cyMax;
  513. }
  514. /******************************Public*Routine******************************\
  515. * OH* pohAllocatePermanent
  516. *
  517. * Allocates an off-screen rectangle that can never be booted of the heap.
  518. * It's the caller's responsibility to manage the rectangle, which includes
  519. * what to do with the memory in DrvAssertMode when the display is changed
  520. * to full-screen mode.
  521. *
  522. \**************************************************************************/
  523. OH* pohAllocatePermanent(
  524. PDEV* ppdev,
  525. LONG cx,
  526. LONG cy)
  527. {
  528. OH* poh;
  529. poh = pohAllocate(ppdev, cx, cy, 0);
  530. if (poh != NULL)
  531. {
  532. // Mark the rectangle as permanent:
  533. poh->ofl = OFL_PERMANENT;
  534. // Remove the node from the most-recently blitted list:
  535. poh->pohPrev->pohNext = poh->pohNext;
  536. poh->pohNext->pohPrev = poh->pohPrev;
  537. poh->pohPrev = NULL;
  538. poh->pohNext = NULL;
  539. // Now calculate the new maximum size rectangle available in the
  540. // heap:
  541. vCalculateMaximum(ppdev);
  542. }
  543. return(poh);
  544. }
  545. /******************************Public*Routine******************************\
  546. * BOOL bMoveDibToOffscreenDfbIfRoom
  547. *
  548. * Converts the DIB DFB to an off-screen DFB, if there's room for it in
  549. * off-screen memory.
  550. *
  551. * Returns: FALSE if there wasn't room, TRUE if successfully moved.
  552. *
  553. \**************************************************************************/
  554. BOOL bMoveDibToOffscreenDfbIfRoom(
  555. PDEV* ppdev,
  556. DSURF* pdsurf)
  557. {
  558. OH* poh;
  559. SURFOBJ* pso;
  560. RECTL rclDst;
  561. POINTL ptlSrc;
  562. HSURF hsurf;
  563. ASSERTDD(pdsurf->dt == DT_DIB,
  564. "Can't move a bitmap off-screen when it's already off-screen");
  565. // If we're in full-screen mode, we can't move anything to off-screen
  566. // memory:
  567. if (!ppdev->bEnabled)
  568. return(FALSE);
  569. poh = pohAllocate(ppdev, pdsurf->sizl.cx, pdsurf->sizl.cy,
  570. FLOH_ONLY_IF_ROOM);
  571. if (poh == NULL)
  572. {
  573. // There wasn't any free room.
  574. return(FALSE);
  575. }
  576. // 'pdsurf->sizl' is the actual bitmap dimension, not 'poh->cx' or
  577. // 'poh->cy'.
  578. rclDst.left = poh->x;
  579. rclDst.top = poh->y;
  580. rclDst.right = rclDst.left + pdsurf->sizl.cx;
  581. rclDst.bottom = rclDst.top + pdsurf->sizl.cy;
  582. ptlSrc.x = 0;
  583. ptlSrc.y = 0;
  584. vPutBits(ppdev, pdsurf->pso, &rclDst, &ptlSrc);
  585. // Update the data structures to reflect the new off-screen node:
  586. pso = pdsurf->pso;
  587. pdsurf->dt = DT_SCREEN;
  588. pdsurf->poh = poh;
  589. poh->pdsurf = pdsurf;
  590. // Now free the DIB. Get the hsurf from the SURFOBJ before we unlock
  591. // it (it's not legal to dereference psoDib when it's unlocked):
  592. hsurf = pso->hsurf;
  593. EngUnlockSurface(pso);
  594. EngDeleteSurface(hsurf);
  595. return(TRUE);
  596. }
  597. /******************************Public*Routine******************************\
  598. * OH* pohMoveOffscreenDfbToDib
  599. *
  600. * Converts the DFB from being off-screen to being a DIB.
  601. *
  602. * Note: The caller does NOT have to call 'pohFree' on 'poh' after making
  603. * this call.
  604. *
  605. * Returns: NULL if the function failed (due to a memory allocation).
  606. * Otherwise, it returns a pointer to the coalesced off-screen heap
  607. * node that has been made available for subsequent allocations
  608. * (useful when trying to free enough memory to make a new
  609. * allocation).
  610. \**************************************************************************/
  611. OH* pohMoveOffscreenDfbToDib(
  612. PDEV* ppdev,
  613. OH* poh)
  614. {
  615. DSURF* pdsurf;
  616. HBITMAP hbmDib;
  617. SURFOBJ* pso;
  618. RECTL rclDst;
  619. POINTL ptlSrc;
  620. DISPDBG((1, "Throwing out %li x %li at (%li, %li)!",
  621. poh->cx, poh->cy, poh->x, poh->y));
  622. pdsurf = poh->pdsurf;
  623. ASSERTDD((poh->x != 0) || (poh->y != 0),
  624. "Can't make the visible screen into a DIB");
  625. ASSERTDD(pdsurf->dt != DT_DIB,
  626. "Can't make a DIB into even more of a DIB");
  627. hbmDib = EngCreateBitmap(pdsurf->sizl, 0, ppdev->iBitmapFormat,
  628. BMF_TOPDOWN, NULL);
  629. if (hbmDib)
  630. {
  631. if (EngAssociateSurface((HSURF) hbmDib, ppdev->hdevEng, 0))
  632. {
  633. pso = EngLockSurface((HSURF) hbmDib);
  634. if (pso != NULL)
  635. {
  636. rclDst.left = 0;
  637. rclDst.top = 0;
  638. rclDst.right = pdsurf->sizl.cx;
  639. rclDst.bottom = pdsurf->sizl.cy;
  640. ptlSrc.x = poh->x;
  641. ptlSrc.y = poh->y;
  642. vGetBits(ppdev, pso, &rclDst, &ptlSrc);
  643. pdsurf->dt = DT_DIB;
  644. pdsurf->pso = pso;
  645. // Don't even bother checking to see if this DIB should
  646. // be put back into off-screen memory until the next
  647. // heap 'free' occurs:
  648. pdsurf->iUniq = ppdev->iHeapUniq;
  649. pdsurf->cBlt = 0;
  650. // Remove this node from the off-screen DFB list, and free
  651. // it. 'pohFree' will never return NULL:
  652. return(pohFree(ppdev, poh));
  653. }
  654. }
  655. // Fail case:
  656. EngDeleteSurface((HSURF) hbmDib);
  657. }
  658. return(NULL);
  659. }
  660. /******************************Public*Routine******************************\
  661. * BOOL bMoveEverythingFromOffscreenToDibs
  662. *
  663. * This function is used when we're about to enter full-screen mode, which
  664. * would wipe all our off-screen bitmaps. GDI can ask us to draw on
  665. * device bitmaps even when we're in full-screen mode, and we do NOT have
  666. * the option of stalling the call until we switch out of full-screen.
  667. * We have no choice but to move all the off-screen DFBs to DIBs.
  668. *
  669. * Returns TRUE if all DSURFs have been successfully moved.
  670. *
  671. \**************************************************************************/
  672. BOOL bMoveAllDfbsFromOffscreenToDibs(
  673. PDEV* ppdev)
  674. {
  675. OH* poh;
  676. OH* pohNext;
  677. BOOL bRet;
  678. bRet = TRUE;
  679. poh = ppdev->heap.ohDfb.pohNext;
  680. while (poh != &ppdev->heap.ohDfb)
  681. {
  682. pohNext = poh->pohNext;
  683. // If something's already a DIB, we shouldn't try to make it even
  684. // more of a DIB:
  685. if (poh->pdsurf->dt == DT_SCREEN)
  686. {
  687. if (!pohMoveOffscreenDfbToDib(ppdev, poh))
  688. bRet = FALSE;
  689. }
  690. poh = pohNext;
  691. }
  692. return(bRet);
  693. }
  694. /******************************Public*Routine******************************\
  695. * HBITMAP DrvCreateDeviceBitmap
  696. *
  697. * Function called by GDI to create a device-format-bitmap (DFB). We will
  698. * always try to allocate the bitmap in off-screen; if we can't, we simply
  699. * fail the call and GDI will create and manage the bitmap itself.
  700. *
  701. * Note: We do not have to zero the bitmap bits. GDI will automatically
  702. * call us via DrvBitBlt to zero the bits (which is a security
  703. * consideration).
  704. *
  705. \**************************************************************************/
  706. HBITMAP DrvCreateDeviceBitmap(
  707. DHPDEV dhpdev,
  708. SIZEL sizl,
  709. ULONG iFormat)
  710. {
  711. PDEV* ppdev;
  712. OH* poh;
  713. DSURF* pdsurf;
  714. HBITMAP hbmDevice;
  715. FLONG flHooks;
  716. ppdev = (PDEV*) dhpdev;
  717. // If we're in full-screen mode, we hardly have any off-screen memory
  718. // in which to allocate a DFB. LATER: We could still allocate an
  719. // OH node and put the bitmap on the DIB DFB list for later promotion.
  720. if (!ppdev->bEnabled)
  721. return(0);
  722. // We only support device bitmaps that are the same colour depth
  723. // as our display.
  724. //
  725. // Actually, those are the only kind GDI will ever call us with,
  726. // but we may as well check. Note that this implies you'll never
  727. // get a crack at 1bpp bitmaps.
  728. if (iFormat != ppdev->iBitmapFormat)
  729. return(0);
  730. poh = pohAllocate(ppdev, sizl.cx, sizl.cy, 0);
  731. if (poh != NULL)
  732. {
  733. pdsurf = EngAllocMem(0, sizeof(DSURF), ALLOC_TAG);
  734. if (pdsurf != NULL)
  735. {
  736. hbmDevice = EngCreateDeviceBitmap((DHSURF) pdsurf, sizl, iFormat);
  737. if (hbmDevice != NULL)
  738. {
  739. flHooks = ppdev->flHooks;
  740. #if SYNCHRONIZEACCESS_WORKS
  741. {
  742. // Setting the SYNCHRONIZEACCESS flag tells GDI that we
  743. // want all drawing to the bitmaps to be synchronized (GDI
  744. // is multi-threaded and by default does not synchronize
  745. // device bitmap drawing -- it would be a Bad Thing for us
  746. // to have multiple threads using the accelerator at the
  747. // same time):
  748. flHooks |= HOOK_SYNCHRONIZEACCESS;
  749. }
  750. #endif // SYNCHRONIZEACCESS_WORKS
  751. if (EngAssociateSurface((HSURF) hbmDevice, ppdev->hdevEng,
  752. flHooks))
  753. {
  754. pdsurf->dt = DT_SCREEN;
  755. pdsurf->poh = poh;
  756. pdsurf->sizl = sizl;
  757. pdsurf->ppdev = ppdev;
  758. poh->pdsurf = pdsurf;
  759. return(hbmDevice);
  760. }
  761. EngDeleteSurface((HSURF) hbmDevice);
  762. }
  763. EngFreeMem(pdsurf);
  764. }
  765. pohFree(ppdev, poh);
  766. }
  767. return(0);
  768. }
  769. /******************************Public*Routine******************************\
  770. * VOID DrvDeleteDeviceBitmap
  771. *
  772. * Deletes a DFB.
  773. *
  774. \**************************************************************************/
  775. VOID DrvDeleteDeviceBitmap(
  776. DHSURF dhsurf)
  777. {
  778. DSURF* pdsurf;
  779. PDEV* ppdev;
  780. SURFOBJ* psoDib;
  781. HSURF hsurfDib;
  782. pdsurf = (DSURF*) dhsurf;
  783. ppdev = pdsurf->ppdev;
  784. if (pdsurf->dt == DT_SCREEN)
  785. {
  786. pohFree(ppdev, pdsurf->poh);
  787. }
  788. else
  789. {
  790. ASSERTDD(pdsurf->dt == DT_DIB, "Expected DIB type");
  791. psoDib = pdsurf->pso;
  792. // Get the hsurf from the SURFOBJ before we unlock it (it's not
  793. // legal to dereference psoDib when it's unlocked):
  794. hsurfDib = psoDib->hsurf;
  795. EngUnlockSurface(psoDib);
  796. EngDeleteSurface(hsurfDib);
  797. }
  798. EngFreeMem(pdsurf);
  799. }
  800. /******************************Public*Routine******************************\
  801. * BOOL bAssertModeOffscreenHeap
  802. *
  803. * This function is called whenever we switch in or out of full-screen
  804. * mode. We have to convert all the off-screen bitmaps to DIBs when
  805. * we switch to full-screen (because we may be asked to draw on them even
  806. * when in full-screen, and the mode switch would probably nuke the video
  807. * memory contents anyway).
  808. *
  809. \**************************************************************************/
  810. BOOL bAssertModeOffscreenHeap(
  811. PDEV* ppdev,
  812. BOOL bEnable)
  813. {
  814. BOOL b;
  815. b = TRUE;
  816. if (!bEnable)
  817. {
  818. b = bMoveAllDfbsFromOffscreenToDibs(ppdev);
  819. }
  820. return(b);
  821. }
  822. /******************************Public*Routine******************************\
  823. * VOID vDisableOffscreenHeap
  824. *
  825. * Frees any resources allocated by the off-screen heap.
  826. *
  827. \**************************************************************************/
  828. VOID vDisableOffscreenHeap(
  829. PDEV* ppdev)
  830. {
  831. OHALLOC* poha;
  832. OHALLOC* pohaNext;
  833. poha = ppdev->heap.pohaChain;
  834. while (poha != NULL)
  835. {
  836. pohaNext = poha->pohaNext; // Grab the next pointer before it's freed
  837. EngFreeMem(poha);
  838. poha = pohaNext;
  839. }
  840. }
  841. /******************************Public*Routine******************************\
  842. * BOOL bEnableOffscreenHeap
  843. *
  844. * Initializes the off-screen heap using all available video memory,
  845. * accounting for the portion taken by the visible screen.
  846. *
  847. * Input: ppdev->cxScreen
  848. * ppdev->cyScreen
  849. * ppdev->cxMemory
  850. * ppdev->cyMemory
  851. *
  852. \**************************************************************************/
  853. BOOL bEnableOffscreenHeap(
  854. PDEV* ppdev)
  855. {
  856. OH* poh;
  857. DISPDBG((5, "Screen: %li x %li Memory: %li x %li",
  858. ppdev->cxScreen, ppdev->cyScreen, ppdev->cxMemory, ppdev->cyMemory));
  859. ppdev->heap.pohaChain = NULL;
  860. ppdev->heap.pohFreeList = NULL;
  861. // Initialize the available list, which will be a circular
  862. // doubly-linked list kept in ascending 'cxcy' order, with a
  863. // 'sentinel' at the end of the list:
  864. poh = pohNewNode(ppdev);
  865. if (poh == NULL)
  866. goto ReturnFalse;
  867. // The first node describes the entire video memory size:
  868. poh->pohNext = &ppdev->heap.ohAvailable;
  869. poh->pohPrev = &ppdev->heap.ohAvailable;
  870. poh->ofl = OFL_AVAILABLE;
  871. poh->x = 0;
  872. poh->y = 0;
  873. poh->cx = ppdev->cxMemory;
  874. poh->cy = ppdev->cyMemory;
  875. poh->cxcy = CXCY(ppdev->cxMemory, ppdev->cyMemory);
  876. poh->pohLeft = &ppdev->heap.ohAvailable;
  877. poh->pohUp = &ppdev->heap.ohAvailable;
  878. poh->pohRight = &ppdev->heap.ohAvailable;
  879. poh->pohDown = &ppdev->heap.ohAvailable;
  880. // The second node is our available list sentinel:
  881. ppdev->heap.ohAvailable.pohNext = poh;
  882. ppdev->heap.ohAvailable.pohPrev = poh;
  883. ppdev->heap.ohAvailable.cxcy = CXCY_SENTINEL;
  884. ppdev->heap.ohAvailable.cx = 0x7fffffff;
  885. ppdev->heap.ohAvailable.cy = 0x7fffffff;
  886. ppdev->heap.ohAvailable.ofl = OFL_PERMANENT;
  887. ppdev->heap.ohDfb.pohLeft = NULL;
  888. ppdev->heap.ohDfb.pohUp = NULL;
  889. ppdev->heap.ohDfb.pohRight = NULL;
  890. ppdev->heap.ohDfb.pohDown = NULL;
  891. // Initialize the most-recently-blitted DFB list, which will be
  892. // a circular doubly-linked list kept in order, with a sentinel at
  893. // the end. This node is also used for the screen-surface, for its
  894. // offset:
  895. ppdev->heap.ohDfb.pohNext = &ppdev->heap.ohDfb;
  896. ppdev->heap.ohDfb.pohPrev = &ppdev->heap.ohDfb;
  897. ppdev->heap.ohDfb.ofl = OFL_PERMANENT;
  898. // For the moment, make the max really big so that the first
  899. // allocation we're about to do will succeed:
  900. ppdev->heap.cxMax = 0x7fffffff;
  901. ppdev->heap.cyMax = 0x7fffffff;
  902. // Finally, reserve the upper-left corner for the screen. We can
  903. // actually throw away 'poh' because we'll never need it again
  904. // (not even for disabling the off-screen heap since everything is
  905. // freed using OHALLOCs):
  906. poh = pohAllocatePermanent(ppdev, ppdev->cxScreen, ppdev->cyScreen);
  907. ASSERTDD((poh != NULL) && (poh->x == 0) && (poh->y == 0),
  908. "We assumed allocator would use the upper-left corner");
  909. DISPDBG((5, "Passed bEnableOffscreenHeap"));
  910. if (poh != NULL)
  911. return(TRUE);
  912. vDisableOffscreenHeap(ppdev);
  913. ReturnFalse:
  914. DISPDBG((0, "Failed bEnableOffscreenHeap"));
  915. return(FALSE);
  916. }