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 #ifndef MOZILLA_PAINTING_MATRIXSTACK_H 8 #define MOZILLA_PAINTING_MATRIXSTACK_H 9 10 #include "nsTArray.h" 11 #include "mozilla/gfx/MatrixFwd.h" 12 13 namespace mozilla { 14 15 /** 16 * MatrixStack stores a stack of matrices and keeps track of the accumulated 17 * transform matrix. 18 */ 19 template <typename T> 20 class MatrixStack { 21 public: 22 MatrixStack() = default; 23 ~MatrixStack()24 ~MatrixStack() { MOZ_ASSERT(mMatrices.IsEmpty()); } 25 26 /** 27 * Returns the current accumulated transform matrix. 28 */ CurrentMatrix()29 const T& CurrentMatrix() const { return mCurrentMatrix; } 30 31 /** 32 * Returns true if any matrices are currently pushed to the stack. 33 */ HasTransform()34 bool HasTransform() const { return mMatrices.Length() > 0; } 35 36 /** 37 * Pushes the given |aMatrix| to the stack. 38 */ Push(const T & aMatrix)39 void Push(const T& aMatrix) { 40 mMatrices.AppendElement(mCurrentMatrix); 41 mCurrentMatrix = aMatrix * mCurrentMatrix; 42 } 43 44 /** 45 * Pops the most recently added matrix from the stack. 46 */ Pop()47 void Pop() { 48 MOZ_ASSERT(mMatrices.Length() > 0); 49 mCurrentMatrix = mMatrices.PopLastElement(); 50 } 51 52 private: 53 T mCurrentMatrix; 54 AutoTArray<T, 2> mMatrices; 55 }; 56 57 typedef MatrixStack<gfx::Matrix4x4Flagged> MatrixStack4x4; 58 59 } // namespace mozilla 60 61 #endif /* MOZILLA_PAINTING_MATRIXSTACK_H */ 62