From 3d55093e17deada2cf386f26f45d2712c3135b2f Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Sun, 30 Jun 2024 17:32:14 +0700 Subject: [PATCH] #4270 use read-write memory so we can use it with cairo --- xpra/codecs/argb/argb.pyx | 38 +++++++++++++-------------- xpra/codecs/avif/decoder.pyx | 2 +- xpra/codecs/jpeg/decoder.pyx | 8 +++--- xpra/codecs/jpeg/encoder.pyx | 4 +-- xpra/codecs/libyuv/converter.pyx | 12 ++++----- xpra/codecs/nvidia/nvjpeg/encoder.pyx | 2 +- xpra/codecs/spng/decoder.pyx | 2 +- xpra/codecs/vpx/decoder.pyx | 2 +- xpra/net/lz4/lz4.pyx | 4 +-- xpra/net/websockets/mask.pyx | 2 +- 10 files changed, 38 insertions(+), 38 deletions(-) diff --git a/xpra/codecs/argb/argb.pyx b/xpra/codecs/argb/argb.pyx index d82d93675e..e43253120c 100644 --- a/xpra/codecs/argb/argb.pyx +++ b/xpra/codecs/argb/argb.pyx @@ -41,7 +41,7 @@ cdef object bgr565data_to_rgbx(const uint16_t* rgb565, const int rgb565_len): if rgb565_len <= 0: return b"" assert rgb565_len>0 and rgb565_len % 2 == 0, "invalid buffer size: %s is not a multiple of 2" % rgb565_len - cdef MemBuf output_buf = getbuf(rgb565_len*2) + cdef MemBuf output_buf = getbuf(rgb565_len*2, 0) cdef uint32_t *rgbx = output_buf.get_mem() cdef uint16_t v cdef unsigned int l = rgb565_len//2 @@ -64,7 +64,7 @@ cdef object bgr565data_to_rgb(const uint16_t* rgb565, const int rgb565_len): if rgb565_len <= 0: return b"" assert rgb565_len>0 and rgb565_len % 2 == 0, "invalid buffer size: %s is not a multiple of 2" % rgb565_len - cdef MemBuf output_buf = getbuf(rgb565_len*3//2) + cdef MemBuf output_buf = getbuf(rgb565_len*3//2, 0) cdef uint8_t *rgb = output_buf.get_mem() cdef uint32_t v, i cdef unsigned int l = rgb565_len//2 @@ -93,7 +93,7 @@ def r210_to_rgba(buf: SizedBuffer, cdef object r210data_to_rgba(unsigned int* r210, const unsigned int w, const unsigned int h, const unsigned int src_stride, const unsigned int dst_stride): - cdef MemBuf output_buf = getbuf(h*dst_stride) + cdef MemBuf output_buf = getbuf(h*dst_stride, 0) cdef unsigned int* rgba = output_buf.get_mem() cdef unsigned int v, x, y = 0 with nogil: @@ -123,7 +123,7 @@ def r210_to_rgbx(buf: SizedBuffer, cdef object r210data_to_rgbx(unsigned int* r210, const unsigned int w, const unsigned int h, const unsigned int src_stride, const unsigned int dst_stride): - cdef MemBuf output_buf = getbuf(h*dst_stride) + cdef MemBuf output_buf = getbuf(h*dst_stride, 0) cdef unsigned int* rgbx = output_buf.get_mem() cdef unsigned int v, x, y = 0 with nogil: @@ -158,7 +158,7 @@ def r210_to_rgb(buf: SizedBuffer, cdef object r210data_to_rgb(unsigned int* r210, const unsigned int w, const unsigned int h, const unsigned int src_stride, const unsigned int dst_stride): - cdef MemBuf output_buf = getbuf(h*dst_stride) + cdef MemBuf output_buf = getbuf(h*dst_stride, 0) cdef unsigned char* rgba = output_buf.get_mem() cdef unsigned int i, v, y = 0 with nogil: @@ -190,7 +190,7 @@ cdef object bgrxdata_to_rgb(const unsigned int *bgrx, const int bgrx_len): #number of pixels: cdef int mi = bgrx_len//4 #3 bytes per pixel: - cdef MemBuf output_buf = getbuf(mi*3) + cdef MemBuf output_buf = getbuf(mi*3, 0) cdef unsigned char* rgb = output_buf.get_mem() cdef int si = 0, di = 0 cdef unsigned int p @@ -220,7 +220,7 @@ cdef object rgbdata_to_bgrx(const unsigned char *rgb, const int rgb_len): #number of pixels: cdef int mi = rgb_len//3 #3 bytes per pixel: - cdef MemBuf output_buf = getbuf(mi*4) + cdef MemBuf output_buf = getbuf(mi*4, 0) cdef unsigned int* bgrx = output_buf.get_mem() cdef int si = 0, di = 0 cdef unsigned int p @@ -247,7 +247,7 @@ cdef object bgrxdata_to_l(const unsigned int *bgrx, const int bgrx_len): #number of pixels: cdef int mi = bgrx_len//4 #3 bytes per pixel: - cdef MemBuf output_buf = getbuf(mi) + cdef MemBuf output_buf = getbuf(mi, 0) cdef unsigned char* l = output_buf.get_mem() cdef int i = 0 cdef unsigned int p @@ -287,7 +287,7 @@ cdef object rgbdata_to_l(const unsigned char *rgb, const int rgb_len, #number of pixels: cdef int mi = rgb_len//3 #3 bytes per pixel: - cdef MemBuf output_buf = getbuf(mi) + cdef MemBuf output_buf = getbuf(mi, 0) cdef unsigned char* l = output_buf.get_mem() cdef int i = 0 cdef unsigned char r, g, b @@ -316,7 +316,7 @@ cdef object bgradata_to_la(const unsigned int *bgra, const int bgra_len): #number of pixels: cdef int mi = bgra_len//4 #3 bytes per pixel: - cdef MemBuf output_buf = getbuf(mi*2) + cdef MemBuf output_buf = getbuf(mi*2, 0) cdef unsigned char* la = output_buf.get_mem() cdef int si = 0, di = 0 cdef unsigned int p @@ -348,7 +348,7 @@ cdef object argbdata_to_rgba(const unsigned int* argb, const int argb_len): return b"" assert argb_len>0 and argb_len % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % argb_len cdef int mi = argb_len//4 - cdef MemBuf output_buf = getbuf(argb_len) + cdef MemBuf output_buf = getbuf(argb_len, 0) cdef unsigned int* rgba = output_buf.get_mem() cdef int i = 0 cdef unsigned int p @@ -375,7 +375,7 @@ cdef object argbdata_to_rgb(const unsigned int* argb, const int argb_len): #number of pixels: cdef int mi = argb_len//4 #3 bytes per pixel: - cdef MemBuf output_buf = getbuf(mi*3) + cdef MemBuf output_buf = getbuf(mi*3, 0) cdef unsigned char* rgb = output_buf.get_mem() cdef int si = 0, di = 0 cdef unsigned int p @@ -405,7 +405,7 @@ cdef object bgradata_to_rgb222(const unsigned char* bgra, const int bgra_len): #number of pixels: cdef int mi = bgra_len//4 #@DuplicateSignature #1 byte per pixel: - cdef MemBuf output_buf = getbuf(mi) + cdef MemBuf output_buf = getbuf(mi, 0) cdef unsigned char* rgb = output_buf.get_mem() cdef int di = 0, si = 0 #@DuplicateSignature with nogil: @@ -431,7 +431,7 @@ cdef object bgradata_to_rgb(const unsigned int* bgra, const int bgra_len): #number of pixels: cdef int mi = bgra_len//4 #3 bytes per pixel: - cdef MemBuf output_buf = getbuf(mi*3) + cdef MemBuf output_buf = getbuf(mi*3, 0) cdef unsigned char* rgb = output_buf.get_mem() cdef int di = 0, si = 0 cdef unsigned int p @@ -459,7 +459,7 @@ cdef object bgradata_to_rgba(const unsigned int* bgra, const int bgra_len): return b"" assert bgra_len>0 and bgra_len % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % bgra_len cdef int mi = bgra_len//4 - cdef MemBuf output_buf = getbuf(bgra_len) + cdef MemBuf output_buf = getbuf(bgra_len, 0) cdef unsigned int* rgba = output_buf.get_mem() cdef int i = 0 cdef unsigned int p @@ -489,7 +489,7 @@ cdef object bgradata_to_rgbx(const unsigned char* bgra, const int bgra_len): return b"" assert bgra_len>0 and bgra_len % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % bgra_len #same number of bytes: - cdef MemBuf output_buf = getbuf(bgra_len) + cdef MemBuf output_buf = getbuf(bgra_len, 0) cdef unsigned char* rgbx = output_buf.get_mem() cdef int i = 0 #@DuplicateSignature with nogil: @@ -517,7 +517,7 @@ cdef do_premultiply_argb(unsigned int *buf, Py_ssize_t argb_len): cdef unsigned char a, r, g, b #@DuplicateSignature cdef unsigned int argb #@DuplicateSignature assert argb_len>0 and argb_len % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % argb_len - cdef MemBuf output_buf = getbuf(argb_len) + cdef MemBuf output_buf = getbuf(argb_len, 0) cdef unsigned int* argb_out = output_buf.get_mem() cdef int i #@DuplicateSignature with nogil: @@ -558,7 +558,7 @@ cdef object do_unpremultiply_argb(unsigned int * argb_in, Py_ssize_t argb_len): cdef unsigned char a, r, g, b #@DuplicateSignature cdef unsigned int argb #@DuplicateSignature assert argb_len>0 and argb_len % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % argb_len - cdef MemBuf output_buf = getbuf(argb_len) + cdef MemBuf output_buf = getbuf(argb_len, 0) cdef unsigned char* argb_out = output_buf.get_mem() cdef int i #@DuplicateSignature with nogil: @@ -604,7 +604,7 @@ cdef object alpha_data(const unsigned char* rgba, const int rgba_len, const char if rgba_len <= 0: return None assert rgba_len>0 and rgba_len % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % rgba_len - cdef MemBuf output_buf = getbuf(rgba_len//4) + cdef MemBuf output_buf = getbuf(rgba_len//4, 0) cdef unsigned char* alpha = output_buf.get_mem() cdef int di = 0, si = index cdef unsigned int p diff --git a/xpra/codecs/avif/decoder.pyx b/xpra/codecs/avif/decoder.pyx index 857c980db2..dddac66733 100644 --- a/xpra/codecs/avif/decoder.pyx +++ b/xpra/codecs/avif/decoder.pyx @@ -148,7 +148,7 @@ def decompress(data: bytes, options: typedict, yuv=False) -> ImageWrapper: bpp = 24 rgb.format = AVIF_RGB_FORMAT_BGRA stride = width*4 - pixels = getbuf(width*4*height) + pixels = getbuf(width*4*height, 0) rgb.pixels = pixels.get_mem() rgb.rowBytes = stride rgb.alphaPremultiplied = 1 diff --git a/xpra/codecs/jpeg/decoder.pyx b/xpra/codecs/jpeg/decoder.pyx index f8bdf5e462..b7eb652e67 100644 --- a/xpra/codecs/jpeg/decoder.pyx +++ b/xpra/codecs/jpeg/decoder.pyx @@ -197,7 +197,7 @@ def decompress_to_yuv(data: bytes, options: typedict, unsigned char nplanes=3) - plane_size = stride * roundup(h, 2)//2 if i==1: #allocate empty U and V planes: - empty = getbuf(plane_size) + empty = getbuf(plane_size, 0) memset( empty.get_mem(), 128, plane_size) pixel_format = "YUV420P" membuf = empty @@ -205,7 +205,7 @@ def decompress_to_yuv(data: bytes, options: typedict, unsigned char nplanes=3) - stride = roundup(stride, ALIGN) strides[i] = stride plane_size = tjPlaneSizeYUV(i, w, stride, h, subsamp) - membuf = getbuf(plane_size) #add padding? + membuf = getbuf(plane_size, 0) #add padding? planes[i] = membuf.get_mem() total_size += plane_size #python objects for each plane: @@ -271,7 +271,7 @@ def decompress_to_rgb(rgb_format: str, data: bytes, unsigned long alpha_offset=0 cdef double start = monotonic() cdef int stride = w*4 cdef unsigned long size = stride*h - cdef MemBuf membuf = getbuf(size) + cdef MemBuf membuf = getbuf(size, 0) cdef unsigned char *dst_buf = membuf.get_mem() with nogil: r = tjDecompress2(decompressor, @@ -312,7 +312,7 @@ def decompress_to_rgb(rgb_format: str, data: bytes, unsigned long alpha_offset=0 alpha_stride = tjPlaneWidth(0, w, subsamp) strides[0] = alpha_stride alpha_size = tjPlaneSizeYUV(0, w, alpha_stride, h, subsamp) - alpha = getbuf(alpha_size) + alpha = getbuf(alpha_size, 0) alpha_plane = alpha.get_mem() planes[0] = alpha_plane with nogil: diff --git a/xpra/codecs/jpeg/encoder.pyx b/xpra/codecs/jpeg/encoder.pyx index 51a6c961dc..c3db90d5fb 100644 --- a/xpra/codecs/jpeg/encoder.pyx +++ b/xpra/codecs/jpeg/encoder.pyx @@ -408,7 +408,7 @@ cdef object do_encode_rgb(tjhandle compressor, pfstr, pixels, log.error(" pixel format=%s", pfstr) return None assert out_size>0 and out!=NULL, "jpeg compression produced no data" - return makebuf(out, out_size) + return makebuf(out, out_size, 0) cdef object encode_yuv(tjhandle compressor, image, int quality, int grayscale=0): @@ -483,7 +483,7 @@ cdef object do_encode_yuv(tjhandle compressor, pfstr, planes, for bc in contexts: bc.__exit__() assert out_size>0 and out!=NULL, "jpeg compression produced no data" - return makebuf(out, out_size) + return makebuf(out, out_size, 0) def selftest(full=False) -> None: diff --git a/xpra/codecs/libyuv/converter.pyx b/xpra/codecs/libyuv/converter.pyx index 99d3729dea..361c3c3809 100644 --- a/xpra/codecs/libyuv/converter.pyx +++ b/xpra/codecs/libyuv/converter.pyx @@ -281,7 +281,7 @@ def argb_to_gray(image: ImageWrapper) -> ImageWrapper: assert pixels, "failed to get pixels from %s" % image #allocate output buffer: cdef int dst_stride = width*4 - cdef MemBuf output_buffer = getbuf(dst_stride*height) + cdef MemBuf output_buffer = getbuf(dst_stride*height, 0) if not output_buffer: raise RuntimeError("failed to allocate %i bytes for output buffer" % (dst_stride*height)) cdef uint8_t* buf = output_buffer.get_mem() @@ -312,7 +312,7 @@ def argb_scale(image, int dst_width, int dst_height, FilterMode filtermode=kFilt assert pixels, "failed to get pixels from %s" % image #allocate output buffer: cdef int dst_stride = dst_width*4 - cdef MemBuf output_buffer = getbuf(dst_stride*dst_height) + cdef MemBuf output_buffer = getbuf(dst_stride*dst_height, 0) if not output_buffer: raise RuntimeError("failed to allocate %i bytes for output buffer" % (dst_stride*height)) cdef uint8_t* buf = output_buffer.get_mem() @@ -568,11 +568,11 @@ cdef class Converter: cdef int uv_stride = strides[1] cdef int Bpp = len(self.dst_format) cdef int rowstride = self.dst_width*Bpp - cdef MemBuf rgb_buffer = getbuf(rowstride*height) + cdef MemBuf rgb_buffer = getbuf(rowstride*height, 0) cdef uintptr_t y, uv cdef uint8_t *rgb cdef int r = 0 - rgb_buffer = getbuf(self.out_buffer_size) + rgb_buffer = getbuf(self.out_buffer_size, 0) if not rgb_buffer: raise RuntimeError(f"failed to allocate {self.out_buffer_size} bytes for output buffer") log("convert_nv12_image(%s) to %s", image, self.dst_format) @@ -690,12 +690,12 @@ cdef class Converter: cdef int v_stride = strides[2] cdef int Bpp = len(self.dst_format) cdef int rowstride = self.dst_width*Bpp - cdef MemBuf rgb_buffer = getbuf(rowstride*height) + cdef MemBuf rgb_buffer = getbuf(rowstride*height, 0) cdef uintptr_t y, u, v cdef uint8_t *rgb cdef int r = 0 cdef int matched = 0 - rgb_buffer = getbuf(self.out_buffer_size) + rgb_buffer = getbuf(self.out_buffer_size, 0) if not rgb_buffer: raise RuntimeError(f"failed to allocate {self.out_buffer_size} bytes for output buffer") log("convert_yuv_image(%s) to %s", image, self.dst_format) diff --git a/xpra/codecs/nvidia/nvjpeg/encoder.pyx b/xpra/codecs/nvidia/nvjpeg/encoder.pyx index dee8262c72..db719aac35 100644 --- a/xpra/codecs/nvidia/nvjpeg/encoder.pyx +++ b/xpra/codecs/nvidia/nvjpeg/encoder.pyx @@ -418,7 +418,7 @@ cdef class Encoder: start = monotonic() r = nvjpegEncodeRetrieveBitstream(self.nv_handle, self.nv_enc_state, NULL, &length, self.stream) errcheck(r, "nvjpegEncodeRetrieveBitstream") - output_buf = getbuf(length) + output_buf = getbuf(length, 0) buf_ptr = output_buf.get_mem() with nogil: r = nvjpegEncodeRetrieveBitstream(self.nv_handle, self.nv_enc_state, buf_ptr, &length, NULL) diff --git a/xpra/codecs/spng/decoder.pyx b/xpra/codecs/spng/decoder.pyx index 6d6ac284ce..5064ac183b 100644 --- a/xpra/codecs/spng/decoder.pyx +++ b/xpra/codecs/spng/decoder.pyx @@ -129,7 +129,7 @@ def decompress(data) -> Tuple[memoryview, str, int, int]: close() return None - cdef MemBuf membuf = getbuf(out_size) + cdef MemBuf membuf = getbuf(out_size, 0) cdef uintptr_t ptr = membuf.get_mem() with nogil: r = spng_decode_image(ctx, ptr, out_size, fmt, flags) diff --git a/xpra/codecs/vpx/decoder.pyx b/xpra/codecs/vpx/decoder.pyx index e6b44273f7..9f31c842a9 100644 --- a/xpra/codecs/vpx/decoder.pyx +++ b/xpra/codecs/vpx/decoder.pyx @@ -347,7 +347,7 @@ cdef class Decoder: plane_len = height * stride #add one extra line of padding: - output_buf = padbuf(plane_len, stride) + output_buf = padbuf(plane_len, stride, 0) output = output_buf.get_mem() memcpy(output, img.planes[i], plane_len) memset(((output)+plane_len), 0, stride) diff --git a/xpra/net/lz4/lz4.pyx b/xpra/net/lz4/lz4.pyx index 96405831a4..175e70abb2 100644 --- a/xpra/net/lz4/lz4.pyx +++ b/xpra/net/lz4/lz4.pyx @@ -99,7 +99,7 @@ def decompress(data: SizedBuffer, int max_size=0, int size=0) -> SizedBuffer: cdef Py_buffer in_buf if PyObject_GetBuffer(data, &in_buf, PyBUF_ANY_CONTIGUOUS): raise ValueError("failed to read data from %s" % type(data)) - cdef MemBuf out_buf = getbuf(size) + cdef MemBuf out_buf = getbuf(size, 0) cdef char *in_ptr = (( in_buf.buf) + size_header) cdef char *out_ptr = out_buf.get_mem() cdef int l = in_buf.len @@ -111,4 +111,4 @@ def decompress(data: SizedBuffer, int max_size=0, int size=0) -> SizedBuffer: msg = f"LZ4_decompress_safe failed for input size {in_buf.len}" log(msg) raise ValueError(msg) - return (memoryview(out_buf)[:r]).toreadonly() + return memoryview(out_buf)[:r] diff --git a/xpra/net/websockets/mask.pyx b/xpra/net/websockets/mask.pyx index 68a9206c7f..cb5b737a39 100644 --- a/xpra/net/websockets/mask.pyx +++ b/xpra/net/websockets/mask.pyx @@ -32,7 +32,7 @@ cdef object do_hybi_mask(uintptr_t mp, uintptr_t dp, unsigned int datalen): # to ensure that its alignment is the same as the input data buffer cdef unsigned int align = ( dp) & 0x3 cdef unsigned int initial_chars = (4 - align) & 0x3 - cdef MemBuf out_buf = getbuf(datalen+align) + cdef MemBuf out_buf = getbuf(datalen+align, 0) cdef uintptr_t op = out_buf.get_mem() # char pointers: cdef unsigned char *mcbuf = mp