Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / common / texmem.h
1 /*
2  * Copyright 2000-2001 VA Linux Systems, Inc.
3  * (c) Copyright IBM Corporation 2002
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * on the rights to use, copy, modify, merge, publish, distribute, sub
10  * license, and/or sell copies of the Software, and to permit persons to whom
11  * the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23  * USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Ian Romanick <idr@us.ibm.com>
27  *    Keith Whitwell <keithw@tungstengraphics.com>
28  *    Kevin E. Martin <kem@users.sourceforge.net>
29  *    Gareth Hughes <gareth@nvidia.com>
30  */
31
32 /** \file texmem.h
33  * Public interface to the DRI texture memory management routines.
34  * 
35  * \sa texmem.c
36  */
37
38 #ifndef DRI_TEXMEM_H
39 #define DRI_TEXMEM_H
40
41 #include "main/mtypes.h"
42 #include "main/formats.h"
43 #include "main/mm.h"
44 #include "xf86drm.h"
45
46 struct dri_tex_heap;
47 typedef struct dri_tex_heap driTexHeap;
48
49 struct dri_texture_object;
50 typedef struct dri_texture_object driTextureObject;
51
52
53 /**
54  * Base texture object type.  Each driver will extend this type with its own
55  * private data members.
56  */
57
58 struct dri_texture_object {
59         struct dri_texture_object * next;
60         struct dri_texture_object * prev;
61
62         driTexHeap * heap;              /**< Texture heap currently stored in */
63         struct gl_texture_object * tObj;/**< Pointer to Mesa texture object
64                                          * If NULL, this texture object is a
65                                          * "placeholder" object representing
66                                          * texture memory in use by another context.
67                                          * A placeholder should have a heap and a memBlock.
68                                          */
69         struct mem_block *memBlock;     /**< Memory block containing texture */
70
71         unsigned    reserved;           /**< Cannot be swapped out by user contexts.  */
72
73         unsigned    bound;              /**< Bitmask indicating which tex units
74                                          * this texture object is bound to.
75                                          * Bit 0 = unit 0, Bit 1 = unit 1, etc
76                                          */
77
78         unsigned    totalSize;          /**< Total size of the texture,
79                                          * including all mipmap levels 
80                                          */
81
82         unsigned    dirty_images[6];    /**< Flags for whether or not images
83                                          * need to be uploaded to local or
84                                          * AGP texture space.  One flag set
85                                          * for each cube face for cubic
86                                          * textures.  Bit zero corresponds to
87                                          * the base-level, which may or may
88                                          * not be the level zero mipmap.
89                                          */
90
91         unsigned    timestamp;          /**< Timestamp used to
92                                          * synchronize with 3d engine
93                                          * in hardware where textures
94                                          * are uploaded directly to
95                                          * the framebuffer.  
96                                          */
97
98         unsigned    firstLevel;         /**< Image in \c tObj->Image[0] that
99                                          * corresponds to the base-level of
100                                          * this texture object.
101                                          */
102
103         unsigned    lastLevel;          /**< Last image in \c tObj->Image[0] 
104                                          * used by the
105                                          * current LOD settings of
106                                          * this texture object.  This
107                                          * value must be greater than
108                                          * or equal to \c firstLevel.
109                                          */
110 };
111
112
113 typedef void (destroy_texture_object_t)( void * driverContext,
114                                         driTextureObject * t );
115
116 /**
117  * Client-private representation of texture memory state.
118  *
119  * Clients will place one or more of these structs in their driver
120  * context struct to manage one or more global texture heaps.
121  */
122
123 struct dri_tex_heap {
124
125         /** Client-supplied heap identifier 
126          */
127         unsigned heapId;        
128
129         /** Pointer to the client's private context 
130          */
131         void *driverContext;
132
133         /** Total size of the heap, in bytes
134          */
135         unsigned size;
136
137         /** \brief \f$log_2\f$ of size of single heap region
138          *
139          * Each context takes memory from the global texture heap in
140          * \f$2^{logGranularity}\f$ byte blocks.  The value of
141          * \a logGranularity is based on the amount of memory represented
142          * by the heap and the maximum number of regions in the SAREA.  Given
143          * \a b bytes of texture memory an \a n regions in the SAREA,
144          * \a logGranularity will be \f$\lfloor\log_2( b / n )\rfloor\f$.
145          */
146         unsigned logGranularity;
147
148         /** \brief Required alignment of allocations in this heap
149          * 
150          * The alignment shift is supplied to \a mmAllocMem when memory is
151          * allocated from this heap.  The value of \a alignmentShift will
152          * typically reflect some require of the hardware.  This value has
153          * \b no \b relation to \a logGranularity.  \a alignmentShift is a
154          * per-context value.
155          *
156          * \sa mmAllocMem
157          */
158         unsigned alignmentShift;
159
160         /** Number of elements in global list (the SAREA).
161          */
162         unsigned nrRegions;      
163
164         /** Pointer to SAREA \a driTexRegion array
165          */
166         drmTextureRegionPtr global_regions;
167
168         /** Pointer to the texture state age (generation number) in the SAREA
169          */
170         unsigned     * global_age;
171
172         /** Local age (generation number) of texture state
173          */
174         unsigned local_age;
175
176         /** Memory heap used to manage texture memory represented by
177          * this texture heap.
178          */
179         struct mem_block * memory_heap;
180
181         /** List of objects that we currently believe to be in texture
182          * memory.
183          */
184         driTextureObject     texture_objects;
185     
186         /** Pointer to the list of texture objects that are not in
187          * texture memory.
188          */
189         driTextureObject   * swapped_objects;
190
191         /** Size of the driver-speicific texture object.
192          */
193         unsigned       texture_object_size;
194
195
196         /**
197          * \brief Function to destroy driver-specific texture object data.
198          * 
199          * This function is supplied by the driver so that the texture manager
200          * can release all resources associated with a texture object.  This
201          * function should only release driver-specific data.  That is,
202          * \a driDestroyTextureObject will release the texture memory
203          * associated with the texture object, it will release the memory
204          * for the texture object itself, and it will unlink the texture
205          * object from the texture object lists.
206          *
207          * \param driverContext Pointer to the driver supplied context
208          * \param t Texture object that is to be destroyed
209          * \sa driDestroyTextureObject
210          */
211
212         destroy_texture_object_t * destroy_texture_object;
213
214
215         /**
216          */
217         unsigned * texture_swaps;
218
219         /**
220          * Timestamp used to synchronize with 3d engine in hardware
221          * where textures are uploaded directly to the
222          * framebuffer.  
223          */
224         unsigned timestamp;
225
226         /** \brief Kick/upload weight
227          *
228          * When not enough free space is available this weight
229          * influences the choice of the heap from which textures are
230          * kicked. By default the weight is equal to the heap size.
231          */
232         double weight;
233
234         /** \brief Kick/upload duty
235          *
236          * The heap with the highest duty will be chosen for kicking
237          * textures if not enough free space is available. The duty is
238          * reduced by the amount of data kicked. Rebalancing of
239          * negative duties takes the weights into account.
240          */
241         int duty;
242 };
243
244
245
246
247 /**
248  * Called by the client on lock contention to determine whether textures have
249  * been stolen.  If another client has modified a region in which we have
250  * textures, then we need to figure out which of our textures have been
251  * removed and update our global LRU.
252  * 
253  * \param heap Texture heap to be updated
254  * \hideinitializer
255  */
256
257 #define DRI_AGE_TEXTURES( heap )                                \
258    do {                                                         \
259        if ( ((heap) != NULL)                                    \
260             && ((heap)->local_age != (heap)->global_age[0]) )   \
261            driAgeTextures( heap );                              \
262    } while( 0 )
263
264
265
266
267 /* This should be called whenever there has been contention on the hardware
268  * lock.  driAgeTextures should not be called directly.  Instead, clients
269  * should use DRI_AGE_TEXTURES, above.
270  */
271
272 void driAgeTextures( driTexHeap * heap );
273
274 void driUpdateTextureLRU( driTextureObject * t );
275 void driSwapOutTextureObject( driTextureObject * t );
276 void driDestroyTextureObject( driTextureObject * t );
277 int driAllocateTexture( driTexHeap * const * heap_array, unsigned nr_heaps,
278     driTextureObject * t );
279
280 GLboolean driIsTextureResident( struct gl_context * ctx, 
281     struct gl_texture_object * texObj );
282
283 driTexHeap * driCreateTextureHeap( unsigned heap_id, void * context,
284     unsigned size, unsigned alignmentShift, unsigned nr_regions,
285     drmTextureRegionPtr global_regions, unsigned * global_age,
286     driTextureObject * swapped_objects, unsigned texture_object_size,
287     destroy_texture_object_t * destroy_tex_obj );
288 void driDestroyTextureHeap( driTexHeap * heap );
289
290 void
291 driCalculateMaxTextureLevels( driTexHeap * const * heaps,
292                               unsigned nr_heaps,
293                               struct gl_constants * limits,
294                               unsigned max_bytes_per_texel,
295                               unsigned max_2D_size,
296                               unsigned max_3D_size,
297                               unsigned max_cube_size,
298                               unsigned max_rect_size,
299                               unsigned mipmaps_at_once,
300                               int all_textures_one_heap,
301                               int allow_larger_textures );
302
303 void
304 driSetTextureSwapCounterLocation( driTexHeap * heap, unsigned * counter );
305
306 #define DRI_TEXMGR_DO_TEXTURE_1D    0x0001
307 #define DRI_TEXMGR_DO_TEXTURE_2D    0x0002
308 #define DRI_TEXMGR_DO_TEXTURE_3D    0x0004
309 #define DRI_TEXMGR_DO_TEXTURE_CUBE  0x0008
310 #define DRI_TEXMGR_DO_TEXTURE_RECT  0x0010
311
312 void driInitTextureObjects( struct gl_context *ctx, driTextureObject * swapped,
313                             GLuint targets );
314
315 GLboolean driValidateTextureHeaps( driTexHeap * const * texture_heaps,
316     unsigned nr_heaps, const driTextureObject * swapped );
317
318 extern void driCalculateTextureFirstLastLevel( driTextureObject * t );
319
320
321 extern gl_format _dri_texformat_rgba8888;
322 extern gl_format _dri_texformat_argb8888;
323 extern gl_format _dri_texformat_rgb565;
324 extern gl_format _dri_texformat_argb4444;
325 extern gl_format _dri_texformat_argb1555;
326 extern gl_format _dri_texformat_al88;
327 extern gl_format _dri_texformat_a8;
328 extern gl_format _dri_texformat_ci8;
329 extern gl_format _dri_texformat_i8;
330 extern gl_format _dri_texformat_l8;
331
332 extern void driInitTextureFormats( void );
333
334 #endif /* DRI_TEXMEM_H */