xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/i915/i915_blit.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2003 VMware, Inc.
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
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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 VMWARE 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.
25  *
26  **************************************************************************/
27 
28 #include "i915_blit.h"
29 #include "i915_batch.h"
30 #include "i915_debug.h"
31 #include "i915_reg.h"
32 
33 void
i915_fill_blit(struct i915_context * i915,unsigned cpp,unsigned rgba_mask,unsigned short dst_pitch,struct i915_winsys_buffer * dst_buffer,unsigned dst_offset,short x,short y,short w,short h,unsigned color)34 i915_fill_blit(struct i915_context *i915, unsigned cpp, unsigned rgba_mask,
35                unsigned short dst_pitch, struct i915_winsys_buffer *dst_buffer,
36                unsigned dst_offset, short x, short y, short w, short h,
37                unsigned color)
38 {
39    unsigned BR13, CMD;
40    bool flushed = false;
41 
42    I915_DBG(DBG_BLIT, "%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n", __func__,
43             dst_buffer, dst_pitch, dst_offset, x, y, w, h);
44 
45    switch (cpp) {
46    case 1:
47    case 2:
48    case 3:
49       BR13 = (((int)dst_pitch) & 0xffff) | (0xF0 << 16) | (1 << 24);
50       CMD = XY_COLOR_BLT_CMD;
51       break;
52    case 4:
53       BR13 = (((int)dst_pitch) & 0xffff) | (0xF0 << 16) | (1 << 24) | (1 << 25);
54       CMD = (XY_COLOR_BLT_CMD | rgba_mask);
55       break;
56    default:
57       return;
58    }
59 
60    i915_winsys_batchbuffer_emit_start(i915->batch);
61    while (true) {
62       if (!BEGIN_BATCH(6)) {
63          FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);
64          assert(BEGIN_BATCH(6));
65       }
66       OUT_BATCH(CMD);
67       OUT_BATCH(BR13);
68       OUT_BATCH((y << 16) | x);
69       OUT_BATCH(((y + h) << 16) | (x + w));
70       OUT_RELOC_FENCED(dst_buffer, I915_USAGE_2D_TARGET, dst_offset);
71       OUT_BATCH(color);
72       if (flushed || i915_winsys_validate_buffers(i915->batch, &dst_buffer, 0))
73          break;
74       i915_winsys_batchbuffer_emit_restart(i915->batch);
75       FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);
76       assert(i915_winsys_validate_buffers(i915->batch, &dst_buffer, 0));
77       flushed = true;
78    }
79    i915_set_flush_dirty(i915, I915_FLUSH_CACHE);
80 }
81 
82 void
i915_copy_blit(struct i915_context * i915,unsigned cpp,unsigned short src_pitch,struct i915_winsys_buffer * src_buffer,unsigned src_offset,unsigned short dst_pitch,struct i915_winsys_buffer * dst_buffer,unsigned dst_offset,short src_x,short src_y,short dst_x,short dst_y,short w,short h)83 i915_copy_blit(struct i915_context *i915, unsigned cpp,
84                unsigned short src_pitch, struct i915_winsys_buffer *src_buffer,
85                unsigned src_offset, unsigned short dst_pitch,
86                struct i915_winsys_buffer *dst_buffer, unsigned dst_offset,
87                short src_x, short src_y, short dst_x, short dst_y, short w,
88                short h)
89 {
90    unsigned CMD, BR13;
91    int dst_y2 = dst_y + h;
92    int dst_x2 = dst_x + w;
93    bool flushed = false;
94 
95    I915_DBG(DBG_BLIT,
96             "%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
97             __func__, src_buffer, src_pitch, src_offset, src_x, src_y,
98             dst_buffer, dst_pitch, dst_offset, dst_x, dst_y, w, h);
99 
100    switch (cpp) {
101    case 1:
102    case 2:
103    case 3:
104       BR13 = (((int)dst_pitch) & 0xffff) | (0xCC << 16) | (1 << 24);
105       CMD = XY_SRC_COPY_BLT_CMD;
106       break;
107    case 4:
108       BR13 = (((int)dst_pitch) & 0xffff) | (0xCC << 16) | (1 << 24) | (1 << 25);
109       CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
110              XY_SRC_COPY_BLT_WRITE_RGB);
111       break;
112    default:
113       return;
114    }
115 
116    if (dst_y2 < dst_y || dst_x2 < dst_x) {
117       return;
118    }
119 
120    /* Hardware can handle negative pitches but loses the ability to do
121     * proper overlapping blits in that case.  We don't really have a
122     * need for either at this stage.
123     */
124    assert(dst_pitch > 0 && src_pitch > 0);
125 
126    i915_winsys_batchbuffer_emit_start(i915->batch);
127    while (true) {
128       if (!BEGIN_BATCH(8)) {
129          FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);
130          assert(BEGIN_BATCH(8));
131       }
132       OUT_BATCH(CMD);
133       OUT_BATCH(BR13);
134       OUT_BATCH((dst_y << 16) | dst_x);
135       OUT_BATCH((dst_y2 << 16) | dst_x2);
136       OUT_RELOC_FENCED(dst_buffer, I915_USAGE_2D_TARGET, dst_offset);
137       OUT_BATCH((src_y << 16) | src_x);
138       OUT_BATCH(((int)src_pitch & 0xffff));
139       OUT_RELOC_FENCED(src_buffer, I915_USAGE_2D_SOURCE, src_offset);
140       if (flushed || i915_winsys_validate_buffers(i915->batch, &dst_buffer, 0))
141          break;
142       i915_winsys_batchbuffer_emit_restart(i915->batch);
143       FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);
144       assert(i915_winsys_validate_buffers(i915->batch, &dst_buffer, 0));
145       flushed = true;
146    }
147    i915_set_flush_dirty(i915, I915_FLUSH_CACHE);
148 }
149