1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef _GFXWINDOWSNATIVEDRAWING_H_
7 #define _GFXWINDOWSNATIVEDRAWING_H_
8 
9 #include <windows.h>
10 
11 #include "gfxContext.h"
12 #include "gfxWindowsSurface.h"
13 
14 class gfxWindowsNativeDrawing {
15  public:
16   /* Flags for notifying this class what kind of operations the native
17    * drawing supports
18    */
19 
20   enum {
21     /* Whether the native drawing can draw to a surface of content COLOR_ALPHA
22      */
23     CAN_DRAW_TO_COLOR_ALPHA = 1 << 0,
24     CANNOT_DRAW_TO_COLOR_ALPHA = 0 << 0,
25 
26     /* Whether the native drawing can be scaled using SetWorldTransform */
27     CAN_AXIS_ALIGNED_SCALE = 1 << 1,
28     CANNOT_AXIS_ALIGNED_SCALE = 0 << 1,
29 
30     /* Whether the native drawing can be both scaled and rotated arbitrarily
31        using SetWorldTransform */
32     CAN_COMPLEX_TRANSFORM = 1 << 2,
33     CANNOT_COMPLEX_TRANSFORM = 0 << 2,
34   };
35 
36   /* Create native win32 drawing for a rectangle bounded by
37    * nativeRect.
38    *
39    * Typical usage looks like:
40    *
41    *   gfxWindowsNativeDrawing nativeDraw(ctx, destGfxRect, capabilities);
42    *   do {
43    *     HDC dc = nativeDraw.BeginNativeDrawing();
44    *     if (!dc)
45    *       return NS_ERROR_FAILURE;
46    *
47    *     RECT winRect;
48    *     nativeDraw.TransformToNativeRect(rect, winRect);
49    *
50    *       ... call win32 operations on HDC to draw to winRect ...
51    *
52    *     nativeDraw.EndNativeDrawing();
53    *   } while (nativeDraw.ShouldRenderAgain());
54    *   nativeDraw.PaintToContext();
55    */
56   gfxWindowsNativeDrawing(
57       gfxContext* ctx, const gfxRect& nativeRect,
58       uint32_t nativeDrawFlags = CANNOT_DRAW_TO_COLOR_ALPHA |
59                                  CANNOT_AXIS_ALIGNED_SCALE |
60                                  CANNOT_COMPLEX_TRANSFORM);
61 
62   /* Returns a HDC which may be used for native drawing.  This HDC is valid
63    * until EndNativeDrawing is called; if it is used for drawing after that
64    * time, the result is undefined. */
65   HDC BeginNativeDrawing();
66 
67   /* Transform the native rect into something valid for rendering
68    * to the HDC.  This may or may not change RECT, depending on
69    * whether SetWorldTransform is used or not. */
70   void TransformToNativeRect(const gfxRect& r, RECT& rout);
71 
72   /* Marks the end of native drawing */
73   void EndNativeDrawing();
74 
75   /* Returns true if the native drawing should be executed again */
76   bool ShouldRenderAgain();
77 
78   /* Places the result to the context, if necessary */
79   void PaintToContext();
80 
81  private:
82   RefPtr<gfxContext> mContext;
83   gfxRect mNativeRect;
84   uint32_t mNativeDrawFlags;
85 
86   // what state the rendering is in
87   uint8_t mRenderState;
88 
89   mozilla::gfx::Point mDeviceOffset;
90   RefPtr<gfxPattern> mBlackPattern, mWhitePattern;
91 
92   enum TransformType { TRANSLATION_ONLY, AXIS_ALIGNED_SCALE, COMPLEX };
93 
94   TransformType mTransformType;
95   gfxPoint mTranslation;
96   gfxSize mScale;
97   XFORM mWorldTransform;
98 
99   // saved state
100   RefPtr<gfxWindowsSurface> mWinSurface, mBlackSurface, mWhiteSurface;
101   HDC mDC;
102   XFORM mOldWorldTransform;
103   POINT mOrigViewportOrigin;
104   mozilla::gfx::IntSize mTempSurfaceSize;
105 };
106 
107 #endif
108