Bitmap.h revision ec4a4b13eae2241d1613890c1c1c096bed891845
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
24namespace android {
25
26enum class PixelStorageType {
27    External,
28    Heap,
29    Ashmem,
30};
31
32typedef void (*FreeFunc)(void* addr, void* context);
33
34class ANDROID_API Bitmap : public SkPixelRef {
35public:
36    static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap, SkColorTable* ctable);
37    static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info);
38
39    static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap, SkColorTable* ctable);
40    static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info,
41        size_t rowBytes, SkColorTable* ctable);
42
43    static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&);
44    Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes,
45            SkColorTable* ctable);
46    Bitmap(void* address, void* context, FreeFunc freeFunc,
47            const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable);
48    Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info,
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
57    // doing on a Bitmap type, not a SkPixelRef, so static
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
69    void setHasHardwareMipMap(bool hasMipMap);
70    bool hasHardwareMipMap() const;
71
72    bool isOpaque() const {return info().isOpaque(); }
73    SkColorType colorType() const { return info().colorType(); }
74    void getBounds(SkRect* bounds) const;
75
76protected:
77    virtual bool onNewLockPixels(LockRec* rec) override;
78    virtual void onUnlockPixels() override { };
79    virtual size_t getAllocatedSizeInBytes() const override;
80private:
81    virtual ~Bitmap();
82    void doFreePixels();
83    void* getStorage() const;
84
85    PixelStorageType mPixelStorageType;
86
87    size_t mRowBytes = 0;
88    sk_sp<SkColorTable> mColorTable;
89    bool mHasHardwareMipMap = false;
90
91    union {
92        struct {
93            void* address;
94            void* context;
95            FreeFunc freeFunc;
96        } external;
97        struct {
98            void* address;
99            int fd;
100            size_t size;
101        } ashmem;
102        struct {
103            void* address;
104            size_t size;
105        } heap;
106    } mPixelStorage;
107};
108
109} //namespace android