1544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin/**************************************************************************
2544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
3544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * Copyright 2009 VMware, Inc.  All Rights Reserved.
4544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
5544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * Permission is hereby granted, free of charge, to any person obtaining a
6544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * copy of this software and associated documentation files (the
7544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * "Software"), to deal in the Software without restriction, including
8544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * without limitation the rights to use, copy, modify, merge, publish,
9544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * distribute, sub license, and/or sell copies of the Software, and to
10544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * permit persons to whom the Software is furnished to do so, subject to
11544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * the following conditions:
12544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
13544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * The above copyright notice and this permission notice (including the
14544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * next paragraph) shall be included in all copies or substantial portions
15544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * of the Software.
16544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
17544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
25544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin **************************************************************************/
26544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
27544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "image.h"
28544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
29544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "VG/openvg.h"
30544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
31544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "vg_context.h"
32544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "vg_translate.h"
33544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "api_consts.h"
3475143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu#include "api.h"
35d41e694cf78ada8c9258f96995115c9da8437894Brian Paul#include "handle.h"
36544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
37544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "pipe/p_context.h"
38544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "pipe/p_screen.h"
3928486880ca3ec39419ccee0cb1a3bedc9ef7117cJosé Fonseca#include "util/u_inlines.h"
40544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "util/u_tile.h"
41b8f6cb380951463f86e6f9e7bb3a18a87fe2f53eChia-I Wu#include "util/u_math.h"
42544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
43544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstatic INLINE VGboolean supported_image_format(VGImageFormat format)
44544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
45544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   switch(format) {
46544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sRGBX_8888:
47544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sRGBA_8888:
48544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sRGBA_8888_PRE:
49544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sRGB_565:
50544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sRGBA_5551:
51544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sRGBA_4444:
52544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sL_8:
53544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lRGBX_8888:
54544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lRGBA_8888:
55544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lRGBA_8888_PRE:
56544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lL_8:
57544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_A_8:
58544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_BW_1:
59544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#ifdef OPENVG_VERSION_1_1
60544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_A_1:
61544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_A_4:
62544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#endif
63544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sXRGB_8888:
64544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sARGB_8888:
65544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sARGB_8888_PRE:
66544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sARGB_1555:
67544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sARGB_4444:
68544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lXRGB_8888:
69544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lARGB_8888:
70544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lARGB_8888_PRE:
71544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sBGRX_8888:
72544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sBGRA_8888:
73544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sBGRA_8888_PRE:
74544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sBGR_565:
75544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sBGRA_5551:
76544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sBGRA_4444:
77544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lBGRX_8888:
78544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lBGRA_8888:
79544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lBGRA_8888_PRE:
80544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sXBGR_8888:
81544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sABGR_8888:
82544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sABGR_8888_PRE:
83544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sABGR_1555:
84544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_sABGR_4444:
85544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lXBGR_8888:
86544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lABGR_8888:
87544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   case VG_lABGR_8888_PRE:
88544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_TRUE;
89544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   default:
90544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_FALSE;
91544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
92544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   return VG_FALSE;
93544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
94544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
9575143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I WuVGImage vegaCreateImage(VGImageFormat format,
9675143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                        VGint width, VGint height,
9775143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                        VGbitfield allowedQuality)
98544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
99544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
100544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
101544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!supported_image_format(format)) {
102544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_UNSUPPORTED_IMAGE_FORMAT_ERROR);
103544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
104544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
105544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0) {
106544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
107544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
108544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
10905e5b53128fc4dc25769fecace671cdaa7004630Chia-I Wu   if (width > vegaGeti(VG_MAX_IMAGE_WIDTH) ||
11005e5b53128fc4dc25769fecace671cdaa7004630Chia-I Wu       height > vegaGeti(VG_MAX_IMAGE_HEIGHT)) {
111544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
112544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
113544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
11405e5b53128fc4dc25769fecace671cdaa7004630Chia-I Wu   if (width * height > vegaGeti(VG_MAX_IMAGE_PIXELS)) {
115544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
116544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
117544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
118544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
119544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!(allowedQuality & ((VG_IMAGE_QUALITY_NONANTIALIASED |
120544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                           VG_IMAGE_QUALITY_FASTER |
121544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                           VG_IMAGE_QUALITY_BETTER)))) {
122544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
123544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
124544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
125544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
126d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   return image_to_handle(image_create(format, width, height));
127544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
128544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
12975143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaDestroyImage(VGImage image)
130544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
131544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
132d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   struct vg_image *img = handle_to_image(image);
133544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
134544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (image == VG_INVALID_HANDLE) {
135544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
136544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
137544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
138d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   if (!vg_object_is_valid(image, VG_OBJECT_IMAGE)) {
139544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
140544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
141544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
142544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   image_destroy(img);
143544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
144544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
14575143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaClearImage(VGImage image,
14675143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                    VGint x, VGint y,
14775143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                    VGint width, VGint height)
148544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
149544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
150544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_image *img;
151544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
152544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (image == VG_INVALID_HANDLE) {
153544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
154544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
155544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
156544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0) {
157544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
158544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
159544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
160544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
161d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   img = handle_to_image(image);
162544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
163544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (x + width < 0 || y + height < 0)
164544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
165544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
166544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   image_clear(img, x, y, width, height);
167544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
168544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
169544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
17075143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaImageSubData(VGImage image,
17175143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                      const void * data,
17275143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                      VGint dataStride,
17375143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                      VGImageFormat dataFormat,
17475143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                      VGint x, VGint y,
17575143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                      VGint width, VGint height)
176544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
177544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
178544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_image *img;
179544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
180544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (image == VG_INVALID_HANDLE) {
181544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
182544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
183544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
184544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!supported_image_format(dataFormat)) {
185544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_UNSUPPORTED_IMAGE_FORMAT_ERROR);
186544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
187544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
188544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0 || !data || !is_aligned(data)) {
189544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
190544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
191544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
192544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
193d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   img = handle_to_image(image);
194544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   image_sub_data(img, data, dataStride, dataFormat,
195544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                  x, y, width, height);
196544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
197544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
19875143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaGetImageSubData(VGImage image,
19975143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                         void * data,
20075143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                         VGint dataStride,
20175143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                         VGImageFormat dataFormat,
20275143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                         VGint x, VGint y,
20375143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                         VGint width, VGint height)
204544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
205544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
206544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_image *img;
207544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
208544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (image == VG_INVALID_HANDLE) {
209544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
210544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
211544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
212544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!supported_image_format(dataFormat)) {
213544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_UNSUPPORTED_IMAGE_FORMAT_ERROR);
214544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
215544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
216544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0 || !data || !is_aligned(data)) {
217544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
218544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
219544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
220d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   img = handle_to_image(image);
221544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   image_get_sub_data(img, data, dataStride, dataFormat,
222544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                      x, y, width, height);
223544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
224544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
22575143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I WuVGImage vegaChildImage(VGImage parent,
22675143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                       VGint x, VGint y,
22775143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                       VGint width, VGint height)
228544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
229544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
230544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_image *p;
231544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
232544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (parent == VG_INVALID_HANDLE ||
233d41e694cf78ada8c9258f96995115c9da8437894Brian Paul       !vg_context_is_object_valid(ctx, VG_OBJECT_IMAGE, parent) ||
234d41e694cf78ada8c9258f96995115c9da8437894Brian Paul       !vg_object_is_valid(parent, VG_OBJECT_IMAGE)) {
235544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
236544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
237544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
238544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0 || x < 0 || y < 0) {
239544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
240544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
241544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
242d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   p = handle_to_image(parent);
243544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (x > p->width  || y > p->height) {
244544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
245544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
246544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
247544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (x + width > p->width  || y + height > p->height) {
248544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
249544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
250544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
251544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
252d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   return image_to_handle(image_child_image(p, x, y, width, height));
253544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
254544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
25575143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I WuVGImage vegaGetParent(VGImage image)
256544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
257544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
258544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_image *img;
259544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
260544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (image == VG_INVALID_HANDLE) {
261544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
262544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return VG_INVALID_HANDLE;
263544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
264544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
265d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   img = handle_to_image(image);
266544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (img->parent)
267d41e694cf78ada8c9258f96995115c9da8437894Brian Paul      return image_to_handle(img->parent);
268544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   else
269544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return image;
270544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
271544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
27275143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaCopyImage(VGImage dst, VGint dx, VGint dy,
27375143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                   VGImage src, VGint sx, VGint sy,
27475143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                   VGint width, VGint height,
27575143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                   VGboolean dither)
276544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
277544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
278544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
279544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (src == VG_INVALID_HANDLE || dst == VG_INVALID_HANDLE) {
280544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
281544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
282544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
283544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
284544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0) {
285544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
286544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
287544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
288544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   vg_validate_state(ctx);
289d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   image_copy(handle_to_image(dst), dx, dy,
290d41e694cf78ada8c9258f96995115c9da8437894Brian Paul              handle_to_image(src), sx, sy,
291544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin              width, height, dither);
292544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
293544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
29475143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaDrawImage(VGImage image)
295544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
296544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
297544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
298544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!ctx)
299544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
300544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
301544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (image == VG_INVALID_HANDLE) {
302544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
303544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
304544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
305544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
306544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   vg_validate_state(ctx);
307d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   image_draw(handle_to_image(image),
30834f466d4e6a720138c0846ab6233c32dc039fe58Chia-I Wu         &ctx->state.vg.image_user_to_surface_matrix);
309544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
310544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
31175143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaSetPixels(VGint dx, VGint dy,
31275143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                   VGImage src, VGint sx, VGint sy,
31375143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                   VGint width, VGint height)
314544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
315544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
316544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
317544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   vg_validate_state(ctx);
318544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
319544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (src == VG_INVALID_HANDLE) {
320544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
321544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
322544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
323544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0) {
324544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
325544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
326544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
327d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   image_set_pixels(dx, dy, handle_to_image(src), sx, sy, width,
328544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                    height);
329544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
330544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
33175143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaGetPixels(VGImage dst, VGint dx, VGint dy,
33275143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                   VGint sx, VGint sy,
33375143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                   VGint width, VGint height)
334544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
335544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
336544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_image *img;
337544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
338544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (dst == VG_INVALID_HANDLE) {
339544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
340544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
341544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
342544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0) {
343544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
344544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
345544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
346544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
347d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   img = handle_to_image(dst);
348544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
349544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   image_get_pixels(img, dx, dy,
350544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                    sx, sy, width, height);
351544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
352544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
35375143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaWritePixels(const void * data, VGint dataStride,
35475143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                     VGImageFormat dataFormat,
35575143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                     VGint dx, VGint dy,
35675143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                     VGint width, VGint height)
357544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
358544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
359544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
360544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!supported_image_format(dataFormat)) {
361544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_UNSUPPORTED_IMAGE_FORMAT_ERROR);
362544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
363544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
364544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!data || !is_aligned(data)) {
365544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
366544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
367544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
368544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0) {
369544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
370544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
371544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
372544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
373544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   vg_validate_state(ctx);
374544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   {
375544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      struct vg_image *img = image_create(dataFormat, width, height);
376544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      image_sub_data(img, data, dataStride, dataFormat, 0, 0,
377544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                     width, height);
378544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#if 0
379544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      struct matrix *matrix = &ctx->state.vg.image_user_to_surface_matrix;
380544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      matrix_translate(matrix, dx, dy);
381544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      image_draw(img);
382544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      matrix_translate(matrix, -dx, -dy);
383544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#else
384544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      /* this looks like a better approach */
385544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      image_set_pixels(dx, dy, img, 0, 0, width, height);
386544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#endif
387544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      image_destroy(img);
388544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
389544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
390544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
39175143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaReadPixels(void * data, VGint dataStride,
39275143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                    VGImageFormat dataFormat,
39375143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                    VGint sx, VGint sy,
39475143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                    VGint width, VGint height)
395544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
396544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
397544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct pipe_context *pipe = ctx->pipe;
398544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
399544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct st_framebuffer *stfb = ctx->draw_buffer;
400544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct st_renderbuffer *strb = stfb->strb;
401544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
402544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGfloat temp[VEGA_MAX_IMAGE_WIDTH][4];
403544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGfloat *df = (VGfloat*)temp;
404544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGint i;
405544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGubyte *dst = (VGubyte *)data;
406544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGint xoffset = 0, yoffset = 0;
407544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
408544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!supported_image_format(dataFormat)) {
409544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_UNSUPPORTED_IMAGE_FORMAT_ERROR);
410544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
411544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
412544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (!data || !is_aligned(data)) {
413544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
414544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
415544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
416544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0) {
417544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
418544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
419544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
420544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
421544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (sx < 0) {
422544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      xoffset = -sx;
423544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      xoffset *= _vega_size_for_format(dataFormat);
424544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      width += sx;
425544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      sx = 0;
426544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
427544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (sy < 0) {
428544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      yoffset = -sy;
429b8f6cb380951463f86e6f9e7bb3a18a87fe2f53eChia-I Wu      yoffset *= dataStride;
430544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      height += sy;
431544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      sy = 0;
432b8f6cb380951463f86e6f9e7bb3a18a87fe2f53eChia-I Wu   }
433b8f6cb380951463f86e6f9e7bb3a18a87fe2f53eChia-I Wu
43496c6637a1360f146bbf49ffb207ae943ecbbdf49Chia-I Wu   if (sx + width > stfb->width || sy + height > stfb->height) {
43596c6637a1360f146bbf49ffb207ae943ecbbdf49Chia-I Wu      width = stfb->width - sx;
43696c6637a1360f146bbf49ffb207ae943ecbbdf49Chia-I Wu      height = stfb->height - sy;
437b8f6cb380951463f86e6f9e7bb3a18a87fe2f53eChia-I Wu      /* nothing to read */
438b8f6cb380951463f86e6f9e7bb3a18a87fe2f53eChia-I Wu      if (width <= 0 || height <= 0)
439b8f6cb380951463f86e6f9e7bb3a18a87fe2f53eChia-I Wu         return;
440544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
441544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
442544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   {
44396c6637a1360f146bbf49ffb207ae943ecbbdf49Chia-I Wu      VGint y = (stfb->height - sy) - 1, yStep = -1;
444544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      struct pipe_transfer *transfer;
445544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
4464c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger      transfer = pipe_get_transfer(pipe, strb->texture,  0, 0,
4474c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                   PIPE_TRANSFER_READ,
4484c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                   0, 0, sx + width, stfb->height - sy);
449544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
450544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      /* Do a row at a time to flip image data vertically */
451544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      for (i = 0; i < height; i++) {
452544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#if 0
453544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         debug_printf("%d-%d  == %d\n", sy, height, y);
454544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#endif
455d35ecca5ee231c072687578642e0c22c6c0590b1Keith Whitwell         pipe_get_tile_rgba(pipe, transfer, sx, y, width, 1, df);
456544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         y += yStep;
457544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         _vega_pack_rgba_span_float(ctx, width, temp, dataFormat,
458544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                                    dst + yoffset + xoffset);
459544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         dst += dataStride;
460544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      }
461544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
462287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      pipe->transfer_destroy(pipe, transfer);
463544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
464544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
465544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
46675143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wuvoid vegaCopyPixels(VGint dx, VGint dy,
46775143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                    VGint sx, VGint sy,
46875143ef05576ee9f25ee176bc28c3c4d03705bf5Chia-I Wu                    VGint width, VGint height)
469544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
470544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *ctx = vg_current_context();
47196c6637a1360f146bbf49ffb207ae943ecbbdf49Chia-I Wu   struct st_framebuffer *stfb = ctx->draw_buffer;
47296c6637a1360f146bbf49ffb207ae943ecbbdf49Chia-I Wu   struct st_renderbuffer *strb = stfb->strb;
473544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
474544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (width <= 0 || height <= 0) {
475544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
476544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
477544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
478544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
479544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   /* do nothing if we copy from outside the fb */
48096c6637a1360f146bbf49ffb207ae943ecbbdf49Chia-I Wu   if (dx >= (VGint)stfb->width || dy >= (VGint)stfb->height ||
48196c6637a1360f146bbf49ffb207ae943ecbbdf49Chia-I Wu       sx >= (VGint)stfb->width || sy >= (VGint)stfb->height)
482544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return;
483544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
484544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   vg_validate_state(ctx);
485544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   /* make sure rendering has completed */
48605e5b53128fc4dc25769fecace671cdaa7004630Chia-I Wu   vegaFinish();
487544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
488544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   vg_copy_surface(ctx, strb->surface, dx, dy,
489544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                   strb->surface, sx, sy, width, height);
490544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
491