00001 // $Id: offattr.cpp 1361 2006-06-25 16:43:38Z alex $ 00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE 00003 ================================XARAHEADERSTART=========================== 00004 00005 Xara LX, a vector drawing and manipulation program. 00006 Copyright (C) 1993-2006 Xara Group Ltd. 00007 Copyright on certain contributions may be held in joint with their 00008 respective authors. See AUTHORS file for details. 00009 00010 LICENSE TO USE AND MODIFY SOFTWARE 00011 ---------------------------------- 00012 00013 This file is part of Xara LX. 00014 00015 Xara LX is free software; you can redistribute it and/or modify it 00016 under the terms of the GNU General Public License version 2 as published 00017 by the Free Software Foundation. 00018 00019 Xara LX and its component source files are distributed in the hope 00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the 00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 See the GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with Xara LX (see the file GPL in the root directory of the 00026 distribution); if not, write to the Free Software Foundation, Inc., 51 00027 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00028 00029 00030 ADDITIONAL RIGHTS 00031 ----------------- 00032 00033 Conditional upon your continuing compliance with the GNU General Public 00034 License described above, Xara Group Ltd grants to you certain additional 00035 rights. 00036 00037 The additional rights are to use, modify, and distribute the software 00038 together with the wxWidgets library, the wxXtra library, and the "CDraw" 00039 library and any other such library that any version of Xara LX relased 00040 by Xara Group Ltd requires in order to compile and execute, including 00041 the static linking of that library to XaraLX. In the case of the 00042 "CDraw" library, you may satisfy obligation under the GNU General Public 00043 License to provide source code by providing a binary copy of the library 00044 concerned and a copy of the license accompanying it. 00045 00046 Nothing in this section restricts any of the rights you have under 00047 the GNU General Public License. 00048 00049 00050 SCOPE OF LICENSE 00051 ---------------- 00052 00053 This license applies to this program (XaraLX) and its constituent source 00054 files only, and does not necessarily apply to other Xara products which may 00055 in part share the same code base, and are subject to their own licensing 00056 terms. 00057 00058 This license does not apply to files in the wxXtra directory, which 00059 are built into a separate library, and are subject to the wxWindows 00060 license contained within that directory in the file "WXXTRA-LICENSE". 00061 00062 This license does not apply to the binary libraries (if any) within 00063 the "libs" directory, which are subject to a separate license contained 00064 within that directory in the file "LIBS-LICENSE". 00065 00066 00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS 00068 ---------------------------------------------- 00069 00070 Subject to the terms of the GNU Public License (see above), you are 00071 free to do whatever you like with your modifications. However, you may 00072 (at your option) wish contribute them to Xara's source tree. You can 00073 find details of how to do this at: 00074 http://www.xaraxtreme.org/developers/ 00075 00076 Prior to contributing your modifications, you will need to complete our 00077 contributor agreement. This can be found at: 00078 http://www.xaraxtreme.org/developers/contribute/ 00079 00080 Please note that Xara will not accept modifications which modify any of 00081 the text between the start and end of this header (marked 00082 XARAHEADERSTART and XARAHEADEREND). 00083 00084 00085 MARKS 00086 ----- 00087 00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara 00089 designs are registered or unregistered trademarks, design-marks, and/or 00090 service marks of Xara Group Ltd. All rights in these marks are reserved. 00091 00092 00093 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK. 00094 http://www.xara.com/ 00095 00096 =================================XARAHEADEREND============================ 00097 */ 00098 00099 #include "camtypes.h" 00100 00101 //#include "docview.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00102 #include "offattr.h" 00103 00104 #include "GDrawIntf.h" 00105 #include "offscrn.h" // GRenderRegionWrapper 00106 #include "grndrgn.h" // GRenderRegion 00107 00108 // Bitmap manipulation 00109 //#include "bitmap.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00110 #include "oilbitmap.h" 00111 00112 //#include "winrect.h" // CalculateWinRect 00113 00114 00115 CC_IMPLEMENT_DYNAMIC( OffscreenAttrValue, GeometryLinkedAttrValue ) 00116 00117 // Declare smart memory handling in Debug builds 00118 #define new CAM_DEBUG_NEW 00119 #define BOGUS_DEFAULT_ATTR_NODE_POINTER (NodeRenderableBounded*) 0x1 00120 00121 00123 // 00124 // Helper functions 00125 // 00127 // Measurement constants are defined in offscrn.h, and 00128 // view Scale determines dpi (ie pix per inch) of screen. 00129 00130 // if there is no view, then returns zero. 00131 FIXED16 GetCurrentViewScale() 00132 { 00133 View* pView = View::GetCurrent(); 00134 if (pView == NULL) 00135 return FIXED16(0); 00136 00137 return pView->GetViewScale(); 00138 } 00139 00140 MILLIPOINT ConvertMeasurementToMillipointsAtCurrentViewScale(INT32 Width, UnitType type) 00141 { 00142 const double dpi = (double)GRenderRegion::GetDefaultDPI(); 00143 const double MillipointsPerPixel = (dpi <= 0) ? 750.0 : MILLIPOINTS_PER_INCH / dpi; 00144 00145 double ScaledWidth = 0; 00146 00147 switch(type) 00148 { 00149 case MILLIPOINTS: 00150 ScaledWidth = Width; 00151 break; 00152 00153 case PIXELS: 00154 ScaledWidth = MillipointsPerPixel * Width; 00155 ScaledWidth /= GetCurrentViewScale().MakeDouble(); 00156 break; 00157 00158 // unsupported unit type. 00159 default: 00160 ScaledWidth = -1; 00161 break; 00162 } 00163 00164 return (MILLIPOINT)ScaledWidth; 00165 } 00166 00167 UINT32 ConvertMillipointsToPixelsAtCurrentViewScale(MILLIPOINT mp) 00168 { 00169 const double dpi = (double)GRenderRegion::GetDefaultDPI(); 00170 const double MillipointsPerPixel = (dpi <= 0) ? 750.0 : MILLIPOINTS_PER_INCH / dpi; 00171 00172 // NB this routine rounds to the nearest pixel, so if you convert 00173 // from mp to pixels and back again, you are highly unlikely 00174 // to get the value you started off with! 00175 double Pixels = (mp / MillipointsPerPixel); 00176 Pixels *= GetCurrentViewScale().MakeDouble(); 00177 00178 return (UINT32)(Pixels + 0.5); 00179 } 00180 00181 INT32 PixelsPerMeterAtCurrentViewScale() 00182 { 00183 // NB return value is given to the nearest pixel. 00184 double ppm = PIXELS_PER_INCH * INCHES_PER_METRE; 00185 ppm *= GetCurrentViewScale().MakeDouble(); 00186 return (INT32)(ppm + 0.5); 00187 } 00188 00190 // 00191 // AttributeValue classes 00192 // 00194 /******************************************************************************************** 00195 > OffscreenAttrValue:: 00196 Author: Ilan_Copelyn (Xara Group Ltd) <camelotdev@xara.com> 00197 Created: 24/01/2000 00198 Purpose: This constructor is only called when instantiating a derived class. 00199 SeeAlso: 00200 ********************************************************************************************/ 00201 OffscreenAttrValue::OffscreenAttrValue() 00202 { 00203 m_fForceTransparency = FALSE; 00204 #ifdef DEBUGOFFSCREENATTRS 00205 m_fHasBeenRendered = FALSE; 00206 #endif 00207 } 00208 00209 /******************************************************************************************** 00210 > OffscreenAttrValue:: 00211 Author: Ilan_Copelyn (Xara Group Ltd) <camelotdev@xara.com> 00212 Created: 24/01/2000 00213 Purpose: Nothing special to do. All memory used for bitmap structures in offscreen 00214 rendering is freed in OffscreenRenderingCompleted() 00215 SeeAlso: 00216 ********************************************************************************************/ 00217 OffscreenAttrValue::~OffscreenAttrValue() 00218 { 00219 } 00220 00221 /******************************************************************************************** 00222 > OffscreenAttrValue:: 00223 Author: Ilan_Copelyn (Xara Group Ltd) <camelotdev@xara.com> 00224 Created: 24/01/2000 00225 Purpose: This funciton is used in context of attribute optimisation. 00226 I'm implementing this function to re-iterate that all OffscreenAttrValues are 00227 unique, even though it may never get called (ie because they are never optimised) 00228 SeeAlso: 00229 ********************************************************************************************/ 00230 BOOL OffscreenAttrValue::IsDifferent(AttributeValue*) 00231 { 00232 return TRUE; 00233 } 00234 00235 00236 /******************************************************************************************** 00237 > OffscreenAttrValue:: 00238 Author: Ilan_Copelyn (Xara Group Ltd) <camelotdev@xara.com> 00239 Created: 24/01/2000 00240 Purpose: Determined how large an area we need to draw ofscreen in this rendering 00241 iterartion. This is calculated in DocRect coords and it is up to the 00242 GRenderRegion Offscreen rendering system to alloca a large enough chunk 00243 of memory to hold the bitmap 00244 SeeAlso: 00245 ********************************************************************************************/ 00246 void OffscreenAttrValue::Render(RenderRegion *pRender, BOOL Temp) 00247 { 00248 // Debug test for an offscreen attribute being Render()'ed multiple times without 00249 // OffscreenRenderingCompleted() being called. This is easily caused by: 00250 // 1. rendering code which isn't bracketed by calls to SaveContext() / RestoreContext() 00251 // 2. rendering an attribute map which includes an offscreen attr - you must be careful 00252 // that you don't inadvertently render the attr twice. 00253 // 00254 // Reason: offscreen attrs initialise and divert subsequent rendering into an offscreen 00255 // bitmap, which is retrieved and processed when the attribute goes out of scope 00256 // after a RestoreContext() call. Render the attr twice => you'll be creating 00257 // and maybe rendering into an offscreen bitmap which is never used + may also 00258 // cause a memory leak of that bitmap. 00259 #ifdef DEBUGOFFSCREENATTRS 00260 if (!IsDefaultFlagSet()) 00261 { 00262 if (m_fHasBeenRendered) 00263 { 00264 char msg[120]; 00265 sprintf(msg, "OffscreenAttrValue::Render; Attr at 0x%x rendered more than once! See code for details!", (DWORD)this); 00266 ERROR3(msg); 00267 return; 00268 } 00269 else 00270 { 00271 m_fHasBeenRendered = TRUE; 00272 } 00273 } 00274 #endif 00275 00276 pRender->SetOffscreen(this); 00277 } 00278 00279 00280 00281 /******************************************************************************************** 00282 > OffscreenAttrValue:: 00283 Author: Ilan_Copelyn (Xara Group Ltd) <camelotdev@xara.com> 00284 Created: 24/01/2000 00285 Purpose: 00286 SeeAlso: GRenderRegion::RestoreGDrawBitmap 00287 ********************************************************************************************/ 00288 void OffscreenAttrValue::Restore(RenderRegion *pRegion, BOOL Temp) 00289 { 00290 pRegion->RestoreOffscreen(this); 00291 } 00292 00293 /******************************************************************************************** 00294 > OffscreenAttrValue:: 00295 Author: Ilan_Copelyn (Xara Group Ltd) <camelotdev@xara.com> 00296 Created: 24/01/2000 00297 Purpose: return docrect which needs to be drawn offscreen in this rendering iteration 00298 SeeAlso: GRenderRegion::SetGDrawBitmap & OffscreenAttrValue::Render (calcualtes m_OffBB) 00299 ********************************************************************************************/ 00300 DocRect OffscreenAttrValue::GetOffscreenBoundingRect() 00301 { 00302 return m_OffBB; 00303 } 00304 00305 00306 00307 /******************************************************************************************** 00308 00309 > BOOL OffscreenAttrValue::DoesOffscreenBmpRequireTransp(GRenderRegion* pGRR = NULL) 00310 00311 Author: Ilan_Copelyn (Xara Group Ltd) <camelotdev@xara.com>, rewritten by Karim MacDonald 00312 Created: 24/01/2000, rewritten 19/11/2000 00313 00314 Inputs: pGRR ptr to the GRenderRegion into which we'll be rendered. 00315 00316 Returns: TRUE if we need a transparency-capable bitmap, 00317 FALSE otherwise. 00318 00319 Purpose: As an optimisation, you can make this call on an offscreen attribute, 00320 and only allocate + use a transparency-capable bitmap if it is necessary. 00321 eg, this results in about a 10% performance increase for feathering. 00322 00323 Notes: pGRR is unused in this method, so may be NULL. 00324 However, at least one offscreen-attr - FeatherAttrValue - requires that 00325 this parameter be non-NULL. 00326 00327 See also: TestNodeForTransparency() 00328 00329 ********************************************************************************************/ 00330 BOOL OffscreenAttrValue::DoesOffscreenBmpRequireTransp(GRenderRegion* pGRR) 00331 { 00332 // if we've been told to always use transparency, then always return TRUE here. 00333 if (m_fForceTransparency) 00334 return TRUE; 00335 00336 // our normal behaviour - test our linked node to see whether it needs transparency. 00337 return TestNodeForTransparency(GetLinkedNode()); 00338 } 00339 00340 00341 00342 /******************************************************************************************** 00343 00344 > BOOL TestNodeForTransparency(Node* pNode) 00345 00346 Author: Karim_MacDonald (Xara Group Ltd) <camelotdev@xara.com> 00347 Created: 16/08/2000 00348 Inputs: pNode the node to test for transparency. 00349 Purpose: Test the given node to see whether it or any of its children requires 00350 transparency. 00351 Returns: TRUE if pNode requires transparency, or is NULL. 00352 See Also: NodeRenderable::IsSeeThrough() 00353 00354 ********************************************************************************************/ 00355 BOOL OffscreenAttrValue::TestNodeForTransparency(Node* pNode) 00356 { 00357 // return TRUE if the node is NULL, just in case transparency is later required. 00358 if (pNode == NULL || !pNode->IsNodeRenderableClass()) 00359 return TRUE; 00360 00361 // check the node to see whether it is see-through, remembering to test 00362 // its indirectly applied attributes. 00363 return ((NodeRenderable*)pNode)->IsSeeThrough(TRUE); 00364 } 00365 00366 00367 00369 // 00370 // Debuging functions 00371 // 00373 /* 00374 NB this is purely for debugging. Remove once you are happy that your offscreen bitmap 00375 is being created correctly 00376 00377 Notes:- 00378 1) only performed when viewscale = 1.0; ie 100% zoom ie 750 mp per pix 00379 00380 2) only comes into play if more than _DEBUG_OFFSCREEN_PIXEL_WIDTH pix wide region of 00381 node is invalid (ie only when intersection of node bounds and current clip rect 00382 is wider than _DEBUG_OFFSCREEN_PIXEL_WIDTH) 00383 00384 3) select a _DEBUG_OFFSCREEN_PIXEL_WIDTH value which make viewing memory using Visual C++ 00385 'memory debug viewer' easier 00386 3.1) Mem Viewer has max 48 bytes wide view (@ 1152*864) 00387 3.2) Use byte view for 8bpp bitmaps 00388 3.3) Use INT32 hex (ie DWORD) view for 32bpp bitmaps 00389 3.4) NB 32bpp scanlines are 4*(pixel width) wide in viewer 00390 00391 4) If used, the invalid region of the node will be changed to so that it is _DEBUG_OFFSCREEN_PIXEL_WIDTH 00392 pixels wide and covers only the left hand side of this region 00393 NB pixel alignment of viewable bounds won't affect FillPath rendering on the nodes path 00394 - ie bottom left corner of node bounds will always be drawn in the bottom left pixel in 00395 the offscreen bitmap 00396 00397 Conditions for use:- 00398 -------------------- 00399 1) pGRR->ScalePixelWidth = 750 00400 2) Width of ViewableRect (ie intersection of NodeBounds and current invalid clip rect) 00401 > _DEBUG_OFFSCREEN_PIXEL_WIDTH 00402 00403 What can be debugged using _DEBUG_OFFSCREEN_PIXEL_WIDTH:- 00404 --------------------------------------------------------- 00405 There are 3 bitmaps which we would want to look at in the memory viewer. Each has a corresponding 00406 bounding box measured in MILLIPOINTS. The bitmaps and mp bounds are listed below:- 00407 00408 1 Offscreen bmp (OffBB) = the bitmap of the node to which this offscreen attr is attached 00409 2 Cached bmp (NodeBB)= bitmap of node generated by GRenderBitmap 00410 (generated when offscreen attr is first attached to the node) 00411 3 Sliced cached bmp (OffBB) = left hand slice of cached bitmap sliced so we can compare it to Offscreen bmp 00412 (copied from cache bmp) 00413 00414 The bounds are calculated as follows:- 00415 -------------------------------------- 00416 NodeBB = the nodes original bounding box 00417 ClipBB = current invalid clip region 00418 NB even in instances where you would expect the ClipBB to be closely related to NodeBB (eg ops applied 00419 on the node which invalidate NodeBB), ClipBB seems to be a variable increment larger than NodeBB 00420 eg for applying col fill increment = ?pix (5 I think) 00421 for applying line fill increment = ?pix (3 I think) 00422 for applying feathering increment = 10 pix 00423 00424 ViewBB = NodeBB.Intersect(ClipBB) => vWidth x vHeight 00425 00426 IF debugging offscreen bitmap: 00427 OffBB = _DEBUG_OFFSCREEN_PIXEL_WIDTH x vHeight 00428 00429 Values to use:- 00430 --------------- 00431 When debugging Offscreen bitmap :- 00432 - use _DEBUG_OFFSCREEN_PIXEL_WIDTH = 48 00433 - compare sliced cached bmp and offscreen bmp in mem viewer 00434 - NB the offscreen bitmap is upside down in memory. ie the pixels comprising the first row 00435 in memory at m_pOffscreenBmpBits is infact the bottom row when displayed 00436 (this is how GDraw draws) 00437 00438 Functions affected:- 00439 -------------------- 00440 // Setup 00441 Create feather 00442 node (ie NodeBB), profile, feather size 00443 if debugging 00444 KernelBitmap* md_NodeBmp; 00445 KernelBitmap* md_SlicedBmp; 00446 00447 Render 00448 DocRect v = NodeBB.Intersect(ClipBB); 00449 if debugging && Zoom=100% and v.Width > _DEBUG_OFFSCREEN_PIXEL_WIDTH 00450 OffBB = _DEBUG_OFFSCREEN_PIXEL_WIDTH x v.Height 00451 else 00452 DocRect m_OffBB = v.Width x v.Height ; 00453 00454 00455 SetFeathering( m_pOffscreenBmpInfo, m_pOffscreenBmpBits); -> sets up Offscreen Bmp 00456 00457 // Use members 00458 OffscreenRenderingCompleted 00459 create bitmap fill from OffscreenBmp 00460 00461 generate feather mask bitmap from OffscreenBmp using Feather32BppBitmap convolution 00462 00463 create transp bmp fill 00464 00465 GRR::RenderBitmapFillWithTransp 00466 */ 00467 00468 #ifdef _DEBUG_OFFSCREEN_PIXEL_WIDTH 00469 KernelBitmap* OffscreenAttrValue::CreateIntermediateBitmap( 00470 GRenderRegion* GRR, 00471 Node* pNode, 00472 double ScreenResMultiplier, 00473 UINT32 bpp, 00474 BOOL UseInternalFmt ) 00475 { 00476 GRenderRegionWrapper* pRendWrap = GRenderRegionWrapper::GetAppropriateRenderWrapper( 00477 GRR, ScreenResMultiplier, ((NodeRenderableBounded*) pNode)->GetBoundingRect(),bpp,UseInternalFmt); 00478 00479 if(!pRendWrap) 00480 { 00481 // failure message already posted 00482 return NULL; 00483 } 00484 00485 pRendWrap->RenderTree(pNode); 00486 00487 KernelBitmap* kBmp = pRendWrap->GetKernelBitmap(); 00488 00489 pRendWrap->RestorePreviousRendererState(); 00490 00491 return kBmp; 00492 } 00493 00494 BOOL OffscreenAttrValue::RenderDebuggingBitmaps( 00495 GRenderRegion* GRR, 00496 Node* pNode, 00497 double ScreenResMultiplier, 00498 UINT32 bpp, 00499 BOOL UseInternalFmt ) 00500 { 00501 md_NodeBmp = CreateIntermediateBitmap( NULL, pNode, 1.0, 32, FALSE); 00502 if(md_NodeBmp==NULL) 00503 { 00504 ENSURE(FALSE,"Failed to create GRenderBitmap for comparison with offscreen rendered image."); 00505 return FALSE; 00506 } 00507 00508 double zoom = GetCurrentViewScale().MakeDouble(); 00509 if( (zoom==1.0) && (md_NodeBmp->GetWidth() > _DEBUG_OFFSCREEN_PIXEL_WIDTH) ) 00510 { 00511 // slice to specified dimensions 00512 LPBITMAPINFO sliceInfo; 00513 LPBYTE sliceBits; 00514 UINT32 sliceWidth = _DEBUG_OFFSCREEN_PIXEL_WIDTH; 00515 sliceInfo = AllocDIB( sliceWidth, md_NodeBmp->GetHeight(), 00516 32,&sliceBits,NULL,TRUE ); 00517 LPBYTE pSetSliceBits = sliceBits; 00518 LPBYTE pNodeBits = md_NodeBmp->GetBitmapBits(); 00519 UINT32 NodeScanline = md_NodeBmp->GetWidth() * 4; 00520 UINT32 sliceScanline = sliceWidth * 4; 00521 UINT32 NodeBmpSize = md_NodeBmp->GetHeight() * NodeScanline; 00522 UINT32 sliceRow=0; 00523 00524 // copy bits to sliced dimensions 00525 for(UINT32 row=0; row < NodeBmpSize; row+=NodeScanline) 00526 { 00527 for(UINT32 col=0; col < sliceScanline; col++) 00528 { 00529 pSetSliceBits[sliceRow + col] = pNodeBits[row + col]; 00530 } 00531 sliceRow+=sliceScanline; 00532 } 00533 00534 Document* pDoc = Document::GetCurrent(); 00535 WinBitmap* wBitmap = new WinBitmap(sliceInfo, sliceBits); 00536 md_SlicedBmp = new KernelBitmap(wBitmap,TRUE); //NB Temp = FALSE doesn't add to kernel bmp gallery 00537 OILBitmap* pOil = md_SlicedBmp->GetActualBitmap(); 00538 pOil->SetName(String_256("SlicedBitmap")); 00539 GetApplication()->GetGlobalBitmapList()->MakeNameUnique(&(pOil->m_BitmapName)); 00540 md_SlicedBmp->Attach(pDoc->GetBitmapList()); 00541 } 00542 } 00543 #endif