blob: c9a5950d153220258273ab5396d19296d11d64d0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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>
Mathias Agopian102f49f2010-02-16 20:43:39 -080019#include <hardware/hardware.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21namespace android {
22
Mathias Agopian102f49f2010-02-16 20:43:39 -080023static const int COMPONENT_YUV = 0xFF;
24
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025size_t PixelFormatInfo::getScanlineSize(unsigned int width) const
26{
27 size_t size;
Mathias Agopian102f49f2010-02-16 20:43:39 -080028 if (components == COMPONENT_YUV) {
29 // YCbCr formats are different.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 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
Mathias Agopian102f49f2010-02-16 20:43:39 -080059 // YUV format from the HAL are handled here
60 switch (format) {
61 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
62 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
63 case HAL_PIXEL_FORMAT_YCbCr_422_P:
64 case HAL_PIXEL_FORMAT_YCbCr_422_I:
65 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
66 info->bitsPerPixel = 16;
67 goto done;
68 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
69 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
70 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Mathias Agopian102f49f2010-02-16 20:43:39 -080071 case HAL_PIXEL_FORMAT_YCbCr_420_P:
Mathias Agopian102f49f2010-02-16 20:43:39 -080072 info->bitsPerPixel = 12;
73 done:
74 info->format = format;
75 info->components = COMPONENT_YUV;
76 info->bytesPerPixel = 1;
77 info->h_alpha = 0;
78 info->l_alpha = 0;
79 info->h_red = info->h_green = info->h_blue = 8;
80 info->l_red = info->l_green = info->l_blue = 0;
81 return NO_ERROR;
82 }
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 size_t numEntries;
85 const GGLFormat *i = gglGetPixelFormatTable(&numEntries) + format;
86 bool valid = uint32_t(format) < numEntries;
87 if (!valid) {
88 return BAD_INDEX;
89 }
Mathias Agopian102f49f2010-02-16 20:43:39 -080090
91 #define COMPONENT(name) \
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 case GGL_##name: info->components = PixelFormatInfo::name; break;
93
94 switch (i->components) {
95 COMPONENT(ALPHA)
96 COMPONENT(RGB)
97 COMPONENT(RGBA)
98 COMPONENT(LUMINANCE)
99 COMPONENT(LUMINANCE_ALPHA)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 default:
101 return BAD_INDEX;
102 }
103
104 #undef COMPONENT
105
106 info->format = format;
107 info->bytesPerPixel = i->size;
108 info->bitsPerPixel = i->bitsPerPixel;
109 info->h_alpha = i->ah;
110 info->l_alpha = i->al;
111 info->h_red = i->rh;
112 info->l_red = i->rl;
113 info->h_green = i->gh;
114 info->l_green = i->gl;
115 info->h_blue = i->bh;
116 info->l_blue = i->bl;
117
118 return NO_ERROR;
119}
120
121}; // namespace android
122