Guest User

Untitled

a guest
Oct 6th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.95 KB | None | 0 0
  1. commit ae124362902ad96f7423b7ad4c3bf44ca0c9d10c
  2. Author: Daniel Brookes <[email protected]>
  3. Date:   Tue Jun 8 08:24:57 2021 +0800
  4.  
  5.     Common: Add CreateDelayedMarkupHandle for vector image handle.
  6.    
  7.     - this is an alternative version of CreateMarkupHandle that won't build
  8.       the markup UI element immediately.
  9.    
  10.     - instead, we use the stored XAML data (or data stream) to build the
  11.       markup UI element on first draw.
  12.    
  13.     - this way we only need to actually build the markup when it is
  14.       required for drawing. since we don't measure the UI element here
  15.       either we don't need to worry about that.
  16.    
  17.     - for Micromine 2022 where we currently are loading ~5000 icons (~1250
  18.       commands, large and small, normal and disabled) the differences are:
  19.    
  20.                     Icon Load Time           Initial Memory Usage
  21.       Before        ~7000ms                  ~560mb
  22.       After         ~27ms                    ~160mb
  23.    
  24.     - the big difference comes from not building icons that aren't actually
  25.       visible yet including things in hidden ribbon tabs, some small icons,
  26.       some large icons and so on. the memory usage grows as those icons are
  27.       loaded on demand.
  28.  
  29. diff --git a/Source/Common/XTPImageManager.cpp b/Source/Common/XTPImageManager.cpp
  30. index dfe106b..35b9d94 100644
  31. --- a/Source/Common/XTPImageManager.cpp
  32. +++ b/Source/Common/XTPImageManager.cpp
  33. @@ -1263,10 +1263,10 @@ CXTPImageManagerVectorImageHandle::CXTPImageManagerVectorImageHandle()
  34.  
  35.  CXTPImageManagerVectorImageHandle::CXTPImageManagerVectorImageHandle(
  36.     CXTPMarkupContext* pContext, BOOL bSharedContext, CXTPMarkupUIElement* pMarkupElement,
  37. -   HGLOBAL SerializationData)
  38. +   HGLOBAL SerializationData, BOOL bWide)
  39.     : m_pResource(NULL)
  40.  {
  41. -   ASSERT(NULL != pMarkupElement);
  42. +   //ASSERT(NULL != pMarkupElement);
  43.     ASSERT(NULL != pContext);
  44.  
  45.     m_pResource                           = new IMAGE_RESOURCE;
  46. @@ -1275,7 +1275,9 @@ CXTPImageManagerVectorImageHandle::CXTPImageManagerVectorImageHandle(
  47.     m_pResource->u.markup.pContext        = pContext;
  48.     m_pResource->u.markup.bSharedContext  = bSharedContext;
  49.     m_pResource->u.markup.pMarkupElement  = pMarkupElement;
  50. +   m_pResource->u.markup.bDelayed = (pMarkupElement == NULL);
  51.     m_pResource->SerializationData        = SerializationData;
  52. +   m_pResource->bWide = bWide;
  53.     m_pResource->pSerializationDataStream = NULL;
  54.  }
  55.  
  56. @@ -1284,7 +1286,7 @@ CXTPImageManagerVectorImageHandle::CXTPImageManagerVectorImageHandle(
  57.     IStream* pSerializationDataStream)
  58.     : m_pResource(NULL)
  59.  {
  60. -   ASSERT(NULL != pMarkupElement);
  61. +   //ASSERT(NULL != pMarkupElement);
  62.     ASSERT(NULL != pContext);
  63.  
  64.     m_pResource                           = new IMAGE_RESOURCE;
  65. @@ -1293,7 +1295,9 @@ CXTPImageManagerVectorImageHandle::CXTPImageManagerVectorImageHandle(
  66.     m_pResource->u.markup.pContext        = pContext;
  67.     m_pResource->u.markup.bSharedContext  = bSharedContext;
  68.     m_pResource->u.markup.pMarkupElement  = pMarkupElement;
  69. +   m_pResource->u.markup.bDelayed = (pMarkupElement == NULL);
  70.     m_pResource->SerializationData        = NULL;
  71. +   m_pResource->bWide = FALSE;
  72.     m_pResource->pSerializationDataStream = pSerializationDataStream;
  73.  
  74.     if (NULL != pSerializationDataStream)
  75. @@ -1395,7 +1399,7 @@ CXTPImageManagerVectorImageHandle CXTPImageManagerVectorImageHandle::CreateMarku
  76.                     GlobalUnlock(hGlobal);
  77.  
  78.                     return CXTPImageManagerVectorImageHandle(pContext, bSharedContext,
  79. -                                                            pMarkupElement, hGlobal);
  80. +                                                            pMarkupElement, hGlobal, FALSE);
  81.                 }
  82.  
  83.                 GlobalFree(hGlobal);
  84. @@ -1452,7 +1456,7 @@ CXTPImageManagerVectorImageHandle CXTPImageManagerVectorImageHandle::CreateMarku
  85.                     GlobalUnlock(hGlobal);
  86.  
  87.                     return CXTPImageManagerVectorImageHandle(pContext, bSharedContext,
  88. -                                                            pMarkupElement, hGlobal);
  89. +                                                            pMarkupElement, hGlobal, TRUE);
  90.                 }
  91.  
  92.                 GlobalFree(hGlobal);
  93. @@ -1465,6 +1469,135 @@ CXTPImageManagerVectorImageHandle CXTPImageManagerVectorImageHandle::CreateMarku
  94.     return CXTPImageManagerVectorImageHandle();
  95.  }
  96.  
  97. +CXTPImageManagerVectorImageHandle CXTPImageManagerVectorImageHandle::CreateDelayedMarkupHandle(
  98. +   CXTPMarkupContext* pContext, BOOL bSharedContext, IStream* pStream)
  99. +{
  100. +   ASSERT(pContext);
  101. +   ASSERT(pStream);
  102. +
  103. +   LARGE_INTEGER pos = { 0 };
  104. +   if (FAILED(pStream->Seek(pos, STREAM_SEEK_SET, 0)))
  105. +       return CXTPImageManagerVectorImageHandle();
  106. +
  107. +   //CXTPMarkupUIElement* pMarkupElement = XTPMarkupParseText(pContext, pStream);
  108. +   if (/*!pMarkupElement*/ FALSE)
  109. +       return CXTPImageManagerVectorImageHandle();
  110. +
  111. +   return CXTPImageManagerVectorImageHandle(pContext, bSharedContext, NULL, pStream);
  112. +}
  113. +
  114. +CXTPImageManagerVectorImageHandle CXTPImageManagerVectorImageHandle::CreateDelayedMarkupHandle(
  115. +   CXTPMarkupContext* pContext, BOOL bSharedContext, HGLOBAL SerializationData)
  116. +{
  117. +   ASSERT(pContext);
  118. +   ASSERT(SerializationData);
  119. +
  120. +   IStreamPtr pStream;
  121. +   if (SUCCEEDED(CreateStreamOnHGlobal(SerializationData, TRUE, &pStream)))
  122. +   {
  123. +       CXTPImageManagerVectorImageHandle imgHandle = CreateDelayedMarkupHandle(pContext, bSharedContext,
  124. +                                                                        pStream);
  125. +       return imgHandle;
  126. +   }
  127. +
  128. +   GlobalFree(SerializationData);
  129. +   return CXTPImageManagerVectorImageHandle();
  130. +}
  131. +
  132. +CXTPImageManagerVectorImageHandle CXTPImageManagerVectorImageHandle::CreateDelayedMarkupHandle(
  133. +   CXTPMarkupContext* pContext, BOOL bSharedContext, LPCSTR lpszCode, INT_PTR nLength)
  134. +{
  135. +   ASSERT(pContext);
  136. +   ASSERT(lpszCode);
  137. +
  138. +   if (nLength < 0)
  139. +       nLength = XTPToIntPtrChecked(strlen(lpszCode));
  140. +
  141. +   if (nLength > 0)
  142. +   {
  143. +       // CXTPMarkupUIElement* pMarkupElement = XTPMarkupParseText(pContext, lpszCode, nLength);
  144. +       if (/*pMarkupElement*/ TRUE)
  145. +       {
  146. +           HGLOBAL hGlobal = GlobalAlloc(GMEM_FIXED, XTPToSizeTChecked(nLength));
  147. +           if (hGlobal)
  148. +           {
  149. +               PVOID pBuff = GlobalLock(hGlobal);
  150. +               if (pBuff)
  151. +               {
  152. +                   MEMCPY_S(pBuff, lpszCode, XTPToSizeTChecked(nLength));
  153. +                   GlobalUnlock(hGlobal);
  154. +
  155. +                   return CXTPImageManagerVectorImageHandle(pContext, bSharedContext,
  156. +                                                            NULL, hGlobal, FALSE);
  157. +               }
  158. +
  159. +               GlobalFree(hGlobal);
  160. +           }
  161. +
  162. +           //XTPMarkupReleaseElement(pMarkupElement);
  163. +       }
  164. +   }
  165. +
  166. +   return CXTPImageManagerVectorImageHandle();
  167. +}
  168. +
  169. +CXTPImageManagerVectorImageHandle CXTPImageManagerVectorImageHandle::CreateDelayedMarkupHandle(
  170. +   CXTPMarkupContext* pContext, BOOL bSharedContext, LPCWSTR lpszCode, INT_PTR nLength)
  171. +{
  172. +   ASSERT(pContext);
  173. +   ASSERT(lpszCode);
  174. +
  175. +   if (nLength < 1)
  176. +       nLength = XTPToIntPtrChecked(wcslen(lpszCode));
  177. +
  178. +   if (nLength > 0)
  179. +   {
  180. +       LPCWSTR lpszInput         = lpszCode;
  181. +       const WCHAR UnicodeMarker = 0xFEFF;
  182. +       BOOL hasUnicodeMarker = memcmp((const void*)lpszInput, &UnicodeMarker, sizeof(WCHAR)) == 0
  183. +                                   ? TRUE
  184. +                                   : FALSE;
  185. +
  186. +       //CXTPMarkupUIElement* pMarkupElement = XTPMarkupParseText(pContext, hasUnicodeMarker
  187. +       //                                                                     ? ++lpszInput
  188. +       //                                                                     : lpszInput);
  189. +       if (/*pMarkupElement*/ TRUE)
  190. +       {
  191. +           INT_PTR sz = XTPToIntPtrChecked(sizeof(WCHAR)
  192. +                                           * (hasUnicodeMarker ? nLength : nLength + 1));
  193. +
  194. +           HGLOBAL hGlobal = GlobalAlloc(GMEM_FIXED, XTPToSizeTChecked(sz));
  195. +           if (hGlobal)
  196. +           {
  197. +               PVOID pBuff = GlobalLock(hGlobal);
  198. +               if (pBuff)
  199. +               {
  200. +                   if (hasUnicodeMarker)
  201. +                   {
  202. +                       MEMCPY_S((WCHAR*)pBuff, lpszCode, XTPToSizeTChecked(sz));
  203. +                   }
  204. +                   else
  205. +                   {
  206. +                       MEMCPY_S(pBuff, &UnicodeMarker, sizeof(WCHAR));
  207. +                       MEMCPY_S((WCHAR*)pBuff + 1, lpszCode, XTPToSizeTChecked(sz));
  208. +                   }
  209. +
  210. +                   GlobalUnlock(hGlobal);
  211. +
  212. +                   return CXTPImageManagerVectorImageHandle(pContext, bSharedContext,
  213. +                                                            NULL, hGlobal, TRUE);
  214. +               }
  215. +
  216. +               GlobalFree(hGlobal);
  217. +           }
  218. +
  219. +           //XTPMarkupReleaseElement(pMarkupElement);
  220. +       }
  221. +   }
  222. +
  223. +   return CXTPImageManagerVectorImageHandle();
  224. +}
  225. +
  226.  void CXTPImageManagerVectorImageHandle::Reset()
  227.  {
  228.     if (NULL != m_pResource)
  229. @@ -1516,6 +1649,61 @@ void CXTPImageManagerVectorImageHandle::Draw(CDC* pDC, CPoint pt, CSize szIcon)
  230.  
  231.     if (IsMarkupImage())
  232.     {
  233. +       if (NULL == m_pResource->u.markup.pMarkupElement && m_pResource->u.markup.bDelayed)
  234. +           {
  235. +           // If markup UI element hasn't been built yet and we are delayed building, build it from the
  236. +           // input markup stored in our resource structure.
  237. +
  238. +           if (NULL != m_pResource->SerializationData)
  239. +               {
  240. +        HGLOBAL hGlobal = m_pResource->SerializationData;
  241. +        if (hGlobal)
  242. +        {
  243. +          LPVOID pBuff = GlobalLock(hGlobal);
  244. +          if (pBuff)
  245. +          {
  246. +            SIZE_T sz = GlobalSize(hGlobal);
  247. +
  248. +                       if (m_pResource->bWide)
  249. +                           {
  250. +                           LPCWSTR lpszCode = (LPCWSTR)pBuff;
  251. +                           INT_PTR nLength = sz / sizeof(WCHAR);
  252. +
  253. +              const WCHAR UnicodeMarker = 0xFEFF;
  254. +              BOOL hasUnicodeMarker = memcmp((const void*)lpszCode, &UnicodeMarker, sizeof(WCHAR)) == 0
  255. +                            ? TRUE
  256. +                            : FALSE;
  257. +                           if (hasUnicodeMarker)
  258. +                               {
  259. +                               ++lpszCode;
  260. +                               --nLength;
  261. +                               }
  262. +
  263. +                           m_pResource->u.markup.pMarkupElement = XTPMarkupParseText(m_pResource->u.markup.pContext,
  264. +                                                                        lpszCode, nLength);
  265. +                           }
  266. +                       else
  267. +                           {
  268. +                           LPCSTR lpszCode = (LPCSTR)pBuff;
  269. +                           INT_PTR nLength = sz / sizeof(CHAR);
  270. +                           m_pResource->u.markup.pMarkupElement = XTPMarkupParseText(m_pResource->u.markup.pContext,
  271. +                                                                        lpszCode, nLength);
  272. +                           }
  273. +
  274. +            GlobalUnlock(hGlobal);
  275. +          }
  276. +        }
  277. +
  278. +               }
  279. +           else if (NULL != m_pResource->pSerializationDataStream)
  280. +               {
  281. +               m_pResource->u.markup.pMarkupElement = XTPMarkupParseText(m_pResource->u.markup.pContext,
  282. +                                                                                                                                   m_pResource->pSerializationDataStream);
  283. +               }
  284. +
  285. +           m_pResource->u.markup.bDelayed = FALSE;
  286. +           }
  287. +
  288.         XTPMarkupRenderElement(m_pResource->u.markup.pMarkupElement, pDC->GetSafeHdc(), rc);
  289.     }
  290.  }
  291. diff --git a/Source/Common/XTPImageManager.h b/Source/Common/XTPImageManager.h
  292. index 78abc99..355d031 100644
  293. --- a/Source/Common/XTPImageManager.h
  294. +++ b/Source/Common/XTPImageManager.h
  295. @@ -184,10 +184,26 @@ public:
  296.                                                                 LPCWSTR lpszCode,
  297.                                                                 INT_PTR nLength = -1);
  298.  
  299. +public:
  300. +   static CXTPImageManagerVectorImageHandle CreateDelayedMarkupHandle(CXTPMarkupContext *pContext,
  301. +                                                                                                                                        BOOL bSharedContext,
  302. +                                                                                                                                        HGLOBAL SerializationData);
  303. +   static CXTPImageManagerVectorImageHandle CreateDelayedMarkupHandle(CXTPMarkupContext *pContext,
  304. +                                                                                                                                        BOOL bSharedContext,
  305. +                                                                                                                                        IStream *pStream);
  306. +   static CXTPImageManagerVectorImageHandle CreateDelayedMarkupHandle(CXTPMarkupContext *pContext,
  307. +                                                                                                                                        BOOL bSharedContext,
  308. +                                                                                                                                        LPCSTR lpszCode,
  309. +                                                                                                                                        INT_PTR nLength = -1);
  310. +   static CXTPImageManagerVectorImageHandle CreateDelayedMarkupHandle(CXTPMarkupContext *pContext,
  311. +                                                                                                                                        BOOL bSharedContext,
  312. +                                                                                                                                        LPCWSTR lpszCode,
  313. +                                                                                                                                        INT_PTR nLength = -1);
  314. +
  315.  private:
  316.     CXTPImageManagerVectorImageHandle(CXTPMarkupContext* pContext, BOOL bSharedContext,
  317.                                       CXTPMarkupUIElement* pMarkupElement,
  318. -                                     HGLOBAL SerializationData);
  319. +                                     HGLOBAL SerializationData, BOOL bWide);
  320.     CXTPImageManagerVectorImageHandle(CXTPMarkupContext* pContext, BOOL bSharedContext,
  321.                                       CXTPMarkupUIElement* pMarkupElement,
  322.                                       IStream* pSerializationData);
  323. @@ -203,6 +219,7 @@ private:
  324.             MarkupType
  325.         } nType;
  326.         HGLOBAL SerializationData;
  327. +       BOOL bWide;
  328.         IStream* pSerializationDataStream;
  329.         union {
  330.             struct
  331. @@ -210,6 +227,7 @@ private:
  332.                 CXTPMarkupContext* pContext;
  333.                 BOOL bSharedContext;
  334.                 CXTPMarkupUIElement* pMarkupElement;
  335. +               BOOL bDelayed;
  336.             } markup;
  337.             // more can be added
  338.         } u;
Advertisement
Add Comment
Please, Sign In to add comment