2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include "nsMathUtils.h"
|
|
|
|
|
|
|
|
#include "gfxWindowsNativeDrawing.h"
|
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#include "gfxAlphaRecovery.h"
|
2010-07-01 20:43:33 +04:00
|
|
|
#include "gfxPattern.h"
|
2013-08-23 08:53:53 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-12-14 01:26:14 +03:00
|
|
|
#include "mozilla/gfx/Helpers.h"
|
2014-09-14 13:58:56 +04:00
|
|
|
#include "gfx2DGlue.h"
|
|
|
|
|
2015-12-15 02:00:08 +03:00
|
|
|
#include "cairo.h"
|
|
|
|
#include "cairo-win32.h"
|
|
|
|
|
2014-09-14 13:58:56 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
RENDER_STATE_INIT,
|
|
|
|
|
|
|
|
RENDER_STATE_NATIVE_DRAWING,
|
|
|
|
RENDER_STATE_NATIVE_DRAWING_DONE,
|
|
|
|
|
|
|
|
RENDER_STATE_ALPHA_RECOVERY_BLACK,
|
|
|
|
RENDER_STATE_ALPHA_RECOVERY_BLACK_DONE,
|
|
|
|
RENDER_STATE_ALPHA_RECOVERY_WHITE,
|
|
|
|
RENDER_STATE_ALPHA_RECOVERY_WHITE_DONE,
|
|
|
|
|
|
|
|
RENDER_STATE_DONE
|
|
|
|
};
|
|
|
|
|
|
|
|
gfxWindowsNativeDrawing::gfxWindowsNativeDrawing(gfxContext* ctx,
|
|
|
|
const gfxRect& nativeRect,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t nativeDrawFlags)
|
2007-03-03 03:18:34 +03:00
|
|
|
: mContext(ctx),
|
|
|
|
mNativeRect(nativeRect),
|
|
|
|
mNativeDrawFlags(nativeDrawFlags),
|
|
|
|
mRenderState(RENDER_STATE_INIT) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-12-15 02:00:08 +03:00
|
|
|
HDC gfxWindowsNativeDrawing::BeginNativeDrawing() {
|
|
|
|
if (mRenderState == RENDER_STATE_INIT) {
|
2016-01-06 02:04:42 +03:00
|
|
|
RefPtr<gfxASurface> surf;
|
2015-12-15 02:00:08 +03:00
|
|
|
DrawTarget* drawTarget = mContext->GetDrawTarget();
|
2012-07-30 18:20:58 +04:00
|
|
|
cairo_t* cairo = nullptr;
|
2014-01-23 22:26:40 +04:00
|
|
|
if (drawTarget->GetBackendType() == BackendType::CAIRO) {
|
2012-01-05 11:17:52 +04:00
|
|
|
cairo = static_cast<cairo_t*>(
|
2010-10-29 07:08:07 +04:00
|
|
|
drawTarget->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT));
|
|
|
|
if (cairo) {
|
2015-12-15 02:00:08 +03:00
|
|
|
cairo_surface_t* s = cairo_get_group_target(cairo);
|
2010-10-29 07:08:07 +04:00
|
|
|
if (s) {
|
|
|
|
mDeviceOffset = mContext->GetDeviceOffset();
|
|
|
|
double sdx, sdy;
|
|
|
|
cairo_surface_get_device_offset(s, &sdx, &sdy);
|
2016-01-06 02:04:42 +03:00
|
|
|
mDeviceOffset.x -= sdx;
|
2010-10-29 07:08:07 +04:00
|
|
|
mDeviceOffset.y -= sdy;
|
|
|
|
surf = gfxASurface::Wrap(s);
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
if (surf && surf->CairoStatus() != 0) return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
gfxMatrix m = mContext->CurrentMatrixDouble();
|
|
|
|
if (!m.HasNonTranslation())
|
|
|
|
mTransformType = TRANSLATION_ONLY;
|
|
|
|
else if (m.HasNonAxisAlignedTransform())
|
|
|
|
mTransformType = COMPLEX;
|
2018-11-30 13:46:48 +03:00
|
|
|
else
|
2007-03-03 03:18:34 +03:00
|
|
|
mTransformType = AXIS_ALIGNED_SCALE;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
// if this is a native win32 surface, we don't have to
|
|
|
|
// redirect rendering to our own HDC; in some cases,
|
|
|
|
// we may be able to use the HDC from the surface directly.
|
|
|
|
if (surf && ((surf->GetType() == gfxSurfaceType::Win32 ||
|
2014-01-23 22:26:40 +04:00
|
|
|
surf->GetType() == gfxSurfaceType::Win32Printing) &&
|
2014-01-23 22:26:40 +04:00
|
|
|
(surf->GetContentType() == gfxContentType::COLOR ||
|
|
|
|
(surf->GetContentType() == gfxContentType::COLOR_ALPHA &&
|
2007-03-03 03:18:34 +03:00
|
|
|
(mNativeDrawFlags & CAN_DRAW_TO_COLOR_ALPHA))))) {
|
2007-08-18 00:30:23 +04:00
|
|
|
// grab the DC. This can fail if there is a complex clipping path,
|
|
|
|
// in which case we'll have to fall back.
|
2011-04-19 07:07:21 +04:00
|
|
|
mWinSurface = static_cast<gfxWindowsSurface*>(
|
2015-06-01 11:26:19 +03:00
|
|
|
static_cast<gfxASurface*>(surf.get()));
|
|
|
|
mDC = cairo_win32_get_dc_with_clip(cairo);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-06-01 11:26:19 +03:00
|
|
|
if (mDC) {
|
|
|
|
if (mTransformType == TRANSLATION_ONLY) {
|
|
|
|
mRenderState = RENDER_STATE_NATIVE_DRAWING;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
mTranslation = m.GetTranslation();
|
|
|
|
} else if (((mTransformType == AXIS_ALIGNED_SCALE) &&
|
|
|
|
(mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) ||
|
2011-10-17 18:59:28 +04:00
|
|
|
(mNativeDrawFlags & CAN_COMPLEX_TRANSFORM)) {
|
2007-03-03 03:18:34 +03:00
|
|
|
mWorldTransform.eM11 = (FLOAT)m._11;
|
|
|
|
mWorldTransform.eM12 = (FLOAT)m._12;
|
|
|
|
mWorldTransform.eM21 = (FLOAT)m._21;
|
|
|
|
mWorldTransform.eM22 = (FLOAT)m._22;
|
|
|
|
mWorldTransform.eDx = (FLOAT)m._31;
|
|
|
|
mWorldTransform.eDy = (FLOAT)m._32;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
mRenderState = RENDER_STATE_NATIVE_DRAWING;
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we couldn't do native drawing, then we have to do two-buffer drawing
|
|
|
|
// and do alpha recovery
|
|
|
|
if (mRenderState == RENDER_STATE_INIT) {
|
|
|
|
mRenderState = RENDER_STATE_ALPHA_RECOVERY_BLACK;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
// We round out our native rect here, that way the snapping will
|
|
|
|
// happen correctly.
|
2010-03-12 13:08:28 +03:00
|
|
|
mNativeRect.RoundOut();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
// we only do the scale bit if we can do an axis aligned
|
|
|
|
// scale; otherwise we scale (if necessary) after
|
|
|
|
// rendering with cairo. Note that if we're doing alpha recovery,
|
|
|
|
// we cannot do a full complex transform with win32 (I mean, we could, but
|
|
|
|
// it would require more code that's not here.)
|
2010-10-29 07:08:07 +04:00
|
|
|
if (mTransformType == TRANSLATION_ONLY ||
|
2007-03-03 03:18:34 +03:00
|
|
|
!(mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) {
|
2022-06-07 01:01:56 +03:00
|
|
|
mScale = MatrixScalesDouble();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
// Add 1 to the surface size; it's guaranteed to not be incorrect,
|
2007-08-18 00:30:23 +04:00
|
|
|
// and it fixes bug 382458
|
|
|
|
// There's probably a better fix, but I haven't figured out
|
2007-03-03 03:18:34 +03:00
|
|
|
// the root cause of the problem.
|
2015-06-01 11:26:19 +03:00
|
|
|
mTempSurfaceSize = IntSize((int32_t)ceil(mNativeRect.Width() + 1),
|
2012-08-22 19:56:38 +04:00
|
|
|
(int32_t)ceil(mNativeRect.Height() + 1));
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2007-03-03 03:18:34 +03:00
|
|
|
// figure out the scale factors
|
2022-06-07 01:01:56 +03:00
|
|
|
mScale = m.ScaleFactors();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2022-06-07 01:01:56 +03:00
|
|
|
mWorldTransform.eM11 = (FLOAT)mScale.xScale;
|
2014-06-17 21:35:51 +04:00
|
|
|
mWorldTransform.eM12 = 0.0f;
|
|
|
|
mWorldTransform.eM21 = 0.0f;
|
2022-06-07 01:01:56 +03:00
|
|
|
mWorldTransform.eM22 = (FLOAT)mScale.yScale;
|
2014-06-17 21:35:51 +04:00
|
|
|
mWorldTransform.eDx = 0.0f;
|
|
|
|
mWorldTransform.eDy = 0.0f;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-08-18 00:30:23 +04:00
|
|
|
// See comment above about "+1"
|
2011-04-19 07:07:21 +04:00
|
|
|
mTempSurfaceSize =
|
2022-06-07 01:01:56 +03:00
|
|
|
IntSize((int32_t)ceil(mNativeRect.Width() * mScale.xScale + 1),
|
|
|
|
(int32_t)ceil(mNativeRect.Height() * mScale.yScale + 1));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
if (mRenderState == RENDER_STATE_NATIVE_DRAWING) {
|
|
|
|
// we can just do native drawing directly to the context's surface
|
|
|
|
|
|
|
|
// do we need to use SetWorldTransform?
|
|
|
|
if (mTransformType != TRANSLATION_ONLY) {
|
|
|
|
SetGraphicsMode(mDC, GM_ADVANCED);
|
|
|
|
GetWorldTransform(mDC, &mOldWorldTransform);
|
|
|
|
SetWorldTransform(mDC, &mWorldTransform);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2007-03-03 03:18:34 +03:00
|
|
|
GetViewportOrgEx(mDC, &mOrigViewportOrigin);
|
2013-07-31 19:44:31 +04:00
|
|
|
SetViewportOrgEx(mDC, mOrigViewportOrigin.x - (int)mDeviceOffset.x,
|
2015-12-15 02:00:08 +03:00
|
|
|
mOrigViewportOrigin.y - (int)mDeviceOffset.y, nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
return mDC;
|
|
|
|
} else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_BLACK ||
|
|
|
|
mRenderState == RENDER_STATE_ALPHA_RECOVERY_WHITE) {
|
|
|
|
// we're going to use mWinSurface to create our temporary surface here
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
// get us a RGB24 DIB; DIB is important, because
|
|
|
|
// we can later call GetImageSurface on it.
|
|
|
|
mWinSurface = new gfxWindowsSurface(mTempSurfaceSize);
|
|
|
|
mDC = mWinSurface->GetDC();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
RECT r = {0, 0, mTempSurfaceSize.width, mTempSurfaceSize.height};
|
|
|
|
if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_BLACK)
|
2012-07-30 18:20:58 +04:00
|
|
|
FillRect(mDC, &r, (HBRUSH)GetStockObject(BLACK_BRUSH));
|
2018-11-30 13:46:48 +03:00
|
|
|
else
|
2007-03-03 03:18:34 +03:00
|
|
|
FillRect(mDC, &r, (HBRUSH)GetStockObject(WHITE_BRUSH));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if ((mTransformType != TRANSLATION_ONLY) &&
|
2007-03-03 03:18:34 +03:00
|
|
|
(mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) {
|
|
|
|
SetGraphicsMode(mDC, GM_ADVANCED);
|
|
|
|
SetWorldTransform(mDC, &mWorldTransform);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
return mDC;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2007-03-03 03:18:34 +03:00
|
|
|
NS_ERROR("Bogus render state!");
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool gfxWindowsNativeDrawing::ShouldRenderAgain() {
|
|
|
|
switch (mRenderState) {
|
|
|
|
case RENDER_STATE_NATIVE_DRAWING_DONE:
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
case RENDER_STATE_ALPHA_RECOVERY_BLACK_DONE:
|
|
|
|
mRenderState = RENDER_STATE_ALPHA_RECOVERY_WHITE;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
case RENDER_STATE_ALPHA_RECOVERY_WHITE_DONE:
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
NS_ERROR(
|
|
|
|
"Invalid RenderState in gfxWindowsNativeDrawing::ShouldRenderAgain");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-08 05:15:57 +04:00
|
|
|
return false;
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void gfxWindowsNativeDrawing::EndNativeDrawing() {
|
|
|
|
if (mRenderState == RENDER_STATE_NATIVE_DRAWING) {
|
|
|
|
// we drew directly to the HDC in the context; undo our changes
|
2013-07-31 19:44:31 +04:00
|
|
|
SetViewportOrgEx(mDC, mOrigViewportOrigin.x, mOrigViewportOrigin.y,
|
|
|
|
nullptr);
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
if (mTransformType != TRANSLATION_ONLY)
|
|
|
|
SetWorldTransform(mDC, &mOldWorldTransform);
|
|
|
|
|
|
|
|
mWinSurface->MarkDirty();
|
|
|
|
|
|
|
|
mRenderState = RENDER_STATE_NATIVE_DRAWING_DONE;
|
|
|
|
} else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_BLACK) {
|
|
|
|
mBlackSurface = mWinSurface;
|
2012-07-30 18:20:58 +04:00
|
|
|
mWinSurface = nullptr;
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
mRenderState = RENDER_STATE_ALPHA_RECOVERY_BLACK_DONE;
|
|
|
|
} else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_WHITE) {
|
|
|
|
mWhiteSurface = mWinSurface;
|
2012-07-30 18:20:58 +04:00
|
|
|
mWinSurface = nullptr;
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
mRenderState = RENDER_STATE_ALPHA_RECOVERY_WHITE_DONE;
|
|
|
|
} else {
|
|
|
|
NS_ERROR(
|
|
|
|
"Invalid RenderState in gfxWindowsNativeDrawing::EndNativeDrawing");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxWindowsNativeDrawing::PaintToContext() {
|
|
|
|
if (mRenderState == RENDER_STATE_NATIVE_DRAWING_DONE) {
|
|
|
|
// nothing to do, it already went to the context
|
|
|
|
mRenderState = RENDER_STATE_DONE;
|
|
|
|
} else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_WHITE_DONE) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxImageSurface> black = mBlackSurface->GetAsImageSurface();
|
|
|
|
RefPtr<gfxImageSurface> white = mWhiteSurface->GetAsImageSurface();
|
2010-08-09 06:19:17 +04:00
|
|
|
if (!gfxAlphaRecovery::RecoverAlpha(black, white)) {
|
|
|
|
NS_ERROR("Alpha recovery failure");
|
|
|
|
return;
|
|
|
|
}
|
2014-09-14 13:58:56 +04:00
|
|
|
RefPtr<DataSourceSurface> source = Factory::CreateWrappingDataSourceSurface(
|
2015-04-07 17:08:57 +03:00
|
|
|
black->Data(), black->Stride(), black->GetSize(),
|
2014-09-14 13:58:56 +04:00
|
|
|
SurfaceFormat::B8G8R8A8);
|
2015-12-14 01:26:14 +03:00
|
|
|
{
|
|
|
|
DrawTarget* dt = mContext->GetDrawTarget();
|
|
|
|
AutoRestoreTransform autoRestoreTransform(dt);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-12-14 01:26:14 +03:00
|
|
|
Matrix newTransform = dt->GetTransform();
|
|
|
|
newTransform.PreTranslate(ToPoint(mNativeRect.TopLeft()));
|
|
|
|
dt->SetTransform(newTransform);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-12-14 01:26:14 +03:00
|
|
|
Rect rect(Point(0.0, 0.0), ToSize(mNativeRect.Size()));
|
2022-06-07 01:01:56 +03:00
|
|
|
Matrix m = Matrix::Scaling(1.0 / mScale.xScale, 1.0 / mScale.yScale);
|
2020-01-09 18:40:25 +03:00
|
|
|
SurfacePattern pat(source, ExtendMode::CLAMP, m);
|
2015-12-14 01:26:14 +03:00
|
|
|
dt->FillRect(rect, pat);
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
mRenderState = RENDER_STATE_DONE;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2007-03-03 03:18:34 +03:00
|
|
|
NS_ERROR("Invalid RenderState in gfxWindowsNativeDrawing::PaintToContext");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void gfxWindowsNativeDrawing::TransformToNativeRect(const gfxRect& r,
|
|
|
|
RECT& rout) {
|
|
|
|
/* If we're doing native drawing, then we're still in the coordinate space
|
|
|
|
* of the context; otherwise, we're in our own little world,
|
|
|
|
* relative to the passed-in nativeRect.
|
|
|
|
*/
|
2007-06-27 20:42:37 +04:00
|
|
|
|
|
|
|
gfxRect roundedRect(r);
|
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
if (mRenderState == RENDER_STATE_NATIVE_DRAWING) {
|
|
|
|
if (mTransformType == TRANSLATION_ONLY) {
|
2007-06-27 20:42:37 +04:00
|
|
|
roundedRect.MoveBy(mTranslation);
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2011-04-19 07:07:21 +04:00
|
|
|
roundedRect.MoveBy(-mNativeRect.TopLeft());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2007-03-03 03:18:34 +03:00
|
|
|
|
2007-06-27 20:42:37 +04:00
|
|
|
roundedRect.Round();
|
2007-03-03 03:18:34 +03:00
|
|
|
|
2007-06-27 20:42:37 +04:00
|
|
|
rout.left = LONG(roundedRect.X());
|
|
|
|
rout.right = LONG(roundedRect.XMost());
|
|
|
|
rout.top = LONG(roundedRect.Y());
|
|
|
|
rout.bottom = LONG(roundedRect.YMost());
|
|
|
|
}
|