blob: ee186c84de9c5f72e170407023b884527041384d [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 Agopian3db21642010-02-16 20:43:39 -080019#include <hardware/hardware.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
21namespace android {
22
Mathias Agopian3db21642010-02-16 20:43:39 -080023static const int COMPONENT_YUV = 0xFF;
24
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025size_t PixelFormatInfo::getScanlineSize(unsigned int width) const
26{
27 size_t size;
Mathias Agopian3db21642010-02-16 20:43:39 -080028 if (components == COMPONENT_YUV) {
29 // YCbCr formats are different.
The Android Open Source Projectedbf3b62009-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 Agopian3db21642010-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:
Mathias Agopian3db21642010-02-16 20:43:39 -080062 case HAL_PIXEL_FORMAT_YCbCr_422_I:
Mathias Agopian3db21642010-02-16 20:43:39 -080063 info->bitsPerPixel = 16;
64 goto done;
Mathias Agopian3db21642010-02-16 20:43:39 -080065 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Mathias Agopian1764c732010-07-01 21:17:56 -070066 case HAL_PIXEL_FORMAT_YV12:
Mathias Agopian3db21642010-02-16 20:43:39 -080067 info->bitsPerPixel = 12;
68 done:
69 info->format = format;
70 info->components = COMPONENT_YUV;
71 info->bytesPerPixel = 1;
72 info->h_alpha = 0;
73 info->l_alpha = 0;
74 info->h_red = info->h_green = info->h_blue = 8;
75 info->l_red = info->l_green = info->l_blue = 0;
76 return NO_ERROR;
77 }
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 size_t numEntries;
80 const GGLFormat *i = gglGetPixelFormatTable(&numEntries) + format;
81 bool valid = uint32_t(format) < numEntries;
82 if (!valid) {
83 return BAD_INDEX;
84 }
Mathias Agopian3db21642010-02-16 20:43:39 -080085
86 #define COMPONENT(name) \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 case GGL_##name: info->components = PixelFormatInfo::name; break;
88
89 switch (i->components) {
90 COMPONENT(ALPHA)
91 COMPONENT(RGB)
92 COMPONENT(RGBA)
93 COMPONENT(LUMINANCE)
94 COMPONENT(LUMINANCE_ALPHA)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 default:
96 return BAD_INDEX;
97 }
98
99 #undef COMPONENT
100
101 info->format = format;
102 info->bytesPerPixel = i->size;
103 info->bitsPerPixel = i->bitsPerPixel;
104 info->h_alpha = i->ah;
105 info->l_alpha = i->al;
106 info->h_red = i->rh;
107 info->l_red = i->rl;
108 info->h_green = i->gh;
109 info->l_green = i->gl;
110 info->h_blue = i->bh;
111 info->l_blue = i->bl;
112
113 return NO_ERROR;
114}
115
116}; // namespace android
117