Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef MINIKIN_LAYOUT_PIECES_H |
| 18 | #define MINIKIN_LAYOUT_PIECES_H |
| 19 | |
| 20 | #include <unordered_map> |
| 21 | |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 22 | #include "minikin/LayoutCache.h" |
Seigo Nonaka | 3e331a4 | 2018-04-12 15:08:02 -0700 | [diff] [blame] | 23 | #include "minikin/LayoutCore.h" |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 24 | #include "minikin/MinikinPaint.h" |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 25 | |
| 26 | namespace minikin { |
| 27 | |
| 28 | struct LayoutPieces { |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 29 | const static uint32_t kNoPaintId = static_cast<uint32_t>(-1); |
| 30 | |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 31 | struct Key { |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 32 | Key(const Range& range, HyphenEdit hyphenEdit, bool dir, uint32_t paintId) |
| 33 | : range(range), hyphenEdit(hyphenEdit), dir(dir), paintId(paintId) {} |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 34 | |
| 35 | Range range; |
| 36 | HyphenEdit hyphenEdit; |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 37 | bool dir; |
| 38 | uint32_t paintId; |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 39 | |
| 40 | uint32_t hash() const { |
Seigo Nonaka | 3904850 | 2018-08-22 16:13:59 -0700 | [diff] [blame] | 41 | return Hasher() |
| 42 | .update(range.getStart()) |
| 43 | .update(range.getEnd()) |
| 44 | .update(hyphenEdit) |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 45 | .update(dir) |
| 46 | .update(paintId) |
Seigo Nonaka | 3904850 | 2018-08-22 16:13:59 -0700 | [diff] [blame] | 47 | .hash(); |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool operator==(const Key& o) const { |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 51 | return range == o.range && hyphenEdit == o.hyphenEdit && dir == o.dir && |
| 52 | paintId == o.paintId; |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 55 | uint32_t getMemoryUsage() const { |
| 56 | return sizeof(Range) + sizeof(HyphenEdit) + sizeof(bool) + sizeof(uint32_t); |
| 57 | } |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 60 | struct KeyHasher { |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 61 | std::size_t operator()(const Key& key) const { return key.hash(); } |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 64 | struct PaintHasher { |
| 65 | std::size_t operator()(const MinikinPaint& paint) const { return paint.hash(); } |
| 66 | }; |
| 67 | |
Seigo Nonaka | 71c3966 | 2019-01-29 00:35:16 -0800 | [diff] [blame] | 68 | LayoutPieces() : nextPaintId(0) {} |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 69 | ~LayoutPieces() {} |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 70 | |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 71 | uint32_t nextPaintId; |
| 72 | std::unordered_map<MinikinPaint, uint32_t, PaintHasher> paintMap; |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 73 | std::unordered_map<Key, LayoutPiece, KeyHasher> offsetMap; |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 74 | |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 75 | void insert(const Range& range, HyphenEdit edit, const LayoutPiece& layout, bool dir, |
| 76 | const MinikinPaint& paint) { |
| 77 | uint32_t paintId = findPaintId(paint); |
| 78 | if (paintId == kNoPaintId) { |
| 79 | paintId = nextPaintId++; |
| 80 | paintMap.insert(std::make_pair(paint, paintId)); |
| 81 | } |
| 82 | offsetMap.emplace(std::piecewise_construct, |
| 83 | std::forward_as_tuple(range, edit, dir, paintId), |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 84 | std::forward_as_tuple(layout)); |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | template <typename F> |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 88 | void getOrCreate(const U16StringPiece& textBuf, const Range& range, const Range& context, |
| 89 | const MinikinPaint& paint, bool dir, StartHyphenEdit startEdit, |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 90 | EndHyphenEdit endEdit, uint32_t paintId, F& f) const { |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 91 | const HyphenEdit edit = packHyphenEdit(startEdit, endEdit); |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 92 | auto it = offsetMap.find(Key(range, edit, dir, paintId)); |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 93 | if (it == offsetMap.end()) { |
Seigo Nonaka | 749722f | 2018-04-27 12:24:28 -0700 | [diff] [blame] | 94 | LayoutCache::getInstance().getOrCreate(textBuf.substr(context), |
| 95 | range - context.getStart(), paint, dir, |
| 96 | startEdit, endEdit, f); |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 97 | } else { |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 98 | f(it->second, paint); |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 102 | uint32_t findPaintId(const MinikinPaint& paint) const { |
| 103 | auto paintIt = paintMap.find(paint); |
| 104 | return paintIt == paintMap.end() ? kNoPaintId : paintIt->second; |
| 105 | } |
| 106 | |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 107 | uint32_t getMemoryUsage() const { |
| 108 | uint32_t result = 0; |
| 109 | for (const auto& i : offsetMap) { |
| 110 | result += i.first.getMemoryUsage() + i.second.getMemoryUsage(); |
| 111 | } |
Seigo Nonaka | 3ca4451 | 2018-11-15 10:47:12 -0800 | [diff] [blame] | 112 | result += (sizeof(MinikinPaint) + sizeof(uint32_t)) * paintMap.size(); |
Seigo Nonaka | 3da4269 | 2018-04-02 21:39:34 -0700 | [diff] [blame] | 113 | return result; |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | } // namespace minikin |
| 118 | |
| 119 | #endif // MINIKIN_LAYOUT_PIECES_H |