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