diff options
author | Marco Cecchetti <marco.cecchetti@collabora.com> | 2015-08-26 13:50:57 +0200 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2015-09-01 16:28:45 +0100 |
commit | ebb0dc14547e698d7b53005178063da72d48f075 (patch) | |
tree | 8cf95d717f32672dc8e6ecf9f6b743227ada889f /vcl/opengl | |
parent | 7cc4cdc5ef6dff279e072af725c2d7fc1e5da0e8 (diff) |
Added support for computing 64-bit checksum of bitmap in OpenGL
Added a C++ and a GLSL implementation of a 64-bit CRC algorithm.
Changed hardcoded checksum value in ooxmlimport unit test (testN777345).
Change-Id: I16bb985a14866775efda49e21fe033ff64645896
Diffstat (limited to 'vcl/opengl')
-rw-r--r-- | vcl/opengl/areaHashCRC64TFragmentShader.glsl | 87 | ||||
-rw-r--r-- | vcl/opengl/program.cxx | 2 | ||||
-rw-r--r-- | vcl/opengl/salbmp.cxx | 90 |
3 files changed, 178 insertions, 1 deletions
diff --git a/vcl/opengl/areaHashCRC64TFragmentShader.glsl b/vcl/opengl/areaHashCRC64TFragmentShader.glsl new file mode 100644 index 000000000000..901b481d8081 --- /dev/null +++ b/vcl/opengl/areaHashCRC64TFragmentShader.glsl @@ -0,0 +1,87 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +/* TODO Use textureOffset for newest version of GLSL */ + + +#version 130 + +uniform sampler2D crc_table; +uniform sampler2D sampler; +uniform float xstep; +uniform float ystep; + +varying vec2 tex_coord; + +const int scale = 4; +const float ratio = 16.0; + + +ivec2 crc64( ivec2 hval, int color ) +{ + int dx = 2 * ((hval[0] ^ color) & 0xff); + float s = dx / 255.0; + vec4 table_value_lo = round(texture2D( crc_table, vec2( s, 0.0 ) ) * 255.0); + s = (dx+1) / 255.0; + vec4 table_value_hi = round(texture2D( crc_table, vec2( s, 0.0 ) ) * 255.0); + + int tvalue_lo = int(table_value_lo[0]) | (int(table_value_lo[1]) << 8) | (int(table_value_lo[2]) << 16) | (int(table_value_lo[3]) << 24); + int tvalue_hi = int(table_value_hi[0]) | (int(table_value_hi[1]) << 8) | (int(table_value_hi[2]) << 16) | (int(table_value_hi[3]) << 24); + + hval[1] = tvalue_hi ^ (hval[1] >> 8); + hval[0] = tvalue_lo ^ ( (hval[1] << 24) | (hval[0] >> 8) ); + + return hval; +} + + +void main(void) +{ + ivec2 Crc = ivec2( 0xffffffff, 0xffffffff ); + vec2 offset = vec2( 0.0, 0.0 ); + vec2 next_coord = tex_coord.st; + for( int y = 0; y < scale && next_coord.y <= 1.0; ++y ) + { + for( int x = 0; x < scale && next_coord.x <= 1.0; ++x ) + { + vec4 pixel = round(texture2D( sampler, next_coord ) * 255.0); + + int r = int(pixel.r); // 0..255 + int g = int(pixel.g); // 0..255 + int b = int(pixel.b); // 0..255 + int a = int(pixel.a); // 0..255 + + Crc = crc64( Crc, r ); + Crc = crc64( Crc, g ); + Crc = crc64( Crc, b ); + Crc = crc64( Crc, a ); + + offset.x += xstep; + next_coord = tex_coord.st + offset; + } + offset.y += ystep; + offset.x = 0.0; + next_coord = tex_coord.st + offset; + } + + Crc[0] = ~Crc[0]; + Crc[1] = ~Crc[1]; + + int Hash = Crc[0] ^ Crc[1]; + + float fr = ( Hash & 0xff) / 255.0; + float fg = ((Hash >> 8) & 0xff) / 255.0; + float fb = ((Hash >> 16) & 0xff) / 255.0; + float fa = ((Hash >> 24) & 0xff) / 255.0; + + + gl_FragColor = vec4(fr, fg, fb, fa); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/opengl/program.cxx b/vcl/opengl/program.cxx index b1d3cfe8291d..eec4e92bad98 100644 --- a/vcl/opengl/program.cxx +++ b/vcl/opengl/program.cxx @@ -276,7 +276,7 @@ void OpenGLProgram::SetBlendMode( GLenum nSFactor, GLenum nDFactor ) mbBlending = true; } -bool OpenGLProgram::DrawTexture( OpenGLTexture& rTexture ) +bool OpenGLProgram::DrawTexture( const OpenGLTexture& rTexture ) { if (!rTexture) return false; diff --git a/vcl/opengl/salbmp.cxx b/vcl/opengl/salbmp.cxx index d6e51d9bfd72..8ab721e25f6c 100644 --- a/vcl/opengl/salbmp.cxx +++ b/vcl/opengl/salbmp.cxx @@ -31,6 +31,7 @@ #include "opengl/program.hxx" #include "opengl/salbmp.hxx" +#include "../inc/checksum.hxx" #include "opengl/FixedTextureAtlas.hxx" namespace @@ -508,6 +509,94 @@ sal_uInt16 OpenGLSalBitmap::GetBitCount() const return mnBits; } +bool OpenGLSalBitmap::calcChecksumGL(OpenGLTexture& rInputTexture, ChecksumType& rChecksum) const +{ + OUString FragShader("areaHashCRC64TFragmentShader"); + + static const OpenGLTexture aCRCTableTexture(512, 1, GL_RGBA, GL_UNSIGNED_BYTE, + (sal_uInt8*)(vcl_crc64Table)); + + // First Pass + + int nWidth = rInputTexture.GetWidth(); + int nHeight = rInputTexture.GetHeight(); + + OpenGLProgram* pProgram = mpContext->UseProgram("textureVertexShader", FragShader); + if (pProgram == 0) + return false; + + int nNewWidth = ceil( nWidth / 4.0 ); + int nNewHeight = ceil( nHeight / 4.0 ); + + OpenGLTexture aFirstPassTexture = OpenGLTexture(nNewWidth, nNewHeight); + OpenGLFramebuffer* pFramebuffer = mpContext->AcquireFramebuffer(aFirstPassTexture); + + pProgram->SetUniform1f( "xstep", 1.0 / mnWidth ); + pProgram->SetUniform1f( "ystep", 1.0 / mnHeight ); + + pProgram->SetTexture("crc_table", (OpenGLTexture&)(aCRCTableTexture)); + pProgram->SetTexture("sampler", rInputTexture); + pProgram->DrawTexture(rInputTexture); + pProgram->Clean(); + + OpenGLContext::ReleaseFramebuffer(pFramebuffer); + + CHECK_GL_ERROR(); + + + // Second Pass + + nWidth = aFirstPassTexture.GetWidth(); + nHeight = aFirstPassTexture.GetHeight(); + + pProgram = mpContext->UseProgram("textureVertexShader", FragShader); + if (pProgram == 0) + return false; + + nNewWidth = ceil( nWidth / 4.0 ); + nNewHeight = ceil( nHeight / 4.0 ); + + OpenGLTexture aSecondPassTexture = OpenGLTexture(nNewWidth, nNewHeight); + pFramebuffer = mpContext->AcquireFramebuffer(aSecondPassTexture); + + pProgram->SetUniform1f( "xstep", 1.0 / mnWidth ); + pProgram->SetUniform1f( "ystep", 1.0 / mnHeight ); + + pProgram->SetTexture("crc_table", (OpenGLTexture&)(aCRCTableTexture)); + pProgram->SetTexture("sampler", aFirstPassTexture); + pProgram->DrawTexture(aFirstPassTexture); + pProgram->Clean(); + + OpenGLContext::ReleaseFramebuffer(pFramebuffer); + + CHECK_GL_ERROR(); + + // Final CRC on CPU + OpenGLTexture& aFinalTexture = aSecondPassTexture; + std::vector<sal_uInt8> aBuf( aFinalTexture.GetWidth() * aFinalTexture.GetHeight() * 4 ); + aFinalTexture.Read(GL_RGBA, GL_UNSIGNED_BYTE, aBuf.data()); + + ChecksumType nCrc = vcl_crc64(0, aBuf.data(), aBuf.size()); + + rChecksum = nCrc; + return true; +} + +void OpenGLSalBitmap::updateChecksum() const +{ + if (mbChecksumValid) + return; + + OpenGLSalBitmap* pThis = const_cast<OpenGLSalBitmap*>(this); + + if (!mpContext) + { + pThis->CreateTexture(); + } + + pThis->mbChecksumValid = calcChecksumGL(pThis->maTexture, pThis->maChecksum); +} + OpenGLContext* OpenGLSalBitmap::GetBitmapContext() { return ImplGetDefaultWindow()->GetGraphics()->GetOpenGLContext(); @@ -581,6 +670,7 @@ void OpenGLSalBitmap::ReleaseBuffer( BitmapBuffer* pBuffer, BitmapAccessMode nMo { maTexture = OpenGLTexture(); mbDirtyTexture = true; + mbChecksumValid = false; } // The palette is modified on read during the BitmapWriteAccess, // but of course, often it is not modified; interesting. |