PixelFormat.cpp revision 48c347637124339a34aff53df40bbcb5b044ee6d
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 <pixelflinger/format.h>
19#include <hardware/hardware.h>
20
21namespace android {
22
23static const int COMPONENT_YUV = 0xFF;
24
25size_t PixelFormatInfo::getScanlineSize(unsigned int width) const
26{
27    size_t size;
28    if (components == COMPONENT_YUV) {
29        // YCbCr formats are different.
30        size = (width * bitsPerPixel)>>3;
31    } else {
32        size = width * bytesPerPixel;
33    }
34    return size;
35}
36
37ssize_t bytesPerPixel(PixelFormat format)
38{
39    PixelFormatInfo info;
40    status_t err = getPixelFormatInfo(format, &info);
41    return (err < 0) ? err : info.bytesPerPixel;
42}
43
44ssize_t bitsPerPixel(PixelFormat format)
45{
46    PixelFormatInfo info;
47    status_t err = getPixelFormatInfo(format, &info);
48    return (err < 0) ? err : info.bitsPerPixel;
49}
50
51status_t getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info)
52{
53    if (format < 0)
54        return BAD_VALUE;
55
56    if (info->version != sizeof(PixelFormatInfo))
57        return INVALID_OPERATION;
58
59    // YUV format from the HAL are handled here
60    switch (format) {
61    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
62    case HAL_PIXEL_FORMAT_YCbCr_422_I:
63        info->bitsPerPixel = 16;
64        goto done;
65    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
66    case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
67    case HAL_PIXEL_FORMAT_YV12:
68        info->bitsPerPixel = 12;
69     done:
70        info->format = format;
71        info->components = COMPONENT_YUV;
72        info->bytesPerPixel = 1;
73        info->h_alpha = 0;
74        info->l_alpha = 0;
75        info->h_red = info->h_green = info->h_blue = 8;
76        info->l_red = info->l_green = info->l_blue = 0;
77        return NO_ERROR;
78    }
79
80    size_t numEntries;
81    const GGLFormat *i = gglGetPixelFormatTable(&numEntries) + format;
82    bool valid = uint32_t(format) < numEntries;
83    if (!valid) {
84        return BAD_INDEX;
85    }
86
87    #define COMPONENT(name) \
88        case GGL_##name: info->components = PixelFormatInfo::name; break;
89
90    switch (i->components) {
91        COMPONENT(ALPHA)
92        COMPONENT(RGB)
93        COMPONENT(RGBA)
94        COMPONENT(LUMINANCE)
95        COMPONENT(LUMINANCE_ALPHA)
96        default:
97            return BAD_INDEX;
98    }
99
100    #undef COMPONENT
101
102    info->format = format;
103    info->bytesPerPixel = i->size;
104    info->bitsPerPixel  = i->bitsPerPixel;
105    info->h_alpha       = i->ah;
106    info->l_alpha       = i->al;
107    info->h_red         = i->rh;
108    info->l_red         = i->rl;
109    info->h_green       = i->gh;
110    info->l_green       = i->gl;
111    info->h_blue        = i->bh;
112    info->l_blue        = i->bl;
113
114    return NO_ERROR;
115}
116
117}; // namespace android
118
119