blob: 4f06656e8e6d6aa156fe10b2b82ba72ec93c03fe [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>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040019#include <SkColorFilter.h>
Romain Guy82426562017-04-04 19:38:50 -070020#include <SkColorSpace.h>
Stan Iliev7bc3bc62017-05-24 13:28:36 -040021#include <SkImage.h>
John Reck1bcacfd2017-11-03 10:12:19 -070022#include <SkImage.h>
sergeyv163f8812016-10-07 16:57:29 -070023#include <SkImageInfo.h>
24#include <SkPixelRef.h>
25#include <cutils/compiler.h>
sergeyv694d4992016-10-27 10:23:13 -070026#include <ui/GraphicBuffer.h>
sergeyv163f8812016-10-07 16:57:29 -070027
28namespace android {
29
30enum class PixelStorageType {
31 External,
32 Heap,
33 Ashmem,
sergeyv694d4992016-10-27 10:23:13 -070034 Hardware,
sergeyv163f8812016-10-07 16:57:29 -070035};
36
sergeyv694d4992016-10-27 10:23:13 -070037namespace uirenderer {
38namespace renderthread {
John Reck1bcacfd2017-11-03 10:12:19 -070039class RenderThread;
sergeyv694d4992016-10-27 10:23:13 -070040}
41}
42
43class PixelStorage;
44
sergeyv163f8812016-10-07 16:57:29 -070045typedef void (*FreeFunc)(void* addr, void* context);
46
sergeyvc1c54062016-10-19 18:47:26 -070047class ANDROID_API Bitmap : public SkPixelRef {
sergeyv163f8812016-10-07 16:57:29 -070048public:
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -040049 static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap);
sergeyvfc9999502016-10-17 13:07:38 -070050 static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info);
sergeyvc36bd6c2016-10-11 15:49:16 -070051
sergeyv694d4992016-10-27 10:23:13 -070052 static sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& bitmap);
53
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -040054 static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap);
sergeyvc1c54062016-10-19 18:47:26 -070055 static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info,
John Reck1bcacfd2017-11-03 10:12:19 -070056 size_t rowBytes);
sergeyvc36bd6c2016-10-11 15:49:16 -070057
sergeyv9a029872016-11-29 10:13:28 -080058 static sk_sp<Bitmap> createFrom(sp<GraphicBuffer> graphicBuffer);
59
sergeyvaed7f582016-10-14 16:30:21 -070060 static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&);
sergeyv694d4992016-10-27 10:23:13 -070061
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -040062 Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes);
John Reck1bcacfd2017-11-03 10:12:19 -070063 Bitmap(void* address, void* context, FreeFunc freeFunc, const SkImageInfo& info,
64 size_t rowBytes);
65 Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info, size_t rowBytes);
Stan Iliev7bc3bc62017-05-24 13:28:36 -040066 Bitmap(GraphicBuffer* buffer, const SkImageInfo& info);
sergeyv163f8812016-10-07 16:57:29 -070067
sergeyv98fa4f92016-10-24 15:35:21 -070068 int rowBytesAsPixels() const {
Mike Reed937c6942018-02-08 11:36:49 -050069 return rowBytes() >> mInfo.shiftPerPixel();
sergeyv98fa4f92016-10-24 15:35:21 -070070 }
71
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -040072 void reconfigure(const SkImageInfo& info, size_t rowBytes);
sergeyv163f8812016-10-07 16:57:29 -070073 void reconfigure(const SkImageInfo& info);
Romain Guy82426562017-04-04 19:38:50 -070074 void setColorSpace(sk_sp<SkColorSpace> colorSpace);
sergeyv163f8812016-10-07 16:57:29 -070075 void setAlphaType(SkAlphaType alphaType);
76
77 void getSkBitmap(SkBitmap* outBitmap);
78
79 int getAshmemFd() const;
80 size_t getAllocationByteCount() const;
81
sergeyvc1c54062016-10-19 18:47:26 -070082 void setHasHardwareMipMap(bool hasMipMap);
83 bool hasHardwareMipMap() const;
84
Matt Sarett489cfff2017-04-21 18:08:40 -040085 bool isOpaque() const { return mInfo.isOpaque(); }
86 SkColorType colorType() const { return mInfo.colorType(); }
John Reck1bcacfd2017-11-03 10:12:19 -070087 const SkImageInfo& info() const { return mInfo; }
Matt Sarett489cfff2017-04-21 18:08:40 -040088
sergeyvec4a4b12016-10-20 18:39:04 -070089 void getBounds(SkRect* bounds) const;
90
John Reck1bcacfd2017-11-03 10:12:19 -070091 bool isHardware() const { return mPixelStorageType == PixelStorageType::Hardware; }
sergeyv694d4992016-10-27 10:23:13 -070092
93 GraphicBuffer* graphicBuffer();
Stan Iliev7bc3bc62017-05-24 13:28:36 -040094
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040095 /**
96 * Creates or returns a cached SkImage and is safe to be invoked from either
97 * the UI or RenderThread.
98 *
99 * @param outputColorFilter is a required param that will be populated by
100 * this function if the bitmap's colorspace is not sRGB. If populated the
101 * filter will convert colors from the bitmaps colorspace into sRGB. It
102 * is the callers responsibility to use this colorFilter when drawing
103 * this image into any destination that is presumed to be sRGB (i.e. a
104 * buffer that has no colorspace defined).
105 */
106 sk_sp<SkImage> makeImage(sk_sp<SkColorFilter>* outputColorFilter);
John Reck1bcacfd2017-11-03 10:12:19 -0700107
sergeyv163f8812016-10-07 16:57:29 -0700108private:
sergeyvc1c54062016-10-19 18:47:26 -0700109 virtual ~Bitmap();
sergeyv163f8812016-10-07 16:57:29 -0700110 void* getStorage() const;
sergeyv163f8812016-10-07 16:57:29 -0700111
Matt Sarett489cfff2017-04-21 18:08:40 -0400112 SkImageInfo mInfo;
113
Mike Reed63df65d2017-04-10 13:31:28 -0400114 const PixelStorageType mPixelStorageType;
sergeyv163f8812016-10-07 16:57:29 -0700115
sergeyv163f8812016-10-07 16:57:29 -0700116 bool mHasHardwareMipMap = false;
117
118 union {
119 struct {
120 void* address;
121 void* context;
122 FreeFunc freeFunc;
123 } external;
124 struct {
125 void* address;
126 int fd;
127 size_t size;
128 } ashmem;
129 struct {
130 void* address;
131 size_t size;
132 } heap;
sergeyv694d4992016-10-27 10:23:13 -0700133 struct {
134 GraphicBuffer* buffer;
135 } hardware;
sergeyv163f8812016-10-07 16:57:29 -0700136 } mPixelStorage;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400137
John Reck1bcacfd2017-11-03 10:12:19 -0700138 sk_sp<SkImage> mImage; // Cache is used only for HW Bitmaps with Skia pipeline.
sergeyv163f8812016-10-07 16:57:29 -0700139};
140
John Reck1bcacfd2017-11-03 10:12:19 -0700141} // namespace android