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> |
| 19 | #include <SkColorTable.h> |
| 20 | #include <SkImageInfo.h> |
| 21 | #include <SkPixelRef.h> |
| 22 | #include <cutils/compiler.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | enum class PixelStorageType { |
| 27 | External, |
| 28 | Heap, |
| 29 | Ashmem, |
| 30 | }; |
| 31 | |
| 32 | typedef void (*FreeFunc)(void* addr, void* context); |
| 33 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 34 | class ANDROID_API Bitmap : public SkPixelRef { |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 35 | public: |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 36 | static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap, SkColorTable* ctable); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame^] | 37 | static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info); |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 38 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 39 | static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap, SkColorTable* ctable); |
| 40 | static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info, |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 41 | size_t rowBytes, SkColorTable* ctable); |
| 42 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 43 | static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&); |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 44 | Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes, |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 45 | SkColorTable* ctable); |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 46 | Bitmap(void* address, void* context, FreeFunc freeFunc, |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 47 | const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable); |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 48 | Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info, |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 49 | size_t rowBytes, SkColorTable* ctable); |
| 50 | |
| 51 | int width() const { return info().width(); } |
| 52 | int height() const { return info().height(); } |
| 53 | |
| 54 | // Can't mark as override since SkPixelRef::rowBytes isn't virtual |
| 55 | // but that's OK since we just want Bitmap to be able to rely |
| 56 | // on calling rowBytes() on an unlocked pixelref, which it will be |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 57 | // doing on a Bitmap type, not a SkPixelRef, so static |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 58 | // dispatching will do what we want. |
| 59 | size_t rowBytes() const { return mRowBytes; } |
| 60 | void reconfigure(const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable); |
| 61 | void reconfigure(const SkImageInfo& info); |
| 62 | void setAlphaType(SkAlphaType alphaType); |
| 63 | |
| 64 | void getSkBitmap(SkBitmap* outBitmap); |
| 65 | |
| 66 | int getAshmemFd() const; |
| 67 | size_t getAllocationByteCount() const; |
| 68 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 69 | void setHasHardwareMipMap(bool hasMipMap); |
| 70 | bool hasHardwareMipMap() const; |
| 71 | |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 72 | protected: |
| 73 | virtual bool onNewLockPixels(LockRec* rec) override; |
| 74 | virtual void onUnlockPixels() override { }; |
| 75 | virtual size_t getAllocatedSizeInBytes() const override; |
| 76 | private: |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 77 | virtual ~Bitmap(); |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 78 | void doFreePixels(); |
| 79 | void* getStorage() const; |
sergeyv | 163f881 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 80 | |
| 81 | PixelStorageType mPixelStorageType; |
| 82 | |
| 83 | size_t mRowBytes = 0; |
| 84 | sk_sp<SkColorTable> mColorTable; |
| 85 | bool mHasHardwareMipMap = false; |
| 86 | |
| 87 | union { |
| 88 | struct { |
| 89 | void* address; |
| 90 | void* context; |
| 91 | FreeFunc freeFunc; |
| 92 | } external; |
| 93 | struct { |
| 94 | void* address; |
| 95 | int fd; |
| 96 | size_t size; |
| 97 | } ashmem; |
| 98 | struct { |
| 99 | void* address; |
| 100 | size_t size; |
| 101 | } heap; |
| 102 | } mPixelStorage; |
| 103 | }; |
| 104 | |
| 105 | } //namespace android |