slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
StyleValuePool.h
浏览该文件的文档.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
10#include <cassert>
11#include <cstdint>
12
18
19namespace facebook::yoga {
20
29 public:
30 void store(StyleValueHandle& handle, StyleLength length) {
31 if (length.isUndefined()) {
33 } else if (length.isAuto()) {
35 } else {
36 auto type = length.isPoints() ? StyleValueHandle::Type::Point
38 storeValue(handle, length.value().unwrap(), type);
39 }
40 }
41
42 void store(StyleValueHandle& handle, StyleSizeLength sizeValue) {
43 if (sizeValue.isUndefined()) {
45 } else if (sizeValue.isAuto()) {
47 } else if (sizeValue.isMaxContent()) {
49 } else if (sizeValue.isStretch()) {
51 } else if (sizeValue.isFitContent()) {
53 } else {
54 auto type = sizeValue.isPoints() ? StyleValueHandle::Type::Point
56 storeValue(handle, sizeValue.value().unwrap(), type);
57 }
58 }
59
60 void store(StyleValueHandle& handle, FloatOptional number) {
61 if (number.isUndefined()) {
63 } else {
65 }
66 }
67
69 if (handle.isUndefined()) {
71 } else if (handle.isAuto()) {
72 return StyleLength::ofAuto();
73 } else {
74 assert(
77 float value = (handle.isValueIndexed())
78 ? std::bit_cast<float>(buffer_.get32(handle.value()))
79 : unpackInlineInteger(handle.value());
80
81 return handle.type() == StyleValueHandle::Type::Point
82 ? StyleLength::points(value)
83 : StyleLength::percent(value);
84 }
85 }
86
88 if (handle.isUndefined()) {
90 } else if (handle.isAuto()) {
98 } else {
99 assert(
102 float value = (handle.isValueIndexed())
103 ? std::bit_cast<float>(buffer_.get32(handle.value()))
104 : unpackInlineInteger(handle.value());
105
106 return handle.type() == StyleValueHandle::Type::Point
109 }
110 }
111
113 if (handle.isUndefined()) {
114 return FloatOptional{};
115 } else {
116 assert(handle.type() == StyleValueHandle::Type::Number);
117 float value = (handle.isValueIndexed())
118 ? std::bit_cast<float>(buffer_.get32(handle.value()))
119 : unpackInlineInteger(handle.value());
120 return FloatOptional{value};
121 }
122 }
123
124 float getStoredValue(StyleValueHandle handle) const {
125 assert(
129 return handle.isValueIndexed()
130 ? std::bit_cast<float>(buffer_.get32(handle.value()))
131 : unpackInlineInteger(handle.value());
132 }
133
134 private:
136 StyleValueHandle& handle,
137 float value,
139 handle.setType(type);
140
141 if (handle.isValueIndexed()) {
142 auto newIndex =
143 buffer_.replace(handle.value(), std::bit_cast<uint32_t>(value));
144 handle.setValue(newIndex);
145 } else if (isIntegerPackable(value)) {
146 handle.setValue(packInlineInteger(value));
147 } else {
148 auto newIndex = buffer_.push(std::bit_cast<uint32_t>(value));
149 handle.setValue(newIndex);
150 handle.setValueIsIndexed();
151 }
152 }
153
155 StyleValueHandle& handle,
158
159 if (handle.isValueIndexed()) {
160 auto newIndex =
161 buffer_.replace(handle.value(), static_cast<uint32_t>(keyword));
162 handle.setValue(newIndex);
163 } else {
164 handle.setValue(static_cast<uint16_t>(keyword));
165 }
166 }
167
168 static constexpr bool isIntegerPackable(float f) {
169 constexpr uint16_t kMaxInlineAbsValue = (1 << 11) - 1;
170
171 auto i = static_cast<int32_t>(f);
172 return static_cast<float>(i) == f && i >= -kMaxInlineAbsValue &&
173 i <= +kMaxInlineAbsValue;
174 }
175
176 static constexpr uint16_t packInlineInteger(float value) {
177 uint16_t isNegative = value < 0 ? 1 : 0;
178 return static_cast<uint16_t>(
179 (isNegative << 11) |
180 (static_cast<int32_t>(value) * (isNegative != 0u ? -1 : 1)));
181 }
182
183 static constexpr float unpackInlineInteger(uint16_t value) {
184 constexpr uint16_t kValueSignMask = 0b0000'1000'0000'0000;
185 constexpr uint16_t kValueMagnitudeMask = 0b0000'0111'1111'1111;
186 const bool isNegative = (value & kValueSignMask) != 0;
187 return static_cast<float>(
188 (value & kValueMagnitudeMask) * (isNegative ? -1 : 1));
189 }
190
192};
193
194} // namespace facebook::yoga
Definition SmallValueBuffer.h:23
uint16_t push(uint32_t value)
Definition SmallValueBuffer.h:32
uint16_t replace(uint16_t index, uint32_t value)
Definition SmallValueBuffer.h:68
uint32_t get32(uint16_t index) const
Definition SmallValueBuffer.h:96
Definition StyleLength.h:28
static constexpr StyleLength undefined()
Definition StyleLength.h:48
constexpr bool isUndefined() const
Definition StyleLength.h:56
constexpr FloatOptional value() const
Definition StyleLength.h:72
static constexpr StyleLength ofAuto()
Definition StyleLength.h:44
static constexpr StyleLength percent(float value)
Definition StyleLength.h:38
constexpr bool isPoints() const
Definition StyleLength.h:60
static constexpr StyleLength points(float value)
Definition StyleLength.h:32
constexpr bool isAuto() const
Definition StyleLength.h:52
Definition StyleSizeLength.h:29
constexpr bool isPoints() const
Definition StyleSizeLength.h:95
static constexpr StyleSizeLength ofMaxContent()
Definition StyleSizeLength.h:55
constexpr bool isStretch() const
Definition StyleSizeLength.h:83
static constexpr StyleSizeLength undefined()
Definition StyleSizeLength.h:67
static constexpr StyleSizeLength ofFitContent()
Definition StyleSizeLength.h:59
constexpr bool isFitContent() const
Definition StyleSizeLength.h:79
constexpr bool isMaxContent() const
Definition StyleSizeLength.h:75
static constexpr StyleSizeLength points(float value)
Definition StyleSizeLength.h:33
constexpr FloatOptional value() const
Definition StyleSizeLength.h:103
static constexpr StyleSizeLength percent(float value)
Definition StyleSizeLength.h:39
constexpr bool isAuto() const
Definition StyleSizeLength.h:71
constexpr bool isUndefined() const
Definition StyleSizeLength.h:87
static constexpr StyleSizeLength ofStretch()
Definition StyleSizeLength.h:63
static constexpr StyleSizeLength ofAuto()
Definition StyleSizeLength.h:51
Definition StyleValueHandle.h:32
constexpr bool isKeyword(Keyword keyword) const
Definition StyleValueHandle.h:79
constexpr bool isAuto() const
Definition StyleValueHandle.h:48
constexpr bool isUndefined() const
Definition StyleValueHandle.h:40
constexpr void setType(Type handleType)
Definition StyleValueHandle.h:87
constexpr void setValueIsIndexed()
Definition StyleValueHandle.h:105
Type
Definition StyleValueHandle.h:67
constexpr Type type() const
Definition StyleValueHandle.h:83
constexpr bool isValueIndexed() const
Definition StyleValueHandle.h:101
Keyword
Definition StyleValueHandle.h:77
constexpr uint16_t value() const
Definition StyleValueHandle.h:92
constexpr void setValue(uint16_t value)
Definition StyleValueHandle.h:96
Definition StyleValuePool.h:28
void store(StyleValueHandle &handle, StyleLength length)
Definition StyleValuePool.h:30
StyleSizeLength getSize(StyleValueHandle handle) const
Definition StyleValuePool.h:87
static constexpr float unpackInlineInteger(uint16_t value)
Definition StyleValuePool.h:183
StyleLength getLength(StyleValueHandle handle) const
Definition StyleValuePool.h:68
void storeValue(StyleValueHandle &handle, float value, StyleValueHandle::Type type)
Definition StyleValuePool.h:135
void store(StyleValueHandle &handle, StyleSizeLength sizeValue)
Definition StyleValuePool.h:42
float getStoredValue(StyleValueHandle handle) const
Definition StyleValuePool.h:124
void storeKeyword(StyleValueHandle &handle, StyleValueHandle::Keyword keyword)
Definition StyleValuePool.h:154
static constexpr bool isIntegerPackable(float f)
Definition StyleValuePool.h:168
FloatOptional getNumber(StyleValueHandle handle) const
Definition StyleValuePool.h:112
static constexpr uint16_t packInlineInteger(float value)
Definition StyleValuePool.h:176
void store(StyleValueHandle &handle, FloatOptional number)
Definition StyleValuePool.h:60
SmallValueBuffer< 4 > buffer_
Definition StyleValuePool.h:191
Definition Benchmark.cpp:19
Definition FloatOptional.h:15
constexpr bool isUndefined() const
Definition FloatOptional.h:32
constexpr float unwrap() const
Definition FloatOptional.h:24