APZTestData.cpp (5342B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "APZTestData.h" 8 #include "mozilla/dom/APZTestDataBinding.h" 9 #include "mozilla/dom/ToJSValue.h" 10 #include "nsString.h" 11 12 namespace mozilla { 13 namespace layers { 14 15 struct APZTestDataToJSConverter { 16 template <typename Key, typename Value, typename KeyValuePair> 17 static void ConvertMap(const std::map<Key, Value>& aFrom, 18 dom::Sequence<KeyValuePair>& aOutTo, 19 void (*aElementConverter)(const Key&, const Value&, 20 KeyValuePair&)) { 21 if (!aOutTo.SetCapacity(aOutTo.Length() + aFrom.size(), fallible)) { 22 mozalloc_handle_oom(0); 23 } 24 for (auto it = aFrom.begin(); it != aFrom.end(); ++it) { 25 (void)aOutTo.AppendElement(fallible); // already checked OOM above 26 aElementConverter(it->first, it->second, aOutTo.LastElement()); 27 } 28 } 29 30 template <typename Src, typename Target> 31 static void ConvertList(const nsTArray<Src>& aFrom, 32 dom::Sequence<Target>& aOutTo, 33 void (*aElementConverter)(const Src&, Target&)) { 34 if (!aOutTo.SetCapacity(aOutTo.Length() + aFrom.Length(), fallible)) { 35 mozalloc_handle_oom(0); 36 } 37 for (auto it = aFrom.begin(); it != aFrom.end(); ++it) { 38 (void)aOutTo.AppendElement(fallible); // already checked OOM above 39 aElementConverter(*it, aOutTo.LastElement()); 40 } 41 } 42 43 static void ConvertAPZTestData(const APZTestData& aFrom, 44 dom::APZTestData& aOutTo) { 45 ConvertMap(aFrom.mPaints, aOutTo.mPaints.Construct(), ConvertBucket); 46 ConvertMap(aFrom.mRepaintRequests, aOutTo.mRepaintRequests.Construct(), 47 ConvertBucket); 48 ConvertList(aFrom.mHitResults, aOutTo.mHitResults.Construct(), 49 ConvertHitResult); 50 ConvertList(aFrom.mSampledResults, aOutTo.mSampledResults.Construct(), 51 ConvertSampledResult); 52 ConvertMap(aFrom.mAdditionalData, aOutTo.mAdditionalData.Construct(), 53 ConvertAdditionalDataEntry); 54 } 55 56 static void ConvertBucket(const SequenceNumber& aKey, 57 const APZTestData::Bucket& aValue, 58 dom::APZBucket& aOutKeyValuePair) { 59 aOutKeyValuePair.mSequenceNumber.Construct() = aKey; 60 ConvertMap(aValue, aOutKeyValuePair.mScrollFrames.Construct(), 61 ConvertScrollFrameData); 62 } 63 64 static void ConvertScrollFrameData(const APZTestData::ViewID& aKey, 65 const APZTestData::ScrollFrameData& aValue, 66 dom::ScrollFrameData& aOutKeyValuePair) { 67 aOutKeyValuePair.mScrollId.Construct() = aKey; 68 ConvertMap(aValue, aOutKeyValuePair.mEntries.Construct(), ConvertEntry); 69 } 70 71 static void ConvertEntry(const std::string& aKey, const std::string& aValue, 72 dom::ScrollFrameDataEntry& aOutKeyValuePair) { 73 ConvertString(aKey, aOutKeyValuePair.mKey.Construct()); 74 ConvertString(aValue, aOutKeyValuePair.mValue.Construct()); 75 } 76 77 static void ConvertAdditionalDataEntry( 78 const std::string& aKey, const std::string& aValue, 79 dom::AdditionalDataEntry& aOutKeyValuePair) { 80 ConvertString(aKey, aOutKeyValuePair.mKey.Construct()); 81 ConvertString(aValue, aOutKeyValuePair.mValue.Construct()); 82 } 83 84 static void ConvertString(const std::string& aFrom, nsString& aOutTo) { 85 CopyUTF8toUTF16(aFrom, aOutTo); 86 } 87 88 static void ConvertHitResult(const APZTestData::HitResult& aResult, 89 dom::APZHitResult& aOutHitResult) { 90 aOutHitResult.mScreenX.Construct() = aResult.point.x; 91 aOutHitResult.mScreenY.Construct() = aResult.point.y; 92 static_assert(MaxEnumValue<gfx::CompositorHitTestInfo::valueType>::value < 93 std::numeric_limits<uint16_t>::digits, 94 "CompositorHitTestFlags MAX value have to be less than " 95 "number of bits in uint16_t"); 96 aOutHitResult.mHitResult.Construct() = 97 static_cast<uint16_t>(aResult.result.serialize()); 98 aOutHitResult.mLayersId.Construct() = aResult.layersId.mId; 99 aOutHitResult.mScrollId.Construct() = aResult.scrollId; 100 } 101 102 static void ConvertSampledResult(const APZTestData::SampledResult& aResult, 103 dom::APZSampledResult& aOutSampledResult) { 104 aOutSampledResult.mScrollOffsetX.Construct() = aResult.scrollOffset.x; 105 aOutSampledResult.mScrollOffsetY.Construct() = aResult.scrollOffset.y; 106 aOutSampledResult.mLayersId.Construct() = aResult.layersId.mId; 107 aOutSampledResult.mScrollId.Construct() = aResult.scrollId; 108 aOutSampledResult.mSampledTimeStamp.Construct() = aResult.sampledTimeStamp; 109 } 110 }; 111 112 bool APZTestData::ToJS(JS::MutableHandle<JS::Value> aOutValue, 113 JSContext* aContext) const { 114 dom::APZTestData result; 115 APZTestDataToJSConverter::ConvertAPZTestData(*this, result); 116 return dom::ToJSValue(aContext, result, aOutValue); 117 } 118 119 } // namespace layers 120 } // namespace mozilla