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 <SkColorFilter.h> 20 #include <SkColorSpace.h> 21 #include <SkImage.h> 22 #include <SkImage.h> 23 #include <SkImageInfo.h> 24 #include <SkPixelRef.h> 25 #include <cutils/compiler.h> 26 #include <ui/GraphicBuffer.h> 27 28 namespace android { 29 30 enum class PixelStorageType { 31 External, 32 Heap, 33 Ashmem, 34 Hardware, 35 }; 36 37 namespace uirenderer { 38 namespace renderthread { 39 class RenderThread; 40 } 41 } 42 43 class PixelStorage; 44 45 typedef void (*FreeFunc)(void* addr, void* context); 46 47 class ANDROID_API Bitmap : public SkPixelRef { 48 public: 49 static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap); 50 static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info); 51 52 static sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& bitmap); 53 54 static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap); 55 static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info, 56 size_t rowBytes); 57 58 static sk_sp<Bitmap> createFrom(sp<GraphicBuffer> graphicBuffer); 59 60 static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&); 61 62 Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes); 63 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); 66 Bitmap(GraphicBuffer* buffer, const SkImageInfo& info); 67 rowBytesAsPixels()68 int rowBytesAsPixels() const { 69 return rowBytes() >> mInfo.shiftPerPixel(); 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 isOpaque()85 bool isOpaque() const { return mInfo.isOpaque(); } colorType()86 SkColorType colorType() const { return mInfo.colorType(); } info()87 const SkImageInfo& info() const { return mInfo; } 88 89 void getBounds(SkRect* bounds) const; 90 isHardware()91 bool isHardware() const { return mPixelStorageType == PixelStorageType::Hardware; } 92 93 GraphicBuffer* graphicBuffer(); 94 95 /** 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); 107 108 private: 109 virtual ~Bitmap(); 110 void* getStorage() const; 111 112 SkImageInfo mInfo; 113 114 const PixelStorageType mPixelStorageType; 115 116 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; 133 struct { 134 GraphicBuffer* buffer; 135 } hardware; 136 } mPixelStorage; 137 138 sk_sp<SkImage> mImage; // Cache is used only for HW Bitmaps with Skia pipeline. 139 }; 140 141 } // namespace android 142