gfx/thebes/gfxWindowsNativeDrawing.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial