|
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 #include <windows.h> |
|
7 |
|
8 #include "nsMathUtils.h" |
|
9 |
|
10 #include "gfxWindowsNativeDrawing.h" |
|
11 #include "gfxWindowsSurface.h" |
|
12 #include "gfxAlphaRecovery.h" |
|
13 #include "gfxPattern.h" |
|
14 #include "mozilla/gfx/2D.h" |
|
15 |
|
16 enum { |
|
17 RENDER_STATE_INIT, |
|
18 |
|
19 RENDER_STATE_NATIVE_DRAWING, |
|
20 RENDER_STATE_NATIVE_DRAWING_DONE, |
|
21 |
|
22 RENDER_STATE_ALPHA_RECOVERY_BLACK, |
|
23 RENDER_STATE_ALPHA_RECOVERY_BLACK_DONE, |
|
24 RENDER_STATE_ALPHA_RECOVERY_WHITE, |
|
25 RENDER_STATE_ALPHA_RECOVERY_WHITE_DONE, |
|
26 |
|
27 RENDER_STATE_DONE |
|
28 }; |
|
29 |
|
30 gfxWindowsNativeDrawing::gfxWindowsNativeDrawing(gfxContext* ctx, |
|
31 const gfxRect& nativeRect, |
|
32 uint32_t nativeDrawFlags) |
|
33 : mContext(ctx), mNativeRect(nativeRect), mNativeDrawFlags(nativeDrawFlags), mRenderState(RENDER_STATE_INIT) |
|
34 { |
|
35 } |
|
36 |
|
37 HDC |
|
38 gfxWindowsNativeDrawing::BeginNativeDrawing() |
|
39 { |
|
40 if (mRenderState == RENDER_STATE_INIT) { |
|
41 nsRefPtr<gfxASurface> surf; |
|
42 |
|
43 if (mContext->GetCairo()) { |
|
44 surf = mContext->CurrentSurface(&mDeviceOffset.x, &mDeviceOffset.y); |
|
45 } |
|
46 |
|
47 if (surf && surf->CairoStatus()) |
|
48 return nullptr; |
|
49 |
|
50 gfxMatrix m = mContext->CurrentMatrix(); |
|
51 if (!m.HasNonTranslation()) |
|
52 mTransformType = TRANSLATION_ONLY; |
|
53 else if (m.HasNonAxisAlignedTransform()) |
|
54 mTransformType = COMPLEX; |
|
55 else |
|
56 mTransformType = AXIS_ALIGNED_SCALE; |
|
57 |
|
58 // if this is a native win32 surface, we don't have to |
|
59 // redirect rendering to our own HDC; in some cases, |
|
60 // we may be able to use the HDC from the surface directly. |
|
61 if (surf && |
|
62 ((surf->GetType() == gfxSurfaceType::Win32 || |
|
63 surf->GetType() == gfxSurfaceType::Win32Printing) && |
|
64 (surf->GetContentType() == gfxContentType::COLOR || |
|
65 (surf->GetContentType() == gfxContentType::COLOR_ALPHA && |
|
66 (mNativeDrawFlags & CAN_DRAW_TO_COLOR_ALPHA))))) |
|
67 { |
|
68 // grab the DC. This can fail if there is a complex clipping path, |
|
69 // in which case we'll have to fall back. |
|
70 mWinSurface = static_cast<gfxWindowsSurface*>(static_cast<gfxASurface*>(surf.get())); |
|
71 mDC = mWinSurface->GetDCWithClip(mContext); |
|
72 |
|
73 if (mDC) { |
|
74 if (mTransformType == TRANSLATION_ONLY) { |
|
75 mRenderState = RENDER_STATE_NATIVE_DRAWING; |
|
76 |
|
77 mTranslation = m.GetTranslation(); |
|
78 } else if (((mTransformType == AXIS_ALIGNED_SCALE) |
|
79 && (mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) || |
|
80 (mNativeDrawFlags & CAN_COMPLEX_TRANSFORM)) |
|
81 { |
|
82 mWorldTransform.eM11 = (FLOAT) m.xx; |
|
83 mWorldTransform.eM12 = (FLOAT) m.yx; |
|
84 mWorldTransform.eM21 = (FLOAT) m.xy; |
|
85 mWorldTransform.eM22 = (FLOAT) m.yy; |
|
86 mWorldTransform.eDx = (FLOAT) m.x0; |
|
87 mWorldTransform.eDy = (FLOAT) m.y0; |
|
88 |
|
89 mRenderState = RENDER_STATE_NATIVE_DRAWING; |
|
90 } |
|
91 } |
|
92 } |
|
93 |
|
94 // If we couldn't do native drawing, then we have to do two-buffer drawing |
|
95 // and do alpha recovery |
|
96 if (mRenderState == RENDER_STATE_INIT) { |
|
97 mRenderState = RENDER_STATE_ALPHA_RECOVERY_BLACK; |
|
98 |
|
99 // We round out our native rect here, that way the snapping will |
|
100 // happen correctly. |
|
101 mNativeRect.RoundOut(); |
|
102 |
|
103 // we only do the scale bit if we can do an axis aligned |
|
104 // scale; otherwise we scale (if necessary) after |
|
105 // rendering with cairo. Note that if we're doing alpha recovery, |
|
106 // we cannot do a full complex transform with win32 (I mean, we could, but |
|
107 // it would require more code that's not here.) |
|
108 if (mTransformType == TRANSLATION_ONLY || !(mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) { |
|
109 mScale = gfxSize(1.0, 1.0); |
|
110 |
|
111 // Add 1 to the surface size; it's guaranteed to not be incorrect, |
|
112 // and it fixes bug 382458 |
|
113 // There's probably a better fix, but I haven't figured out |
|
114 // the root cause of the problem. |
|
115 mTempSurfaceSize = |
|
116 gfxIntSize((int32_t) ceil(mNativeRect.Width() + 1), |
|
117 (int32_t) ceil(mNativeRect.Height() + 1)); |
|
118 } else { |
|
119 // figure out the scale factors |
|
120 mScale = m.ScaleFactors(true); |
|
121 |
|
122 mWorldTransform.eM11 = (FLOAT) mScale.width; |
|
123 mWorldTransform.eM12 = 0.0f; |
|
124 mWorldTransform.eM21 = 0.0f; |
|
125 mWorldTransform.eM22 = (FLOAT) mScale.height; |
|
126 mWorldTransform.eDx = 0.0f; |
|
127 mWorldTransform.eDy = 0.0f; |
|
128 |
|
129 // See comment above about "+1" |
|
130 mTempSurfaceSize = |
|
131 gfxIntSize((int32_t) ceil(mNativeRect.Width() * mScale.width + 1), |
|
132 (int32_t) ceil(mNativeRect.Height() * mScale.height + 1)); |
|
133 } |
|
134 } |
|
135 } |
|
136 |
|
137 if (mRenderState == RENDER_STATE_NATIVE_DRAWING) { |
|
138 // we can just do native drawing directly to the context's surface |
|
139 |
|
140 // do we need to use SetWorldTransform? |
|
141 if (mTransformType != TRANSLATION_ONLY) { |
|
142 SetGraphicsMode(mDC, GM_ADVANCED); |
|
143 GetWorldTransform(mDC, &mOldWorldTransform); |
|
144 SetWorldTransform(mDC, &mWorldTransform); |
|
145 } |
|
146 GetViewportOrgEx(mDC, &mOrigViewportOrigin); |
|
147 SetViewportOrgEx(mDC, |
|
148 mOrigViewportOrigin.x + (int)mDeviceOffset.x, |
|
149 mOrigViewportOrigin.y + (int)mDeviceOffset.y, |
|
150 nullptr); |
|
151 |
|
152 return mDC; |
|
153 } else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_BLACK || |
|
154 mRenderState == RENDER_STATE_ALPHA_RECOVERY_WHITE) |
|
155 { |
|
156 // we're going to use mWinSurface to create our temporary surface here |
|
157 |
|
158 // get us a RGB24 DIB; DIB is important, because |
|
159 // we can later call GetImageSurface on it. |
|
160 mWinSurface = new gfxWindowsSurface(mTempSurfaceSize); |
|
161 mDC = mWinSurface->GetDC(); |
|
162 |
|
163 RECT r = { 0, 0, mTempSurfaceSize.width, mTempSurfaceSize.height }; |
|
164 if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_BLACK) |
|
165 FillRect(mDC, &r, (HBRUSH)GetStockObject(BLACK_BRUSH)); |
|
166 else |
|
167 FillRect(mDC, &r, (HBRUSH)GetStockObject(WHITE_BRUSH)); |
|
168 |
|
169 if ((mTransformType != TRANSLATION_ONLY) && |
|
170 (mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) |
|
171 { |
|
172 SetGraphicsMode(mDC, GM_ADVANCED); |
|
173 SetWorldTransform(mDC, &mWorldTransform); |
|
174 } |
|
175 |
|
176 return mDC; |
|
177 } else { |
|
178 NS_ERROR("Bogus render state!"); |
|
179 return nullptr; |
|
180 } |
|
181 } |
|
182 |
|
183 bool |
|
184 gfxWindowsNativeDrawing::IsDoublePass() |
|
185 { |
|
186 if (!mContext->IsCairo() && |
|
187 (mContext->GetDrawTarget()->GetType() != mozilla::gfx::BackendType::CAIRO || |
|
188 mContext->GetDrawTarget()->IsDualDrawTarget())) { |
|
189 return true; |
|
190 } |
|
191 |
|
192 nsRefPtr<gfxASurface> surf = mContext->CurrentSurface(&mDeviceOffset.x, &mDeviceOffset.y); |
|
193 if (!surf || surf->CairoStatus()) |
|
194 return false; |
|
195 if (surf->GetType() != gfxSurfaceType::Win32 && |
|
196 surf->GetType() != gfxSurfaceType::Win32Printing) { |
|
197 return true; |
|
198 } |
|
199 if ((surf->GetContentType() != gfxContentType::COLOR || |
|
200 (surf->GetContentType() == gfxContentType::COLOR_ALPHA && |
|
201 !(mNativeDrawFlags & CAN_DRAW_TO_COLOR_ALPHA)))) |
|
202 return true; |
|
203 return false; |
|
204 } |
|
205 |
|
206 bool |
|
207 gfxWindowsNativeDrawing::ShouldRenderAgain() |
|
208 { |
|
209 switch (mRenderState) { |
|
210 case RENDER_STATE_NATIVE_DRAWING_DONE: |
|
211 return false; |
|
212 |
|
213 case RENDER_STATE_ALPHA_RECOVERY_BLACK_DONE: |
|
214 mRenderState = RENDER_STATE_ALPHA_RECOVERY_WHITE; |
|
215 return true; |
|
216 |
|
217 case RENDER_STATE_ALPHA_RECOVERY_WHITE_DONE: |
|
218 return false; |
|
219 |
|
220 default: |
|
221 NS_ERROR("Invalid RenderState in gfxWindowsNativeDrawing::ShouldRenderAgain"); |
|
222 break; |
|
223 } |
|
224 |
|
225 return false; |
|
226 } |
|
227 |
|
228 void |
|
229 gfxWindowsNativeDrawing::EndNativeDrawing() |
|
230 { |
|
231 if (mRenderState == RENDER_STATE_NATIVE_DRAWING) { |
|
232 // we drew directly to the HDC in the context; undo our changes |
|
233 SetViewportOrgEx(mDC, mOrigViewportOrigin.x, mOrigViewportOrigin.y, nullptr); |
|
234 |
|
235 if (mTransformType != TRANSLATION_ONLY) |
|
236 SetWorldTransform(mDC, &mOldWorldTransform); |
|
237 |
|
238 mWinSurface->MarkDirty(); |
|
239 |
|
240 mRenderState = RENDER_STATE_NATIVE_DRAWING_DONE; |
|
241 } else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_BLACK) { |
|
242 mBlackSurface = mWinSurface; |
|
243 mWinSurface = nullptr; |
|
244 |
|
245 mRenderState = RENDER_STATE_ALPHA_RECOVERY_BLACK_DONE; |
|
246 } else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_WHITE) { |
|
247 mWhiteSurface = mWinSurface; |
|
248 mWinSurface = nullptr; |
|
249 |
|
250 mRenderState = RENDER_STATE_ALPHA_RECOVERY_WHITE_DONE; |
|
251 } else { |
|
252 NS_ERROR("Invalid RenderState in gfxWindowsNativeDrawing::EndNativeDrawing"); |
|
253 } |
|
254 } |
|
255 |
|
256 void |
|
257 gfxWindowsNativeDrawing::PaintToContext() |
|
258 { |
|
259 if (mRenderState == RENDER_STATE_NATIVE_DRAWING_DONE) { |
|
260 // nothing to do, it already went to the context |
|
261 mRenderState = RENDER_STATE_DONE; |
|
262 } else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_WHITE_DONE) { |
|
263 nsRefPtr<gfxImageSurface> black = mBlackSurface->GetAsImageSurface(); |
|
264 nsRefPtr<gfxImageSurface> white = mWhiteSurface->GetAsImageSurface(); |
|
265 if (!gfxAlphaRecovery::RecoverAlpha(black, white)) { |
|
266 NS_ERROR("Alpha recovery failure"); |
|
267 return; |
|
268 } |
|
269 nsRefPtr<gfxImageSurface> alphaSurface = |
|
270 new gfxImageSurface(black->Data(), black->GetSize(), |
|
271 black->Stride(), |
|
272 gfxImageFormat::ARGB32); |
|
273 |
|
274 mContext->Save(); |
|
275 mContext->Translate(mNativeRect.TopLeft()); |
|
276 mContext->NewPath(); |
|
277 mContext->Rectangle(gfxRect(gfxPoint(0.0, 0.0), mNativeRect.Size())); |
|
278 |
|
279 nsRefPtr<gfxPattern> pat = new gfxPattern(alphaSurface); |
|
280 |
|
281 gfxMatrix m; |
|
282 m.Scale(mScale.width, mScale.height); |
|
283 pat->SetMatrix(m); |
|
284 |
|
285 if (mNativeDrawFlags & DO_NEAREST_NEIGHBOR_FILTERING) |
|
286 pat->SetFilter(GraphicsFilter::FILTER_FAST); |
|
287 |
|
288 pat->SetExtend(gfxPattern::EXTEND_PAD); |
|
289 mContext->SetPattern(pat); |
|
290 mContext->Fill(); |
|
291 mContext->Restore(); |
|
292 |
|
293 mRenderState = RENDER_STATE_DONE; |
|
294 } else { |
|
295 NS_ERROR("Invalid RenderState in gfxWindowsNativeDrawing::PaintToContext"); |
|
296 } |
|
297 } |
|
298 |
|
299 void |
|
300 gfxWindowsNativeDrawing::TransformToNativeRect(const gfxRect& r, |
|
301 RECT& rout) |
|
302 { |
|
303 /* If we're doing native drawing, then we're still in the coordinate space |
|
304 * of the context; otherwise, we're in our own little world, |
|
305 * relative to the passed-in nativeRect. |
|
306 */ |
|
307 |
|
308 gfxRect roundedRect(r); |
|
309 |
|
310 if (mRenderState == RENDER_STATE_NATIVE_DRAWING) { |
|
311 if (mTransformType == TRANSLATION_ONLY) { |
|
312 roundedRect.MoveBy(mTranslation); |
|
313 } |
|
314 } else { |
|
315 roundedRect.MoveBy(-mNativeRect.TopLeft()); |
|
316 } |
|
317 |
|
318 roundedRect.Round(); |
|
319 |
|
320 rout.left = LONG(roundedRect.X()); |
|
321 rout.right = LONG(roundedRect.XMost()); |
|
322 rout.top = LONG(roundedRect.Y()); |
|
323 rout.bottom = LONG(roundedRect.YMost()); |
|
324 } |