Sally Qi | 587fb57 | 2023-03-03 15:50:06 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2023 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 | #include "Gainmap.h" |
| 17 | |
John Reck | 947eade | 2024-09-27 17:59:37 -0400 | [diff] [blame] | 18 | #include <SkBitmap.h> |
| 19 | #include <SkCanvas.h> |
| 20 | #include <SkColorFilter.h> |
| 21 | #include <SkImagePriv.h> |
| 22 | #include <SkPaint.h> |
| 23 | |
| 24 | #include "HardwareBitmapUploader.h" |
| 25 | |
Sally Qi | 587fb57 | 2023-03-03 15:50:06 -0800 | [diff] [blame] | 26 | namespace android::uirenderer { |
| 27 | |
| 28 | sp<Gainmap> Gainmap::allocateHardwareGainmap(const sp<Gainmap>& srcGainmap) { |
| 29 | auto gainmap = sp<Gainmap>::make(); |
| 30 | gainmap->info = srcGainmap->info; |
John Reck | 947eade | 2024-09-27 17:59:37 -0400 | [diff] [blame] | 31 | SkBitmap skSrcBitmap = srcGainmap->bitmap->getSkBitmap(); |
| 32 | if (skSrcBitmap.info().colorType() == kAlpha_8_SkColorType && |
| 33 | !HardwareBitmapUploader::hasAlpha8Support()) { |
| 34 | // The regular Bitmap::allocateHardwareBitmap will do a conversion that preserves channels, |
| 35 | // so alpha8 maps to the alpha channel of rgba. However, for gainmaps we will interpret |
| 36 | // the data of an rgba buffer differently as we'll only look at the rgb channels |
| 37 | // So we need to map alpha8 to rgbx_8888 essentially |
| 38 | SkBitmap bitmap; |
| 39 | bitmap.allocPixels(skSrcBitmap.info().makeColorType(kN32_SkColorType)); |
| 40 | SkCanvas canvas(bitmap); |
| 41 | SkPaint paint; |
| 42 | const float alphaToOpaque[] = {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, |
| 43 | 0, 0, 0, 1, 0, 0, 0, 0, 0, 255}; |
| 44 | paint.setColorFilter(SkColorFilters::Matrix(alphaToOpaque, SkColorFilters::Clamp::kNo)); |
| 45 | canvas.drawImage(SkMakeImageFromRasterBitmap(skSrcBitmap, kNever_SkCopyPixelsMode), 0, 0, |
| 46 | SkSamplingOptions{}, &paint); |
| 47 | skSrcBitmap = bitmap; |
| 48 | } |
Sally Qi | 587fb57 | 2023-03-03 15:50:06 -0800 | [diff] [blame] | 49 | sk_sp<Bitmap> skBitmap(Bitmap::allocateHardwareBitmap(skSrcBitmap)); |
| 50 | if (!skBitmap.get()) { |
| 51 | return nullptr; |
| 52 | } |
| 53 | gainmap->bitmap = std::move(skBitmap); |
| 54 | return gainmap; |
| 55 | } |
| 56 | |
| 57 | } // namespace android::uirenderer |