blob: 3ced41d1644a219dee0b9035ed242d7f9872ac5c [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 },
Mathias Agopianff615cc2012-02-24 14:58:36 -080051 { 1, 8, { 8, 0, 0, 0, 0, 0, 0, 0 }, PixelFormatInfo::ALPHA},
52 { 1, 8, { 0, 0, 8, 0, 8, 0, 8, 0 }, PixelFormatInfo::L },
53 { 2, 16, {16, 8, 8, 0, 8, 0, 8, 0 }, PixelFormatInfo::LA },
54 { 1, 8, { 0, 0, 8, 5, 5, 2, 2, 0 }, PixelFormatInfo::RGB },
Mathias Agopian42e24582012-02-21 18:56:08 -080055};
56
57static const Info* gGetPixelFormatTable(size_t* numEntries) {
58 if (numEntries) {
59 *numEntries = sizeof(sPixelFormatInfos)/sizeof(Info);
60 }
61 return sPixelFormatInfos;
62}
63
64// ----------------------------------------------------------------------------
65
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066size_t PixelFormatInfo::getScanlineSize(unsigned int width) const
67{
68 size_t size;
Mathias Agopian3db21642010-02-16 20:43:39 -080069 if (components == COMPONENT_YUV) {
70 // YCbCr formats are different.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 size = (width * bitsPerPixel)>>3;
72 } else {
73 size = width * bytesPerPixel;
74 }
75 return size;
76}
77
78ssize_t bytesPerPixel(PixelFormat format)
79{
80 PixelFormatInfo info;
81 status_t err = getPixelFormatInfo(format, &info);
82 return (err < 0) ? err : info.bytesPerPixel;
83}
84
85ssize_t bitsPerPixel(PixelFormat format)
86{
87 PixelFormatInfo info;
88 status_t err = getPixelFormatInfo(format, &info);
89 return (err < 0) ? err : info.bitsPerPixel;
90}
91
92status_t getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info)
93{
Mathias Agopian6b8bef62012-04-24 23:27:31 -070094 if (format <= 0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 return BAD_VALUE;
96
97 if (info->version != sizeof(PixelFormatInfo))
98 return INVALID_OPERATION;
99
Mathias Agopian3db21642010-02-16 20:43:39 -0800100 // YUV format from the HAL are handled here
101 switch (format) {
102 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
Mathias Agopian3db21642010-02-16 20:43:39 -0800103 case HAL_PIXEL_FORMAT_YCbCr_422_I:
Mathias Agopian3db21642010-02-16 20:43:39 -0800104 info->bitsPerPixel = 16;
105 goto done;
Mathias Agopian3db21642010-02-16 20:43:39 -0800106 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Mathias Agopian1764c732010-07-01 21:17:56 -0700107 case HAL_PIXEL_FORMAT_YV12:
Mathias Agopian3db21642010-02-16 20:43:39 -0800108 info->bitsPerPixel = 12;
109 done:
110 info->format = format;
111 info->components = COMPONENT_YUV;
112 info->bytesPerPixel = 1;
113 info->h_alpha = 0;
114 info->l_alpha = 0;
115 info->h_red = info->h_green = info->h_blue = 8;
116 info->l_red = info->l_green = info->l_blue = 0;
117 return NO_ERROR;
118 }
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 size_t numEntries;
Mathias Agopian42e24582012-02-21 18:56:08 -0800121 const Info *i = gGetPixelFormatTable(&numEntries) + format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 bool valid = uint32_t(format) < numEntries;
123 if (!valid) {
124 return BAD_INDEX;
125 }
Mathias Agopian3db21642010-02-16 20:43:39 -0800126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 info->format = format;
128 info->bytesPerPixel = i->size;
129 info->bitsPerPixel = i->bitsPerPixel;
130 info->h_alpha = i->ah;
131 info->l_alpha = i->al;
132 info->h_red = i->rh;
133 info->l_red = i->rl;
134 info->h_green = i->gh;
135 info->l_green = i->gl;
136 info->h_blue = i->bh;
137 info->l_blue = i->bl;
Mathias Agopian42e24582012-02-21 18:56:08 -0800138 info->components = i->components;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139
140 return NO_ERROR;
141}
142
Mathias Agopian42e24582012-02-21 18:56:08 -0800143// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144}; // namespace android
Mathias Agopian42e24582012-02-21 18:56:08 -0800145// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146