Bitmap.h revision 554ffeb8b7c836da43a637c59eedfc617895b19d
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#include <ui/GraphicBuffer.h> 24 25namespace android { 26 27enum class PixelStorageType { 28 External, 29 Heap, 30 Ashmem, 31 Hardware, 32}; 33 34namespace uirenderer { 35namespace renderthread { 36 class RenderThread; 37} 38} 39 40class PixelStorage; 41 42typedef void (*FreeFunc)(void* addr, void* context); 43 44class ANDROID_API Bitmap : public SkPixelRef { 45public: 46 static sk_sp<Bitmap> allocateHeapBitmap(SkBitmap* bitmap, SkColorTable* ctable); 47 static sk_sp<Bitmap> allocateHeapBitmap(const SkImageInfo& info); 48 49 static sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& bitmap); 50 51 static sk_sp<Bitmap> allocateAshmemBitmap(SkBitmap* bitmap, SkColorTable* ctable); 52 static sk_sp<Bitmap> allocateAshmemBitmap(size_t allocSize, const SkImageInfo& info, 53 size_t rowBytes, SkColorTable* ctable); 54 55 static sk_sp<Bitmap> createFrom(const SkImageInfo&, SkPixelRef&); 56 57 static sk_sp<Bitmap> allocateHardwareBitmap(uirenderer::renderthread::RenderThread&, 58 SkBitmap& bitmap); 59 60 Bitmap(void* address, size_t allocSize, const SkImageInfo& info, size_t rowBytes, 61 SkColorTable* ctable); 62 Bitmap(void* address, void* context, FreeFunc freeFunc, 63 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable); 64 Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info, 65 size_t rowBytes, SkColorTable* ctable); 66 Bitmap(sp<GraphicBuffer>&& buffer, const SkImageInfo& info); 67 68 int width() const { return info().width(); } 69 int height() const { return info().height(); } 70 71 // Can't mark as override since SkPixelRef::rowBytes isn't virtual 72 // but that's OK since we just want Bitmap to be able to rely 73 // on calling rowBytes() on an unlocked pixelref, which it will be 74 // doing on a Bitmap type, not a SkPixelRef, so static 75 // dispatching will do what we want. 76 size_t rowBytes() const { return mRowBytes; } 77 78 int rowBytesAsPixels() const { 79 return mRowBytes >> info().shiftPerPixel(); 80 } 81 82 void reconfigure(const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable); 83 void reconfigure(const SkImageInfo& info); 84 void setAlphaType(SkAlphaType alphaType); 85 86 void getSkBitmap(SkBitmap* outBitmap); 87 88 // Ugly hack: in case of hardware bitmaps, it sets nullptr as pixels pointer 89 // so it would crash if anyone tries to render this bitmap. 90 void getSkBitmapForShaders(SkBitmap* outBitmap); 91 92 int getAshmemFd() const; 93 size_t getAllocationByteCount() const; 94 95 void setHasHardwareMipMap(bool hasMipMap); 96 bool hasHardwareMipMap() const; 97 98 bool isOpaque() const {return info().isOpaque(); } 99 SkColorType colorType() const { return info().colorType(); } 100 void getBounds(SkRect* bounds) const; 101 102 bool readyToDraw() const { 103 return this->colorType() != kIndex_8_SkColorType || mColorTable; 104 } 105 106 bool isHardware() const { 107 return mPixelStorageType == PixelStorageType::Hardware; 108 } 109 110 GraphicBuffer* graphicBuffer(); 111protected: 112 virtual bool onNewLockPixels(LockRec* rec) override; 113 virtual void onUnlockPixels() override { }; 114 virtual size_t getAllocatedSizeInBytes() const override; 115private: 116 virtual ~Bitmap(); 117 void* getStorage() const; 118 119 PixelStorageType mPixelStorageType; 120 121 size_t mRowBytes = 0; 122 sk_sp<SkColorTable> mColorTable; 123 bool mHasHardwareMipMap = false; 124 125 union { 126 struct { 127 void* address; 128 void* context; 129 FreeFunc freeFunc; 130 } external; 131 struct { 132 void* address; 133 int fd; 134 size_t size; 135 } ashmem; 136 struct { 137 void* address; 138 size_t size; 139 } heap; 140 struct { 141 GraphicBuffer* buffer; 142 } hardware; 143 } mPixelStorage; 144}; 145 146} //namespace android