1 /* 2 * Copyright (C) 2011 Google Inc. 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 copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef LoopBlinnPathCache_h 27 #define LoopBlinnPathCache_h 28 29 #include <wtf/Noncopyable.h> 30 #include <wtf/Vector.h> 31 32 namespace WebCore { 33 34 // A cache of the processed triangle mesh for a given path. Because these 35 // might be expensive to allocate (using malloc/free internally), it is 36 // recommended to try to reuse them when possible. 37 38 // Uncomment the following to obtain debugging information for the edges 39 // facing the interior region of the mesh. 40 // #define LOOP_BLINN_PATH_CACHE_DEBUG_INTERIOR_EDGES 41 42 class LoopBlinnPathCache { 43 WTF_MAKE_NONCOPYABLE(LoopBlinnPathCache); 44 public: 45 LoopBlinnPathCache(); 46 ~LoopBlinnPathCache(); 47 48 unsigned numberOfVertices() const { return m_vertices.size() / 2; } 49 50 // Get the base pointer to the vertex information. There are two 51 // coordinates per vertex. This pointer is valid until the cache is 52 // cleared or another vertex is added. Returns 0 if there are no 53 // vertices in the mesh. 54 const float* vertices() const 55 { 56 if (!numberOfVertices()) 57 return 0; 58 return m_vertices.data(); 59 } 60 61 // Get the base pointer to the texture coordinate information. There 62 // are three coordinates per vertex. This pointer is valid until the 63 // cache is cleared or another vertex is added. Returns 0 if 64 // there are no vertices in the mesh. 65 const float* texcoords() const 66 { 67 if (!numberOfVertices()) 68 return 0; 69 return m_texcoords.data(); 70 } 71 72 // Adds a vertex's information to the cache. The first two arguments 73 // are the x and y coordinates of the vertex on the plane; the last 74 // three arguments are the cubic texture coordinates associated with 75 // this vertex. 76 void addVertex(float x, float y, 77 float /*k*/, float /*l*/, float /*m*/); 78 79 unsigned numberOfInteriorVertices() const { return m_interiorVertices.size() / 2; } 80 81 // Base pointer to the interior vertices; two coordinates per 82 // vertex, which can be drawn as GL_TRIANGLES. Returns 0 if there 83 // are no interior vertices in the mesh. 84 const float* interiorVertices() const 85 { 86 if (!numberOfInteriorVertices()) 87 return 0; 88 return m_interiorVertices.data(); 89 } 90 91 void addInteriorVertex(float x, float y); 92 93 // Clears all of the stored vertex information in this cache. 94 void clear(); 95 96 #ifdef LOOP_BLINN_PATH_CACHE_DEBUG_INTERIOR_EDGES 97 // The number of interior edge vertices 98 unsigned numberOfInteriorEdgeVertices() const; 99 // Base pointer to the interior vertices; two coordinates per 100 // vertex, which can be drawn as GL_LINES. Returns 0 if there are 101 // no interior edge vertices in the mesh. 102 const float* interiorEdgeVertices() const; 103 void addInteriorEdgeVertex(float x, float y); 104 #endif // LOOP_BLINN_PATH_CACHE_DEBUG_INTERIOR_EDGES 105 106 private: 107 // The two-dimensional vertices of the triangle mesh. 108 Vector<float> m_vertices; 109 110 // The three-dimensional cubic texture coordinates. 111 Vector<float> m_texcoords; 112 113 Vector<float> m_interiorVertices; 114 115 #ifdef LOOP_BLINN_PATH_CACHE_DEBUG_INTERIOR_EDGES 116 // The following is only for debugging 117 Vector<float> m_interiorEdgeVertices; 118 #endif // LOOP_BLINN_PATH_CACHE_DEBUG_INTERIOR_EDGES 119 }; 120 121 } // namespace WebCore 122 123 #endif // LoopBlinnPathCache_h 124