1 /* 2 * Copyright (C) 2007-2009 Torch Mobile, Inc. All rights reserved. 3 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com> 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21 #ifndef SharedBitmap_h 22 #define SharedBitmap_h 23 24 #include "AffineTransform.h" 25 #include "BitmapInfo.h" 26 #include "ColorSpace.h" 27 #include "GraphicsTypes.h" 28 #include <wtf/PassOwnPtr.h> 29 #include <wtf/PassRefPtr.h> 30 #include <wtf/OwnArrayPtr.h> 31 #include <wtf/RefCounted.h> 32 #include <wtf/Vector.h> 33 #include <wingdi.h> 34 35 namespace WebCore { 36 37 class FloatPoint; 38 class FloatRect; 39 class GraphicsContext; 40 class IntRect; 41 class IntSize; 42 class TransformationMatrix; 43 44 class SharedBitmap: public RefCounted<SharedBitmap> { 45 public: 46 ~SharedBitmap(); 47 static PassRefPtr<SharedBitmap> create(const IntSize&, BitmapInfo::BitCount = BitmapInfo::BitCount32, bool initPixels = true); 48 static PassRefPtr<SharedBitmap> create(const Vector<unsigned>&, const IntSize&, bool hasAlpha = true); 49 bitmapInfo()50 const BitmapInfo& bitmapInfo() const { return m_bmpInfo; } bytes()51 void* bytes() { return m_pixels; } bytes()52 const void* bytes() const { return m_pixels; } width()53 unsigned width() const { return m_bmpInfo.width(); } height()54 unsigned height() const { return m_bmpInfo.height(); } validHeight()55 unsigned validHeight() const { return m_validHeight; } setValidHeight(unsigned validHeight)56 void setValidHeight(unsigned validHeight) { m_validHeight = validHeight; } 57 void resetPixels(bool black = false); 58 void clearPixels(const IntRect& r); locked()59 bool locked() const { return m_locked; } lock()60 void lock() { m_locked = true; } unlock()61 void unlock() { m_locked = false; } 62 bool freeMemory(); is16bit()63 bool is16bit() const { return m_bmpInfo.is16bit(); } is32bit()64 bool is32bit() const { return m_bmpInfo.is32bit(); } 65 bool to16bit(); hasAlpha()66 bool hasAlpha() const { return m_hasAlpha; } setHasAlpha(bool alpha)67 void setHasAlpha(bool alpha) { m_hasAlpha = alpha; } 68 bool ensureHandle(); getHandle()69 HBITMAP getHandle() { return m_hbitmap.get(); } 70 PassOwnPtr<HBITMAP> createHandle(void** pixels, BitmapInfo* bmpInfo, int h = -1, bool use16bit = true) const; usesTransparentColor()71 bool usesTransparentColor() const { return m_usesTransparentColor; } transparentColor()72 COLORREF transparentColor() const { return m_transparentColor; } setTransparentColor(COLORREF c)73 void setTransparentColor(COLORREF c) 74 { 75 m_usesTransparentColor = true; 76 m_transparentColor = c; 77 } canUseDIBits()78 bool canUseDIBits() const { return !hasAlpha() && !usesTransparentColor(); } 79 80 PassOwnPtr<HBITMAP> clipBitmap(const IntRect& rect, bool useAlpha, BitmapInfo& bmpInfo, void*& pixels); 81 82 PassRefPtr<SharedBitmap> clipBitmap(const IntRect& rect, bool useAlpha); 83 84 void draw(GraphicsContext* ctxt, const IntRect& dstRect, const IntRect& srcRect, ColorSpace styleColorSpace, CompositeOperator compositeOp); 85 void drawPattern(GraphicsContext* ctxt, const FloatRect& tileRectIn, const AffineTransform& patternTransform, 86 const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize); 87 void draw(HDC, const IntRect& dstRect, const IntRect& srcRect, CompositeOperator compositeOp); 88 void drawPattern(HDC, const AffineTransform&, const FloatRect& tileRectIn, const AffineTransform& patternTransform, 89 const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize); 90 91 class DCProvider { 92 public: 93 virtual HDC getDC(SharedBitmap*, unsigned*); 94 virtual void releaseDC(SharedBitmap*, HDC, unsigned); 95 }; 96 97 static DCProvider* s_dcProvider; 98 getDC(unsigned * key1)99 HDC getDC(unsigned* key1) { return s_dcProvider->getDC(this, key1); } releaseDC(HDC hdc,unsigned key1)100 void releaseDC(HDC hdc, unsigned key1) { s_dcProvider->releaseDC(this, hdc, key1); } 101 102 class DCHolder { 103 public: 104 DCHolder(SharedBitmap* bmp = 0) { setInternal(bmp); } ~DCHolder()105 ~DCHolder() { clearInternal(); } 106 void set(SharedBitmap* bmp = 0) 107 { 108 clearInternal(); 109 setInternal(bmp); 110 } get()111 HDC get() const { return m_hdc; } 112 private: 113 DCHolder& operator=(const DCHolder&); 114 DCHolder(const DCHolder&); clearInternal()115 void clearInternal() 116 { 117 if (m_hdc) 118 m_bitmap->releaseDC(m_hdc, m_key); 119 } setInternal(SharedBitmap * bmp)120 void setInternal(SharedBitmap* bmp) 121 { 122 m_bitmap = bmp; 123 m_hdc = bmp ? bmp->getDC(&m_key) : 0; 124 } 125 SharedBitmap* m_bitmap; 126 HDC m_hdc; 127 unsigned m_key; 128 }; 129 130 private: 131 SharedBitmap(const IntSize&, BitmapInfo::BitCount, bool initPixels); 132 BitmapInfo m_bmpInfo; 133 OwnPtr<HBITMAP> m_hbitmap; 134 void* m_pixels; 135 OwnArrayPtr<unsigned> m_pixelData; 136 COLORREF m_transparentColor; 137 int m_validHeight; 138 bool m_locked; 139 bool m_usesTransparentColor; 140 bool m_hasAlpha; 141 }; 142 143 } // namespace WebCore 144 145 #endif // SharedBitmap_h 146