sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #pragma once |
| 17 | |
| 18 | #include <SkBitmap.h> |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 19 | #include <SkColorFilter.h> |
Romain Guy | 8242656 | 2017-04-04 19:38:50 -0700 | [diff] [blame] | 20 | #include <SkColorSpace.h> |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 21 | #include <SkImage.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include <SkImage.h> |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 23 | #include <SkImageInfo.h> |
| 24 | #include <SkPixelRef.h> |
| 25 | #include <cutils/compiler.h> |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 26 | #include <ui/GraphicBuffer.h> |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | enum class PixelStorageType { |
| 31 | External, |
| 32 | Heap, |
| 33 | Ashmem, |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 34 | Hardware, |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 37 | namespace uirenderer { |
| 38 | namespace renderthread { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | class RenderThread; |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | class PixelStorage; |
| 44 | |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 45 | typedef void (*FreeFunc)(void* addr, void* context); |
| 46 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 47 | class ANDROID_API Bitmap : public SkPixelRef { |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 48 | public: |
Leon Scroggins III | f51a80d | 2017-07-12 10:46:35 -0400 | [diff] [blame] | 49 | static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 50 | static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info); |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 51 | |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 52 | static sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& bitmap); |
| 53 | |
Leon Scroggins III | f51a80d | 2017-07-12 10:46:35 -0400 | [diff] [blame] | 54 | static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap); |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 55 | static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 56 | size_t rowBytes); |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 57 | |
sergeyv | 9a02987 | 2016-11-29 10:13:28 -0800 | [diff] [blame] | 58 | static sk_sp<Bitmap> createFrom(sp<GraphicBuffer> graphicBuffer); |
| 59 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 60 | static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&); |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 61 | |
Leon Scroggins III | f51a80d | 2017-07-12 10:46:35 -0400 | [diff] [blame] | 62 | Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 63 | Bitmap(void* address, void* context, FreeFunc freeFunc, const SkImageInfo& info, |
| 64 | size_t rowBytes); |
| 65 | Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info, size_t rowBytes); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 66 | Bitmap(GraphicBuffer* buffer, const SkImageInfo& info); |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 67 | |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 68 | int rowBytesAsPixels() const { |
Mike Reed | 937c694 | 2018-02-08 11:36:49 -0500 | [diff] [blame] | 69 | return rowBytes() >> mInfo.shiftPerPixel(); |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Leon Scroggins III | f51a80d | 2017-07-12 10:46:35 -0400 | [diff] [blame] | 72 | void reconfigure(const SkImageInfo& info, size_t rowBytes); |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 73 | void reconfigure(const SkImageInfo& info); |
Romain Guy | 8242656 | 2017-04-04 19:38:50 -0700 | [diff] [blame] | 74 | void setColorSpace(sk_sp<SkColorSpace> colorSpace); |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 75 | void setAlphaType(SkAlphaType alphaType); |
| 76 | |
| 77 | void getSkBitmap(SkBitmap* outBitmap); |
| 78 | |
| 79 | int getAshmemFd() const; |
| 80 | size_t getAllocationByteCount() const; |
| 81 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 82 | void setHasHardwareMipMap(bool hasMipMap); |
| 83 | bool hasHardwareMipMap() const; |
| 84 | |
Matt Sarett | 489cfff | 2017-04-21 18:08:40 -0400 | [diff] [blame] | 85 | bool isOpaque() const { return mInfo.isOpaque(); } |
| 86 | SkColorType colorType() const { return mInfo.colorType(); } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 87 | const SkImageInfo& info() const { return mInfo; } |
Matt Sarett | 489cfff | 2017-04-21 18:08:40 -0400 | [diff] [blame] | 88 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 89 | void getBounds(SkRect* bounds) const; |
| 90 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 | bool isHardware() const { return mPixelStorageType == PixelStorageType::Hardware; } |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 92 | |
| 93 | GraphicBuffer* graphicBuffer(); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 94 | |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 95 | /** |
| 96 | * Creates or returns a cached SkImage and is safe to be invoked from either |
| 97 | * the UI or RenderThread. |
| 98 | * |
| 99 | * @param outputColorFilter is a required param that will be populated by |
| 100 | * this function if the bitmap's colorspace is not sRGB. If populated the |
| 101 | * filter will convert colors from the bitmaps colorspace into sRGB. It |
| 102 | * is the callers responsibility to use this colorFilter when drawing |
| 103 | * this image into any destination that is presumed to be sRGB (i.e. a |
| 104 | * buffer that has no colorspace defined). |
| 105 | */ |
| 106 | sk_sp<SkImage> makeImage(sk_sp<SkColorFilter>* outputColorFilter); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 107 | |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 108 | private: |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 109 | virtual ~Bitmap(); |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 110 | void* getStorage() const; |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 111 | |
Matt Sarett | 489cfff | 2017-04-21 18:08:40 -0400 | [diff] [blame] | 112 | SkImageInfo mInfo; |
| 113 | |
Mike Reed | 63df65d | 2017-04-10 13:31:28 -0400 | [diff] [blame] | 114 | const PixelStorageType mPixelStorageType; |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 115 | |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 116 | bool mHasHardwareMipMap = false; |
| 117 | |
| 118 | union { |
| 119 | struct { |
| 120 | void* address; |
| 121 | void* context; |
| 122 | FreeFunc freeFunc; |
| 123 | } external; |
| 124 | struct { |
| 125 | void* address; |
| 126 | int fd; |
| 127 | size_t size; |
| 128 | } ashmem; |
| 129 | struct { |
| 130 | void* address; |
| 131 | size_t size; |
| 132 | } heap; |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 133 | struct { |
| 134 | GraphicBuffer* buffer; |
| 135 | } hardware; |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 136 | } mPixelStorage; |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 137 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 138 | sk_sp<SkImage> mImage; // Cache is used only for HW Bitmaps with Skia pipeline. |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 141 | } // namespace android |