PixelFormat.cpp revision 42e2458144778596281ea2c4a98ad671053234e0
1/*
2 * Copyright (C) 2007 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#include <ui/PixelFormat.h>
18#include <hardware/hardware.h>
19
20// ----------------------------------------------------------------------------
21namespace android {
22// ----------------------------------------------------------------------------
23
24static const int COMPONENT_YUV = 0xFF;
25
26struct Info {
27    size_t      size;
28    size_t      bitsPerPixel;
29    struct {
30        uint8_t     ah;
31        uint8_t     al;
32        uint8_t     rh;
33        uint8_t     rl;
34        uint8_t     gh;
35        uint8_t     gl;
36        uint8_t     bh;
37        uint8_t     bl;
38    };
39    uint8_t     components;
40};
41
42static Info const sPixelFormatInfos[] = {
43        { 0,  0, { 0, 0,   0, 0,   0, 0,   0, 0 }, 0 },
44        { 4, 32, {32,24,   8, 0,  16, 8,  24,16 }, PixelFormatInfo::RGBA },
45        { 4, 24, { 0, 0,   8, 0,  16, 8,  24,16 }, PixelFormatInfo::RGB  },
46        { 3, 24, { 0, 0,   8, 0,  16, 8,  24,16 }, PixelFormatInfo::RGB  },
47        { 2, 16, { 0, 0,  16,11,  11, 5,   5, 0 }, PixelFormatInfo::RGB  },
48        { 4, 32, {32,24,  24,16,  16, 8,   8, 0 }, PixelFormatInfo::RGBA },
49        { 2, 16, { 1, 0,  16,11,  11, 6,   6, 1 }, PixelFormatInfo::RGBA },
50        { 2, 16, { 4, 0,  16,12,  12, 8,   8, 4 }, PixelFormatInfo::RGBA },
51        { 1,  8, { 8, 0,   0, 0,   0, 0,   0, 0 }, PixelFormatInfo::ALPHA}
52};
53
54static const Info* gGetPixelFormatTable(size_t* numEntries) {
55    if (numEntries) {
56        *numEntries = sizeof(sPixelFormatInfos)/sizeof(Info);
57    }
58    return sPixelFormatInfos;
59}
60
61// ----------------------------------------------------------------------------
62
63size_t PixelFormatInfo::getScanlineSize(unsigned int width) const
64{
65    size_t size;
66    if (components == COMPONENT_YUV) {
67        // YCbCr formats are different.
68        size = (width * bitsPerPixel)>>3;
69    } else {
70        size = width * bytesPerPixel;
71    }
72    return size;
73}
74
75ssize_t bytesPerPixel(PixelFormat format)
76{
77    PixelFormatInfo info;
78    status_t err = getPixelFormatInfo(format, &info);
79    return (err < 0) ? err : info.bytesPerPixel;
80}
81
82ssize_t bitsPerPixel(PixelFormat format)
83{
84    PixelFormatInfo info;
85    status_t err = getPixelFormatInfo(format, &info);
86    return (err < 0) ? err : info.bitsPerPixel;
87}
88
89status_t getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info)
90{
91    if (format < 0)
92        return BAD_VALUE;
93
94    if (info->version != sizeof(PixelFormatInfo))
95        return INVALID_OPERATION;
96
97    // YUV format from the HAL are handled here
98    switch (format) {
99    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
100    case HAL_PIXEL_FORMAT_YCbCr_422_I:
101        info->bitsPerPixel = 16;
102        goto done;
103    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
104    case HAL_PIXEL_FORMAT_YV12:
105        info->bitsPerPixel = 12;
106     done:
107        info->format = format;
108        info->components = COMPONENT_YUV;
109        info->bytesPerPixel = 1;
110        info->h_alpha = 0;
111        info->l_alpha = 0;
112        info->h_red = info->h_green = info->h_blue = 8;
113        info->l_red = info->l_green = info->l_blue = 0;
114        return NO_ERROR;
115    }
116
117    size_t numEntries;
118    const Info *i = gGetPixelFormatTable(&numEntries) + format;
119    bool valid = uint32_t(format) < numEntries;
120    if (!valid) {
121        return BAD_INDEX;
122    }
123
124    info->format = format;
125    info->bytesPerPixel = i->size;
126    info->bitsPerPixel  = i->bitsPerPixel;
127    info->h_alpha       = i->ah;
128    info->l_alpha       = i->al;
129    info->h_red         = i->rh;
130    info->l_red         = i->rl;
131    info->h_green       = i->gh;
132    info->l_green       = i->gl;
133    info->h_blue        = i->bh;
134    info->l_blue        = i->bl;
135    info->components    = i->components;
136
137    return NO_ERROR;
138}
139
140// ----------------------------------------------------------------------------
141}; // namespace android
142// ----------------------------------------------------------------------------
143
144