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/. */
|
2008-02-20 14:33:27 +03:00
|
|
|
|
|
|
|
#include "gfxQuartzNativeDrawing.h"
|
2013-10-24 20:23:22 +04:00
|
|
|
#include "gfxPlatform.h"
|
2014-10-24 11:26:27 +04:00
|
|
|
#include "mozilla/gfx/Helpers.h"
|
2013-09-23 07:28:16 +04:00
|
|
|
|
2013-07-13 01:19:29 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-10-24 11:26:27 +04:00
|
|
|
gfxQuartzNativeDrawing::gfxQuartzNativeDrawing(DrawTarget& aDrawTarget,
|
|
|
|
const Rect& nativeRect)
|
|
|
|
: mDrawTarget(&aDrawTarget), mNativeRect(nativeRect), mCGContext(nullptr) {}
|
2008-02-20 14:33:27 +03:00
|
|
|
|
|
|
|
CGContextRef gfxQuartzNativeDrawing::BeginNativeDrawing() {
|
2014-06-04 16:44:25 +04:00
|
|
|
NS_ASSERTION(!mCGContext,
|
|
|
|
"BeginNativeDrawing called when drawing already in progress");
|
|
|
|
|
2014-10-24 11:26:27 +04:00
|
|
|
DrawTarget* dt = mDrawTarget;
|
2021-08-31 01:55:32 +03:00
|
|
|
if (dt->IsTiledDrawTarget() || dt->GetBackendType() != BackendType::SKIA ||
|
|
|
|
dt->IsRecording()) {
|
2014-10-24 11:26:27 +04:00
|
|
|
// We need a DrawTarget that we can get a CGContextRef from:
|
2014-08-29 07:08:15 +04:00
|
|
|
Matrix transform = dt->GetTransform();
|
2014-10-24 11:26:27 +04:00
|
|
|
|
2014-08-29 07:08:15 +04:00
|
|
|
mNativeRect = transform.TransformBounds(mNativeRect);
|
|
|
|
mNativeRect.RoundOut();
|
|
|
|
if (mNativeRect.IsEmpty()) {
|
2014-06-04 16:44:25 +04:00
|
|
|
return nullptr;
|
2008-02-20 14:33:27 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 17:53:12 +03:00
|
|
|
mTempDrawTarget = Factory::CreateDrawTarget(
|
|
|
|
BackendType::SKIA,
|
2016-07-26 19:55:28 +03:00
|
|
|
IntSize::Truncate(mNativeRect.width, mNativeRect.height),
|
2014-10-24 11:26:27 +04:00
|
|
|
SurfaceFormat::B8G8R8A8);
|
2017-05-12 18:59:22 +03:00
|
|
|
if (!mTempDrawTarget) {
|
|
|
|
return nullptr;
|
2015-03-06 00:38:23 +03:00
|
|
|
}
|
2017-05-12 18:59:22 +03:00
|
|
|
|
|
|
|
transform.PostTranslate(-mNativeRect.x, -mNativeRect.y);
|
|
|
|
mTempDrawTarget->SetTransform(transform);
|
|
|
|
|
2014-10-24 11:26:27 +04:00
|
|
|
dt = mTempDrawTarget;
|
2017-05-12 18:59:22 +03:00
|
|
|
} else {
|
|
|
|
// Clip the DT in case BorrowedCGContext needs to create a new layer.
|
|
|
|
// This prevents it from creating a new layer the size of the window.
|
2017-06-07 02:19:51 +03:00
|
|
|
// But make sure that this clip is device pixel aligned.
|
|
|
|
Matrix transform = dt->GetTransform();
|
|
|
|
|
|
|
|
Rect deviceRect = transform.TransformBounds(mNativeRect);
|
|
|
|
deviceRect.RoundOut();
|
|
|
|
mNativeRect = transform.Inverse().TransformBounds(deviceRect);
|
2017-05-12 18:59:22 +03:00
|
|
|
mDrawTarget->PushClipRect(mNativeRect);
|
2014-06-04 16:44:25 +04:00
|
|
|
}
|
2017-05-12 18:59:22 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(dt->GetBackendType() == BackendType::SKIA);
|
|
|
|
mCGContext = mBorrowedContext.Init(dt);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mCGContext)) {
|
|
|
|
// Failed borrowing CG context, so we need to clean up.
|
|
|
|
if (!mTempDrawTarget) {
|
|
|
|
mDrawTarget->PopClip();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2015-03-06 00:38:23 +03:00
|
|
|
}
|
2017-05-12 18:59:22 +03:00
|
|
|
|
2014-06-04 16:44:25 +04:00
|
|
|
return mCGContext;
|
2008-02-20 14:33:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void gfxQuartzNativeDrawing::EndNativeDrawing() {
|
2014-06-04 16:44:25 +04:00
|
|
|
NS_ASSERTION(mCGContext,
|
|
|
|
"EndNativeDrawing called without BeginNativeDrawing");
|
2013-07-13 01:19:29 +04:00
|
|
|
|
2014-06-04 16:44:25 +04:00
|
|
|
mBorrowedContext.Finish();
|
2014-10-24 11:26:27 +04:00
|
|
|
if (mTempDrawTarget) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> source = mTempDrawTarget->Snapshot();
|
2008-02-20 14:33:27 +03:00
|
|
|
|
2014-10-24 11:26:27 +04:00
|
|
|
AutoRestoreTransform autoRestore(mDrawTarget);
|
|
|
|
mDrawTarget->SetTransform(Matrix());
|
|
|
|
mDrawTarget->DrawSurface(source, mNativeRect,
|
|
|
|
Rect(0, 0, mNativeRect.width, mNativeRect.height));
|
2017-05-12 18:59:22 +03:00
|
|
|
} else {
|
|
|
|
mDrawTarget->PopClip();
|
2014-06-04 16:44:25 +04:00
|
|
|
}
|
2008-02-20 14:33:27 +03:00
|
|
|
}
|