2008-02-20 03:33:27 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07: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 03:33:27 -08:00
|
|
|
|
|
|
|
#include "gfxQuartzNativeDrawing.h"
|
|
|
|
#include "gfxQuartzSurface.h"
|
2013-10-24 09:23:22 -07:00
|
|
|
#include "gfxPlatform.h"
|
2010-04-29 20:19:08 -07:00
|
|
|
#include "cairo-quartz.h"
|
2013-09-22 20:28:16 -07:00
|
|
|
|
2013-07-12 14:19:29 -07:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2008-02-20 03:33:27 -08:00
|
|
|
gfxQuartzNativeDrawing::gfxQuartzNativeDrawing(gfxContext* ctx,
|
2012-10-29 02:22:30 -07:00
|
|
|
const gfxRect& nativeRect,
|
|
|
|
gfxFloat aBackingScale)
|
2014-06-04 05:44:25 -07:00
|
|
|
: mContext(ctx)
|
|
|
|
, mNativeRect(nativeRect)
|
|
|
|
, mBackingScale(aBackingScale)
|
|
|
|
, mCGContext(nullptr)
|
2008-02-20 03:33:27 -08:00
|
|
|
{
|
2014-06-04 05:44:25 -07:00
|
|
|
mNativeRect.RoundOut();
|
2008-02-20 03:33:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
CGContextRef
|
|
|
|
gfxQuartzNativeDrawing::BeginNativeDrawing()
|
|
|
|
{
|
2014-06-04 05:44:25 -07:00
|
|
|
NS_ASSERTION(!mCGContext, "BeginNativeDrawing called when drawing already in progress");
|
|
|
|
|
|
|
|
if (mContext->IsCairo()) {
|
|
|
|
// We're past that now. Any callers that still supply a Cairo context
|
|
|
|
// don't deserve native theming.
|
|
|
|
NS_WARNING("gfxQuartzNativeDrawing being used with a gfxContext that is not backed by a DrawTarget");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawTarget *dt = mContext->GetDrawTarget();
|
2014-06-19 13:35:33 -07:00
|
|
|
if (dt->GetBackendType() != BackendType::COREGRAPHICS || dt->IsDualDrawTarget()) {
|
2014-06-04 05:44:25 -07:00
|
|
|
IntSize backingSize(NSToIntFloor(mNativeRect.width * mBackingScale),
|
|
|
|
NSToIntFloor(mNativeRect.height * mBackingScale));
|
|
|
|
|
|
|
|
if (backingSize.IsEmpty()) {
|
|
|
|
return nullptr;
|
2008-02-20 03:33:27 -08:00
|
|
|
}
|
|
|
|
|
2014-06-04 05:44:25 -07:00
|
|
|
mDrawTarget = Factory::CreateDrawTarget(BackendType::COREGRAPHICS, backingSize, SurfaceFormat::B8G8R8A8);
|
|
|
|
|
|
|
|
Matrix transform;
|
|
|
|
transform.Scale(mBackingScale, mBackingScale);
|
|
|
|
transform.Translate(-mNativeRect.x, -mNativeRect.y);
|
|
|
|
|
|
|
|
mDrawTarget->SetTransform(transform);
|
|
|
|
dt = mDrawTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCGContext = mBorrowedContext.Init(dt);
|
|
|
|
MOZ_ASSERT(mCGContext);
|
|
|
|
return mCGContext;
|
2008-02-20 03:33:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxQuartzNativeDrawing::EndNativeDrawing()
|
|
|
|
{
|
2014-06-04 05:44:25 -07:00
|
|
|
NS_ASSERTION(mCGContext, "EndNativeDrawing called without BeginNativeDrawing");
|
|
|
|
MOZ_ASSERT(!mContext->IsCairo(), "BeginNativeDrawing succeeded with cairo context?");
|
2013-07-12 14:19:29 -07:00
|
|
|
|
2014-06-04 05:44:25 -07:00
|
|
|
mBorrowedContext.Finish();
|
|
|
|
if (mDrawTarget) {
|
|
|
|
DrawTarget *dest = mContext->GetDrawTarget();
|
|
|
|
RefPtr<SourceSurface> source = mDrawTarget->Snapshot();
|
2013-07-12 14:19:29 -07:00
|
|
|
|
2014-06-04 05:44:25 -07:00
|
|
|
IntSize backingSize(NSToIntFloor(mNativeRect.width * mBackingScale),
|
|
|
|
NSToIntFloor(mNativeRect.height * mBackingScale));
|
2013-07-12 14:19:29 -07:00
|
|
|
|
2014-06-04 05:44:25 -07:00
|
|
|
Matrix oldTransform = dest->GetTransform();
|
|
|
|
Matrix newTransform = oldTransform;
|
|
|
|
newTransform.Translate(mNativeRect.x, mNativeRect.y);
|
|
|
|
newTransform.Scale(1.0f / mBackingScale, 1.0f / mBackingScale);
|
2013-07-12 14:19:29 -07:00
|
|
|
|
2014-06-04 05:44:25 -07:00
|
|
|
dest->SetTransform(newTransform);
|
2013-07-12 14:19:29 -07:00
|
|
|
|
2014-06-04 05:44:25 -07:00
|
|
|
dest->DrawSurface(source,
|
|
|
|
gfx::Rect(0, 0, backingSize.width, backingSize.height),
|
|
|
|
gfx::Rect(0, 0, backingSize.width, backingSize.height));
|
2013-07-12 14:19:29 -07:00
|
|
|
|
2008-02-20 03:33:27 -08:00
|
|
|
|
2014-06-04 05:44:25 -07:00
|
|
|
dest->SetTransform(oldTransform);
|
|
|
|
}
|
2008-02-20 03:33:27 -08:00
|
|
|
}
|