summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/sis/sis_alloc.c
blob: ce34e44da22fd302863ab976b38a9fc1fb17c88e (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
/**************************************************************************

Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
Copyright 2003 Eric Anholt
All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/*
 * Authors:
 *   Sung-Ching Lin <sclin@sis.com.tw>
 *   Eric Anholt <anholt@FreeBSD.org>
 */

#include "sis_context.h"
#include "sis_alloc.h"

#include "sis_common.h"

#include <unistd.h>

#define Z_BUFFER_HW_ALIGNMENT 16
#define Z_BUFFER_HW_PLUS (16 + 4)

/* 3D engine uses 2, and bitblt uses 4 */
#define DRAW_BUFFER_HW_ALIGNMENT 16
#define DRAW_BUFFER_HW_PLUS (16 + 4)

#define ALIGNMENT(value, align) (((value) + (align) - 1) / (align) * (align))

static int _total_video_memory_used = 0;
static int _total_video_memory_count = 0;

void *
sisAllocFB( sisContextPtr smesa, GLuint size, void **handle )
{
   drm_sis_mem_t fb;

   _total_video_memory_used += size;

   fb.context = smesa->hHWContext;
   fb.size = size;
   if (drmCommandWriteRead( smesa->driFd, DRM_SIS_FB_ALLOC, &fb, 
      sizeof(drm_sis_mem_t) ) || fb.offset == 0)
   {
      return NULL;
   }
   *handle = (void *)fb.free;

   if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
      fprintf(stderr, "sisAllocFB: size=%d, offset=%lu, pid=%d, count=%d\n", 
              size, fb.offset, (GLint)getpid(), 
              ++_total_video_memory_count);
   }

   return (void *)(smesa->FbBase + fb.offset);
}

void
sisFreeFB( sisContextPtr smesa, void *handle )
{
   drm_sis_mem_t fb;

   if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
      fprintf(stderr, "sisFreeFB: free=%p, pid=%d, count=%d\n", 
              handle, (GLint)getpid(), --_total_video_memory_count);
   }

   fb.context = smesa->hHWContext;
   fb.free = handle;
   drmCommandWrite( smesa->driFd, DRM_SIS_FB_FREE, &fb, sizeof(drm_sis_mem_t) );
}

void *
sisAllocAGP( sisContextPtr smesa, GLuint size, void **handle )
{
   drm_sis_mem_t agp;
   
   if (smesa->AGPSize == 0)
      return NULL;

   agp.context = smesa->hHWContext;
   agp.size = size;
   if (drmCommandWriteRead( smesa->driFd, DRM_SIS_AGP_ALLOC, &agp,
      sizeof(drm_sis_mem_t) ) || agp.offset == 0)
   {
      return NULL;
   }
   *handle = (void *)agp.free;

   if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
      fprintf(stderr, "sisAllocAGP: size=%u, offset=%lu, pid=%d, count=%d\n", 
              size, agp.offset, (GLint)getpid(), 
              ++_total_video_memory_count);
   }

   return (void *)(smesa->AGPBase + agp.offset);
}

void
sisFreeAGP( sisContextPtr smesa, void *handle )
{
   drm_sis_mem_t agp;

   if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
      fprintf(stderr, "sisFreeAGP: free=%p, pid=%d, count=%d\n", 
              handle, (GLint)getpid(), --_total_video_memory_count);
   }
  
   agp.context = smesa->hHWContext;
   agp.free = handle;
   drmCommandWrite( smesa->driFd, DRM_SIS_AGP_FREE, &agp,
      sizeof(drm_sis_mem_t) );
}

void
sisAllocZStencilBuffer( sisContextPtr smesa )
{
   int cpp = ( smesa->glCtx->Visual.depthBits +
               smesa->glCtx->Visual.stencilBits ) / 8;
   char *addr;

   smesa->depth.bpp = cpp * 8;
   smesa->depth.pitch = ALIGNMENT(smesa->driDrawable->w * cpp, 4);
   smesa->depth.size = smesa->depth.pitch * smesa->driDrawable->h;
   smesa->depth.size += Z_BUFFER_HW_PLUS;

   addr = sisAllocFB(smesa, smesa->depth.size, &smesa->depth.handle);
   if (addr == NULL)
      sis_fatal_error("Failure to allocate Z buffer.\n");
   addr = (char *)ALIGNMENT((unsigned long)addr, Z_BUFFER_HW_ALIGNMENT);

   smesa->depth.map = addr;
   smesa->depth.offset = addr - (char *)smesa->FbBase;

   /* stencil buffer is same as depth buffer */
   smesa->stencil.size = smesa->depth.size;
   smesa->stencil.offset = smesa->depth.offset;
   smesa->stencil.handle = smesa->depth.handle;
   smesa->stencil.pitch = smesa->depth.pitch;
   smesa->stencil.bpp = smesa->depth.bpp;
   smesa->stencil.map = smesa->depth.map;
}

void
sisFreeZStencilBuffer( sisContextPtr smesa )
{
   sisFreeFB(smesa, smesa->depth.handle);
   smesa->depth.map = NULL; 
   smesa->depth.offset = 0; 
}

void
sisAllocBackbuffer( sisContextPtr smesa )
{
   int cpp = smesa->bytesPerPixel;
   char *addr;

   smesa->back.bpp = smesa->bytesPerPixel * 8;
   smesa->back.pitch = ALIGNMENT(smesa->driDrawable->w * cpp, 4);
   smesa->back.size = smesa->back.pitch * smesa->driDrawable->h;
   smesa->back.size += DRAW_BUFFER_HW_PLUS;

   addr = sisAllocFB(smesa, smesa->back.size, &smesa->back.handle);
   if (addr == NULL)
      sis_fatal_error("Failure to allocate back buffer.\n");
   addr = (char *)ALIGNMENT((unsigned long)addr, DRAW_BUFFER_HW_ALIGNMENT);

   smesa->back.map = addr;
   smesa->back.offset = addr - (char *)smesa->FbBase;
}

void
sisFreeBackbuffer( sisContextPtr smesa )
{
   sisFreeFB(smesa, smesa->back.handle);
   smesa->back.map = NULL; 
   smesa->back.offset = 0; 
}