PixelFormat.h revision 076b1cc3a9b90aa5b381a1ed268ca0b548444c9b
1/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//
18
19// Pixel formats used across the system.
20// These formats might not all be supported by all renderers, for instance
21// skia or SurfaceFlinger are not required to support all of these formats
22// (either as source or destination)
23
24// XXX: we should consolidate these formats and skia's
25
26#ifndef UI_PIXELFORMAT_H
27#define UI_PIXELFORMAT_H
28
29#include <stdint.h>
30#include <sys/types.h>
31#include <utils/Errors.h>
32#include <pixelflinger/format.h>
33
34namespace android {
35
36enum {
37    //
38    // these constants need to match those
39    // in graphics/PixelFormat.java & pixelflinger/format.h
40    //
41    PIXEL_FORMAT_UNKNOWN    =   0,
42    PIXEL_FORMAT_NONE       =   0,
43
44    // logical pixel formats used by the SurfaceFlinger -----------------------
45    PIXEL_FORMAT_CUSTOM         = -4,
46        // Custom pixel-format described by a PixelFormatInfo structure
47
48    PIXEL_FORMAT_TRANSLUCENT    = -3,
49        // System chooses a format that supports translucency (many alpha bits)
50
51    PIXEL_FORMAT_TRANSPARENT    = -2,
52        // System chooses a format that supports transparency
53        // (at least 1 alpha bit)
54
55    PIXEL_FORMAT_OPAQUE         = -1,
56        // System chooses an opaque format (no alpha bits required)
57
58    // real pixel formats supported for rendering -----------------------------
59
60    PIXEL_FORMAT_RGBA_8888   = GGL_PIXEL_FORMAT_RGBA_8888,  // 4x8-bit RGBA
61    PIXEL_FORMAT_RGBX_8888   = GGL_PIXEL_FORMAT_RGBX_8888,  // 4x8-bit RGB0
62    PIXEL_FORMAT_RGB_888     = GGL_PIXEL_FORMAT_RGB_888,    // 3x8-bit RGB
63    PIXEL_FORMAT_RGB_565     = GGL_PIXEL_FORMAT_RGB_565,    // 16-bit RGB
64    PIXEL_FORMAT_BGRA_8888   = GGL_PIXEL_FORMAT_BGRA_8888,  // 4x8-bit BGRA
65    PIXEL_FORMAT_RGBA_5551   = GGL_PIXEL_FORMAT_RGBA_5551,  // 16-bit ARGB
66    PIXEL_FORMAT_RGBA_4444   = GGL_PIXEL_FORMAT_RGBA_4444,  // 16-bit ARGB
67    PIXEL_FORMAT_A_8         = GGL_PIXEL_FORMAT_A_8,        // 8-bit A
68    PIXEL_FORMAT_L_8         = GGL_PIXEL_FORMAT_L_8,        // 8-bit L (R=G=B=L)
69    PIXEL_FORMAT_LA_88       = GGL_PIXEL_FORMAT_LA_88,      // 16-bit LA
70    PIXEL_FORMAT_RGB_332     = GGL_PIXEL_FORMAT_RGB_332,    // 8-bit RGB
71
72    PIXEL_FORMAT_YCbCr_422_SP= GGL_PIXEL_FORMAT_YCbCr_422_SP,
73    PIXEL_FORMAT_YCbCr_420_SP= GGL_PIXEL_FORMAT_YCbCr_420_SP,
74    PIXEL_FORMAT_YCbCr_422_P = GGL_PIXEL_FORMAT_YCbCr_422_P,
75    PIXEL_FORMAT_YCbCr_420_P = GGL_PIXEL_FORMAT_YCbCr_420_P,
76    PIXEL_FORMAT_YCbCr_422_I = GGL_PIXEL_FORMAT_YCbCr_422_I,
77    PIXEL_FORMAT_YCbCr_420_I = GGL_PIXEL_FORMAT_YCbCr_420_I,
78
79    // New formats can be added if they're also defined in
80    // pixelflinger/format.h
81};
82
83typedef int32_t PixelFormat;
84
85struct PixelFormatInfo
86{
87    enum {
88        INDEX_ALPHA   = 0,
89        INDEX_RED     = 1,
90        INDEX_GREEN   = 2,
91        INDEX_BLUE    = 3
92    };
93
94    enum { // components
95        ALPHA               = 1,
96        RGB                 = 2,
97        RGBA                = 3,
98        LUMINANCE           = 4,
99        LUMINANCE_ALPHA     = 5,
100        Y_CB_CR_SP          = 6,
101        Y_CB_CR_P           = 7,
102        Y_CB_CR_I           = 8,
103    };
104
105    struct szinfo {
106        uint8_t h;
107        uint8_t l;
108    };
109
110    inline PixelFormatInfo() : version(sizeof(PixelFormatInfo)) { }
111    size_t getScanlineSize(unsigned int width) const;
112    size_t getSize(size_t ci) const {
113        return (ci <= 3) ? (cinfo[ci].h - cinfo[ci].l) : 0;
114    }
115    size_t      version;
116    PixelFormat format;
117    size_t      bytesPerPixel;
118    size_t      bitsPerPixel;
119    union {
120        szinfo      cinfo[4];
121        struct {
122            uint8_t     h_alpha;
123            uint8_t     l_alpha;
124            uint8_t     h_red;
125            uint8_t     l_red;
126            uint8_t     h_green;
127            uint8_t     l_green;
128            uint8_t     h_blue;
129            uint8_t     l_blue;
130        };
131    };
132    uint8_t     components;
133    uint8_t     reserved0[3];
134    uint32_t    reserved1;
135};
136
137// Consider caching the results of these functions are they're not
138// guaranteed to be fast.
139ssize_t     bytesPerPixel(PixelFormat format);
140ssize_t     bitsPerPixel(PixelFormat format);
141status_t    getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info);
142
143}; // namespace android
144
145#endif // UI_PIXELFORMAT_H
146