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
33void
34i915_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
41   I915_DBG(DBG_BLIT, "%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n", __FUNCTION__,
42            dst_buffer, dst_pitch, dst_offset, x, y, w, h);
43
44   if (!i915_winsys_validate_buffers(i915->batch, &dst_buffer, 1)) {
45      FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);
46      assert(i915_winsys_validate_buffers(i915->batch, &dst_buffer, 1));
47   }
48
49   switch (cpp) {
50   case 1:
51   case 2:
52   case 3:
53      BR13 = (((int)dst_pitch) & 0xffff) | (0xF0 << 16) | (1 << 24);
54      CMD = XY_COLOR_BLT_CMD;
55      break;
56   case 4:
57      BR13 = (((int)dst_pitch) & 0xffff) | (0xF0 << 16) | (1 << 24) | (1 << 25);
58      CMD = (XY_COLOR_BLT_CMD | rgba_mask);
59      break;
60   default:
61      return;
62   }
63
64   if (!BEGIN_BATCH(6)) {
65      FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);
66      assert(BEGIN_BATCH(6));
67   }
68   OUT_BATCH(CMD);
69   OUT_BATCH(BR13);
70   OUT_BATCH((y << 16) | x);
71   OUT_BATCH(((y + h) << 16) | (x + w));
72   OUT_RELOC_FENCED(dst_buffer, I915_USAGE_2D_TARGET, dst_offset);
73   OUT_BATCH(color);
74
75   i915_set_flush_dirty(i915, I915_FLUSH_CACHE);
76}
77
78void
79i915_copy_blit(struct i915_context *i915, unsigned cpp,
80               unsigned short src_pitch, struct i915_winsys_buffer *src_buffer,
81               unsigned src_offset, unsigned short dst_pitch,
82               struct i915_winsys_buffer *dst_buffer, unsigned dst_offset,
83               short src_x, short src_y, short dst_x, short dst_y, short w,
84               short h)
85{
86   unsigned CMD, BR13;
87   int dst_y2 = dst_y + h;
88   int dst_x2 = dst_x + w;
89   struct i915_winsys_buffer *buffers[2] = {src_buffer, dst_buffer};
90
91   I915_DBG(DBG_BLIT,
92            "%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
93            __FUNCTION__, src_buffer, src_pitch, src_offset, src_x, src_y,
94            dst_buffer, dst_pitch, dst_offset, dst_x, dst_y, w, h);
95
96   if (!i915_winsys_validate_buffers(i915->batch, buffers, 2)) {
97      FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);
98      assert(i915_winsys_validate_buffers(i915->batch, buffers, 2));
99   }
100
101   switch (cpp) {
102   case 1:
103   case 2:
104   case 3:
105      BR13 = (((int)dst_pitch) & 0xffff) | (0xCC << 16) | (1 << 24);
106      CMD = XY_SRC_COPY_BLT_CMD;
107      break;
108   case 4:
109      BR13 = (((int)dst_pitch) & 0xffff) | (0xCC << 16) | (1 << 24) | (1 << 25);
110      CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
111             XY_SRC_COPY_BLT_WRITE_RGB);
112      break;
113   default:
114      return;
115   }
116
117   if (dst_y2 < dst_y || dst_x2 < dst_x) {
118      return;
119   }
120
121   /* Hardware can handle negative pitches but loses the ability to do
122    * proper overlapping blits in that case.  We don't really have a
123    * need for either at this stage.
124    */
125   assert(dst_pitch > 0 && src_pitch > 0);
126
127   if (!BEGIN_BATCH(8)) {
128      FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);
129      assert(BEGIN_BATCH(8));
130   }
131   OUT_BATCH(CMD);
132   OUT_BATCH(BR13);
133   OUT_BATCH((dst_y << 16) | dst_x);
134   OUT_BATCH((dst_y2 << 16) | dst_x2);
135   OUT_RELOC_FENCED(dst_buffer, I915_USAGE_2D_TARGET, dst_offset);
136   OUT_BATCH((src_y << 16) | src_x);
137   OUT_BATCH(((int)src_pitch & 0xffff));
138   OUT_RELOC_FENCED(src_buffer, I915_USAGE_2D_SOURCE, src_offset);
139
140   i915_set_flush_dirty(i915, I915_FLUSH_CACHE);
141}
142