xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #465: xor-debug.patch

File xor-debug.patch, 11.3 KB (added by Antoine Martin, 6 years ago)

the patch I am using for debugging

  • xpra/codecs/argb/argb.pyx

     
    1414cdef extern from "string.h":
    1515    void * memcpy(void * destination, void * source, size_t num)
    1616
     17cdef extern from "stdlib.h":
     18    void free(void* mem)
    1719
     20
    1821import struct
    1922from xpra.log import Logger
    2023log = Logger("encoding")
     
    247250    assert object_as_buffer(pixels, <const void**> &img_buf, &img_buf_len)==0, "cannot convert %s to a readable buffer" % type(pixels)
    248251    if img_buf_len<=0:
    249252        return False
    250     cdef int out_size = rstride*height                 #desirable size we could have
     253    cdef int out_size = rstride*height                  #desirable size we could have
    251254    #is it worth re-striding to save space:
    252255    if img_buf_len-out_size<1024 or out_size*110/100>img_buf_len:
    253256        return False
     
    254257    #we'll save at least 1KB and 10%, do it
    255258    #Note: we could also change the pixel format whilst we're at it
    256259    # and convert BGRX to RGB for example (assuming RGB is also supported by the client)
    257     #this buffer is allocated by the imagewrapper, so it will be freed after use for us:
    258     cdef unsigned long ptr = int(image.allocate_buffer(out_size))
    259     assert ptr>0
     260    #this buffer is allocated by the imagewrapper, so it will be freed after use for us,
     261    #but we need to tell allocate_buffer not to free the current buffer (if there is one),
     262    #and we have to deal with this ourselves after we're done copying it
     263    cdef unsigned long ptr
     264
     265    #save pixels pointer to free later:
     266    ptr = int(image.get_pixel_ptr())
     267    cdef unsigned long pixptr = ptr
     268
     269    ptr = int(image.allocate_buffer(out_size, False))
     270    assert ptr>0, "allocate_buffer failed"
    260271    cdef unsigned char *out = <unsigned char*> ptr
     272
    261273    cdef int ry = height
    262     while ry>0:
     274    for 0 <= ry < height:
    263275        memcpy(out, img_buf, rstride)
    264276        out += rstride
    265277        img_buf += stride
    266         ry -= 1
     278    if pixptr:
     279        free(<void *> pixptr)
    267280    log("restride_image: %s pixels re-stride saving %i%% from %s (%s bytes) to %s (%s bytes)" % (pixel_format, 100-100*out_size/img_buf_len, stride, img_buf_len, rstride, out_size))
    268281    image.set_rowstride(rstride)
    269282    return True
  • xpra/codecs/xor/cyxor.pyx

     
    1414cdef extern from "../buffers/buffers.h":
    1515    int    object_as_buffer(object obj, const void ** buffer, Py_ssize_t * buffer_len)
    1616
     17from numpy import frombuffer, bitwise_xor, byte
     18from xpra.os_util import memoryview_to_bytes
    1719
    18 def xor_str(buf, xor_string):
     20def pp(info, v):
     21    cdef const unsigned char * buf                  #@DuplicatedSignature
     22    cdef Py_ssize_t buf_len = 0                     #@DuplicatedSignature
     23    assert object_as_buffer(v, <const void**> &buf, &buf_len)==0, "cannot get buffer pointer for %s: %s" % (type(v), v)
     24    print("%s=%#x" % (info, <unsigned long> buf))
     25
     26def xor_str(aa, bb):
     27    assert len(aa)==len(bb), "cannot xor strings of different lengths (numpyxor)"
     28    pp("aa", aa)
     29    pp("bb", bb)
     30    a = frombuffer(memoryview_to_bytes(aa), dtype=byte)
     31    b = frombuffer(memoryview_to_bytes(bb), dtype=byte)
     32    c = bitwise_xor(a, b)
     33    import binascii
     34    print("xor_str(%s..," % binascii.hexlify(memoryview_to_bytes(aa))[:256])
     35    print("        %s..," % binascii.hexlify(memoryview_to_bytes(bb))[:256])
     36    print("       =%s)" % binascii.hexlify(memoryview_to_bytes(c.tostring()))[:256])
     37    return c.tostring()
     38
     39def cyxor_str(buf, xor_string):
    1940    assert len(buf)==len(xor_string), "cyxor cannot xor strings of different lengths (%s:%s vs %s:%s)" % (type(buf), len(buf), type(xor_string), len(xor_string))
    2041    cdef const unsigned char * cbuf                 #@DuplicatedSignature
    2142    cdef Py_ssize_t cbuf_len = 0                    #@DuplicatedSignature
  • xpra/server/picture_encode.py

     
    8383    pixel_format = image.get_pixel_format()
    8484    #log("rgb_encode%s pixel_format=%s, rgb_formats=%s", (coding, image, rgb_formats, supports_transparency, speed, rgb_zlib, rgb_lz4), pixel_format, rgb_formats)
    8585    if pixel_format not in rgb_formats:
     86        log.info("reformatting from %s to %s", pixel_format, rgb_formats)
    8687        if not rgb_reformat(image, rgb_formats, supports_transparency):
    8788            raise Exception("cannot find compatible rgb format to use for %s! (supported: %s)" % (pixel_format, rgb_formats))
    8889        #get the new format:
     
    9697    options = {"rgb_format" : pixel_format}
    9798
    9899    #we may want to re-stride:
    99     restride_image(image)
     100    if False:
     101        restride_image(image)
    100102
    101103    #compress here and return a wrapper so network code knows it is already zlib compressed:
    102     pixels = image.get_pixels()
     104    pixels = image.get_pixels()[:]
    103105    width = image.get_width()
    104106    height = image.get_height()
    105107    stride = image.get_rowstride()
     
    106108
    107109    #compression stage:
    108110    #by default, wire=raw:
    109     raw_data = memoryview_to_bytes(pixels)
     111    raw_data = memoryview_to_bytes(pixels)[:]
    110112    wire_data = raw_data
    111113    level = 0
    112114    algo = "not"
    113     if len(pixels)>=256 and (rgb_zlib and compression.use_zlib) or (rgb_lz4 and compression.lz4) or (rgb_lzo and compression.use_lzo):
     115    if False and len(pixels)>=256 and (rgb_zlib and compression.use_zlib) or (rgb_lz4 and compression.lz4) or (rgb_lzo and compression.use_lzo):
    114116        level = max(0, min(5, int(115-speed)/20))
    115117        if len(pixels)<1024:
    116118            #fewer pixels, make it more likely we won't bother compressing:
     
    140142        bpp = 32
    141143    else:
    142144        bpp = 24
    143     log("rgb_encode using level=%s, %s compressed %sx%s in %s/%s: %s bytes down to %s", level, algo, image.get_width(), image.get_height(), coding, pixel_format, len(pixels), len(wire_data))
     145    log.info("rgb_encode using level=%s, %s compressed %sx%s in %s/%s: %s bytes down to %s", level, algo, image.get_width(), image.get_height(), coding, pixel_format, len(pixels), len(wire_data))
    144146    #wrap it using "Compressed" so the network layer receiving it
    145147    #won't decompress it (leave it to the client's draw thread)
    146148    return coding, compression.Compressed(coding, wire_data, True), options, width, height, stride, bpp
  • xpra/server/window_source.py

     
    14541454            * 'mmap' will use 'mmap_send' + 'mmap_encode' - always if available, otherwise:
    14551455            * 'jpeg' and 'png' are handled by 'PIL_encode'.
    14561456            * 'webp' uses 'webp_encode'
    1457             * 'h264' and 'vp8' use 'video_encode'
     1457            * 'h264', 'h265', 'vp8' and 'vp9' use 'video_encode'
    14581458            * 'rgb24' and 'rgb32' use 'rgb_encode'
    14591459        """
    14601460        if self.is_cancelled(sequence) or self.suspended:
     
    14751475            mmap_data = mmap_send(self._mmap, self._mmap_size, image, self.rgb_formats, self.supports_transparency)
    14761476        if mmap_data:
    14771477            #the mmap area has been populated, now tell the client about it:
    1478             data, mmap_free_size, written = mmap_data
     1478            mmap_info, mmap_free_size, written = mmap_data
    14791479            self.global_statistics.mmap_bytes_sent += written
    14801480            self.global_statistics.mmap_free_size = mmap_free_size
    14811481            #hackish: pass data to mmap_encode using "options":
    14821482            coding = "mmap"         #changed encoding!
    1483             options["mmap_data"] = data
     1483            options["mmap_data"] = mmap_info
    14841484        #if client supports delta pre-compression for this encoding, use it if we can:
    14851485        elif self.delta_buckets>0 and (coding in self.supports_delta) and self.min_delta_size<isize<self.max_delta_size:
    14861486            #this may save space (and lower the cost of xoring):
    1487             restride_image(image)
     1487            if True:
     1488                restride_image(image)
    14881489            #we need to copy the pixels because some encodings
    14891490            #may modify the pixel array in-place!
    14901491            dpixels = image.get_pixels()[:]
     
    14981499                    #xor with this matching delta bucket:
    14991500                    delta = lsequence
    15001501                    bucket = i
    1501                     data = xor_str(dpixels, ldata)
    1502                     image.set_pixels(data)
     1502                    image.set_pixels(xor_str(dpixels, ldata))
    15031503                    dr[-1] = time.time()            #update last used time
    1504                     deltalog("delta: using matching bucket %s: %sx%s (%s)", i, lw, lh, lcoding)
     1504                    deltalog("delta: using matching bucket %s: %sx%s (%s, %s bytes, sequence=%s)", i, lw, lh, lcoding, dlen, lsequence)
    15051505                    break
    15061506
    15071507        #by default, don't set rowstride (the container format will take care of providing it):
     
    15281528            client_options["bucket"] = bucket
    15291529        csize = len(data)
    15301530        if store>0:
    1531             if delta>0 and csize>=psize/3:
     1531            if delta>0 and False:    #csize>=psize*2//5:
    15321532                #compressed size is more than 33% of the original
    15331533                #maybe delta is not helping us, so clear it:
    15341534                self.delta_pixel_data[bucket] = None
  • xpra/x11/bindings/ximage.pyx

     
    289289        return self.pixel_format
    290290
    291291    def get_pixels(self):
     292        log.info("get_pixels() pixels ptr=%#x", <unsigned long> self.pixels)
    292293        if self.pixels!=NULL:
    293294            return self.get_char_pixels()
    294295        return self.get_image_pixels()
    295296
    296     cdef get_char_pixels(self):
     297    cdef object get_char_pixels(self):
    297298        assert self.pixels!=NULL
    298299        return memory_as_pybuffer(self.pixels, self.get_size(), False)
    299300
    300     cdef get_image_pixels(self):
     301    cdef object get_image_pixels(self):
    301302        assert self.image!=NULL
    302303        return memory_as_pybuffer(self.image.data, self.get_size(), False)
    303304
     
    328329        cdef const unsigned char * buf = NULL
    329330        cdef Py_ssize_t buf_len = 0
    330331        assert object_as_buffer(pixels, <const void**> &buf, &buf_len)==0
    331         self.allocate_buffer(buf_len)
     332        self.allocate_buffer(buf_len, 1)
     333        log.info("set_pixels(%s) copying %s bytes to %#x", type(pixels), buf_len, <unsigned long> self.pixels)
     334        import binascii
     335        log.info("pixels=%s", binascii.hexlify(pixels)[:256])
    332336        memcpy(self.pixels, buf, buf_len)
    333337
    334     def allocate_buffer(self, buf_len):
    335         if self.pixels!=NULL:
     338    def get_pixel_ptr(self):
     339        return int(<unsigned long> self.pixels)
     340
     341    def allocate_buffer(self, Py_ssize_t buf_len, int free_existing=1):
     342        if self.pixels!=NULL and free_existing!=0:
    336343            free(self.pixels)
    337             self.pixels = NULL
     344        self.pixels = NULL
    338345        #Note: we can't free the XImage, because it may
    339346        #still be used somewhere else (see XShmWrapper)
    340347        if posix_memalign(<void **> &self.pixels, 64, buf_len):