|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the LICENSE |
| 5 | + * file in the root directory of this source tree. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +package com.facebook.yoga; |
| 10 | + |
| 11 | +import com.facebook.proguard.annotations.DoNotStrip; |
| 12 | +import java.nio.ByteBuffer; |
| 13 | + |
| 14 | +@DoNotStrip |
| 15 | +/* package */ final class YogaNodeMemoryLayout { |
| 16 | + |
| 17 | + private static final int FLOAT_SIZE = 4; |
| 18 | + private static final int INT_SIZE = 4; |
| 19 | + private static final int VALUE_SIZE = FLOAT_SIZE + INT_SIZE; |
| 20 | + private static final byte FALSE = 0; |
| 21 | + private static final byte TRUE = 1; |
| 22 | + private static final int AUTO = YogaUnit.AUTO.intValue(); |
| 23 | + private static final int POINT = YogaUnit.POINT.intValue(); |
| 24 | + private static final int PERCENT = YogaUnit.PERCENT.intValue(); |
| 25 | + private static final int UNDEFINED = YogaUnit.UNDEFINED.intValue(); |
| 26 | + |
| 27 | + // TODO(davidaurelio) code-gen these values |
| 28 | + static final int styleDirection = 0; |
| 29 | + static final int styleFlexDirection = 4; |
| 30 | + static final int styleJustifyContent = 8; |
| 31 | + static final int styleAlignContent = 12; |
| 32 | + static final int styleAlignItems = 16; |
| 33 | + static final int styleAlignSelf = 20; |
| 34 | + static final int stylePositionType = 24; |
| 35 | + static final int styleFlexWrap = 28; |
| 36 | + static final int styleOverflow = 32; |
| 37 | + static final int styleDisplay = 36; |
| 38 | + static final int styleFlex = 40; |
| 39 | + static final int styleFlexGrow = 48; |
| 40 | + static final int styleFlexShrink = 56; |
| 41 | + static final int styleFlexBasis = 64; |
| 42 | + static final int styleMargin = 72; |
| 43 | + static final int stylePosition = 144; |
| 44 | + static final int stylePadding = 216; |
| 45 | + static final int styleBorder = 288; |
| 46 | + static final int styleDimensions = 360; |
| 47 | + static final int styleMinDimensions = 376; |
| 48 | + static final int styleMaxDimensions = 392; |
| 49 | + static final int styleAspectRatio = 408; |
| 50 | + |
| 51 | + static final int styleWidth = styleDimensions; |
| 52 | + static final int styleHeight = styleDimensions + VALUE_SIZE; |
| 53 | + static final int styleMinWidth = styleMinDimensions; |
| 54 | + static final int styleMinHeight = styleMinDimensions + VALUE_SIZE; |
| 55 | + static final int styleMaxWidth = styleMaxDimensions; |
| 56 | + static final int styleMaxHeight = styleMaxDimensions + VALUE_SIZE; |
| 57 | + |
| 58 | + static final int layoutPosition = 0; |
| 59 | + static final int layoutDimensions = 16; |
| 60 | + static final int layoutMargin = 24; |
| 61 | + static final int layoutBorder = 48; |
| 62 | + static final int layoutPadding = 72; |
| 63 | + static final int layoutDirection = 96; |
| 64 | + static final int layoutComputedFlexBasisGeneration = 100; |
| 65 | + static final int layoutComputedFlexBasis = 104; |
| 66 | + static final int layoutHadOverflow = 112; |
| 67 | + static final int layoutGenerationCount = 116; |
| 68 | + static final int layoutLastOwnerDirection = 120; |
| 69 | + static final int layoutNextCachedMeasurementsIndex = 124; |
| 70 | + static final int layoutCachedMeasurements = 128; |
| 71 | + static final int layoutMeasuredDimensions = 512; |
| 72 | + static final int layoutCachedLayout = 520; |
| 73 | + static final int layoutDidUseLegacyFlag = 544; |
| 74 | + static final int layoutDoesLegacyStretchFlagAffectsLayout = 545; |
| 75 | + |
| 76 | + static final int layoutX = layoutPosition; |
| 77 | + static final int layoutY = layoutPosition + FLOAT_SIZE; |
| 78 | + static final int layoutWidth = layoutDimensions; |
| 79 | + static final int layoutHeight = layoutDimensions + FLOAT_SIZE; |
| 80 | + |
| 81 | + static int stylePositionOffset(YogaEdge edge) { |
| 82 | + return stylePosition + edge.intValue() * VALUE_SIZE; |
| 83 | + } |
| 84 | + |
| 85 | + static int styleMarginOffset(YogaEdge edge) { |
| 86 | + return styleMargin + edge.intValue() * VALUE_SIZE; |
| 87 | + } |
| 88 | + |
| 89 | + static int layoutMarginOffset(YogaEdge edge) { |
| 90 | + return layoutMargin + edge.intValue() * FLOAT_SIZE; |
| 91 | + } |
| 92 | + |
| 93 | + static int stylePaddingOffset(YogaEdge edge) { |
| 94 | + return stylePadding + edge.intValue() * VALUE_SIZE; |
| 95 | + } |
| 96 | + |
| 97 | + static int layoutPaddingOffset(YogaEdge edge) { |
| 98 | + return layoutPadding + edge.intValue() * FLOAT_SIZE; |
| 99 | + } |
| 100 | + |
| 101 | + static int styleBorderOffset(YogaEdge edge) { |
| 102 | + return styleBorder + edge.intValue() * VALUE_SIZE; |
| 103 | + } |
| 104 | + |
| 105 | + static int layoutBorderOffset(YogaEdge edge) { |
| 106 | + return layoutBorder + edge.intValue() * FLOAT_SIZE; |
| 107 | + } |
| 108 | + |
| 109 | + static void putOptional(ByteBuffer buffer, int offset, float value) { |
| 110 | + buffer.putFloat(offset, value); |
| 111 | + buffer.put( |
| 112 | + offset + FLOAT_SIZE, YogaConstants.isUndefined(value) ? TRUE : FALSE); // bool isUndefined_ |
| 113 | + } |
| 114 | + |
| 115 | + static float getOptional(ByteBuffer buffer, int offset) { |
| 116 | + return getBoolean(buffer, offset + FLOAT_SIZE) |
| 117 | + ? YogaConstants.UNDEFINED |
| 118 | + : buffer.getFloat(offset); |
| 119 | + } |
| 120 | + |
| 121 | + private static void putValue(ByteBuffer buffer, int offset, float value, int unit) { |
| 122 | + if (YogaConstants.isUndefined(value)) { |
| 123 | + value = YogaConstants.UNDEFINED; |
| 124 | + unit = UNDEFINED; |
| 125 | + } |
| 126 | + buffer.putFloat(offset, value); |
| 127 | + buffer.putInt(offset + FLOAT_SIZE, unit); |
| 128 | + } |
| 129 | + |
| 130 | + static void putAutoValue(ByteBuffer buffer, int offset) { |
| 131 | + putValue(buffer, offset, 0, AUTO); |
| 132 | + } |
| 133 | + |
| 134 | + static void putPointValue(ByteBuffer buffer, int offset, float value) { |
| 135 | + putValue(buffer, offset, value, POINT); |
| 136 | + } |
| 137 | + |
| 138 | + static void putPercentValue(ByteBuffer buffer, int offset, float value) { |
| 139 | + putValue(buffer, offset, value, PERCENT); |
| 140 | + } |
| 141 | + |
| 142 | + static YogaValue getValue(ByteBuffer buffer, int offset) { |
| 143 | + float value = buffer.getFloat(offset); |
| 144 | + int unit = buffer.getInt(offset + FLOAT_SIZE); |
| 145 | + return new YogaValue(value, YogaUnit.fromInt(unit)); |
| 146 | + } |
| 147 | + |
| 148 | + static boolean getBoolean(ByteBuffer buffer, int offset) { |
| 149 | + return buffer.get(offset) != 0; |
| 150 | + } |
| 151 | +} |
0 commit comments