PixelFormat.cpp revision a6938bab1f6fa76ae98ebbe44f4e534e05fa0993
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
20namespace android {
21
22size_t PixelFormatInfo::getScanlineSize(unsigned int width) const
23{
24    size_t size;
25    if ((components >= 6) && (components <= 8)) {
26        // YCbCr formats are differents.
27        size = (width * bitsPerPixel)>>3;
28    } else {
29        size = width * bytesPerPixel;
30    }
31    return size;
32}
33
34ssize_t bytesPerPixel(PixelFormat format)
35{
36    PixelFormatInfo info;
37    status_t err = getPixelFormatInfo(format, &info);
38    return (err < 0) ? err : info.bytesPerPixel;
39}
40
41ssize_t bitsPerPixel(PixelFormat format)
42{
43    PixelFormatInfo info;
44    status_t err = getPixelFormatInfo(format, &info);
45    return (err < 0) ? err : info.bitsPerPixel;
46}
47
48status_t getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info)
49{
50    if (format < 0)
51        return BAD_VALUE;
52
53    if (info->version != sizeof(PixelFormatInfo))
54        return INVALID_OPERATION;
55
56    size_t numEntries;
57    const GGLFormat *i = gglGetPixelFormatTable(&numEntries) + format;
58    bool valid = uint32_t(format) < numEntries;
59    if (!valid) {
60        return BAD_INDEX;
61    }
62
63    #define COMPONENT(name) \
64        case GGL_##name: info->components = PixelFormatInfo::name; break;
65
66    switch (i->components) {
67        COMPONENT(ALPHA)
68        COMPONENT(RGB)
69        COMPONENT(RGBA)
70        COMPONENT(LUMINANCE)
71        COMPONENT(LUMINANCE_ALPHA)
72        COMPONENT(Y_CB_CR_SP)
73        COMPONENT(Y_CB_CR_P)
74        COMPONENT(Y_CB_CR_I)
75        default:
76            return BAD_INDEX;
77    }
78
79    #undef COMPONENT
80
81    info->format = format;
82    info->bytesPerPixel = i->size;
83    info->bitsPerPixel  = i->bitsPerPixel;
84    info->h_alpha       = i->ah;
85    info->l_alpha       = i->al;
86    info->h_red         = i->rh;
87    info->l_red         = i->rl;
88    info->h_green       = i->gh;
89    info->l_green       = i->gl;
90    info->h_blue        = i->bh;
91    info->l_blue        = i->bl;
92
93    return NO_ERROR;
94}
95
96}; // namespace android
97
98