Exemplo n.º 1
0
 static boolean needsRelayout(CSSNode node, float parentMaxWidth, float parentMaxHeight)
 {
     return node.isDirty() ||
         !FloatUtil.floatsEqual(
             node.lastLayout.requestedHeight,
             node.layout.dimensions[DIMENSION_HEIGHT]) ||
         !FloatUtil.floatsEqual(
             node.lastLayout.requestedWidth,
             node.layout.dimensions[DIMENSION_WIDTH]) ||
         !FloatUtil.floatsEqual(node.lastLayout.parentMaxWidth, parentMaxWidth) ||
         !FloatUtil.floatsEqual(node.lastLayout.parentMaxHeight, parentMaxHeight);
 }
Exemplo n.º 2
0
        //
        // This is a wrapper around the layoutNodeImpl function. It determines
        // whether the layout request is redundant and can be skipped.
        //
        // Parameters:
        //  Input parameters are the same as layoutNodeImpl (see below)
        //  Return parameter is true if layout was performed, false if skipped
        //
        internal static boolean layoutNodeInternal(CSSLayoutContext layoutContext, CSSNode node, float availableWidth, float availableHeight, CSSDirection? parentDirection, CSSMeasureMode widthMeasureMode, CSSMeasureMode heightMeasureMode, boolean performLayout, string reason)
        {
            CSSLayout layout = node.layout;

            boolean needToVisitNode = (node.isDirty() && layout.generationCount != layoutContext.currentGenerationCount) ||
              layout.lastParentDirection != parentDirection;

            if (needToVisitNode)
            {
                // Invalidate the cached results.
                layout.nextCachedMeasurementsIndex = 0;
                layout.cachedLayout.widthMeasureMode = null;
                layout.cachedLayout.heightMeasureMode = null;
            }

            CSSCachedMeasurement cachedResults = null;

            // Determine whether the results are already cached. We maintain a separate
            // cache for layouts and measurements. A layout operation modifies the positions
            // and dimensions for nodes in the subtree. The algorithm assumes that each node
            // gets layed out a maximum of one time per tree layout, but multiple measurements
            // may be required to resolve all of the flex dimensions.
            // We handle nodes with measure functions specially here because they are the most
            // expensive to measure, so it's worth avoiding redundant measurements if at all possible.
            if (isMeasureDefined(node))
            {
                float marginAxisRow =
                    node.style.margin.getWithFallback(leadingSpacing[CSS_FLEX_DIRECTION_ROW], leading[CSS_FLEX_DIRECTION_ROW]) +
                    node.style.margin.getWithFallback(trailingSpacing[CSS_FLEX_DIRECTION_ROW], trailing[CSS_FLEX_DIRECTION_ROW]);
                float marginAxisColumn =
                    node.style.margin.getWithFallback(leadingSpacing[CSS_FLEX_DIRECTION_COLUMN], leading[CSS_FLEX_DIRECTION_COLUMN]) +
                    node.style.margin.getWithFallback(trailingSpacing[CSS_FLEX_DIRECTION_COLUMN], trailing[CSS_FLEX_DIRECTION_COLUMN]);

                // First, try to use the layout cache.
                if (canUseCachedMeasurement(node.IsTextNode, availableWidth, availableHeight, marginAxisRow, marginAxisColumn,
                    widthMeasureMode, heightMeasureMode, layout.cachedLayout))
                {
                    cachedResults = layout.cachedLayout;
                }
                else
                {
                    // Try to use the measurement cache.
                    for (int i = 0; i < layout.nextCachedMeasurementsIndex; i++)
                    {
                        if (canUseCachedMeasurement(node.IsTextNode, availableWidth, availableHeight, marginAxisRow, marginAxisColumn,
                            widthMeasureMode, heightMeasureMode, layout.cachedMeasurements[i]))
                        {
                            cachedResults = layout.cachedMeasurements[i];
                            break;
                        }
                    }
                }
            }
            else if (performLayout)
            {
                if (FloatUtil.floatsEqual(layout.cachedLayout.availableWidth, availableWidth) &&
                    FloatUtil.floatsEqual(layout.cachedLayout.availableHeight, availableHeight) &&
                    layout.cachedLayout.widthMeasureMode == widthMeasureMode &&
                    layout.cachedLayout.heightMeasureMode == heightMeasureMode)
                {

                    cachedResults = layout.cachedLayout;
                }
            }
            else
            {
                for (int i = 0; i < layout.nextCachedMeasurementsIndex; i++)
                {
                    if (FloatUtil.floatsEqual(layout.cachedMeasurements[i].availableWidth, availableWidth) &&
                        FloatUtil.floatsEqual(layout.cachedMeasurements[i].availableHeight, availableHeight) &&
                        layout.cachedMeasurements[i].widthMeasureMode == widthMeasureMode &&
                        layout.cachedMeasurements[i].heightMeasureMode == heightMeasureMode)
                    {

                        cachedResults = layout.cachedMeasurements[i];
                        break;
                    }
                }
            }

            if (!needToVisitNode && cachedResults != null)
            {
                layout.measuredDimensions[DIMENSION_WIDTH] = cachedResults.computedWidth;
                layout.measuredDimensions[DIMENSION_HEIGHT] = cachedResults.computedHeight;
            }
            else
            {
                layoutNodeImpl(layoutContext, node, availableWidth, availableHeight, parentDirection, widthMeasureMode, heightMeasureMode, performLayout);

                layout.lastParentDirection = parentDirection;

                if (cachedResults == null)
                {
                    if (layout.nextCachedMeasurementsIndex == CSSLayout.MAX_CACHED_RESULT_COUNT)
                    {
                        layout.nextCachedMeasurementsIndex = 0;
                    }

                    CSSCachedMeasurement newCacheEntry = null;
                    if (performLayout)
                    {
                        // Use the single layout cache entry.
                        newCacheEntry = layout.cachedLayout;
                    }
                    else
                    {
                        // Allocate a new measurement cache entry.
                        newCacheEntry = layout.cachedMeasurements[layout.nextCachedMeasurementsIndex];
                        if (newCacheEntry == null)
                        {
                            newCacheEntry = new CSSCachedMeasurement();
                            layout.cachedMeasurements[layout.nextCachedMeasurementsIndex] = newCacheEntry;
                        }
                        layout.nextCachedMeasurementsIndex++;
                    }

                    newCacheEntry.availableWidth = availableWidth;
                    newCacheEntry.availableHeight = availableHeight;
                    newCacheEntry.widthMeasureMode = widthMeasureMode;
                    newCacheEntry.heightMeasureMode = heightMeasureMode;
                    newCacheEntry.computedWidth = layout.measuredDimensions[DIMENSION_WIDTH];
                    newCacheEntry.computedHeight = layout.measuredDimensions[DIMENSION_HEIGHT];
                }
            }

            if (performLayout)
            {
                node.layout.dimensions[DIMENSION_WIDTH] = node.layout.measuredDimensions[DIMENSION_WIDTH];
                node.layout.dimensions[DIMENSION_HEIGHT] = node.layout.measuredDimensions[DIMENSION_HEIGHT];
                node.markHasNewLayout();
            }

            layout.generationCount = layoutContext.currentGenerationCount;
            return (needToVisitNode || cachedResults == null);
        }