summaryrefslogtreecommitdiffstats
path: root/client/canvas.h
blob: 1dc2acb272f3a249632ed0c66254f2ec9c0ed3a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _H_CANVAS
#define _H_CANVAS

#include "common.h"
#include "debug.h"
#include "region.h"
#include <spice/protocol.h>
#include "cache.hpp"
#include "shared_cache.hpp"
#include "canvas_base.h"
#include "canvas_utils.h"
#include "glz_decoded_image.h"
#include "glz_decoder.h"

enum CanvasType {
    CANVAS_TYPE_INVALID,
    CANVAS_TYPE_CAIRO,
    CANVAS_TYPE_GL,
    CANVAS_TYPE_GDI,
};

template <class T, int HASH_SIZE, class Base = EmptyBase>
class CHash : public Base {
public:
    CHash()
    {
        memset(_hash, 0, sizeof(_hash));
    }

    ~CHash()
    {
    }

    void add(uint32_t id, T* data)
    {
        Item** item = &_hash[key(id)];

        while (*item) {
            PANIC_ON((*item)->id == id);
            item = &(*item)->next;
        }
        *item = new Item(id, data);
    }

    bool is_present(uint32_t id)
    {
        Item* item = _hash[key(id)];

        for (;;) {
            if (!item) {
                return false;
            }

            if (item->id != id) {
                item = item->next;
                continue;
            }

            return true;
        }
    }

    T* get(uint32_t id)
    {
        Item* item = _hash[key(id)];

        for (;;) {
            PANIC_ON(!item);

            if (item->id != id) {
                item = item->next;
                continue;
            }

            return item->data;
        }
    }

    void remove(uint32_t id)
    {
        Item** item = &_hash[key(id)];

        while (*item) {
            if ((*item)->id == id) {
                Item *rm_item = *item;
                *item = rm_item->next;
                delete rm_item;
                return;
            }
            item = &(*item)->next;
        }
        THROW("id %lu, not found", id);
    }

private:
    inline uint32_t key(uint32_t id) {return id % HASH_SIZE;}

private:
    class Item {
    public:
        Item(uint32_t in_id, T* data)
            : id (in_id)
            , next (NULL)
            , data (data) {}

        ~Item()
        {
        }

        uint64_t id;
        Item* next;
        T* data;
    };

    Item* _hash[HASH_SIZE];
};

class PixmapCacheTreat {
public:
    static inline pixman_image_t *get(pixman_image_t *surf)
    {
        return pixman_image_ref(surf);
    }

    static inline void release(pixman_image_t *surf)
    {
        pixman_image_unref(surf);
    }

    static const char* name() { return "pixmap";}
};

class SpiceImageCacheBase;

typedef SharedCache<pixman_image_t, PixmapCacheTreat, 1024, SpiceImageCacheBase> PixmapCache;

class SpiceImageCacheBase {
public:
    SpiceImageCache base;

    static void op_put(SpiceImageCache *c, uint64_t id, pixman_image_t *surface)
    {
        PixmapCache* cache = reinterpret_cast<PixmapCache*>(c);
        cache->add(id, surface);
    }

    static pixman_image_t* op_get(SpiceImageCache *c, uint64_t id)
    {
        PixmapCache* cache = reinterpret_cast<PixmapCache*>(c);
        return cache->get(id);
    }

    SpiceImageCacheBase()
    {
        static SpiceImageCacheOps cache_ops = {
            op_put,
            op_get
        };
        base.ops = &cache_ops;
    }
};

class SpiceImageSurfacesBase;

typedef CHash<SpiceCanvas, 1024, SpiceImageSurfacesBase> CSurfaces;

class SpiceImageSurfacesBase {
public:
    SpiceImageSurfaces base;

    static void op_put(SpiceImageSurfaces *c, uint32_t surface_id, SpiceCanvas *surface)
    {
        CSurfaces* cache = reinterpret_cast<CSurfaces*>(c);
        cache->add(surface_id, surface);
    }

