initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / openvg / VGUtils.h
1 /*
2  * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef VGUtils_h
21 #define VGUtils_h
22
23 #include <openvg.h>
24 #include <wtf/Assertions.h>
25
26 static inline const char* toVGErrorConstant(VGErrorCode error)
27 {
28     switch (error) {
29     case VG_BAD_HANDLE_ERROR:
30         return "VG_BAD_HANDLE_ERROR";
31     case VG_ILLEGAL_ARGUMENT_ERROR:
32         return "VG_ILLEGAL_ARGUMENT_ERROR";
33     case VG_OUT_OF_MEMORY_ERROR:
34         return "VG_OUT_OF_MEMORY_ERROR";
35     case VG_PATH_CAPABILITY_ERROR:
36         return "VG_PATH_CAPABILITY_ERROR";
37     case VG_UNSUPPORTED_IMAGE_FORMAT_ERROR:
38         return "VG_UNSUPPORTED_IMAGE_FORMAT_ERROR";
39     case VG_UNSUPPORTED_PATH_FORMAT_ERROR:
40         return "VG_UNSUPPORTED_PATH_FORMAT_ERROR";
41     case VG_IMAGE_IN_USE_ERROR:
42         return "VG_IMAGE_IN_USE_ERROR";
43     case VG_NO_CONTEXT_ERROR:
44         return "VG_NO_CONTEXT_ERROR";
45     default:
46         return "UNKNOWN_ERROR";
47     }
48 }
49
50 #if ASSERT_DISABLED
51 #define ASSERT_VG_NO_ERROR() ((void)0)
52 #else
53 #define ASSERT_VG_NO_ERROR() do { \
54     VGErrorCode vgErrorCode = vgGetError(); \
55     ASSERT_WITH_MESSAGE(vgErrorCode == VG_NO_ERROR, "Found %s", toVGErrorConstant(vgErrorCode)); \
56 } while (0)
57 #endif
58
59
60 namespace WebCore {
61
62 class AffineTransform;
63 class FloatRect;
64 class TransformationMatrix;
65
66 class VGMatrix {
67 public:
68     VGMatrix(const VGfloat data[9]);
69     VGMatrix(const AffineTransform&);
70     VGMatrix(const TransformationMatrix&);
71     const VGfloat* toVGfloat() const { return m_data; }
72     operator AffineTransform() const;
73     operator TransformationMatrix() const;
74 private:
75     VGfloat m_data[9];
76 };
77
78 class VGRect {
79 public:
80     VGRect(const VGfloat data[4]);
81     VGRect(const FloatRect&);
82     const VGfloat* toVGfloat() const { return m_data; }
83     operator FloatRect() const;
84 private:
85     VGfloat m_data[4];
86 };
87
88 class VGUtils {
89 public:
90     static int bytesForImage(VGImageFormat, VGint width, VGint height);
91     static int bytesForImageScanline(VGImageFormat, VGint width);
92     static int imageFormatBitsPerPixel(VGImageFormat);
93
94     /**
95      * Return a flipped VGImageFormat if the platform is little endian
96      * (e.g. VG_ABGR_8888 for a given VG_RGBA_8888), or return the image format
97      * as is if the platform is big endian.
98      *
99      * OpenVG itself is indifferent to endianness, it will always work on a
100      * single machine word with the bytes going from left to right as specified
101      * in the image format, no matter which one of the bytes is most or least
102      * significant.
103      *
104      * However, if you interface with vgImageSubData()/vgGetImageSubData()
105      * using a byte array then you want to make sure the byte order is
106      * appropriate for the given platform (otherwise the byte indexes need
107      * to be swapped depending on endianness). So, use this function when
108      * interfacing with byte arrays, and don't use it otherwise.
109      */
110     static VGImageFormat endianAwareImageFormat(VGImageFormat bigEndianFormat);
111 };
112
113 }
114
115 #endif