1 /* 2 * Copyright (C) 2012 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 #include "config.h" 31 32 #include "core/platform/graphics/filters/custom/CustomFilterProgramInfo.h" 33 34 #include "wtf/HashFunctions.h" 35 #include "wtf/text/StringHash.h" 36 37 namespace WebCore { 38 39 static unsigned hashPossiblyNullString(const String& string) 40 { 41 return string.isNull() ? 0 : DefaultHash<String>::Hash::hash(string); 42 } 43 44 CustomFilterProgramInfo::CustomFilterProgramInfo() 45 { 46 } 47 48 bool CustomFilterProgramInfo::isEmptyValue() const 49 { 50 return m_vertexShaderString.isNull() 51 && m_fragmentShaderString.isNull(); 52 } 53 54 CustomFilterProgramInfo::CustomFilterProgramInfo(WTF::HashTableDeletedValueType) 55 : m_vertexShaderString(WTF::HashTableDeletedValue) 56 , m_fragmentShaderString(WTF::HashTableDeletedValue) 57 { 58 } 59 60 bool CustomFilterProgramInfo::isHashTableDeletedValue() const 61 { 62 return m_vertexShaderString.isHashTableDeletedValue() 63 && m_fragmentShaderString.isHashTableDeletedValue(); 64 } 65 66 CustomFilterProgramInfo::CustomFilterProgramInfo(const String& vertexShader, const String& fragmentShader, CustomFilterProgramType programType, const CustomFilterProgramMixSettings& mixSettings, CustomFilterMeshType meshType) 67 : m_vertexShaderString(vertexShader) 68 , m_fragmentShaderString(fragmentShader) 69 , m_programType(programType) 70 , m_mixSettings(mixSettings) 71 , m_meshType(meshType) 72 { 73 // At least one of the shaders needs to be non-null. 74 ASSERT(!m_vertexShaderString.isNull() || !m_fragmentShaderString.isNull()); 75 } 76 77 unsigned CustomFilterProgramInfo::hash() const 78 { 79 // At least one of the shaders needs to be non-null. 80 ASSERT(!m_vertexShaderString.isNull() || !m_fragmentShaderString.isNull()); 81 82 bool blendsElementTexture = (m_programType == PROGRAM_TYPE_BLENDS_ELEMENT_TEXTURE); 83 uintptr_t hashCodes[6] = { 84 hashPossiblyNullString(m_vertexShaderString), 85 hashPossiblyNullString(m_fragmentShaderString), 86 blendsElementTexture, 87 static_cast<uintptr_t>(blendsElementTexture ? m_mixSettings.blendMode : 0), 88 static_cast<uintptr_t>(blendsElementTexture ? m_mixSettings.compositeOperator : 0), 89 m_meshType 90 }; 91 return StringHasher::hashMemory<sizeof(hashCodes)>(&hashCodes); 92 } 93 94 bool CustomFilterProgramInfo::operator==(const CustomFilterProgramInfo& o) const 95 { 96 ASSERT(!isHashTableDeletedValue()); 97 ASSERT(!o.isHashTableDeletedValue()); 98 99 return m_programType == o.m_programType 100 && (m_programType != PROGRAM_TYPE_BLENDS_ELEMENT_TEXTURE || m_mixSettings == o.m_mixSettings) 101 && m_meshType == o.m_meshType 102 && m_vertexShaderString == o.m_vertexShaderString 103 && m_fragmentShaderString == o.m_fragmentShaderString; 104 } 105 106 } // namespace WebCore 107 108