    static SpiceCanvas* op_get(SpiceImageSurfaces *s, uint32_t surface_id)
    {
        CSurfaces* cache = reinterpret_cast<CSurfaces*>(s);
        return cache->get(surface_id);
    }

    static void op_del(SpiceImageSurfaces *c, uint32_t surface_id)
    {
        CSurfaces* cache = reinterpret_cast<CSurfaces*>(c);
        cache->remove(surface_id);
    }

    SpiceImageSurfacesBase()
    {
        static SpiceImageSurfacesOps cache_ops = {
            op_get
        };
        base.ops = &cache_ops;
    }
};

class Canvas;

typedef CHash<Canvas, 1024, SpiceImageSurfacesBase> CCanvases;

class CachedPalette {
public:
    CachedPalette(SpicePalette* palette)
        : _refs(1)
    {
        int size = sizeof(SpicePalette) + palette->num_ents * sizeof(uint32_t);
        CachedPalette **ptr = (CachedPalette **)new uint8_t[size + sizeof(CachedPalette *)];
        *ptr = this;
        _palette = (SpicePalette*)(ptr + 1);
        memcpy(_palette, palette, size);
    }

    CachedPalette* ref()
    {
        _refs++;
        return this;
    }

    void unref()
    {
        if (--_refs == 0) {
            delete this;
        }
    }

    static void unref(SpicePalette *pal)
    {
        CachedPalette **ptr = (CachedPalette **)pal;
        (*(ptr - 1))->unref();
    }

    SpicePalette* palette() { return _palette;}

private:
    ~CachedPalette()
    {
        delete[] (uint8_t *)((CachedPalette **)_palette - 1);
    }

private:
    int _refs;
    SpicePalette* _palette;
};

class PaletteCacheTreat {
public:
    static inline CachedPalette* get(CachedPalette* palette)
    {
        return palette->ref();
    }

    static inline void release(CachedPalette* palette)
    {
        palette->unref();
    }

    static const char* name() { return "palette";}
};

class SpicePaletteCacheBase;
typedef Cache<CachedPalette, PaletteCacheTreat, 1024, SpicePaletteCacheBase> PaletteCache;

class SpicePaletteCacheBase {
public:
    SpicePaletteCache base;

    static void op_put(SpicePaletteCache *c, SpicePalette *palette)
    {
        PaletteCache* cache = reinterpret_cast<PaletteCache*>(c);
        AutoRef<CachedPalette> cached_palette(new CachedPalette(palette));
        cache->add(palette->unique, *cached_palette);
    }

    static SpicePalette* op_get(SpicePaletteCache *c, uint64_t id)
    {
        PaletteCache* cache = reinterpret_cast<PaletteCache*>(c);
        return cache->get(id)->palette();
    }

    static void op_release (SpicePaletteCache *c,
                            SpicePalette *palette)
    {
        CachedPalette::unref(palette);
    }

    SpicePaletteCacheBase()
    {
        static SpicePaletteCacheOps cache_ops = {
            op_put,
            op_get,
            op_release
        };
        base.ops = &cache_ops;
    }
};


/* Lz decoder related classes */

class GlzDecodedSurface: public GlzDecodedImage {
public:
    GlzDecodedSurface(uint64_t id, uint64_t win_head_id, uint8_t *data, int size,
                      int bytes_per_pixel, pixman_image_t *surface)
        : GlzDecodedImage(id, win_head_id, data, size, bytes_per_pixel)
        , _surface (surface)
    {
        pixman_image_ref(_surface);
    }

    virtual ~GlzDecodedSurface()
    {
        pixman_image_unref(_surface);
    }

private:
    pixman_image_t *_surface;
};

class GlzDecodeSurfaceHandler: public GlzDecodeHandler {
public:
    virtual GlzDecodedImage *alloc_image(void *opaque_usr_info, uint64_t image_id,
                                         uint64_t image_win_head_id, LzImageType type,
                                         int width, int height, int gross_pixels,
                                         int n_bytes_per_pixel, bool top_down)
    {
        pixman_image_t *surface = alloc_lz_image_surface((LzDecodeUsrData *)opaque_usr_info,
                                                         type, width, height, gross_pixels,
                                                         top_down);
        uint8_t *data = (uint8_t *)pixman_image_get_data(surface);
        if (!top_down) {
            data = data - (gross_pixels / height) * n_bytes_per_pixel * (height - 1);
        }

        return (new GlzDecodedSurface(image_id, image_win_head_id, data,
                                      gross_pixels, n_bytes_per_pixel, surface));
    }
};

