Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / mga / mgatexmem.c
1 /*
2  * Copyright 2000-2001 VA Linux Systems, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Keith Whitwell <keith@tungstengraphics.com>
26  */
27
28 #include "main/glheader.h"
29
30 #include "main/mm.h"
31 #include "mgacontext.h"
32 #include "mgatex.h"
33 #include "mgaregs.h"
34 #include "mgaioctl.h"
35 #include "mga_xmesa.h"
36
37 #include "main/imports.h"
38 #include "main/simple_list.h"
39
40 /**
41  * Destroy any device-dependent state associated with the texture.  This may
42  * include NULLing out hardware state that points to the texture.
43  */
44 void
45 mgaDestroyTexObj( mgaContextPtr mmesa, mgaTextureObjectPtr t )
46 {
47     unsigned   i;
48
49
50     /* See if it was the driver's current object.
51      */
52
53     if ( mmesa != NULL )
54     { 
55         if ( t->age > mmesa->dirtyAge )
56             mmesa->dirtyAge = t->age;
57
58         for ( i = 0 ; i < mmesa->glCtx->Const.MaxTextureUnits ; i++ )
59         {
60             if ( t == mmesa->CurrentTexObj[ i ] ) {
61                 mmesa->CurrentTexObj[ i ] = NULL;
62             }
63         }
64     }
65 }
66
67
68 /**
69  * Upload a texture image from system memory to either on-card or AGP
70  * memory.  Uploads to on-card memory are performed using an ILOAD operation.
71  * This is used for both initial loading of the entire image, and texSubImage
72  * updates.
73  *
74  * Performed with the hardware lock held.
75  * 
76  * Even though this function is named "upload subimage," the entire image
77  * is uploaded.
78  * 
79  * \param mmesa  Driver context.
80  * \param t      Texture to be uploaded.
81  * \param hwlevel  Mipmap level of the texture to be uploaded.
82  * 
83  * \bug As mentioned above, this fuction actually copies the entier mipmap
84  *      level.  There should be a version of this function that performs
85  *      sub-rectangle uploads.  This will perform quite a bit better if only
86  *      a small portion of a larger texture has been updated.  Care would
87  *      need to be take with such an implementation once glCopyTexImage has
88  *      been hardware accelerated.
89  */
90 static void mgaUploadSubImage( mgaContextPtr mmesa,
91                                mgaTextureObjectPtr t, GLint hwlevel )
92 {
93    struct gl_texture_image * texImage;
94    unsigned     offset;
95    unsigned     texelBytes;
96    unsigned     length;
97    const int level = hwlevel + t->base.firstLevel;
98
99
100    if ( (hwlevel < 0) 
101         || (hwlevel >= (MGA_IS_G200(mmesa) 
102                       ? G200_TEX_MAXLEVELS : G400_TEX_MAXLEVELS)) ) {
103       fprintf( stderr, "[%s:%d] level = %d\n", __FILE__, __LINE__, level );
104       return;
105    }
106
107    texImage = t->base.tObj->Image[0][level];
108    if ( texImage == NULL ) {
109       fprintf( stderr, "[%s:%d] Image[%d] = NULL\n", __FILE__, __LINE__,
110                level );
111       return;
112    }
113
114
115    if (texImage->Data == NULL) {
116       fprintf(stderr, "null texture image data tObj %p level %d\n",
117               (void *) t->base.tObj, level);
118       return;
119    }
120
121
122    /* find the proper destination offset for this level */
123    if ( MGA_IS_G200(mmesa) ) {
124       offset = (t->base.memBlock->ofs + t->offsets[hwlevel]);
125    }
126    else {
127       unsigned  i;
128
129       offset = t->base.memBlock->ofs;
130       for ( i = 0 ; i < hwlevel ; i++ ) {
131          offset += (t->offsets[1] >> (i * 2));
132       }
133    }
134
135
136    /* Copy the texture from system memory to a memory space that can be
137     * directly used by the hardware for texturing.
138     */
139
140    texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
141    length = texImage->Width * texImage->Height * texelBytes;
142    if ( t->base.heap->heapId == MGA_CARD_HEAP ) {
143       unsigned  tex_offset = 0;
144       unsigned  to_copy;
145
146
147       /* We may not be able to upload the entire texture in one batch due to
148        * register limits or dma buffer limits.  Split the copy up into maximum
149        * sized chunks.
150        */
151
152       offset += mmesa->mgaScreen->textureOffset[ t->base.heap->heapId ];
153       while ( length != 0 ) {
154          mgaGetILoadBufferLocked( mmesa );
155
156          /* The kernel ILOAD ioctl requires that the lenght be an even multiple
157           * of MGA_ILOAD_ALIGN.
158           */
159          length = ((length) + MGA_ILOAD_MASK) & ~MGA_ILOAD_MASK;
160
161          to_copy = MIN2( length, MGA_BUFFER_SIZE );
162          (void) memcpy( mmesa->iload_buffer->address,
163                         (GLubyte *) texImage->Data + tex_offset, to_copy );
164
165          if ( MGA_DEBUG & DEBUG_VERBOSE_TEXTURE )
166              fprintf(stderr, "[%s:%d] address/size = 0x%08lx/%d\n",
167                      __FILE__, __LINE__,
168                      (long) (offset + tex_offset),
169                      to_copy );
170
171          mgaFireILoadLocked( mmesa, offset + tex_offset, to_copy );
172          tex_offset += to_copy;
173          length -= to_copy;
174       }
175    } else {
176       /* FIXME: the sync for direct copy reduces speed.. */
177       /* This works, is slower for uploads to card space and needs
178        * additional synchronization with the dma stream.
179        */
180        
181       UPDATE_LOCK(mmesa, DRM_LOCK_FLUSH | DRM_LOCK_QUIESCENT);
182
183       memcpy( mmesa->mgaScreen->texVirtual[t->base.heap->heapId] + offset,
184               texImage->Data, length );
185
186       if ( MGA_DEBUG & DEBUG_VERBOSE_TEXTURE )
187          fprintf(stderr, "[%s:%d] address/size = 0x%08lx/%d\n",
188                  __FILE__, __LINE__,
189                  (long) (mmesa->mgaScreen->texVirtual[t->base.heap->heapId] 
190                          + offset),
191                  length);
192    }
193 }
194
195
196 /**
197  * Upload the texture images associated with texture \a t.  This might
198  * require the allocation of texture memory.
199  * 
200  * \param mmesa Context pointer
201  * \param t Texture to be uploaded
202  */
203
204 int mgaUploadTexImages( mgaContextPtr mmesa, mgaTextureObjectPtr t )
205 {
206    int i;
207    int ofs;
208
209
210    if ( (t == NULL) || (t->base.totalSize == 0) )
211       return 0;
212
213    LOCK_HARDWARE( mmesa );
214
215    if (t->base.memBlock == NULL ) {
216       int heap;
217
218       heap = driAllocateTexture( mmesa->texture_heaps, mmesa->nr_heaps,
219                                  (driTextureObject *) t );
220       if ( heap == -1 ) {
221          UNLOCK_HARDWARE( mmesa );
222          return -1;
223       }
224
225       assert(t->base.memBlock);
226       ofs = mmesa->mgaScreen->textureOffset[ heap ]
227            + t->base.memBlock->ofs;
228
229       if ( MGA_IS_G200(mmesa) ) {
230          t->setup.texorg  = ofs;
231          t->setup.texorg1 = ofs + t->offsets[1];
232          t->setup.texorg2 = ofs + t->offsets[2];
233          t->setup.texorg3 = ofs + t->offsets[3];
234          t->setup.texorg4 = ofs + t->offsets[4];
235       }
236       else {
237          t->setup.texorg  = ofs | TO_texorgoffsetsel;
238          t->setup.texorg1 = t->offsets[1];
239          t->setup.texorg2 = 0;
240          t->setup.texorg3 = 0;
241          t->setup.texorg4 = 0;
242       }
243
244       mmesa->dirty |= MGA_UPLOAD_CONTEXT;
245    }
246
247    /* Let the world know we've used this memory recently.
248     */
249    driUpdateTextureLRU( (driTextureObject *) t );
250
251    if (MGA_DEBUG&DEBUG_VERBOSE_TEXTURE)
252       fprintf(stderr, "[%s:%d] dispatch age: %d age freed memory: %d\n",
253               __FILE__, __LINE__,
254               GET_DISPATCH_AGE(mmesa), mmesa->dirtyAge);
255
256    if (mmesa->dirtyAge >= GET_DISPATCH_AGE(mmesa))
257       mgaWaitAgeLocked( mmesa, mmesa->dirtyAge );
258
259    if (t->base.dirty_images[0]) {
260       const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;
261
262       if (MGA_DEBUG&DEBUG_VERBOSE_TEXTURE)
263          fprintf(stderr, "[%s:%d] dirty_images[0] = 0x%04x\n",
264                  __FILE__, __LINE__, t->base.dirty_images[0] );
265
266       for (i = 0 ; i < numLevels ; i++) {
267          if ( (t->base.dirty_images[0] & (1U << i)) != 0 ) {
268             mgaUploadSubImage( mmesa, t, i );
269          }
270       }
271       t->base.dirty_images[0] = 0;
272    }
273
274
275    UNLOCK_HARDWARE( mmesa );
276
277    return 0;
278 }