Skip to content

Commit

Permalink
#4270 use read-write memory so we can use it with cairo
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Jun 30, 2024
1 parent 7c024f4 commit 3d55093
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 38 deletions.
38 changes: 19 additions & 19 deletions xpra/codecs/argb/argb.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <uint32_t*> output_buf.get_mem()
cdef uint16_t v
cdef unsigned int l = rgb565_len//2
Expand All @@ -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 = <uint8_t*> output_buf.get_mem()
cdef uint32_t v, i
cdef unsigned int l = rgb565_len//2
Expand Down Expand Up @@ -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 = <unsigned int*> output_buf.get_mem()
cdef unsigned int v, x, y = 0
with nogil:
Expand Down Expand Up @@ -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 = <unsigned int*> output_buf.get_mem()
cdef unsigned int v, x, y = 0
with nogil:
Expand Down Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
cdef unsigned int i, v, y = 0
with nogil:
Expand Down Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int si = 0, di = 0
cdef unsigned int p
Expand Down Expand Up @@ -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 = <unsigned int*> output_buf.get_mem()
cdef int si = 0, di = 0
cdef unsigned int p
Expand All @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int i = 0
cdef unsigned int p
Expand Down Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int i = 0
cdef unsigned char r, g, b
Expand Down Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int si = 0, di = 0
cdef unsigned int p
Expand Down Expand Up @@ -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 = <unsigned int*> output_buf.get_mem()
cdef int i = 0
cdef unsigned int p
Expand All @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int si = 0, di = 0
cdef unsigned int p
Expand Down Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int di = 0, si = 0 #@DuplicateSignature
with nogil:
Expand All @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int di = 0, si = 0
cdef unsigned int p
Expand Down Expand Up @@ -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 = <unsigned int*> output_buf.get_mem()
cdef int i = 0
cdef unsigned int p
Expand Down Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int i = 0 #@DuplicateSignature
with nogil:
Expand Down Expand Up @@ -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 = <unsigned int*> output_buf.get_mem()
cdef int i #@DuplicateSignature
with nogil:
Expand Down Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int i #@DuplicateSignature
with nogil:
Expand Down Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
cdef int di = 0, si = index
cdef unsigned int p
Expand Down
2 changes: 1 addition & 1 deletion xpra/codecs/avif/decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <uint8_t *> pixels.get_mem()
rgb.rowBytes = stride
rgb.alphaPremultiplied = 1
Expand Down
8 changes: 4 additions & 4 deletions xpra/codecs/jpeg/decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ 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(<void *> empty.get_mem(), 128, plane_size)
pixel_format = "YUV420P"
membuf = empty
else:
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] = <unsigned char*> membuf.get_mem()
total_size += plane_size
#python objects for each plane:
Expand Down Expand Up @@ -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 = <unsigned char*> membuf.get_mem()
with nogil:
r = tjDecompress2(decompressor,
Expand Down Expand Up @@ -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 = <unsigned char*> alpha.get_mem()
planes[0] = alpha_plane
with nogil:
Expand Down
4 changes: 2 additions & 2 deletions xpra/codecs/jpeg/encoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions xpra/codecs/libyuv/converter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <uint8_t*> output_buffer.get_mem()
Expand Down Expand Up @@ -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 = <uint8_t*> output_buffer.get_mem()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion xpra/codecs/nvidia/nvjpeg/encoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <unsigned char*> output_buf.get_mem()
with nogil:
r = nvjpegEncodeRetrieveBitstream(self.nv_handle, self.nv_enc_state, buf_ptr, &length, NULL)
Expand Down
2 changes: 1 addition & 1 deletion xpra/codecs/spng/decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <uintptr_t> membuf.get_mem()
with nogil:
r = spng_decode_image(ctx, <void *> ptr, out_size, fmt, flags)
Expand Down
2 changes: 1 addition & 1 deletion xpra/codecs/vpx/decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <void *>output_buf.get_mem()
memcpy(output, <void *>img.planes[i], plane_len)
memset(<void *>((<char *>output)+plane_len), 0, stride)
Expand Down
4 changes: 2 additions & 2 deletions xpra/net/lz4/lz4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <char*> ((<uintptr_t> in_buf.buf) + size_header)
cdef char *out_ptr = <char *> out_buf.get_mem()
cdef int l = <int> in_buf.len
Expand All @@ -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]
2 changes: 1 addition & 1 deletion xpra/net/websockets/mask.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (<uintptr_t> 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 = <uintptr_t> out_buf.get_mem()
# char pointers:
cdef unsigned char *mcbuf = <unsigned char *> mp
Expand Down

0 comments on commit 3d55093

Please sign in to comment.