[KERNEL]: Replaced magic numbers with constants and cleaned code
[kolibrios.git] / kernel / trunk / gui / background.inc
blobed866fd1ce10b5eac185306736f10a38918cac85
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;                                                              ;;
3 ;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;;
4 ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa      ;;
5 ;; Distributed under terms of the GNU General Public License    ;;
6 ;;                                                              ;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 $Revision$
11 ; Background graphics implementation
13 uglobal
14 ;  bgrchanged  dd  0x0
15 align 4
16 bgrlockpid dd 0
17 bgrlock db 0
18 endg
19 ;--------------------------------------
20 align 4
21 sys_background:
22         cmp     ebx, 1                     ; BACKGROUND SIZE
23         jnz     .nosb1
24         test    ecx, ecx
25         jz      .sbgrr
27         test    edx, edx
28         jz      .sbgrr
29 ;--------------------------------------
30 align 4
31 @@:
32 ;;Maxis use atomic bts for mutexes  4.4.2009
33         bts     dword [bgrlock], 0
34         jnc     @f
35         call    change_task
36         jmp     @b
37 ;--------------------------------------
38 align 4
39 @@:
40         mov     [BgrDataWidth], ecx
41         mov     [BgrDataHeight], edx
42 ;    mov   [bgrchanged],1
44         pushad
45 ; return memory for old background
46         mov     eax, [img_background]
47         cmp     eax, static_background_data
48         jz      @f
49         stdcall kernel_free, eax
50 ;--------------------------------------
51 align 4
52 @@:
53 ; calculate RAW size
54         xor     eax, eax
55         inc     eax
56         cmp     [BgrDataWidth], eax
57         jae     @f
58         mov     [BgrDataWidth], eax
59 ;--------------------------------------
60 align 4
61 @@:
62         cmp     [BgrDataHeight], eax
63         jae     @f
64         mov     [BgrDataHeight], eax
65 ;--------------------------------------
66 align 4
67 @@:
68         mov     eax, [BgrDataWidth]
69         imul    eax, [BgrDataHeight]
70         lea     eax, [eax*3]
71 ; it is reserved with aligned to the boundary of 4 KB pages,
72 ; otherwise there may be exceptions a page fault for vesa20_drawbackground_tiled
73 ; because the 32 bit read is used for  high performance: "mov eax,[esi]"
74         shr     eax, 12
75         inc     eax
76         shl     eax, 12
77         mov     [mem_BACKGROUND], eax
78 ; get memory for new background
79         stdcall kernel_alloc, eax
80         test    eax, eax
81         jz      .memfailed
82         mov     [img_background], eax
83         jmp     .exit
84 ;--------------------------------------
85 align 4
86 .memfailed:
87 ; revert to static monotone data
88         mov     [img_background], static_background_data
89         xor     eax, eax
90         inc     eax
91         mov     [BgrDataWidth], eax
92         mov     [BgrDataHeight], eax
93         mov     [mem_BACKGROUND], 4
94 ;--------------------------------------
95 align 4
96 .exit:
97         popad
98         mov     [bgrlock], 0
99 ;--------------------------------------
100 align 4
101 .sbgrr:
102         ret
103 ;--------------------------------------
104 align 4
105 .nosb1:
106         cmp     ebx, 2                     ; SET PIXEL
107         jnz     .nosb2
109         mov     eax, [img_background]
110         test    ecx, ecx
111         jz      @f
112         cmp     eax, static_background_data
113         jz      .ret
114 ;--------------------------------------
115 align 4
117         mov     ebx, [mem_BACKGROUND]
118         add     ebx, PAGE_SIZE-1
119         and     ebx, -PAGE_SIZE
120         sub     ebx, 4
121         cmp     ecx, ebx
122         ja      .ret
124         mov     ebx, [eax+ecx]
125         and     ebx, 0xFF000000;255*256*256*256
126         and     edx, 0x00FFFFFF;255*256*256+255*256+255
127         add     edx, ebx
128         mov     [eax+ecx], edx
129 ;--------------------------------------
130 align 4
131 .ret:
132         ret
133 ;--------------------------------------
134 align 4
135 .nosb2:
136         cmp     ebx, 3                     ; DRAW BACKGROUND
137         jnz     nosb3
138 ;--------------------------------------
139 align 4
140 draw_background_temp:
141         mov     [background_defined], 1
142         call    force_redraw_background
143 ;--------------------------------------
144 align 4
145 nosb31:
146         ret
147 ;--------------------------------------
148 align 4
149 nosb3:
150         cmp     ebx, 4                     ; TILED / STRETCHED
151         jnz     nosb4
152         cmp     ecx, [BgrDrawMode]
153         je      nosb41
154         mov     [BgrDrawMode], ecx
155 ;--------------------------------------
156 align 4
157 nosb41:
158         ret
159 ;--------------------------------------
160 align 4
161 nosb4:
162         cmp     ebx, 5                     ; BLOCK MOVE TO BGR
163         jnz     nosb5
165 ; add check pointer
166         stdcall is_region_userspace, ecx, esi
167         jnz     .fin
169         cmp     [img_background], static_background_data
170         jnz     @f
171         test    edx, edx
172         jnz     .fin
173         cmp     esi, 4
174         ja      .fin
175 ;--------------------------------------
176 align 4
178   ; bughere
179         mov     eax, ecx
180         mov     ebx, edx
181         add     ebx, [img_background];IMG_BACKGROUND
182         mov     ecx, esi
183         call    memmove
184 ;--------------------------------------
185 align 4
186 .fin:
187         ret
188 ;---------------------------------------
189 align 4
190 nosb5:
191         cmp     ebx, 6
192         jnz     nosb6
193 ;--------------------------------------
194 align 4
195 ;;Maxis use atomic bts for mutex 4.4.2009
197         bts     dword [bgrlock], 0
198         jnc     @f
199         call    change_task
200         jmp     @b
201 ;--------------------------------------
202 align 4
204         mov     eax, [current_slot_idx]
205         mov     [bgrlockpid], eax
206         cmp     [img_background], static_background_data
207         jz      .nomem
208         stdcall user_alloc, [mem_BACKGROUND]
209         mov     [esp + SYSCALL_STACK.eax], eax
210         test    eax, eax
211         jz      .nomem
212         mov     ebx, eax
213         shr     ebx, 12
214         or      dword [page_tabs+(ebx-1)*4], MEM_BLOCK_DONT_FREE
215         mov     esi, [img_background]
216         shr     esi, 12
217         mov     ecx, [mem_BACKGROUND]
218         add     ecx, 0xFFF
219         shr     ecx, 12
220 ;--------------------------------------
221 align 4
223         mov     eax, [page_tabs+ebx*4]
224         test    al, 1
225         jz      @f
226         call    free_page
227 ;--------------------------------------
228 align 4
230         mov     eax, [page_tabs+esi*4]
231         or      al, PG_UWR
232         mov     [page_tabs+ebx*4], eax
233         mov     eax, ebx
234         shl     eax, 12
235         invlpg  [eax]
236         inc     ebx
237         inc     esi
238         loop    .z
239         ret
240 ;--------------------------------------
241 align 4
242 .nomem:
243         and     [bgrlockpid], 0
244         mov     [bgrlock], 0
245 ;--------------------------------------
246 align 4
247 nosb6:
248         cmp     ebx, 7
249         jnz     nosb7
250         cmp     [bgrlock], 0
251         jz      .err
252         mov     eax, [current_slot_idx]
253         cmp     [bgrlockpid], eax
254         jnz     .err
255         mov     eax, ecx
256         mov     ebx, ecx
257         shr     eax, 12
258         mov     ecx, [page_tabs+(eax-1)*4]
259         test    cl, MEM_BLOCK_USED or MEM_BLOCK_DONT_FREE
260         jz      .err
261         jnp     .err
262         push    eax
263         shr     ecx, 12
264         dec     ecx
265 ;--------------------------------------
266 align 4
268         and     dword [page_tabs+eax*4], 0
269         mov     edx, eax
270         shl     edx, 12
271         push    eax
272         invlpg  [edx]
273         pop     eax
274         inc     eax
275         loop    @b
276         pop     eax
277         and     dword [page_tabs+(eax-1)*4], not MEM_BLOCK_DONT_FREE
278         stdcall user_free, ebx
279         mov     [esp + SYSCALL_STACK.eax], eax
280         and     [bgrlockpid], 0
281         mov     [bgrlock], 0
282         ret
283 ;--------------------------------------
284 align 4
285 .err:
286         and     dword [esp + SYSCALL_STACK.eax], 0
287         ret
288 ;-------------------------------------
289 align 4
290 nosb7:
291         cmp     ebx, 8
292         jnz     nosb8
294         mov     ecx, [current_slot]
295         xor     eax, eax
296         xchg    eax, [ecx + APPDATA.draw_bgr_x]
297         mov     [esp + SYSCALL_STACK.eax], eax ; eax = [left]*65536 + [right]
298         xor     eax, eax
299         xchg    eax, [ecx + APPDATA.draw_bgr_y]
300         mov     [esp + SYSCALL_STACK.ebx], eax ; ebx = [top]*65536 + [bottom]
301         ret
302 ;---------------------------------------
303 align 4
304 nosb8:
305         cmp     ebx, 9
306         jnz     .exit
307 ; ecx = [left]*65536 + [right]
308 ; edx = [top]*65536 + [bottom]
309         mov     eax, [_display.width]
310         mov     ebx, [_display.height]
311 ; check [right]
312         cmp     cx, ax
313         jae     .exit
314 ; check [left]
315         ror     ecx, 16
316         cmp     cx, ax
317         jae     .exit
318 ; check [bottom]
319         cmp     dx, bx
320         jae     .exit
321 ; check [top]
322         ror     edx, 16
323         cmp     dx, bx
324         jae     .exit
326         movzx   eax, cx  ; [left]
327         movzx   ebx, dx  ; [top]
329         shr     ecx, 16 ; [right]
330         shr     edx, 16 ; [bottom]
332         mov     [background_defined], 1
334         mov     [draw_data + sizeof.RECT + RECT.left], eax
335         mov     [draw_data + sizeof.RECT + RECT.top], ebx
337         mov     [draw_data + sizeof.RECT + RECT.right], ecx
338         mov     [draw_data + sizeof.RECT + RECT.bottom], edx
340         inc     [REDRAW_BACKGROUND]
341         call    wakeup_osloop
342 ;--------------------------------------
343 align 4
344 .exit:
345         ret
346 ;------------------------------------------------------------------------------
349 align 4
350 uglobal
351   BG_Rect_X_left_right  dd   0x0
352   BG_Rect_Y_top_bottom  dd   0x0
353 endg
354 ;------------------------------------------------------------------------------
357 align 4
358 force_redraw_background:
359         and     [draw_data + sizeof.RECT + RECT.left], 0
360         and     [draw_data + sizeof.RECT + RECT.top], 0
361         push    eax ebx
362         mov     eax, [_display.width]
363         mov     ebx, [_display.height]
364         dec     eax
365         dec     ebx
366         mov     [draw_data + sizeof.RECT + RECT.right], eax
367         mov     [draw_data + sizeof.RECT + RECT.bottom], ebx
368         pop     ebx eax
369         inc     [REDRAW_BACKGROUND]
370         call    wakeup_osloop
371         ret
372 ;------------------------------------------------------------------------------
375 align 4
376 sys_getbackground: ; sysfn 39
377         dec     ebx
378         jnz     .nogb1
379         ; sysfn 39.1:
380         mov     eax, [BgrDataWidth]
381         shl     eax, 16
382         mov     ax, word [BgrDataHeight]
383         mov     [esp + SYSCALL_STACK.eax], eax
384         ret
385 ;---------------------------------------
386 align 4
387 .nogb1:
388         dec     ebx
389         jnz     .nogb2
390         ; sysfn 39.2:
391         mov     eax, [img_background]
392         test    ecx, ecx
393         jz      @f
394         cmp     eax, static_background_data
395         jz      .ret
396 align 4
398         mov     ebx, [mem_BACKGROUND]
399         add     ebx, PAGE_SIZE-1
400         and     ebx, -PAGE_SIZE
401         sub     ebx, 4
402         cmp     ecx, ebx
403         ja      .ret
405         mov     eax, [ecx+eax]
407         and     eax, 0xFFFFFF
408         mov     [esp + SYSCALL_STACK.eax], eax
409 align 4
410 .ret:
411         ret
412 ;------------------------------------------------------------------------------
413 align 4
414         .x      dd ?
415         .y      dd ?
416         .w      dd ?
417         .h      dd ?
418         .subrect_startptr dd ?
419         .subrect_bytes dd ?
420 align 4
421 .nogb2:
422         dec     ebx
423         jnz     .nogb3
424         ; sysfn 39.3 read background subrect to buffer
425         ; ecx - <x, y>
426         ; edx - <w, h>
427         ; esi - buffer of 0x00RRGGBB
428         mov     eax, [img_background]
429         cmp     eax, static_background_data
430         jz      .ret_39_3
431 align 4
433         movzx   eax, cx ; store y in eax
434         mov     [.y], eax
436         shr     ecx, 16 ; ecx = x
437         mov     [.x], ecx
439         imul    eax, [BgrDataWidth]
440         add     eax, ecx
441         imul    eax, 3
442         mov     [.subrect_startptr], eax
444         movzx   eax, dx ; store h in eax
445         mov     [.h], eax
447         shr     edx, 16 ; edx = w
448         mov     [.w], edx
450         imul    eax, edx
451         mov     [.subrect_bytes], eax
453         ; check bounds
454         mov     ebx, [mem_BACKGROUND]
455         add     ebx, PAGE_SIZE-1
456         and     ebx, -PAGE_SIZE
457         sub     ebx, 4
458         add     eax, [.subrect_startptr]
459         cmp     eax, ebx
460         ja      .fail_39_3
462         ; copy contents
463         mov     edi, [img_background]
464         xor     ecx, ecx ; ecx - row index
465 .copy_rect:
466         cmp     ecx, [.h]
467         jae     .end_copy_rect
468         
469         xor     edx, edx ; edx - column index
470 .copy_row:
471         cmp     edx, [.w]
472         jae     .end_copy_row
474         mov     ebx, ecx
475         imul    ebx, [BgrDataWidth]
476         add     ebx, edx
477         imul    ebx, 3
478         add     ebx, [.subrect_startptr]
479         mov     eax, [edi + ebx]
480         mov     ebx, ecx
481         imul    ebx, [.w]
482         add     ebx, edx
483         and     eax, 0xFFFFFF
484         mov     [esi + ebx*4], eax       
486         inc     edx
487         jmp     .copy_row
488 .end_copy_row:        
490         inc     ecx
491         jmp     .copy_rect
492 .end_copy_rect:
493         xor     eax, eax
494         mov     [esp + SYSCALL_STACK.eax], eax
495 ;--------------------------------------
496 align 4
497 .fail_39_3:
498         mov     eax, -1
499 align 4
500 .ret_39_3:
501         ret
502 ;--------------------------------------
503 align 4
504 .nogb3:
505         dec     ebx
506         jnz     .nogb4
507         ; sysfn 39.4:
508         mov     eax, [BgrDrawMode]
509 ;--------------------------------------
510 align 4
511 .nogb4:
512         mov     [esp + SYSCALL_STACK.eax], eax
513         ret
514 ;-----------------------------------------------------------------------------
517 align 4
518 syscall_putarea_backgr: ; sysfn 25
519 ;eax = 25
520 ;ebx = pointer to bufer for img BBGGRRBBGGRR...
521 ;ecx = [size x]*65536 + [size y]
522 ;edx = [start x]*65536 + [start y]
523         pushad
524         mov     edi, ebx
525         mov     eax, edx
526         shr     eax, 16
527         mov     ebx, edx
528         and     ebx, 0xffff
529         dec     eax
530         dec     ebx
531 ; eax - x, ebx - y
532         mov     edx, ecx
533         shr     ecx, 16
534         and     edx, 0xffff
535         mov     esi, ecx
536 ; ecx - size x, edx - size y
537         mov     ebp, edx
538         
539         lea     ebp, [ebp*4]
540         imul    ebp, esi
541         stdcall is_region_userspace, edi, ebp
542         jnz     .exit
544         mov     ebp, edx
546         dec     ebp
547         shl     ebp, 2
549         imul    ebp, esi
551         mov     esi, ecx
552         dec     esi
553         shl     esi, 2
555         add     ebp, esi
556         add     ebp, edi
558         add     ebx, edx
559 ;--------------------------------------
560 align 4
561 .start_y:
562         push    ecx edx
563 ;--------------------------------------
564 align 4
565 .start_x:
566         push    eax ecx
567         add     eax, ecx
569         mov     ecx, [ebp]
570         rol     ecx, 8
571         test    cl, cl        ; transparensy = 0
572         jz      .no_put
574         xor     cl, cl
575         ror     ecx, 8
577         pushad
578         mov     edx, [d_width_calc_area + ebx*4]
579         add     edx, [_display.win_map]
580         movzx   edx, byte [eax+edx]
581         cmp     dl, byte 1
582         jne     @f
584         call    dword [PUTPIXEL]; eax - x, ebx - y
585 ;--------------------------------------
586 align 4
588         popad
589 ;--------------------------------------
590 align 4
591 .no_put:
592         pop     ecx eax
594         sub     ebp, 4
595         dec     ecx
596         jnz     .start_x
598         pop     edx ecx
599         dec     ebx
600         dec     edx
601         jnz     .start_y
603 .exit:
604         popad
605         ret