blob: 6993dac6998abc480d92b3edb7b22f34d161a6ee [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>
Mathias Agopian3db21642010-02-16 20:43:39 -080018#include <hardware/hardware.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019
Mathias Agopian42e24582012-02-21 18:56:08 -080020// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021namespace android {
Mathias Agopian42e24582012-02-21 18:56:08 -080022// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Mathias Agopian3db21642010-02-16 20:43:39 -080024static const int COMPONENT_YUV = 0xFF;
25
Mathias Agopian42e24582012-02-21 18:56:08 -080026struct Info {
27 size_t size;
28 size_t bitsPerPixel;
29 struct {
30 uint8_t ah;
31 uint8_t al;
32 uint8_t rh;
33 uint8_t rl;
34 uint8_t gh;
35 uint8_t gl;
36 uint8_t bh;
37 uint8_t bl;
38 };
39 uint8_t components;
40};
41
42static Info const sPixelFormatInfos[] = {
43 { 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
44 { 4, 32, {32,24, 8, 0, 16, 8, 24,16 }, PixelFormatInfo::RGBA },
45 { 4, 24, { 0, 0, 8, 0, 16, 8, 24,16 }, PixelFormatInfo::RGB },
46 { 3, 24, { 0, 0, 8, 0, 16, 8, 24,16 }, PixelFormatInfo::RGB },
47 { 2, 16, { 0, 0, 16,11, 11, 5, 5, 0 }, PixelFormatInfo::RGB },
48 { 4, 32, {32,24, 24,16, 16, 8, 8, 0 }, PixelFormatInfo::RGBA },
49 { 2, 16, { 1, 0, 16,11, 11, 6, 6, 1 }, PixelFormatInfo::RGBA },
50 { 2, 16, { 4, 0, 16,12, 12, 8, 8, 4 }, PixelFormatInfo::RGBA },
51 { 1, 8, { 8, 0, 0, 0, 0, 0, 0, 0 }, PixelFormatInfo::ALPHA}
52};
53
54static const Info* gGetPixelFormatTable(size_t* numEntries) {
55 if (numEntries) {
56 *numEntries = sizeof(sPixelFormatInfos)/sizeof(Info);
57 }
58 return sPixelFormatInfos;
59}
60
61// ----------------------------------------------------------------------------
62
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063size_t PixelFormatInfo::getScanlineSize(unsigned int width) const
64{
65 size_t size;
Mathias Agopian3db21642010-02-16 20:43:39 -080066 if (components == COMPONENT_YUV) {
67 // YCbCr formats are different.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 size = (width * bitsPerPixel)>>3;
69 } else {
70 size = width * bytesPerPixel;
71 }
72 return size;
73}
74
75ssize_t bytesPerPixel(PixelFormat format)
76{
77 PixelFormatInfo info;
78 status_t err = getPixelFormatInfo(format, &info);
79 return (err < 0) ? err : info.bytesPerPixel;
80}
81
82ssize_t bitsPerPixel(PixelFormat format)
83{
84 PixelFormatInfo info;
85 status_t err = getPixelFormatInfo(format, &info);
86 return (err < 0) ? err : info.bitsPerPixel;
87}
88
89status_t getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info)
90{
91 if (format < 0)
92 return BAD_VALUE;
93
94 if (info->version != sizeof(PixelFormatInfo))
95 return INVALID_OPERATION;
96
Mathias Agopian3db21642010-02-16 20:43:39 -080097 // YUV format from the HAL are handled here
98 switch (format) {
99 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
Mathias Agopian3db21642010-02-16 20:43:39 -0800100 case HAL_PIXEL_FORMAT_YCbCr_422_I:
Mathias Agopian3db21642010-02-16 20:43:39 -0800101 info->bitsPerPixel = 16;
102 goto done;
Mathias Agopian3db21642010-02-16 20:43:39 -0800103 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Mathias Agopian1764c732010-07-01 21:17:56 -0700104 case HAL_PIXEL_FORMAT_YV12:
Mathias Agopian3db21642010-02-16 20:43:39 -0800105 info->bitsPerPixel = 12;
106 done:
107 info->format = format;
108 info->components = COMPONENT_YUV;
109 info->bytesPerPixel = 1;
110 info->h_alpha = 0;
111 info->l_alpha = 0;
112 info->h_red = info->h_green = info->h_blue = 8;
113 info->l_red = info->l_green = info->l_blue = 0;
114 return NO_ERROR;
115 }
116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 size_t numEntries;
Mathias Agopian42e24582012-02-21 18:56:08 -0800118 const Info *i = gGetPixelFormatTable(&numEntries) + format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 bool valid = uint32_t(format) < numEntries;
120 if (!valid) {
121 return BAD_INDEX;
122 }
Mathias Agopian3db21642010-02-16 20:43:39 -0800123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 info->format = format;
125 info->bytesPerPixel = i->size;
126 info->bitsPerPixel = i->bitsPerPixel;
127 info->h_alpha = i->ah;
128 info->l_alpha = i->al;
129 info->h_red = i->rh;
130 info->l_red = i->rl;
131 info->h_green = i->gh;
132 info->l_green = i->gl;
133 info->h_blue = i->bh;
134 info->l_blue = i->bl;
Mathias Agopian42e24582012-02-21 18:56:08 -0800135 info->components = i->components;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136
137 return NO_ERROR;
138}
139
Mathias Agopian42e24582012-02-21 18:56:08 -0800140// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141}; // namespace android
Mathias Agopian42e24582012-02-21 18:56:08 -0800142// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143