/* TODO: unite with the window debug callbacks? */
class GlzDecoderCanvasDebug: public GlzDecoderDebug {
public:
    virtual void error(const std::string& str)
    {
        throw Exception(str);
    }

    virtual void warn(const std::string& str)
    {
        LOG_WARN("%s", str.c_str());
    }

    virtual void info(const std::string& str)
    {
        LOG_INFO("%s", str.c_str());
    }
};

class Canvas {
public:
    Canvas(PixmapCache& bits_cache, PaletteCache& palette_cache,
           GlzDecoderWindow &glz_decoder_window, CSurfaces& csurfaces);
    virtual ~Canvas();

    virtual void copy_pixels(const QRegion& region, RedDrawable* dc,
                             const PixmapHeader* pixmap) = 0;
    virtual void copy_pixels(const QRegion& region, RedDrawable& dc) = 0;
    virtual void thread_touch() = 0;

    void clear();

    void draw_fill(SpiceMsgDisplayDrawFill& fill, int size);
    void draw_text(SpiceMsgDisplayDrawText& text, int size);
    void draw_opaque(SpiceMsgDisplayDrawOpaque& opaque, int size);
    void draw_copy(SpiceMsgDisplayDrawCopy& copy, int size);
    void draw_transparent(SpiceMsgDisplayDrawTransparent& transparent, int size);
    void draw_alpha_blend(SpiceMsgDisplayDrawAlphaBlend& alpha_blend, int size);
    void copy_bits(SpiceMsgDisplayCopyBits& copy_bits, int size);
    void draw_blend(SpiceMsgDisplayDrawBlend& blend, int size);
    void draw_blackness(SpiceMsgDisplayDrawBlackness& blackness, int size);
    void draw_whiteness(SpiceMsgDisplayDrawWhiteness& whiteness, int size);
    void draw_invers(SpiceMsgDisplayDrawInvers& invers, int size);
    void draw_rop3(SpiceMsgDisplayDrawRop3& rop3, int size);
    void draw_stroke(SpiceMsgDisplayDrawStroke& stroke, int size);

    void put_image(
#ifdef WIN32
                   HDC dc,
#endif
                   const PixmapHeader& image,
                   const SpiceRect& dest, const QRegion* clip);

    virtual CanvasType get_pixmap_type() { return CANVAS_TYPE_INVALID; }

    virtual SpiceCanvas *get_internal_canvas() { return _canvas; }

protected:
    virtual void touched_bbox(const SpiceRect *bbox) {};

    PixmapCache& pixmap_cache() { return _pixmap_cache;}
    PaletteCache& palette_cache() { return _palette_cache;}
    CSurfaces& csurfaces() { return _csurfaces; }

    GlzDecoder& glz_decoder() {return _glz_decoder;}

private:
    void access_test(void* ptr, size_t size);
    void localalize_ptr(SPICE_ADDRESS* data);
    void localalize_image(SPICE_ADDRESS* in_bitmap);
    void localalize_brush(SpiceBrush& brush);
    void localalize_attr(SpiceLineAttr& attr);
    void localalize_mask(SpiceQMask& mask);
    void begin_draw(SpiceMsgDisplayBase& base, int size, size_t min_size);

protected:
    SpiceCanvas* _canvas;
    CSurfaces _surfaces;

private:
    PixmapCache& _pixmap_cache;
    PaletteCache& _palette_cache;

    GlzDecodeSurfaceHandler _glz_handler;
    GlzDecoderCanvasDebug _glz_debug;
    GlzDecoder _glz_decoder;

    CSurfaces& _csurfaces;

    unsigned long _base;
    unsigned long _max;
};


#endif