blob: 3ade3fe95cc31ab38a29666900c831ca298adb43 [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>
Romain Guy82426562017-04-04 19:38:50 -070019#include <SkColorSpace.h>
sergeyv163f8812016-10-07 16:57:29 -070020#include <SkColorTable.h>
21#include <SkImageInfo.h>
22#include <SkPixelRef.h>
23#include <cutils/compiler.h>
sergeyv694d4992016-10-27 10:23:13 -070024#include <ui/GraphicBuffer.h>
sergeyv163f8812016-10-07 16:57:29 -070025
26namespace android {
27
28enum class PixelStorageType {
29 External,
30 Heap,
31 Ashmem,
sergeyv694d4992016-10-27 10:23:13 -070032 Hardware,
sergeyv163f8812016-10-07 16:57:29 -070033};
34
sergeyv694d4992016-10-27 10:23:13 -070035namespace uirenderer {
36namespace renderthread {
37 class RenderThread;
38}
39}
40
41class PixelStorage;
42
sergeyv163f8812016-10-07 16:57:29 -070043typedef void (*FreeFunc)(void* addr, void* context);
44
sergeyvc1c54062016-10-19 18:47:26 -070045class ANDROID_API Bitmap : public SkPixelRef {
sergeyv163f8812016-10-07 16:57:29 -070046public:
sergeyvc1c54062016-10-19 18:47:26 -070047 static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap, SkColorTable* ctable);
sergeyvfc9999502016-10-17 13:07:38 -070048 static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info);
sergeyvc36bd6c2016-10-11 15:49:16 -070049
sergeyv694d4992016-10-27 10:23:13 -070050 static sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& bitmap);
51
sergeyvc1c54062016-10-19 18:47:26 -070052 static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap, SkColorTable* ctable);
53 static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info,
sergeyvc36bd6c2016-10-11 15:49:16 -070054 size_t rowBytes, SkColorTable* ctable);
55
sergeyv9a029872016-11-29 10:13:28 -080056 static sk_sp<Bitmap> createFrom(sp<GraphicBuffer> graphicBuffer);
57
sergeyvaed7f582016-10-14 16:30:21 -070058 static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&);
sergeyv694d4992016-10-27 10:23:13 -070059
60 static sk_sp<Bitmap> allocateHardwareBitmap(uirenderer::renderthread::RenderThread&,
61 SkBitmap& bitmap);
62
sergeyvc1c54062016-10-19 18:47:26 -070063 Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes,
sergeyv163f8812016-10-07 16:57:29 -070064 SkColorTable* ctable);
sergeyvc1c54062016-10-19 18:47:26 -070065 Bitmap(void* address, void* context, FreeFunc freeFunc,
sergeyv163f8812016-10-07 16:57:29 -070066 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable);
sergeyvc1c54062016-10-19 18:47:26 -070067 Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info,
sergeyv163f8812016-10-07 16:57:29 -070068 size_t rowBytes, SkColorTable* ctable);
69
70 int width() const { return info().width(); }
71 int height() const { return info().height(); }
72
73 // Can't mark as override since SkPixelRef::rowBytes isn't virtual
74 // but that's OK since we just want Bitmap to be able to rely
75 // on calling rowBytes() on an unlocked pixelref, which it will be
sergeyvc1c54062016-10-19 18:47:26 -070076 // doing on a Bitmap type, not a SkPixelRef, so static
sergeyv163f8812016-10-07 16:57:29 -070077 // dispatching will do what we want.
78 size_t rowBytes() const { return mRowBytes; }
sergeyv98fa4f92016-10-24 15:35:21 -070079
80 int rowBytesAsPixels() const {
81 return mRowBytes >> info().shiftPerPixel();
82 }
83
sergeyv163f8812016-10-07 16:57:29 -070084 void reconfigure(const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable);
85 void reconfigure(const SkImageInfo& info);
Romain Guy82426562017-04-04 19:38:50 -070086 void setColorSpace(sk_sp<SkColorSpace> colorSpace);
sergeyv163f8812016-10-07 16:57:29 -070087 void setAlphaType(SkAlphaType alphaType);
88
89 void getSkBitmap(SkBitmap* outBitmap);
90
sergeyv554ffeb2016-11-15 18:01:21 -080091 // Ugly hack: in case of hardware bitmaps, it sets nullptr as pixels pointer
92 // so it would crash if anyone tries to render this bitmap.
93 void getSkBitmapForShaders(SkBitmap* outBitmap);
94
sergeyv163f8812016-10-07 16:57:29 -070095 int getAshmemFd() const;
96 size_t getAllocationByteCount() const;
97
sergeyvc1c54062016-10-19 18:47:26 -070098 void setHasHardwareMipMap(bool hasMipMap);
99 bool hasHardwareMipMap() const;
100
sergeyvec4a4b12016-10-20 18:39:04 -0700101 bool isOpaque() const {return info().isOpaque(); }
102 SkColorType colorType() const { return info().colorType(); }
103 void getBounds(SkRect* bounds) const;
104
sergeyv98fa4f92016-10-24 15:35:21 -0700105 bool readyToDraw() const {
106 return this->colorType() != kIndex_8_SkColorType || mColorTable;
107 }
sergeyv694d4992016-10-27 10:23:13 -0700108
109 bool isHardware() const {
110 return mPixelStorageType == PixelStorageType::Hardware;
111 }
112
113 GraphicBuffer* graphicBuffer();
sergeyv163f8812016-10-07 16:57:29 -0700114protected:
115 virtual bool onNewLockPixels(LockRec* rec) override;
116 virtual void onUnlockPixels() override { };
117 virtual size_t getAllocatedSizeInBytes() const override;
118private:
sergeyv9a029872016-11-29 10:13:28 -0800119 Bitmap(GraphicBuffer* buffer, const SkImageInfo& info);
sergeyvc1c54062016-10-19 18:47:26 -0700120 virtual ~Bitmap();
sergeyv163f8812016-10-07 16:57:29 -0700121 void* getStorage() const;
sergeyv163f8812016-10-07 16:57:29 -0700122
Mike Reed63df65d2017-04-10 13:31:28 -0400123 const PixelStorageType mPixelStorageType;
sergeyv163f8812016-10-07 16:57:29 -0700124
125 size_t mRowBytes = 0;
126 sk_sp<SkColorTable> mColorTable;
127 bool mHasHardwareMipMap = false;
128
129 union {
130 struct {
131 void* address;
132 void* context;
133 FreeFunc freeFunc;
134 } external;
135 struct {
136 void* address;
137 int fd;
138 size_t size;
139 } ashmem;
140 struct {
141 void* address;
142 size_t size;
143 } heap;
sergeyv694d4992016-10-27 10:23:13 -0700144 struct {
145 GraphicBuffer* buffer;
146 } hardware;
sergeyv163f8812016-10-07 16:57:29 -0700147 } mPixelStorage;
148};
149
150} //namespace android