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 <SkColorSpace.h>
20#include <SkImage.h>
21#include <SkImageInfo.h>
22#include <SkPixelRef.h>
23#include <cutils/compiler.h>
24#include <ui/GraphicBuffer.h>
25#include <SkImage.h>
26
27namespace android {
28
29enum class PixelStorageType {
30    External,
31    Heap,
32    Ashmem,
33    Hardware,
34};
35
36namespace uirenderer {
37namespace renderthread {
38    class RenderThread;
39}
40}
41
42class PixelStorage;
43
44typedef void (*FreeFunc)(void* addr, void* context);
45
46class ANDROID_API Bitmap : public SkPixelRef {
47public:
48    static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap);
49    static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info);
50
51    static sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& bitmap);
52
53    static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap);
54    static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info,
55        size_t rowBytes);
56
57    static sk_sp<Bitmap> createFrom(sp<GraphicBuffer> graphicBuffer);
58
59    static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&);
60
61    Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes);
62    Bitmap(void* address, void* context, FreeFunc freeFunc,
63            const SkImageInfo& info, size_t rowBytes);
64    Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info,
65            size_t rowBytes);
66    Bitmap(GraphicBuffer* buffer, const SkImageInfo& info);
67
68    int rowBytesAsPixels() const {
69        return rowBytes() >> SkColorTypeShiftPerPixel(mInfo.colorType());
70    }
71
72    void reconfigure(const SkImageInfo& info, size_t rowBytes);
73    void reconfigure(const SkImageInfo& info);
74    void setColorSpace(sk_sp<SkColorSpace> colorSpace);
75    void setAlphaType(SkAlphaType alphaType);
76
77    void getSkBitmap(SkBitmap* outBitmap);
78
79    int getAshmemFd() const;
80    size_t getAllocationByteCount() const;
81
82    void setHasHardwareMipMap(bool hasMipMap);
83    bool hasHardwareMipMap() const;
84
85    bool isOpaque() const { return mInfo.isOpaque(); }
86    SkColorType colorType() const { return mInfo.colorType(); }
87    const SkImageInfo& info() const {
88        return mInfo;
89    }
90
91    void getBounds(SkRect* bounds) const;
92
93    bool isHardware() const {
94        return mPixelStorageType == PixelStorageType::Hardware;
95    }
96
97    GraphicBuffer* graphicBuffer();
98
99    // makeImage creates or returns a cached SkImage. Can be invoked from UI or render thread.
100    // Caching is supported only for HW Bitmaps with skia pipeline.
101    sk_sp<SkImage> makeImage();
102private:
103    virtual ~Bitmap();
104    void* getStorage() const;
105
106    SkImageInfo mInfo;
107
108    const PixelStorageType mPixelStorageType;
109
110    bool mHasHardwareMipMap = false;
111
112    union {
113        struct {
114            void* address;
115            void* context;
116            FreeFunc freeFunc;
117        } external;
118        struct {
119            void* address;
120            int fd;
121            size_t size;
122        } ashmem;
123        struct {
124            void* address;
125            size_t size;
126        } heap;
127        struct {
128            GraphicBuffer* buffer;
129        } hardware;
130    } mPixelStorage;
131
132    sk_sp<SkImage> mImage; // Cache is used only for HW Bitmaps with Skia pipeline.
133};
134
135} //namespace android
136