1 /* 2 * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following 13 * disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AS IS AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef CustomFilterMeshGenerator_h 31 #define CustomFilterMeshGenerator_h 32 33 #include "core/platform/graphics/FloatRect.h" 34 #include "core/platform/graphics/filters/custom/CustomFilterConstants.h" 35 #include "core/platform/graphics/filters/custom/CustomFilterOperation.h" 36 37 namespace WebCore { 38 39 class CustomFilterMeshGenerator { 40 public: 41 // Lines and columns are the values passed in CSS. The result is vertex mesh that has 'rows' numbers of rows 42 // and 'columns' number of columns with a total of 'rows + 1' * 'columns + 1' vertices. 43 // MeshBox is the filtered area calculated defined using the border-box, padding-box, content-box or filter-box 44 // attributes. A value of (0, 0, 1, 1) will cover the entire output surface. 45 CustomFilterMeshGenerator(unsigned columns, unsigned rows, const FloatRect& meshBox, CustomFilterMeshType); 46 47 const Vector<float>& vertices() const { return m_vertices; } 48 const Vector<uint16_t>& indices() const { return m_indices; } 49 50 const IntSize& points() const { return m_points; } 51 unsigned pointsCount() const { return m_points.width() * m_points.height(); } 52 53 const IntSize& tiles() const { return m_tiles; } 54 unsigned tilesCount() const { return m_tiles.width() * m_tiles.height(); } 55 56 unsigned indicesCount() const 57 { 58 const unsigned trianglesPerTile = 2; 59 const unsigned indicesPerTriangle = 3; 60 return tilesCount() * trianglesPerTile * indicesPerTriangle; 61 } 62 63 unsigned floatsPerVertex() const 64 { 65 static const unsigned AttachedMeshVertexSize = PositionAttribSize + TexAttribSize + MeshAttribSize; 66 static const unsigned DetachedMeshVertexSize = AttachedMeshVertexSize + TriangleAttribSize; 67 return m_meshType == MeshTypeAttached ? AttachedMeshVertexSize : DetachedMeshVertexSize; 68 } 69 70 unsigned verticesCount() const 71 { 72 return m_meshType == MeshTypeAttached ? pointsCount() : indicesCount(); 73 } 74 75 private: 76 typedef void (CustomFilterMeshGenerator::*AddTriangleVertexFunction)(int quadX, int quadY, int triangleX, int triangleY, int triangle); 77 78 template <AddTriangleVertexFunction addTriangleVertex> 79 void addTile(int quadX, int quadY) 80 { 81 ((*this).*(addTriangleVertex))(quadX, quadY, 0, 0, 1); 82 ((*this).*(addTriangleVertex))(quadX, quadY, 1, 0, 2); 83 ((*this).*(addTriangleVertex))(quadX, quadY, 1, 1, 3); 84 ((*this).*(addTriangleVertex))(quadX, quadY, 0, 0, 4); 85 ((*this).*(addTriangleVertex))(quadX, quadY, 1, 1, 5); 86 ((*this).*(addTriangleVertex))(quadX, quadY, 0, 1, 6); 87 } 88 89 void addAttachedMeshIndex(int quadX, int quadY, int triangleX, int triangleY, int triangle); 90 91 void generateAttachedMesh(); 92 93 void addDetachedMeshVertexAndIndex(int quadX, int quadY, int triangleX, int triangleY, int triangle); 94 95 void generateDetachedMesh(); 96 void addPositionAttribute(int quadX, int quadY); 97 void addTexCoordAttribute(int quadX, int quadY); 98 void addMeshCoordAttribute(int quadX, int quadY); 99 void addTriangleCoordAttribute(int quadX, int quadY, int triangle); 100 void addAttachedMeshVertexAttributes(int quadX, int quadY); 101 void addDetachedMeshVertexAttributes(int quadX, int quadY, int triangleX, int triangleY, int triangle); 102 103 #ifndef NDEBUG 104 void dumpBuffers() const; 105 #endif 106 107 private: 108 Vector<float> m_vertices; 109 Vector<uint16_t> m_indices; 110 111 CustomFilterMeshType m_meshType; 112 IntSize m_points; 113 IntSize m_tiles; 114 FloatSize m_tileSizeInPixels; 115 FloatSize m_tileSizeInDeviceSpace; 116 FloatRect m_meshBox; 117 }; 118 119 } // namespace WebCore 120 121 #endif // CustomFilterMeshGenerator_h 122