The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 18 | #include <hardware/hardware.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
Mathias Agopian | 42e2458 | 2012-02-21 18:56:08 -0800 | [diff] [blame] | 20 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | namespace android { |
Mathias Agopian | 42e2458 | 2012-02-21 18:56:08 -0800 | [diff] [blame] | 22 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 24 | static const int COMPONENT_YUV = 0xFF; |
| 25 | |
Mathias Agopian | 42e2458 | 2012-02-21 18:56:08 -0800 | [diff] [blame] | 26 | struct 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 | |
| 42 | static 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 Agopian | ff615cc | 2012-02-24 14:58:36 -0800 | [diff] [blame] | 51 | { 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 Agopian | 42e2458 | 2012-02-21 18:56:08 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | size_t PixelFormatInfo::getScanlineSize(unsigned int width) const |
| 67 | { |
| 68 | size_t size; |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 69 | if (components == COMPONENT_YUV) { |
| 70 | // YCbCr formats are different. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | size = (width * bitsPerPixel)>>3; |
| 72 | } else { |
| 73 | size = width * bytesPerPixel; |
| 74 | } |
| 75 | return size; |
| 76 | } |
| 77 | |
| 78 | ssize_t bytesPerPixel(PixelFormat format) |
| 79 | { |
| 80 | PixelFormatInfo info; |
| 81 | status_t err = getPixelFormatInfo(format, &info); |
| 82 | return (err < 0) ? err : info.bytesPerPixel; |
| 83 | } |
| 84 | |
| 85 | ssize_t bitsPerPixel(PixelFormat format) |
| 86 | { |
| 87 | PixelFormatInfo info; |
| 88 | status_t err = getPixelFormatInfo(format, &info); |
| 89 | return (err < 0) ? err : info.bitsPerPixel; |
| 90 | } |
| 91 | |
| 92 | status_t getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info) |
| 93 | { |
Mathias Agopian | 6b8bef6 | 2012-04-24 23:27:31 -0700 | [diff] [blame] | 94 | if (format <= 0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | return BAD_VALUE; |
| 96 | |
| 97 | if (info->version != sizeof(PixelFormatInfo)) |
| 98 | return INVALID_OPERATION; |
| 99 | |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 100 | // YUV format from the HAL are handled here |
| 101 | switch (format) { |
| 102 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 103 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 104 | info->bitsPerPixel = 16; |
| 105 | goto done; |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 106 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
Mathias Agopian | 1764c73 | 2010-07-01 21:17:56 -0700 | [diff] [blame] | 107 | case HAL_PIXEL_FORMAT_YV12: |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 108 | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | size_t numEntries; |
Mathias Agopian | 42e2458 | 2012-02-21 18:56:08 -0800 | [diff] [blame] | 121 | const Info *i = gGetPixelFormatTable(&numEntries) + format; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | bool valid = uint32_t(format) < numEntries; |
| 123 | if (!valid) { |
| 124 | return BAD_INDEX; |
| 125 | } |
Mathias Agopian | 3db2164 | 2010-02-16 20:43:39 -0800 | [diff] [blame] | 126 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | 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 Agopian | 42e2458 | 2012-02-21 18:56:08 -0800 | [diff] [blame] | 138 | info->components = i->components; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | |
| 140 | return NO_ERROR; |
| 141 | } |
| 142 | |
Mathias Agopian | 42e2458 | 2012-02-21 18:56:08 -0800 | [diff] [blame] | 143 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | }; // namespace android |
Mathias Agopian | 42e2458 | 2012-02-21 18:56:08 -0800 | [diff] [blame] | 145 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | |