1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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     /* If we have to do transforms with cairo, should we use nearest-neighbour
36        filtering? */
37     DO_NEAREST_NEIGHBOR_FILTERING = 1 << 3,
38     DO_BILINEAR_FILTERING = 0 << 3
39   };
40 
41   /* Create native win32 drawing for a rectangle bounded by
42    * nativeRect.
43    *
44    * Typical usage looks like:
45    *
46    *   gfxWindowsNativeDrawing nativeDraw(ctx, destGfxRect, capabilities);
47    *   do {
48    *     HDC dc = nativeDraw.BeginNativeDrawing();
49    *     if (!dc)
50    *       return NS_ERROR_FAILURE;
51    *
52    *     RECT winRect;
53    *     nativeDraw.TransformToNativeRect(rect, winRect);
54    *
55    *       ... call win32 operations on HDC to draw to winRect ...
56    *
57    *     nativeDraw.EndNativeDrawing();
58    *   } while (nativeDraw.ShouldRenderAgain());
59    *   nativeDraw.PaintToContext();
60    */
61   gfxWindowsNativeDrawing(gfxContext* ctx, const gfxRect& nativeRect,
62                           uint32_t nativeDrawFlags =
63                               CANNOT_DRAW_TO_COLOR_ALPHA |
64                               CANNOT_AXIS_ALIGNED_SCALE |
65                               CANNOT_COMPLEX_TRANSFORM | DO_BILINEAR_FILTERING);
66 
67   /* Returns a HDC which may be used for native drawing.  This HDC is valid
68    * until EndNativeDrawing is called; if it is used for drawing after that
69    * time, the result is undefined. */
70   HDC BeginNativeDrawing();
71 
72   /* Transform the native rect into something valid for rendering
73    * to the HDC.  This may or may not change RECT, depending on
74    * whether SetWorldTransform is used or not. */
75   void TransformToNativeRect(const gfxRect& r, RECT& rout);
76 
77   /* Marks the end of native drawing */
78   void EndNativeDrawing();
79 
80   /* Returns true if the native drawing should be executed again */
81   bool ShouldRenderAgain();
82 
83   /* Places the result to the context, if necessary */
84   void PaintToContext();
85 
86  private:
87   RefPtr<gfxContext> mContext;
88   gfxRect mNativeRect;
89   uint32_t mNativeDrawFlags;
90 
91   // what state the rendering is in
92   uint8_t mRenderState;
93 
94   mozilla::gfx::Point mDeviceOffset;
95   RefPtr<gfxPattern> mBlackPattern, mWhitePattern;
96 
97   enum TransformType { TRANSLATION_ONLY, AXIS_ALIGNED_SCALE, COMPLEX };
98 
99   TransformType mTransformType;
100   gfxPoint mTranslation;
101   gfxSize mScale;
102   XFORM mWorldTransform;
103 
104   // saved state
105   RefPtr<gfxWindowsSurface> mWinSurface, mBlackSurface, mWhiteSurface;
106   HDC mDC;
107   XFORM mOldWorldTransform;
108   POINT mOrigViewportOrigin;
109   mozilla::gfx::IntSize mTempSurfaceSize;
110 };
111 
112 #endif
113