Bug 1634272 - Don't use DocumentChannel for about:newtab r=mattwoodrow
[gecko.git] / gfx / 2d / FilterNodeCapture.cpp
blob07d644546b0b88f3c0fac00b5e0707bab1ec7477
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/. */
7 #include "FilterNodeCapture.h"
9 namespace mozilla {
10 namespace gfx {
12 struct Setter {
13 Setter(FilterNode* aNode, DrawTarget* aDT, bool aInputsChanged)
14 : mNode{aNode}, mIndex{0}, mDT{aDT}, mInputsChanged{aInputsChanged} {}
15 template <typename T>
16 void operator()(T& aValue) {
17 mNode->SetAttribute(mIndex, aValue);
20 FilterNode* mNode;
21 uint32_t mIndex;
22 DrawTarget* mDT;
23 bool mInputsChanged;
26 template <>
27 void Setter::operator()<std::vector<Float>>(std::vector<Float>& aValue) {
28 mNode->SetAttribute(mIndex, aValue.data(), aValue.size());
31 template <>
32 void Setter::operator()<RefPtr<SourceSurface>>(
33 RefPtr<SourceSurface>& aSurface) {
34 if (!mInputsChanged) {
35 return;
37 mNode->SetInput(mIndex, aSurface);
40 template <>
41 void Setter::operator()<RefPtr<FilterNode>>(RefPtr<FilterNode>& aNode) {
42 RefPtr<FilterNode> node = aNode;
43 if (node->GetBackendType() == FilterBackend::FILTER_BACKEND_CAPTURE) {
44 FilterNodeCapture* captureNode =
45 static_cast<FilterNodeCapture*>(node.get());
46 node = captureNode->Validate(mDT);
48 if (!mInputsChanged) {
49 return;
52 mNode->SetInput(mIndex, node);
55 RefPtr<FilterNode> FilterNodeCapture::Validate(DrawTarget* aDT) {
56 if (!mFilterNodeInternal) {
57 mFilterNodeInternal = aDT->CreateFilter(mType);
60 if (!mFilterNodeInternal) {
61 return nullptr;
64 Setter setter(mFilterNodeInternal, aDT, mInputsChanged);
66 for (auto attribute : mAttributes) {
67 setter.mIndex = attribute.first;
68 // Variant's matching doesn't seem to compile to terribly efficient code,
69 // this is probably fine since this happens on the paint thread, if ever
70 // needed it would be fairly simple to write something more optimized.
71 attribute.second.match(setter);
73 mAttributes.clear();
75 for (auto input : mInputs) {
76 setter.mIndex = input.first;
77 input.second.match(setter);
79 mInputsChanged = false;
81 return mFilterNodeInternal.get();
84 void FilterNodeCapture::SetAttribute(uint32_t aIndex, const Float* aValues,
85 uint32_t aSize) {
86 std::vector<Float> vec(aSize);
87 memcpy(vec.data(), aValues, sizeof(Float) * aSize);
88 AttributeValue att(std::move(vec));
89 auto result = mAttributes.insert({aIndex, att});
90 if (!result.second) {
91 result.first->second = att;
95 } // namespace gfx
96 } // namespace mozilla