PixelFormat.cpp revision 7c1b96a165f970a09ed239bb4fb3f1b0d8f2a407
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
22ssize_t bytesPerPixel(PixelFormat format)
23{
24    PixelFormatInfo info;
25    status_t err = getPixelFormatInfo(format, &info);
26    return (err < 0) ? err : info.bytesPerPixel;
27}
28
29ssize_t bitsPerPixel(PixelFormat format)
30{
31    PixelFormatInfo info;
32    status_t err = getPixelFormatInfo(format, &info);
33    return (err < 0) ? err : info.bitsPerPixel;
34}
35
36status_t getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info)
37{
38    if (format < 0)
39        return BAD_VALUE;
40
41    if (info->version != sizeof(PixelFormatInfo))
42        return INVALID_OPERATION;
43
44    size_t numEntries;
45    const GGLFormat *i = gglGetPixelFormatTable(&numEntries) + format;
46    bool valid = uint32_t(format) < numEntries;
47    if (!valid) {
48        return BAD_INDEX;
49    }
50
51    info->format = format;
52    info->bytesPerPixel = i->size;
53    info->bitsPerPixel  = i->bitsPerPixel;
54    info->h_alpha       = i->ah;
55    info->l_alpha       = i->al;
56    info->h_red         = i->rh;
57    info->l_red         = i->rl;
58    info->h_green       = i->gh;
59    info->l_green       = i->gl;
60    info->h_blue        = i->bh;
61    info->l_blue        = i->bl;
62    return NO_ERROR;
63}
64
65}; // namespace android
66
67