Changeset 134

Show
Ignore:
Timestamp:
06/28/09 14:40:31 (3 years ago)
Author:
JoeCoder
Message:

Fixed most of the performance issues in TextLayout?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/yage/core/timer.d

    r132 r134  
    99import tango.stdc.math; 
    1010import tango.util.Convert; 
     11import tango.text.convert.Format; 
    1112import tango.time.StopWatch; 
    1213 
     
    3031    protected StopWatch hpc; 
    3132     
    32     protected Timer source; 
     33    protected Timer source; // TODO:    allow using one timer as the souce of another, so pausing a scene could pause all timers in that scene. 
    3334 
    3435    /// Initialize and start the Timer. 
     
    171172    /// 
    172173    char[] toString() 
    173     {   return to!(char[])(get()); 
     174    {   return Format.convert("{:d8}", tell()); 
    174175    } 
    175176     
  • trunk/src/yage/gui/style.d

    r131 r134  
    2222import yage.gui.exceptions; 
    2323 
    24 import std.stdio
     24import yage.core.timer
    2525 
    2626/** 
     
    254254    int zIndex; 
    255255     
     256    private static Regex rxStyles; 
     257    private static Regex rxTokens; 
     258    private static Regex rxProperties; 
     259    static this() { 
     260        rxStyles = Regex(";\\s*"); 
     261        rxTokens = Regex(":\\s*"); 
     262        rxProperties = Regex("\\s+"); 
     263    } 
     264     
    256265    /** 
    257266     * Struct constructor, returns the default style. */ 
     
    271280        if (!style.length) 
    272281            return; 
    273          
     282 
    274283        // Parse and apply the style 
    275         Regex rxTokens = Regex(":\\s*"); 
    276         Regex rxProperties = Regex("\\s+"); 
    277         foreach (exp; Regex(";\\s*").split(style)) 
     284        scope styles = rxStyles.split(style); 
     285        foreach (exp; styles) 
    278286        {   char[][] tokens = rxTokens.split(exp); 
    279287            if (tokens.length<2) 
     
    343351                    throw new CSSException("Unsupported CSS Property: '", property, "'."); 
    344352            } 
    345         }       
     353        } 
    346354    } 
    347355 
  • trunk/src/yage/gui/surface.d

    r131 r134  
    251251                textTexture = new GPUTexture(textImage, false, false, text, true); 
    252252            else 
     253            {   //Timer a = new Timer(true); 
    253254                textTexture.commit(textImage, false, false, text, true); 
     255                //Stdout("texture upload: ", a.tell()*1000).newline; 
     256            } 
    254257            old_text = text; 
    255258        } 
  • trunk/src/yage/gui/textlayout.d

    r133 r134  
    2929{ 
    3030    private const char[] breaks = " *()-+=/\\,.;:|()[]{}<>\r\n"; // breaking characters 
     31    private static Regex rxSpaces; 
     32    private static Regex rxTags; 
     33    static this () 
     34    {   rxSpaces = Regex(`\s{2,}`); 
     35        rxTags = Regex(`\>[^\<]+\<`); 
     36    } 
    3137     
    3238    static double lastRenderTime=0;  
     
    138144        //text = .toString(lastRenderTime*1000, 6) ~ "<br/>" ~ text; // temporary profiling 
    139145        //Stdout("test").newline(); 
    140         Timer a = new Timer(); 
     146        Timer a = new Timer(false); 
    141147         
    142148        if (text.length && style.fontFamily) 
    143149        {   letters.length = 0; 
    144150            lines.length = 0;    
    145                  
    146             // Get an array of letter structs for the entire text. 
    147             scope nodes = getNodes(text, InlineStyle(style)); 
    148              
     151             
     152            // Get an array of letter structs for the entire text. (1ms) 
     153            scope nodes = getNodes(text, InlineStyle(style));            
    149154            foreach (ref node; nodes) // ref is req'd so that &node.style points to the real style. 
    150             {   // Aditional text replacement               
     155            {   // Aditional text replacement 
    151156                node.text = htmlToText(node.text); 
    152                  
    153                 foreach (c; node.text) // render each letter 
     157                foreach (c; node.text)  
    154158                {   if (node.style.fontFamily) 
    155159                    {   int h = node.style.fontSize; 
    156160                        int w = node.style.fontWeight == Style.FontWeight.BOLD ? h*3/2 : h; 
    157                         Letter l = node.style.fontFamily.getLetter(c, w, h); 
     161                        Letter l= node.style.fontFamily.getLetter(c, w, h); 
    158162                        l.extra = &node.style; 
    159163                        letters ~= l; 
    160164            }   }   } 
    161              
    162             // Build lines      
     165 
     166            // Build lines (.7ms) 
    163167            {   int i; 
    164168                while (i<letters.length) 
     
    210214            }   } 
    211215             
     216            lastRenderTime = a.tell(); 
     217             
    212218            // Get total height 
    213219            int totalHeight; 
    214220            foreach (line; lines) 
    215221                totalHeight += line.height; 
    216             totalHeight += lines[$-1].height / 3; 
    217              
    218             // Render Image 
     222            if (lines.length) 
     223                totalHeight += lines[$-1].height / 3; 
     224             
     225             
     226             
     227            // Render Image (7ms) 
    219228            int x, y; 
    220             result = new Image(4, width, min(totalHeight, height)); 
     229            result = new Image(4, width, min(totalHeight, height)); // 1ms by itself 
    221230            foreach (i, line; lines) 
    222231            { 
     
    258267                    break; 
    259268            } 
    260         } 
    261         lastRenderTime = a.tell(); 
    262          
     269            //Stdout(a.tell()*1000).newline; 
     270            lastRenderTime = a.tell(); 
     271        } 
     272         
     273        //lastRenderTime = a.tell(); 
    263274         
    264275        return result; 
    265276    } 
     277     
    266278     
    267279    /* 
     
    270282    private static HtmlNode[] getNodes(char[] htmlText, InlineStyle style) 
    271283    {    
    272         // Preprocessing 
    273         htmlText = Regex(`\s{2,}`).replaceAll(htmlText, " "); // remove excess whitespace 
     284        // Preprocessing (3ms !) 
     285        htmlText = rxSpaces.replaceAll(htmlText, " ");    // remove excess whitespace 
    274286        htmlText = substitute(htmlText, "<br/>", "\n");         // Add line returns 
    275287        htmlText = "<span>"~htmlText~"</span>"; 
    276288         
     289         
     290        // (3ms !) 
    277291        // This will be fixed in Tango 0.99.9: http://dsource.org/projects/tango/ticket/1619#comment:1 
    278292        int i=0; // [below] ensure every plain text child is wrapped in <span></span> to fix tango's xml parsing. 
    279         htmlText = Regex(`\>[^\<]+\<`).replaceAll(htmlText, (RegExpT!(char) input) { 
     293        htmlText = rxTags.replaceAll(htmlText, (RegExpT!(char) input) { 
    280294            return "><span"~input[i]~"/span><"; 
    281295            i++; 
    282296        }); 
    283297         
     298        // .05ms 
    284299        auto doc = new Document!(char); 
    285300        doc.parse(htmlText); 
    286         return getNodesHelper(doc.query.nodes[0], style); 
     301         
     302        Timer a = new Timer(); 
     303        auto result = getNodesHelper(doc.query.nodes[0], style); // 0.15 ms 
     304        return result; 
    287305    } 
    288306     
     
    291309     * T is always of type NodeImpl.  This only has to be templated because Tango's NodeImpl is private. */ 
    292310    private static HtmlNode[] getNodesHelper(T)(T input, InlineStyle parentStyle) 
    293     {   HtmlNode[] result; 
     311    {    
     312        HtmlNode[] result; 
    294313        char[] tagName = toLower(input.name); 
    295314         
     
    297316        HtmlNode node; // our own node class. 
    298317        if (input.query.attribute("style").count) 
    299         {   Style temp = parentStyle.toStyle();             
    300             temp.set(input.query.attribute("style").nodes[0].value); 
     318        {   Style temp = parentStyle.toStyle(); 
     319            temp.set(input.query.attribute("style").nodes[0].value); // 30ms for temp.set! 
    301320            node.style = InlineStyle(temp); 
    302321        } else 
     
    315334        // Get any text children from the node. 
    316335        if (input.value.length) 
    317         {   node.text = input.value.dup; 
     336        {   node.text = input.value.dup; // garbage! 
    318337            result ~= node; 
    319338        } else if (tagName=="br") 
     
    326345            for (auto child = input.query.child.nodes[0]; child; child=child.next()) 
    327346                result ~= getNodesHelper(child, node.style); 
     347         
    328348        return result; 
    329349    } 
  • trunk/src/yage/resource/lazyresource.d

    r126 r134  
    1414/** 
    1515 * This class manages a queue of operations that can only be performed in the rendering thread. 
    16  * This should eventually be replaced with a glContext that manages openGL state and can allow synchronized OpenGL calls from any thread.*/ 
     16 * This should eventually be replaced with a glContext that manages openGL state and can allow synchronized OpenGL calls from any thread. 
     17 * Alternatively, the renderer itself should be lazy and only load recources during rendering? */ 
    1718class LazyResourceManager 
    1819{