meson, util: Make zlib optional again

This adds a HAVE_COMPRESSION macro, which is undefined if neither zlib
nor zstd are present, and is used to no-op compress.h/c. This also has
a side effect of fixing SCons, since it won't define this macro.

Fixes: d7ecbd5bf8 ("util: create some standalone compression helpers")
Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9689>
This commit is contained in:
Jesse Natalie 2021-03-18 09:12:46 -07:00 committed by Marge Bot
parent 28bf06f350
commit 012bc2fc77
4 changed files with 29 additions and 4 deletions

View File

@ -73,6 +73,7 @@ LOCAL_CFLAGS += \
-DHAVE_LINUX_FUTEX_H \
-DHAVE_ENDIAN_H \
-DHAVE_ZLIB \
-DHAVE_COMPRESSION \
-DMAJOR_IN_SYSMACROS \
-DVK_USE_PLATFORM_ANDROID_KHR \
-fvisibility=hidden \

View File

@ -1400,6 +1400,13 @@ else
dep_zstd = null_dep
endif
with_compression = dep_zlib.found() or dep_zstd.found()
if with_compression
pre_args += '-DHAVE_COMPRESSION'
elif with_shader_cache
error('Shader Cache requires compression')
endif
dep_thread = dependency('threads')
if dep_thread.found() and host_machine.system() != 'windows'
pre_args += '-DHAVE_PTHREAD'

View File

@ -21,6 +21,8 @@
* IN THE SOFTWARE.
*/
#ifdef HAVE_COMPRESSION
#include <assert.h>
/* Ensure that zlib uses 'const' in 'z_const' declarations. */
@ -28,13 +30,16 @@
#define ZLIB_CONST
#endif
#ifdef HAVE_ZLIB
#include "zlib.h"
#endif
#ifdef HAVE_ZSTD
#include "zstd.h"
#endif
#include "util/compress.h"
#include "macros.h"
/* 3 is the recomended level, with 22 as the absolute maximum */
#define ZSTD_COMPRESSION_LEVEL 3
@ -47,7 +52,7 @@ util_compress_max_compressed_len(size_t in_data_size)
* compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
*/
return ZSTD_compressBound(in_data_size);
#else
#elif defined(HAVE_ZLIB)
/* From https://zlib.net/zlib_tech.html:
*
* "In the worst possible case, where the other block types would expand
@ -58,7 +63,9 @@ util_compress_max_compressed_len(size_t in_data_size)
* entire stream."
*/
size_t num_blocks = (in_data_size + 16383) / 16384; /* round up blocks */
return in_data_size + 6 + (num_blocks * 5);
return in_data_size + 6 + (num_blocks * 5);
#else
STATIC_ASSERT(false);
#endif
}
@ -74,7 +81,7 @@ util_compress_deflate(const uint8_t *in_data, size_t in_data_size,
return 0;
return ret;
#else
#elif defined(HAVE_ZLIB)
size_t compressed_size = 0;
/* allocate deflate state */
@ -105,6 +112,8 @@ util_compress_deflate(const uint8_t *in_data, size_t in_data_size,
/* clean up and return */
(void) deflateEnd(&strm);
return compressed_size;
#else
STATIC_ASSERT(false);
# endif
}
@ -118,7 +127,7 @@ util_compress_inflate(const uint8_t *in_data, size_t in_data_size,
#ifdef HAVE_ZSTD
size_t ret = ZSTD_decompress(out_data, out_data_size, in_data, in_data_size);
return !ZSTD_isError(ret);
#else
#elif defined(HAVE_ZLIB)
z_stream strm;
/* allocate inflate state */
@ -149,5 +158,9 @@ util_compress_inflate(const uint8_t *in_data, size_t in_data_size,
/* clean up and return */
(void)inflateEnd(&strm);
return true;
#else
STATIC_ASSERT(false);
#endif
}
#endif

View File

@ -21,6 +21,8 @@
* IN THE SOFTWARE.
*/
#ifdef HAVE_COMPRESSION
#include <stdbool.h>
#include <inttypes.h>
@ -34,3 +36,5 @@ util_compress_inflate(const uint8_t *in_data, size_t in_data_size,
size_t
util_compress_deflate(const uint8_t *in_data, size_t in_data_size,
uint8_t *out_data, size_t out_buff_size);
#endif