summaryrefslogtreecommitdiff
path: root/display/surface.c
blob: d9fc6f40d5b837912c1c97d6a51aa2b9ac717c30 (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
/*
   Copyright (C) 2009 Red Hat, Inc.

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

   This program 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 General Public License for more details.

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

#include "stddef.h"

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include "os_dep.h"

#include "winerror.h"
#include "windef.h"
#include "wingdi.h"
#include "winddi.h"
#include "devioctl.h"
#include "ntddvdeo.h"
#include "ioaccess.h"

#include "qxldd.h"
#include "utils.h"
#include "mspace.h"
#include "res.h"
#include "surface.h"

BOOL CreateDrawArea(PDev *pdev, UINT8 *base_mem, ULONG format, UINT32 cx, UINT32 cy, UINT32 stride,
                    UINT32 surface_id)
{
    SIZEL  size;
    DrawArea *drawarea;

    size.cx = cx;
    size.cy = cy;

    drawarea = &GetSurfaceInfo(pdev, surface_id)->draw_area;

    if (!(drawarea->bitmap = (HSURF)EngCreateBitmap(size, stride, format, 0, base_mem))) {
        DEBUG_PRINT((pdev, 0, "%s: EngCreateBitmap failed\n", __FUNCTION__));
        return FALSE;
    }

    if (!EngAssociateSurface(drawarea->bitmap, pdev->eng, 0)) {
        DEBUG_PRINT((pdev, 0, "%s: EngAssociateSurface failed\n", __FUNCTION__));
        goto error;
    }

    if (!(drawarea->surf_obj = EngLockSurface(drawarea->bitmap))) {
        DEBUG_PRINT((pdev, 0, "%s: EngLockSurface failed\n", __FUNCTION__));
        goto error;
    }

    drawarea->base_mem = base_mem;

    return TRUE;
error:
    EngDeleteSurface(drawarea->bitmap);
    return FALSE;
}

VOID FreeDrawArea(DrawArea *drawarea)
{
    if (drawarea->surf_obj) {
        EngUnlockSurface(drawarea->surf_obj);
        EngDeleteSurface(drawarea->bitmap);
        drawarea->surf_obj = NULL;
    }
}

HBITMAP CreateDeviceBitmap(PDev *pdev, SIZEL size, ULONG format, QXLPHYSICAL *phys_mem,
                           UINT8 **base_mem, UINT32 surface_id, UINT8 allocation_type)
{
    UINT32 surface_format, depth;
    HBITMAP surf;
    UINT32 stride;

    switch (format) {
        case BMF_16BPP:
            surface_format = SPICE_SURFACE_FMT_16_555;
            depth = 16;
            break;
        case BMF_24BPP:
        case BMF_32BPP:
            surface_format = SPICE_SURFACE_FMT_32_xRGB;
            depth = 32;
            break;
        case BMF_8BPP:
        default:
            return 0;
    };

    if (!(surf = EngCreateDeviceBitmap((DHSURF)GetSurfaceInfo(pdev, surface_id), size, format))) {
        DEBUG_PRINT((NULL, 0, "%s: create device surface failed, 0x%lx\n",
                     __FUNCTION__, pdev));
        goto out_error1;
    }

    if (!EngAssociateSurface((HSURF)surf, pdev->eng,  HOOK_SYNCHRONIZE | HOOK_COPYBITS |
                             HOOK_BITBLT | HOOK_TEXTOUT | HOOK_STROKEPATH | HOOK_STRETCHBLT |
                             HOOK_STRETCHBLTROP | HOOK_TRANSPARENTBLT | HOOK_ALPHABLEND
#ifdef CALL_TEST
                             | HOOK_PLGBLT | HOOK_FILLPATH | HOOK_STROKEANDFILLPATH | HOOK_LINETO |
                             HOOK_GRADIENTFILL 
#endif
                             )) {
        DEBUG_PRINT((pdev, 0, "%s: EngAssociateSurface failed\n", __FUNCTION__));
        goto out_error2;
    }

    GetSurfaceInfo(pdev, surface_id)->pdev = pdev;

    QXLGetSurface(pdev, phys_mem, size.cx, size.cy, depth,
                  &stride, base_mem, allocation_type);
    if (!*base_mem) {
        goto out_error2;
    }

    if (!CreateDrawArea(pdev, *base_mem, format, size.cx, size.cy, stride, surface_id)) {
        goto out_error3;
    }

    if (allocation_type != DEVICE_BITMAP_ALLOCATION_TYPE_SURF0) {
        QXLSurfaceCmd *surface;

        surface = SurfaceCmd(pdev, QXL_SURFACE_CMD_CREATE, surface_id);
        surface->u.surface_create.format = surface_format;
        surface->u.surface_create.width = size.cx;
        surface->u.surface_create.height = size.cy;
        surface->u.surface_create.stride = -(INT32)stride;
        surface->u.surface_create.data = *phys_mem;
        PushSurfaceCmd(pdev, surface);
    }

    return surf;

out_error3:
    QXLDelSurface(pdev, *base_mem, allocation_type);
out_error2:
    FreeSurface(pdev, surface_id);
    EngDeleteSurface((HSURF)surf);
out_error1:
    return 0;
}

VOID DeleteDeviceBitmap(PDev *pdev, UINT32 surface_id, UINT8 allocation_type)
{
    DrawArea *drawarea;

    drawarea = &GetSurfaceInfo(pdev,surface_id)->draw_area;

    FreeDrawArea(drawarea);

    if (allocation_type != DEVICE_BITMAP_ALLOCATION_TYPE_SURF0 &&
        pdev->Res.surfaces_used[surface_id]) {
        QXLSurfaceCmd *surface;

        surface = SurfaceCmd(pdev, QXL_SURFACE_CMD_DESTROY, surface_id);
        QXLGetDelSurface(pdev, surface, surface_id, allocation_type);
        PushSurfaceCmd(pdev, surface);
    }
}