1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __d2dbitmap__
6 #define __d2dbitmap__
7 
8 #include "../win32bitmapbase.h"
9 
10 #if WINDOWS
11 
12 #include "../../../cpoint.h"
13 
14 struct IWICBitmapSource;
15 struct ID2D1Bitmap;
16 struct ID2D1RenderTarget;
17 struct IWICBitmap;
18 struct IWICBitmapLock;
19 
20 #include <map>
21 
22 namespace VSTGUI {
23 
24 //-----------------------------------------------------------------------------
25 class D2DBitmap : public Win32BitmapBase
26 {
27 public:
28 	D2DBitmap ();
29 	D2DBitmap (const CPoint& size);
30 	~D2DBitmap ();
31 
32 	bool load (const CResourceDescription& desc) override;
getSize()33 	const CPoint& getSize () const override { return size; }
34 	SharedPointer<IPlatformBitmapPixelAccess> lockPixels (bool alphaPremultiplied) override;
setScaleFactor(double factor)35 	void setScaleFactor (double factor) override { scaleFactor = factor; }
getScaleFactor()36 	double getScaleFactor () const override { return scaleFactor; }
37 
38 	HBITMAP createHBitmap () override;
39 	bool loadFromStream (IStream* stream) override;
40 
getSource()41 	IWICBitmapSource* getSource () const { return source; }
42 	IWICBitmap* getBitmap ();
43 	PNGBitmapBuffer createMemoryPNGRepresentation () override;
44 //-----------------------------------------------------------------------------
45 protected:
46 	void replaceBitmapSource (IWICBitmapSource* newSourceBitmap);
47 
48 	class PixelAccess : public IPlatformBitmapPixelAccess
49 	{
50 	public:
51 		PixelAccess ();
52 		~PixelAccess ();
53 
54 		bool init (D2DBitmap* bitmap, bool alphaPremultiplied);
55 
getAddress()56 		uint8_t* getAddress () const { return (uint8_t*)ptr; }
getBytesPerRow()57 		uint32_t getBytesPerRow () const { return bytesPerRow; }
getPixelFormat()58 		PixelFormat getPixelFormat () const { return kBGRA; }
59 
60 	protected:
61 		static void premultiplyAlpha (BYTE* ptr, UINT bytesPerRow, const CPoint& size);
62 		static void unpremultiplyAlpha (BYTE* ptr, UINT bytesPerRow, const CPoint& size);
63 
64 		D2DBitmap* bitmap;
65 		IWICBitmapLock* bLock;
66 		BYTE* ptr;
67 		UINT bytesPerRow;
68 		bool alphaPremultiplied;
69 	};
70 
71 	CPoint size;
72 	double scaleFactor;
73 	IWICBitmapSource* source;
74 };
75 
76 //-----------------------------------------------------------------------------
77 class D2DBitmapCache
78 {
79 public:
80 	ID2D1Bitmap* getBitmap (D2DBitmap* bitmap, ID2D1RenderTarget* renderTarget);
81 
82 	void removeBitmap (D2DBitmap* bitmap);
83 	void removeRenderTarget (ID2D1RenderTarget* renderTarget);
84 
85 	static D2DBitmapCache* instance ();
86 //-----------------------------------------------------------------------------
87 protected:
88 	D2DBitmapCache ();
89 	~D2DBitmapCache ();
90 	ID2D1Bitmap* createBitmap (D2DBitmap* bitmap, ID2D1RenderTarget* renderTarget);
91 	using RenderTargetBitmapMap = std::map<ID2D1RenderTarget*, ID2D1Bitmap*>;
92 	using BitmapCache = std::map<D2DBitmap*, RenderTargetBitmapMap>;
93 	BitmapCache cache;
94 };
95 
96 } // namespace
97 
98 #endif // WINDOWS
99 
100 #endif // __d2dbitmap__
101