1 /**************************************************************************
3 Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
4 Copyright 2003 Eric Anholt
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Sung-Ching Lin <sclin@sis.com.tw>
31 * Eric Anholt <anholt@FreeBSD.org>
34 #include "sis_context.h"
35 #include "sis_alloc.h"
37 #include "sis_common.h"
41 #define Z_BUFFER_HW_ALIGNMENT 16
42 #define Z_BUFFER_HW_PLUS (16 + 4)
44 /* 3D engine uses 2, and bitblt uses 4 */
45 #define DRAW_BUFFER_HW_ALIGNMENT 16
46 #define DRAW_BUFFER_HW_PLUS (16 + 4)
48 #define ALIGNMENT(value, align) (((value) + (align) - 1) / (align) * (align))
50 static int _total_video_memory_used
= 0;
51 static int _total_video_memory_count
= 0;
54 sisAllocFB( sisContextPtr smesa
, GLuint size
, void **handle
)
58 _total_video_memory_used
+= size
;
60 fb
.context
= smesa
->hHWContext
;
62 if (drmCommandWriteRead( smesa
->driFd
, DRM_SIS_FB_ALLOC
, &fb
,
63 sizeof(drm_sis_mem_t
) ) || fb
.offset
== 0)
67 *handle
= (void *)fb
.free
;
69 if (SIS_VERBOSE
& VERBOSE_SIS_MEMORY
) {
70 fprintf(stderr
, "sisAllocFB: size=%d, offset=%lu, pid=%d, count=%d\n",
71 size
, fb
.offset
, (GLint
)getpid(),
72 ++_total_video_memory_count
);
75 return (void *)(smesa
->FbBase
+ fb
.offset
);
79 sisFreeFB( sisContextPtr smesa
, void *handle
)
83 if (SIS_VERBOSE
& VERBOSE_SIS_MEMORY
) {
84 fprintf(stderr
, "sisFreeFB: free=%p, pid=%d, count=%d\n",
85 handle
, (GLint
)getpid(), --_total_video_memory_count
);
88 fb
.context
= smesa
->hHWContext
;
90 drmCommandWrite( smesa
->driFd
, DRM_SIS_FB_FREE
, &fb
, sizeof(drm_sis_mem_t
) );
94 sisAllocAGP( sisContextPtr smesa
, GLuint size
, void **handle
)
98 if (smesa
->AGPSize
== 0)
101 agp
.context
= smesa
->hHWContext
;
103 if (drmCommandWriteRead( smesa
->driFd
, DRM_SIS_AGP_ALLOC
, &agp
,
104 sizeof(drm_sis_mem_t
) ) || agp
.offset
== 0)
108 *handle
= (void *)agp
.free
;
110 if (SIS_VERBOSE
& VERBOSE_SIS_MEMORY
) {
111 fprintf(stderr
, "sisAllocAGP: size=%u, offset=%lu, pid=%d, count=%d\n",
112 size
, agp
.offset
, (GLint
)getpid(),
113 ++_total_video_memory_count
);
116 return (void *)(smesa
->AGPBase
+ agp
.offset
);
120 sisFreeAGP( sisContextPtr smesa
, void *handle
)
124 if (SIS_VERBOSE
& VERBOSE_SIS_MEMORY
) {
125 fprintf(stderr
, "sisFreeAGP: free=%p, pid=%d, count=%d\n",
126 handle
, (GLint
)getpid(), --_total_video_memory_count
);
129 agp
.context
= smesa
->hHWContext
;
131 drmCommandWrite( smesa
->driFd
, DRM_SIS_AGP_FREE
, &agp
,
132 sizeof(drm_sis_mem_t
) );
136 sisAllocZStencilBuffer( sisContextPtr smesa
)
138 int cpp
= ( smesa
->glCtx
->Visual
.depthBits
+
139 smesa
->glCtx
->Visual
.stencilBits
) / 8;
142 smesa
->depth
.bpp
= cpp
* 8;
143 smesa
->depth
.pitch
= ALIGNMENT(smesa
->driDrawable
->w
* cpp
, 4);
144 smesa
->depth
.size
= smesa
->depth
.pitch
* smesa
->driDrawable
->h
;
145 smesa
->depth
.size
+= Z_BUFFER_HW_PLUS
;
147 addr
= sisAllocFB(smesa
, smesa
->depth
.size
, &smesa
->depth
.handle
);
149 sis_fatal_error("Failure to allocate Z buffer.\n");
150 addr
= (char *)ALIGNMENT((unsigned long)addr
, Z_BUFFER_HW_ALIGNMENT
);
152 smesa
->depth
.map
= addr
;
153 smesa
->depth
.offset
= addr
- (char *)smesa
->FbBase
;
155 /* stencil buffer is same as depth buffer */
156 smesa
->stencil
.size
= smesa
->depth
.size
;
157 smesa
->stencil
.offset
= smesa
->depth
.offset
;
158 smesa
->stencil
.handle
= smesa
->depth
.handle
;
159 smesa
->stencil
.pitch
= smesa
->depth
.pitch
;
160 smesa
->stencil
.bpp
= smesa
->depth
.bpp
;
161 smesa
->stencil
.map
= smesa
->depth
.map
;
165 sisFreeZStencilBuffer( sisContextPtr smesa
)
167 sisFreeFB(smesa
, smesa
->depth
.handle
);
168 smesa
->depth
.map
= NULL
;
169 smesa
->depth
.offset
= 0;
173 sisAllocBackbuffer( sisContextPtr smesa
)
175 int cpp
= smesa
->bytesPerPixel
;
178 smesa
->back
.bpp
= smesa
->bytesPerPixel
* 8;
179 smesa
->back
.pitch
= ALIGNMENT(smesa
->driDrawable
->w
* cpp
, 4);
180 smesa
->back
.size
= smesa
->back
.pitch
* smesa
->driDrawable
->h
;
181 smesa
->back
.size
+= DRAW_BUFFER_HW_PLUS
;
183 addr
= sisAllocFB(smesa
, smesa
->back
.size
, &smesa
->back
.handle
);
185 sis_fatal_error("Failure to allocate back buffer.\n");
186 addr
= (char *)ALIGNMENT((unsigned long)addr
, DRAW_BUFFER_HW_ALIGNMENT
);
188 smesa
->back
.map
= addr
;
189 smesa
->back
.offset
= addr
- (char *)smesa
->FbBase
;
193 sisFreeBackbuffer( sisContextPtr smesa
)
195 sisFreeFB(smesa
, smesa
->back
.handle
);
196 smesa
->back
.map
= NULL
;
197 smesa
->back
.offset
= 0;