windowcapture
исходный код / Native/Direct2D.cs

Direct2D.cs

706 строк · 34,467 байт · модуль Native
  1using System;
  2using System.Runtime.InteropServices;
  3
  4namespace WindowCapture.Native
  5{
  6    // ============================================================
  7    // Direct2D COM Interop for .NET Framework 4.x
  8    // Hardware-accelerated rendering via ID2D1HwndRenderTarget
  9    // with GDI interop for annotation overlay
 10    // ============================================================
 11
 12    #region Enumerations
 13
 14    public enum D2D1_FACTORY_TYPE
 15    {
 16        SINGLE_THREADED = 0,
 17        MULTI_THREADED = 1
 18    }
 19
 20    public enum D2D1_RENDER_TARGET_TYPE
 21    {
 22        DEFAULT = 0,
 23        SOFTWARE = 1,
 24        HARDWARE = 2
 25    }
 26
 27    public enum D2D1_RENDER_TARGET_USAGE : uint
 28    {
 29        NONE = 0x00000000,
 30        FORCE_BITMAP_REMOTING = 0x00000001,
 31        GDI_COMPATIBLE = 0x00000002
 32    }
 33
 34    public enum D2D1_FEATURE_LEVEL
 35    {
 36        DEFAULT = 0,
 37        LEVEL_9 = 0x9100,
 38        LEVEL_10 = 0xa000
 39    }
 40
 41    public enum D2D1_PRESENT_OPTIONS : uint
 42    {
 43        NONE = 0x00000000,
 44        RETAIN_CONTENTS = 0x00000001,
 45        IMMEDIATELY = 0x00000002
 46    }
 47
 48    public enum DXGI_FORMAT : uint
 49    {
 50        UNKNOWN = 0,
 51        B8G8R8A8_UNORM = 87
 52    }
 53
 54    public enum D2D1_ALPHA_MODE : uint
 55    {
 56        UNKNOWN = 0,
 57        PREMULTIPLIED = 1,
 58        STRAIGHT = 2,
 59        IGNORE = 3
 60    }
 61
 62    public enum D2D1_BITMAP_INTERPOLATION_MODE
 63    {
 64        NEAREST_NEIGHBOR = 0,
 65        LINEAR = 1
 66    }
 67
 68    public enum D2D1_DRAW_TEXT_OPTIONS
 69    {
 70        NO_SNAP = 0x00000001,
 71        CLIP = 0x00000002,
 72        ENABLE_COLOR_FONT = 0x00000004,
 73        NONE = 0x00000000
 74    }
 75
 76    public enum D2D1_ANTIALIAS_MODE
 77    {
 78        PER_PRIMITIVE = 0,
 79        ALIASED = 1
 80    }
 81
 82    public enum D2D1_TEXT_ANTIALIAS_MODE
 83    {
 84        DEFAULT = 0,
 85        CLEARTYPE = 1,
 86        GRAYSCALE = 2,
 87        ALIASED = 3
 88    }
 89
 90    public enum D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS
 91    {
 92        NONE = 0x00000000,
 93        GDI_COMPATIBLE = 0x00000001
 94    }
 95
 96    public enum D2D1_OPACITY_MASK_CONTENT
 97    {
 98        GRAPHICS = 0,
 99        TEXT_NATURAL = 1,
100        TEXT_GDI_COMPATIBLE = 2
101    }
102
103    public enum D2D1_DC_INITIALIZE_MODE
104    {
105        COPY = 0,
106        CLEAR = 1
107    }
108
109    public enum D2D1_EXTEND_MODE
110    {
111        CLAMP = 0,
112        WRAP = 1,
113        MIRROR = 2
114    }
115
116    public enum D2D1_GAMMA
117    {
118        GAMMA_2_2 = 0,
119        GAMMA_1_0 = 1
120    }
121
122    public enum D2D1_DASH_STYLE
123    {
124        SOLID = 0,
125        DASH = 1,
126        DOT = 2,
127        DASH_DOT = 3,
128        DASH_DOT_DOT = 4,
129        CUSTOM = 5
130    }
131
132    public enum D2D1_CAP_STYLE
133    {
134        FLAT = 0,
135        SQUARE = 1,
136        ROUND = 2,
137        TRIANGLE = 3
138    }
139
140    public enum D2D1_LINE_JOIN
141    {
142        MITER = 0,
143        BEVEL = 1,
144        ROUND = 2,
145        MITER_OR_BEVEL = 3
146    }
147
148    #endregion
149
150    #region Structures
151
152    [StructLayout(LayoutKind.Sequential)]
153    public struct D2D1_POINT_2F
154    {
155        public float x, y;
156        public D2D1_POINT_2F(float x, float y) { this.x = x; this.y = y; }
157    }
158
159    [StructLayout(LayoutKind.Sequential)]
160    public struct D2D1_POINT_2U
161    {
162        public uint x, y;
163    }
164
165    [StructLayout(LayoutKind.Sequential)]
166    public struct D2D1_SIZE_F
167    {
168        public float width, height;
169        public D2D1_SIZE_F(float w, float h) { width = w; height = h; }
170    }
171
172    [StructLayout(LayoutKind.Sequential)]
173    public struct D2D1_SIZE_U
174    {
175        public uint width, height;
176        public D2D1_SIZE_U(uint w, uint h) { width = w; height = h; }
177    }
178
179    [StructLayout(LayoutKind.Sequential)]
180    public struct D2D1_RECT_F
181    {
182        public float left, top, right, bottom;
183        public D2D1_RECT_F(float l, float t, float r, float b)
184        { left = l; top = t; right = r; bottom = b; }
185    }
186
187    [StructLayout(LayoutKind.Sequential)]
188    public struct D2D1_RECT_U
189    {
190        public uint left, top, right, bottom;
191    }
192
193    [StructLayout(LayoutKind.Sequential)]
194    public struct D2D1_COLOR_F
195    {
196        public float r, g, b, a;
197        public D2D1_COLOR_F(float r, float g, float b, float a)
198        { this.r = r; this.g = g; this.b = b; this.a = a; }
199    }
200
201    [StructLayout(LayoutKind.Sequential)]
202    public struct D2D1_PIXEL_FORMAT
203    {
204        public DXGI_FORMAT format;
205        public D2D1_ALPHA_MODE alphaMode;
206    }
207
208    [StructLayout(LayoutKind.Sequential)]
209    public struct D2D1_RENDER_TARGET_PROPERTIES
210    {
211        public D2D1_RENDER_TARGET_TYPE type;
212        public D2D1_PIXEL_FORMAT pixelFormat;
213        public float dpiX, dpiY;
214        public D2D1_RENDER_TARGET_USAGE usage;
215        public D2D1_FEATURE_LEVEL minLevel;
216    }
217
218    [StructLayout(LayoutKind.Sequential)]
219    public struct D2D1_HWND_RENDER_TARGET_PROPERTIES
220    {
221        public IntPtr hwnd;
222        public D2D1_SIZE_U pixelSize;
223        public D2D1_PRESENT_OPTIONS presentOptions;
224    }
225
226    [StructLayout(LayoutKind.Sequential)]
227    public struct D2D1_BITMAP_PROPERTIES
228    {
229        public D2D1_PIXEL_FORMAT pixelFormat;
230        public float dpiX, dpiY;
231    }
232
233    [StructLayout(LayoutKind.Sequential)]
234    public struct D2D1_MATRIX_3X2_F
235    {
236        public float _11, _12;
237        public float _21, _22;
238        public float _31, _32;
239
240        public static D2D1_MATRIX_3X2_F Identity
241        {
242            get { return new D2D1_MATRIX_3X2_F { _11 = 1f, _22 = 1f }; }
243        }
244
245        public static D2D1_MATRIX_3X2_F Scale(float sx, float sy)
246        {
247            return new D2D1_MATRIX_3X2_F { _11 = sx, _22 = sy };
248        }
249
250        public static D2D1_MATRIX_3X2_F Translation(float dx, float dy)
251        {
252            return new D2D1_MATRIX_3X2_F { _11 = 1f, _22 = 1f, _31 = dx, _32 = dy };
253        }
254    }
255
256    [StructLayout(LayoutKind.Sequential)]
257    public struct D2D1_BRUSH_PROPERTIES
258    {
259        public float opacity;
260        public D2D1_MATRIX_3X2_F transform;
261    }
262
263    [StructLayout(LayoutKind.Sequential)]
264    public struct D2D1_GRADIENT_STOP
265    {
266        public float position;
267        public D2D1_COLOR_F color;
268    }
269
270    [StructLayout(LayoutKind.Sequential)]
271    public struct D2D1_STROKE_STYLE_PROPERTIES
272    {
273        public D2D1_CAP_STYLE startCap;
274        public D2D1_CAP_STYLE endCap;
275        public D2D1_CAP_STYLE dashCap;
276        public D2D1_LINE_JOIN lineJoin;
277        public float miterLimit;
278        public D2D1_DASH_STYLE dashStyle;
279        public float dashOffset;
280    }
281
282    #endregion
283
284    #region COM Interfaces
285
286    // ============================================================
287    // ID2D1Resource — base for all D2D resources
288    // vtable: IUnknown(3) + GetFactory(1) = 4 methods
289    // ============================================================
290    [ComImport, Guid("2cd90691-12e2-11dc-9fed-001143a055f9")]
291    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
292    public interface ID2D1Resource
293    {
294        void GetFactory(out ID2D1Factory factory);
295    }
296
297    // ============================================================
298    // ID2D1Image — base for bitmaps
299    // vtable: ID2D1Resource(4) + 0 = 4 methods
300    // ============================================================
301    [ComImport, Guid("65019f75-8da2-497c-b32c-dfa34e48ede6")]
302    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
303    public interface ID2D1Image
304    {
305        // ID2D1Resource
306        void GetFactory(out ID2D1Factory factory);
307    }
308
309    // ============================================================
310    // ID2D1Bitmap
311    // vtable: ID2D1Image(4) + GetSize(1) + GetPixelSize(1) + GetPixelFormat(1) + GetDpi(1) + CopyFromBitmap(1) + CopyFromRenderTarget(1) + CopyFromMemory(1) = 11
312    // ============================================================
313    [ComImport, Guid("a2296057-ea42-4099-983b-539fb6505426")]
314    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
315    public interface ID2D1Bitmap
316    {
317        // ID2D1Resource
318        void GetFactory(out ID2D1Factory factory);
319        // ID2D1Bitmap
320        D2D1_SIZE_F GetSize();
321        D2D1_SIZE_U GetPixelSize();
322        D2D1_PIXEL_FORMAT GetPixelFormat();
323        void GetDpi(out float dpiX, out float dpiY);
324        [PreserveSig] int CopyFromBitmap(IntPtr destPoint, ID2D1Bitmap bitmap, IntPtr srcRect);
325        [PreserveSig] int CopyFromRenderTarget(IntPtr destPoint, IntPtr renderTarget, IntPtr srcRect);
326        [PreserveSig] int CopyFromMemory(IntPtr dstRect, IntPtr srcData, uint pitch);
327    }
328
329    // ============================================================
330    // ID2D1Brush — base for brushes
331    // vtable: ID2D1Resource(4) + SetOpacity(1) + GetOpacity(1) + SetTransform(1) + GetTransform(1) = 8
332    // ============================================================
333    [ComImport, Guid("2cd906a8-12e2-11dc-9fed-001143a055f9")]
334    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
335    public interface ID2D1Brush
336    {
337        // ID2D1Resource
338        void GetFactory(out ID2D1Factory factory);
339        // ID2D1Brush
340        void SetOpacity(float opacity);
341        [PreserveSig] float GetOpacity();
342        void SetTransform(ref D2D1_MATRIX_3X2_F transform);
343        void GetTransform(out D2D1_MATRIX_3X2_F transform);
344    }
345
346    // ============================================================
347    // ID2D1SolidColorBrush
348    // vtable: ID2D1Brush(8) + SetColor(1) + GetColor(1) = 10
349    // ============================================================
350    [ComImport, Guid("2cd906a9-12e2-11dc-9fed-001143a055f9")]
351    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
352    public interface ID2D1SolidColorBrush
353    {
354        // ID2D1Resource
355        void GetFactory(out ID2D1Factory factory);
356        // ID2D1Brush
357        void SetOpacity(float opacity);
358        [PreserveSig] float GetOpacity();
359        void SetTransform(ref D2D1_MATRIX_3X2_F transform);
360        void GetTransform(out D2D1_MATRIX_3X2_F transform);
361        // ID2D1SolidColorBrush
362        void SetColor(ref D2D1_COLOR_F color);
363        D2D1_COLOR_F GetColor();
364    }
365
366    // ============================================================
367    // ID2D1StrokeStyle
368    // ============================================================
369    [ComImport, Guid("2cd9069d-12e2-11dc-9fed-001143a055f9")]
370    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
371    public interface ID2D1StrokeStyle
372    {
373        // ID2D1Resource
374        void GetFactory(out ID2D1Factory factory);
375    }
376
377    // ============================================================
378    // ID2D1BitmapRenderTarget — off-screen render target (for blur)
379    // Inherits ID2D1RenderTarget, adds GetBitmap
380    // ============================================================
381    [ComImport, Guid("2cd90695-12e2-11dc-9fed-001143a055f9")]
382    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
383    public interface ID2D1BitmapRenderTarget
384    {
385        #region ID2D1Resource
386        void GetFactory(out ID2D1Factory factory);
387        #endregion
388
389        #region ID2D1RenderTarget (all 55 methods in vtable order)
390        [PreserveSig] int CreateBitmap(D2D1_SIZE_U size, IntPtr srcData, uint pitch, ref D2D1_BITMAP_PROPERTIES bitmapProperties, out ID2D1Bitmap bitmap);
391        [PreserveSig] int CreateBitmapFromWicBitmap(IntPtr wicBitmapSource, IntPtr bitmapProperties, out ID2D1Bitmap bitmap);
392        [PreserveSig] int CreateSharedBitmap(ref Guid riid, IntPtr data, IntPtr bitmapProperties, out ID2D1Bitmap bitmap);
393        [PreserveSig] int CreateBitmapBrush(ID2D1Bitmap bitmap, IntPtr bitmapBrushProperties, IntPtr brushProperties, out IntPtr bitmapBrush);
394        [PreserveSig] int CreateSolidColorBrush(ref D2D1_COLOR_F color, IntPtr brushProperties, out ID2D1SolidColorBrush solidColorBrush);
395        [PreserveSig] int CreateGradientStopCollection(IntPtr gradientStops, uint gradientStopsCount, D2D1_GAMMA colorInterpolationGamma, D2D1_EXTEND_MODE extendMode, out IntPtr gradientStopCollection);
396        [PreserveSig] int CreateLinearGradientBrush(IntPtr linearGradientBrushProperties, IntPtr brushProperties, IntPtr gradientStopCollection, out IntPtr linearGradientBrush);
397        [PreserveSig] int CreateRadialGradientBrush(IntPtr radialGradientBrushProperties, IntPtr brushProperties, IntPtr gradientStopCollection, out IntPtr radialGradientBrush);
398        [PreserveSig] int CreateCompatibleRenderTarget(IntPtr desiredSize, IntPtr desiredPixelSize, IntPtr desiredFormat, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS options, out ID2D1BitmapRenderTarget bitmapRenderTarget);
399        [PreserveSig] int CreateLayer(IntPtr size, out IntPtr layer);
400        [PreserveSig] int CreateMesh(out IntPtr mesh);
401        void DrawLine(D2D1_POINT_2F point0, D2D1_POINT_2F point1, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
402        void DrawRectangle(ref D2D1_RECT_F rect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
403        void FillRectangle(ref D2D1_RECT_F rect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
404        void DrawRoundedRectangle(IntPtr roundedRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
405        void FillRoundedRectangle(IntPtr roundedRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
406        void DrawEllipse(IntPtr ellipse, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
407        void FillEllipse(IntPtr ellipse, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
408        void DrawGeometry(IntPtr geometry, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
409        void FillGeometry(IntPtr geometry, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush opacityBrush);
410        void FillMesh(IntPtr mesh, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
411        void FillOpacityMask(ID2D1Bitmap opacityMask, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, D2D1_OPACITY_MASK_CONTENT content, IntPtr destinationRectangle, IntPtr sourceRectangle);
412        void DrawBitmap(ID2D1Bitmap bitmap, IntPtr destinationRectangle, float opacity, D2D1_BITMAP_INTERPOLATION_MODE interpolationMode, IntPtr sourceRectangle);
413        void DrawText([MarshalAs(UnmanagedType.LPWStr)] string str, uint stringLength, IntPtr textFormat, ref D2D1_RECT_F layoutRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush defaultFillBrush, D2D1_DRAW_TEXT_OPTIONS options, int measuringMode);
414        void DrawTextLayout(D2D1_POINT_2F origin, IntPtr textLayout, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush defaultFillBrush, D2D1_DRAW_TEXT_OPTIONS options);
415        void DrawGlyphRun(D2D1_POINT_2F baselineOrigin, IntPtr glyphRun, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush foregroundBrush, int measuringMode);
416        void SetTransform(ref D2D1_MATRIX_3X2_F transform);
417        void GetTransform(out D2D1_MATRIX_3X2_F transform);
418        void SetAntialiasMode(D2D1_ANTIALIAS_MODE antialiasMode);
419        D2D1_ANTIALIAS_MODE GetAntialiasMode();
420        void SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE textAntialiasMode);
421        D2D1_TEXT_ANTIALIAS_MODE GetTextAntialiasMode();
422        void SetTextRenderingParams(IntPtr textRenderingParams);
423        void GetTextRenderingParams(out IntPtr textRenderingParams);
424        void SetTags(ulong tag1, ulong tag2);
425        void GetTags(out ulong tag1, out ulong tag2);
426        void PushLayer(IntPtr layerParameters, IntPtr layer);
427        void PopLayer();
428        [PreserveSig] int Flush(out ulong tag1, out ulong tag2);
429        void SaveDrawingState(IntPtr drawingStateBlock);
430        void RestoreDrawingState(IntPtr drawingStateBlock);
431        void PushAxisAlignedClip(ref D2D1_RECT_F clipRect, D2D1_ANTIALIAS_MODE antialiasMode);
432        void PopAxisAlignedClip();
433        void Clear(ref D2D1_COLOR_F clearColor);
434        void BeginDraw();
435        [PreserveSig] int EndDraw(out ulong tag1, out ulong tag2);
436        D2D1_PIXEL_FORMAT GetPixelFormat();
437        void SetDpi(float dpiX, float dpiY);
438        void GetDpi(out float dpiX, out float dpiY);
439        D2D1_SIZE_F GetSize();
440        D2D1_SIZE_U GetPixelSize();
441        uint GetMaximumBitmapSize();
442        [PreserveSig] int IsSupported(ref D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties);
443        #endregion
444
445        // ID2D1BitmapRenderTarget own method
446        [PreserveSig] int GetBitmap(out ID2D1Bitmap bitmap);
447    }
448
449    // ============================================================
450    // ID2D1RenderTarget — main rendering interface
451    // vtable: ID2D1Resource(4) + 55 methods = 59 total
452    // ============================================================
453    [ComImport, Guid("2cd90694-12e2-11dc-9fed-001143a055f9")]
454    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
455    public interface ID2D1RenderTarget
456    {
457        #region ID2D1Resource
458        void GetFactory(out ID2D1Factory factory);
459        #endregion
460
461        #region ID2D1RenderTarget
462        [PreserveSig] int CreateBitmap(D2D1_SIZE_U size, IntPtr srcData, uint pitch, ref D2D1_BITMAP_PROPERTIES bitmapProperties, out ID2D1Bitmap bitmap);
463        [PreserveSig] int CreateBitmapFromWicBitmap(IntPtr wicBitmapSource, IntPtr bitmapProperties, out ID2D1Bitmap bitmap);
464        [PreserveSig] int CreateSharedBitmap(ref Guid riid, IntPtr data, IntPtr bitmapProperties, out ID2D1Bitmap bitmap);
465        [PreserveSig] int CreateBitmapBrush(ID2D1Bitmap bitmap, IntPtr bitmapBrushProperties, IntPtr brushProperties, out IntPtr bitmapBrush);
466        [PreserveSig] int CreateSolidColorBrush(ref D2D1_COLOR_F color, IntPtr brushProperties, out ID2D1SolidColorBrush solidColorBrush);
467        [PreserveSig] int CreateGradientStopCollection(IntPtr gradientStops, uint gradientStopsCount, D2D1_GAMMA colorInterpolationGamma, D2D1_EXTEND_MODE extendMode, out IntPtr gradientStopCollection);
468        [PreserveSig] int CreateLinearGradientBrush(IntPtr linearGradientBrushProperties, IntPtr brushProperties, IntPtr gradientStopCollection, out IntPtr linearGradientBrush);
469        [PreserveSig] int CreateRadialGradientBrush(IntPtr radialGradientBrushProperties, IntPtr brushProperties, IntPtr gradientStopCollection, out IntPtr radialGradientBrush);
470        [PreserveSig] int CreateCompatibleRenderTarget(IntPtr desiredSize, IntPtr desiredPixelSize, IntPtr desiredFormat, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS options, out ID2D1BitmapRenderTarget bitmapRenderTarget);
471        [PreserveSig] int CreateLayer(IntPtr size, out IntPtr layer);
472        [PreserveSig] int CreateMesh(out IntPtr mesh);
473        void DrawLine(D2D1_POINT_2F point0, D2D1_POINT_2F point1, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
474        void DrawRectangle(ref D2D1_RECT_F rect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
475        void FillRectangle(ref D2D1_RECT_F rect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
476        void DrawRoundedRectangle(IntPtr roundedRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
477        void FillRoundedRectangle(IntPtr roundedRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
478        void DrawEllipse(IntPtr ellipse, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
479        void FillEllipse(IntPtr ellipse, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
480        void DrawGeometry(IntPtr geometry, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
481        void FillGeometry(IntPtr geometry, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush opacityBrush);
482        void FillMesh(IntPtr mesh, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
483        void FillOpacityMask(ID2D1Bitmap opacityMask, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, D2D1_OPACITY_MASK_CONTENT content, IntPtr destinationRectangle, IntPtr sourceRectangle);
484        void DrawBitmap(ID2D1Bitmap bitmap, IntPtr destinationRectangle, float opacity, D2D1_BITMAP_INTERPOLATION_MODE interpolationMode, IntPtr sourceRectangle);
485        void DrawText([MarshalAs(UnmanagedType.LPWStr)] string str, uint stringLength, IntPtr textFormat, ref D2D1_RECT_F layoutRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush defaultFillBrush, D2D1_DRAW_TEXT_OPTIONS options, int measuringMode);
486        void DrawTextLayout(D2D1_POINT_2F origin, IntPtr textLayout, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush defaultFillBrush, D2D1_DRAW_TEXT_OPTIONS options);
487        void DrawGlyphRun(D2D1_POINT_2F baselineOrigin, IntPtr glyphRun, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush foregroundBrush, int measuringMode);
488        void SetTransform(ref D2D1_MATRIX_3X2_F transform);
489        void GetTransform(out D2D1_MATRIX_3X2_F transform);
490        void SetAntialiasMode(D2D1_ANTIALIAS_MODE antialiasMode);
491        D2D1_ANTIALIAS_MODE GetAntialiasMode();
492        void SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE textAntialiasMode);
493        D2D1_TEXT_ANTIALIAS_MODE GetTextAntialiasMode();
494        void SetTextRenderingParams(IntPtr textRenderingParams);
495        void GetTextRenderingParams(out IntPtr textRenderingParams);
496        void SetTags(ulong tag1, ulong tag2);
497        void GetTags(out ulong tag1, out ulong tag2);
498        void PushLayer(IntPtr layerParameters, IntPtr layer);
499        void PopLayer();
500        [PreserveSig] int Flush(out ulong tag1, out ulong tag2);
501        void SaveDrawingState(IntPtr drawingStateBlock);
502        void RestoreDrawingState(IntPtr drawingStateBlock);
503        void PushAxisAlignedClip(ref D2D1_RECT_F clipRect, D2D1_ANTIALIAS_MODE antialiasMode);
504        void PopAxisAlignedClip();
505        void Clear(ref D2D1_COLOR_F clearColor);
506        void BeginDraw();
507        [PreserveSig] int EndDraw(out ulong tag1, out ulong tag2);
508        D2D1_PIXEL_FORMAT GetPixelFormat();
509        void SetDpi(float dpiX, float dpiY);
510        void GetDpi(out float dpiX, out float dpiY);
511        D2D1_SIZE_F GetSize();
512        D2D1_SIZE_U GetPixelSize();
513        uint GetMaximumBitmapSize();
514        [PreserveSig] int IsSupported(ref D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties);
515        #endregion
516    }
517
518    // ============================================================
519    // ID2D1HwndRenderTarget — renders to window
520    // vtable: ID2D1RenderTarget(59) + CheckWindowState(1) + Resize(1) + GetHwnd(1) = 62
521    // ============================================================
522    [ComImport, Guid("2cd90698-12e2-11dc-9fed-001143a055f9")]
523    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
524    public interface ID2D1HwndRenderTarget
525    {
526        #region ID2D1Resource
527        void GetFactory(out ID2D1Factory factory);
528        #endregion
529
530        #region ID2D1RenderTarget (55 methods)
531        [PreserveSig] int CreateBitmap(D2D1_SIZE_U size, IntPtr srcData, uint pitch, ref D2D1_BITMAP_PROPERTIES bitmapProperties, out ID2D1Bitmap bitmap);
532        [PreserveSig] int CreateBitmapFromWicBitmap(IntPtr wicBitmapSource, IntPtr bitmapProperties, out ID2D1Bitmap bitmap);
533        [PreserveSig] int CreateSharedBitmap(ref Guid riid, IntPtr data, IntPtr bitmapProperties, out ID2D1Bitmap bitmap);
534        [PreserveSig] int CreateBitmapBrush(ID2D1Bitmap bitmap, IntPtr bitmapBrushProperties, IntPtr brushProperties, out IntPtr bitmapBrush);
535        [PreserveSig] int CreateSolidColorBrush(ref D2D1_COLOR_F color, IntPtr brushProperties, out ID2D1SolidColorBrush solidColorBrush);
536        [PreserveSig] int CreateGradientStopCollection(IntPtr gradientStops, uint gradientStopsCount, D2D1_GAMMA colorInterpolationGamma, D2D1_EXTEND_MODE extendMode, out IntPtr gradientStopCollection);
537        [PreserveSig] int CreateLinearGradientBrush(IntPtr linearGradientBrushProperties, IntPtr brushProperties, IntPtr gradientStopCollection, out IntPtr linearGradientBrush);
538        [PreserveSig] int CreateRadialGradientBrush(IntPtr radialGradientBrushProperties, IntPtr brushProperties, IntPtr gradientStopCollection, out IntPtr radialGradientBrush);
539        [PreserveSig] int CreateCompatibleRenderTarget(IntPtr desiredSize, IntPtr desiredPixelSize, IntPtr desiredFormat, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS options, out ID2D1BitmapRenderTarget bitmapRenderTarget);
540        [PreserveSig] int CreateLayer(IntPtr size, out IntPtr layer);
541        [PreserveSig] int CreateMesh(out IntPtr mesh);
542        void DrawLine(D2D1_POINT_2F point0, D2D1_POINT_2F point1, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
543        void DrawRectangle(ref D2D1_RECT_F rect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
544        void FillRectangle(ref D2D1_RECT_F rect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
545        void DrawRoundedRectangle(IntPtr roundedRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
546        void FillRoundedRectangle(IntPtr roundedRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
547        void DrawEllipse(IntPtr ellipse, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
548        void FillEllipse(IntPtr ellipse, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
549        void DrawGeometry(IntPtr geometry, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, float strokeWidth, ID2D1StrokeStyle strokeStyle);
550        void FillGeometry(IntPtr geometry, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush opacityBrush);
551        void FillMesh(IntPtr mesh, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush);
552        void FillOpacityMask(ID2D1Bitmap opacityMask, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush brush, D2D1_OPACITY_MASK_CONTENT content, IntPtr destinationRectangle, IntPtr sourceRectangle);
553        void DrawBitmap(ID2D1Bitmap bitmap, IntPtr destinationRectangle, float opacity, D2D1_BITMAP_INTERPOLATION_MODE interpolationMode, IntPtr sourceRectangle);
554        void DrawText([MarshalAs(UnmanagedType.LPWStr)] string str, uint stringLength, IntPtr textFormat, ref D2D1_RECT_F layoutRect, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush defaultFillBrush, D2D1_DRAW_TEXT_OPTIONS options, int measuringMode);
555        void DrawTextLayout(D2D1_POINT_2F origin, IntPtr textLayout, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush defaultFillBrush, D2D1_DRAW_TEXT_OPTIONS options);
556        void DrawGlyphRun(D2D1_POINT_2F baselineOrigin, IntPtr glyphRun, [MarshalAs(UnmanagedType.Interface)] ID2D1Brush foregroundBrush, int measuringMode);
557        void SetTransform(ref D2D1_MATRIX_3X2_F transform);
558        void GetTransform(out D2D1_MATRIX_3X2_F transform);
559        void SetAntialiasMode(D2D1_ANTIALIAS_MODE antialiasMode);
560        D2D1_ANTIALIAS_MODE GetAntialiasMode();
561        void SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE textAntialiasMode);
562        D2D1_TEXT_ANTIALIAS_MODE GetTextAntialiasMode();
563        void SetTextRenderingParams(IntPtr textRenderingParams);
564        void GetTextRenderingParams(out IntPtr textRenderingParams);
565        void SetTags(ulong tag1, ulong tag2);
566        void GetTags(out ulong tag1, out ulong tag2);
567        void PushLayer(IntPtr layerParameters, IntPtr layer);
568        void PopLayer();
569        [PreserveSig] int Flush(out ulong tag1, out ulong tag2);
570        void SaveDrawingState(IntPtr drawingStateBlock);
571        void RestoreDrawingState(IntPtr drawingStateBlock);
572        void PushAxisAlignedClip(ref D2D1_RECT_F clipRect, D2D1_ANTIALIAS_MODE antialiasMode);
573        void PopAxisAlignedClip();
574        void Clear(ref D2D1_COLOR_F clearColor);
575        void BeginDraw();
576        [PreserveSig] int EndDraw(out ulong tag1, out ulong tag2);
577        D2D1_PIXEL_FORMAT GetPixelFormat();
578        void SetDpi(float dpiX, float dpiY);
579        void GetDpi(out float dpiX, out float dpiY);
580        D2D1_SIZE_F GetSize();
581        D2D1_SIZE_U GetPixelSize();
582        uint GetMaximumBitmapSize();
583        [PreserveSig] int IsSupported(ref D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties);
584        #endregion
585
586        // ID2D1HwndRenderTarget own methods
587        [PreserveSig] int CheckWindowState();
588        [PreserveSig] int Resize(ref D2D1_SIZE_U pixelSize);
589        IntPtr GetHwnd();
590    }
591
592    // ============================================================
593    // ID2D1GdiInteropRenderTarget — bridge between D2D and GDI+
594    // ============================================================
595    [ComImport, Guid("e0db51c3-6f77-4bae-b3d5-e47509b35838")]
596    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
597    public interface ID2D1GdiInteropRenderTarget
598    {
599        [PreserveSig] int GetDC(D2D1_DC_INITIALIZE_MODE mode, out IntPtr hdc);
600        [PreserveSig] int ReleaseDC(IntPtr updateRect);
601    }
602
603    // ============================================================
604    // ID2D1Factory
605    // vtable: IUnknown(3) + ReloadSystemMetrics(1) + GetDesktopDpi(1) +
606    //   CreateRectangleGeometry(1) + CreateRoundedRectangleGeometry(1) +
607    //   CreateEllipseGeometry(1) + CreateGeometryGroup(1) +
608    //   CreateTransformedGeometry(1) + CreatePathGeometry(1) +
609    //   CreateStrokeStyle(1) + CreateDrawingStateBlock(1) + CreateDrawingStateBlock2(1) +
610    //   CreateWicBitmapRenderTarget(1) + CreateHwndRenderTarget(1) +
611    //   CreateDxgiSurfaceRenderTarget(1) + CreateDCRenderTarget(1) = 19
612    // ============================================================
613    [ComImport, Guid("06152247-6f50-465a-9245-118bfd3b6007")]
614    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
615    public interface ID2D1Factory
616    {
617        [PreserveSig] int ReloadSystemMetrics();
618        void GetDesktopDpi(out float dpiX, out float dpiY);
619        [PreserveSig] int CreateRectangleGeometry(ref D2D1_RECT_F rectangle, out IntPtr rectangleGeometry);
620        [PreserveSig] int CreateRoundedRectangleGeometry(IntPtr roundedRectangle, out IntPtr roundedRectangleGeometry);
621        [PreserveSig] int CreateEllipseGeometry(IntPtr ellipse, out IntPtr ellipseGeometry);
622        [PreserveSig] int CreateGeometryGroup(int fillMode, IntPtr geometries, uint geometriesCount, out IntPtr geometryGroup);
623        [PreserveSig] int CreateTransformedGeometry(IntPtr sourceGeometry, ref D2D1_MATRIX_3X2_F transform, out IntPtr transformedGeometry);
624        [PreserveSig] int CreatePathGeometry(out IntPtr pathGeometry);
625        [PreserveSig] int CreateStrokeStyle(ref D2D1_STROKE_STYLE_PROPERTIES strokeStyleProperties, IntPtr dashes, uint dashesCount, out ID2D1StrokeStyle strokeStyle);
626        [PreserveSig] int CreateDrawingStateBlock(IntPtr drawingStateDescription, IntPtr textRenderingParams, out IntPtr drawingStateBlock);
627        [PreserveSig] int CreateWicBitmapRenderTarget(IntPtr target, ref D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties, out IntPtr renderTarget);
628        [PreserveSig] int CreateHwndRenderTarget(ref D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties, ref D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderTargetProperties, out ID2D1HwndRenderTarget hwndRenderTarget);
629        [PreserveSig] int CreateDxgiSurfaceRenderTarget(IntPtr dxgiSurface, ref D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties, out IntPtr renderTarget);
630        [PreserveSig] int CreateDCRenderTarget(ref D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties, out IntPtr dcRenderTarget);
631    }
632
633    #endregion
634
635    #region P/Invoke
636
637    public static class D2DApi
638    {
639        [DllImport("d2d1.dll")]
640        public static extern int D2D1CreateFactory(
641            D2D1_FACTORY_TYPE factoryType,
642            [MarshalAs(UnmanagedType.LPStruct)] Guid riid,
643            IntPtr pFactoryOptions,
644            out ID2D1Factory ppFactory);
645
646        public static readonly Guid IID_ID2D1Factory =
647            new Guid("06152247-6f50-465a-9245-118bfd3b6007");
648
649        // DirectWrite
650        [DllImport("dwrite.dll")]
651        public static extern int DWriteCreateFactory(
652            int factoryType, // 0=Shared
653            [MarshalAs(UnmanagedType.LPStruct)] Guid iid,
654            [MarshalAs(UnmanagedType.IUnknown)] out object factory);
655
656        public static readonly Guid IID_IDWriteFactory =
657            new Guid("b859ee5a-d838-4b5b-a2e8-1adc7d93db48");
658    }
659
660    #endregion
661
662    #region DirectWrite
663
664    [ComImport, Guid("b859ee5a-d838-4b5b-a2e8-1adc7d93db48")]
665    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
666    public interface IDWriteFactory
667    {
668        // slots 3-14: skip 12 methods (GetSystemFontCollection...UnregisterFontFileLoader)
669        void _VtblGap1_12();
670        // slot 15: CreateTextFormat
671        [PreserveSig]
672        int CreateTextFormat(
673            [MarshalAs(UnmanagedType.LPWStr)] string fontFamilyName,
674            IntPtr fontCollection, // NULL = system
675            int fontWeight,        // 400=Normal, 700=Bold
676            int fontStyle,         // 0=Normal, 2=Italic
677            int fontStretch,       // 5=Normal
678            float fontSize,
679            [MarshalAs(UnmanagedType.LPWStr)] string localeName,
680            out IDWriteTextFormat textFormat);
681    }
682
683    [ComImport, Guid("9c906818-31d7-4fd3-a151-7c5e225db55a")]
684    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
685    public interface IDWriteTextFormat
686    {
687        // slot 3: SetTextAlignment
688        [PreserveSig] int SetTextAlignment(int textAlignment); // 0=Leading, 1=Trailing, 2=Center
689        // slot 4: SetParagraphAlignment
690        [PreserveSig] int SetParagraphAlignment(int paragraphAlignment); // 0=Near, 1=Far, 2=Center
691        // slot 5: SetWordWrapping
692        [PreserveSig] int SetWordWrapping(int wordWrapping); // 0=Wrap, 1=NoWrap
693        // slot 6: SetReadingDirection
694        [PreserveSig] int SetReadingDirection(int readingDirection);
695        // slot 7: SetFlowDirection
696        [PreserveSig] int SetFlowDirection(int flowDirection);
697        // slot 8: SetIncrementalTabStop
698        [PreserveSig] int SetIncrementalTabStop(float incrementalTabStop);
699        // slot 9: SetTrimming
700        [PreserveSig] int SetTrimming(IntPtr trimmingOptions, IntPtr trimmingSign);
701        // slot 10: SetLineSpacing
702        [PreserveSig] int SetLineSpacing(int lineSpacingMethod, float lineSpacing, float baseline);
703    }
704
705    #endregion
706}