blob: 029d80d6279fcf6e6bf31649327036dd7418794b [file] [log] [blame]
sergeyv163f8812016-10-07 16:57:29 -07001/*
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
24namespace android {
25
26enum class PixelStorageType {
27 External,
28 Heap,
29 Ashmem,
30};
31
32typedef void (*FreeFunc)(void* addr, void* context);
33
sergeyvc1c54062016-10-19 18:47:26 -070034class ANDROID_API Bitmap : public SkPixelRef {
sergeyv163f8812016-10-07 16:57:29 -070035public:
sergeyvc1c54062016-10-19 18:47:26 -070036 static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap, SkColorTable* ctable);
sergeyvfc9999502016-10-17 13:07:38 -070037 static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info);
sergeyvc36bd6c2016-10-11 15:49:16 -070038
sergeyvc1c54062016-10-19 18:47:26 -070039 static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap, SkColorTable* ctable);
40 static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info,
sergeyvc36bd6c2016-10-11 15:49:16 -070041 size_t rowBytes, SkColorTable* ctable);
42
sergeyvaed7f582016-10-14 16:30:21 -070043 static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&);
sergeyvc1c54062016-10-19 18:47:26 -070044 Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes,
sergeyv163f8812016-10-07 16:57:29 -070045 SkColorTable* ctable);
sergeyvc1c54062016-10-19 18:47:26 -070046 Bitmap(void* address, void* context, FreeFunc freeFunc,
sergeyv163f8812016-10-07 16:57:29 -070047 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable);
sergeyvc1c54062016-10-19 18:47:26 -070048 Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info,
sergeyv163f8812016-10-07 16:57:29 -070049 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
sergeyvc1c54062016-10-19 18:47:26 -070057 // doing on a Bitmap type, not a SkPixelRef, so static
sergeyv163f8812016-10-07 16:57:29 -070058 // 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
sergeyvc1c54062016-10-19 18:47:26 -070069 void setHasHardwareMipMap(bool hasMipMap);
70 bool hasHardwareMipMap() const;
71
sergeyv163f8812016-10-07 16:57:29 -070072protected:
73 virtual bool onNewLockPixels(LockRec* rec) override;
74 virtual void onUnlockPixels() override { };
75 virtual size_t getAllocatedSizeInBytes() const override;
76private:
sergeyvc1c54062016-10-19 18:47:26 -070077 virtual ~Bitmap();
sergeyv163f8812016-10-07 16:57:29 -070078 void doFreePixels();
79 void* getStorage() const;
sergeyv163f8812016-10-07 16:57:29 -070080
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