1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Stream implementation for the Windows Display driver.
33 #include "pipe/p_config.h"
35 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
40 #include "util/u_memory.h"
41 #include "util/u_string.h"
46 #define MAP_FILE_SIZE (4*1024*1024)
51 char filename
[MAX_PATH
+ 1];
52 WCHAR wFileName
[MAX_PATH
+ 1];
63 util_stream_map(struct util_stream
*stream
)
65 ULONG BytesInUnicodeString
;
66 static char filename
[MAX_PATH
+ 1];
67 unsigned filename_len
;
70 filename_len
= util_snprintf(filename
,
76 filename_len
= util_snprintf(filename
,
81 EngMultiByteToUnicodeN(
83 sizeof(stream
->wFileName
),
84 &BytesInUnicodeString
,
88 stream
->pMap
= EngMapFile(stream
->wFileName
, stream
->map_size
, &stream
->iFile
);
92 memset(stream
->pMap
, 0, stream
->map_size
);
100 util_stream_unmap(struct util_stream
*stream
)
102 EngUnmapFile(stream
->iFile
);
103 if(stream
->written
< stream
->map_size
) {
104 /* Truncate file size */
105 stream
->pMap
= EngMapFile(stream
->wFileName
, stream
->written
, &stream
->iFile
);
107 EngUnmapFile(stream
->iFile
);
115 util_stream_full_qualified_filename(char *dst
, size_t size
, const char *src
)
117 boolean need_drive
, need_root
;
119 if((('A' <= src
[0] && src
[0] <= 'Z') || ('a' <= src
[0] && src
[0] <= 'z')) && src
[1] == ':') {
121 need_root
= src
[2] == '\\' ? FALSE
: TRUE
;
125 need_root
= src
[0] == '\\' ? FALSE
: TRUE
;
128 util_snprintf(dst
, size
,
130 need_drive
? "C:" : "",
131 need_root
? "\\" : "",
137 util_stream_create(const char *filename
, size_t max_size
)
139 struct util_stream
*stream
;
141 stream
= CALLOC_STRUCT(util_stream
);
145 util_stream_full_qualified_filename(stream
->filename
,
146 sizeof(stream
->filename
),
150 stream
->growable
= FALSE
;
151 stream
->map_size
= max_size
;
154 stream
->growable
= TRUE
;
155 stream
->map_size
= MAP_FILE_SIZE
;
158 if(!util_stream_map(stream
))
171 util_stream_copy(struct util_stream
*stream
, const char *data
, size_t size
)
173 assert(stream
->written
+ size
<= stream
->map_size
);
174 memcpy(stream
->pMap
+ stream
->written
, data
, size
);
175 stream
->written
+= size
;
180 util_stream_write(struct util_stream
*stream
, const void *data
, size_t size
)
188 while(stream
->written
+ size
> stream
->map_size
) {
189 size_t step
= stream
->map_size
- stream
->written
;
190 util_stream_copy(stream
, data
, step
);
191 data
= (const char *)data
+ step
;
194 util_stream_unmap(stream
);
195 if(!stream
->growable
|| !util_stream_map(stream
))
199 util_stream_copy(stream
, data
, size
);
206 util_stream_flush(struct util_stream
*stream
)
213 util_stream_close(struct util_stream
*stream
)
218 util_stream_unmap(stream
);