make it possible to write canvas unittests

One of the problems is that canvas apparently works only with windows,
but tests obviously need an offscreen surface. This patch moves
Window::GetCanvas() to OutputDevice, and makes vclcanvas capable
of working with OutputDevice classes that are not windows. Other
canvas implementations still don't work, but presumably at least
cairocanvas could be fixed too.

This commit adds a "simple" test that just draws a line and tries
to verify it's been drawn properly. Adding another test should
be a matter of basing it on this existing one, and then copy&pasting
the complicated UNO way of drawing using canvas from somewhere,
such as canvas/workben/canvasdemo.cxx.

Change-Id: I42db12b09433763cd31c3dd497c10157424b8598
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115117
Tested-by: Jenkins
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
This commit is contained in:
Luboš Luňák 2021-05-04 11:32:11 +02:00
parent 834822413d
commit 402acbcfad
13 changed files with 343 additions and 79 deletions

View file

@ -0,0 +1,46 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#*************************************************************************
#
# This file is part of the LibreOffice project.
#
# 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/.
#
#*************************************************************************
$(eval $(call gb_CppunitTest_CppunitTest,canvas_test))
$(eval $(call gb_CppunitTest_add_exception_objects,canvas_test, \
canvas/qa/cppunit/canvastest \
))
$(eval $(call gb_CppunitTest_use_external,canvas_test,boost_headers))
$(eval $(call gb_CppunitTest_use_libraries,canvas_test, \
basegfx \
comphelper \
cppu \
cppuhelper \
sal \
test \
tl \
unotest \
vcl \
))
$(eval $(call gb_CppunitTest_use_sdk_api,canvas_test))
$(eval $(call gb_CppunitTest_use_ure,canvas_test))
$(eval $(call gb_CppunitTest_use_vcl,canvas_test))
$(eval $(call gb_CppunitTest_use_components,canvas_test,\
canvas/source/vcl/vclcanvas \
canvas/source/factory/canvasfactory \
configmgr/source/configmgr \
i18npool/util/i18npool \
))
$(eval $(call gb_CppunitTest_use_configuration,canvas_test))
# vim: set noet sw=4 ts=4:

View file

@ -49,4 +49,8 @@ $(eval $(call gb_Module_add_targets,canvas,\
))
endif
$(eval $(call gb_Module_add_check_targets,canvas,\
CppunitTest_canvas_test \
))
# vim: set noet sw=4 ts=4:

11
canvas/qa/cppunit/README Normal file
View file

@ -0,0 +1,11 @@
How to write a canvas test:
===========================
The easiest (relatively speaking) way is to copy&paste from somewhere. Use existing tests
as the base, or canvas/workben/canvasdemo.cxx should be a good source too. If that doesn't
help, then you'll need to find the right UNO interfaces (see canvas/README). Have "fun".
Currently only the vclcanvas implementation is tested, because it's the only one
capable of working with offscreen surfaces. Other implementations would need to
be fixed, and then added to the gb_CppunitTest_use_components list in the makefile.

View file

@ -0,0 +1,109 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* 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/.
*
*/
#include <test/bootstrapfixture.hxx>
#include <vcl/virdev.hxx>
#include <vcl/BitmapReadAccess.hxx>
#include <vcl/canvastools.hxx>
#include <vcl/graphicfilter.hxx>
#include <tools/stream.hxx>
#include <com/sun/star/rendering/XBitmap.hpp>
#include <com/sun/star/rendering/XCanvas.hpp>
#include <com/sun/star/rendering/XBitmapCanvas.hpp>
#include <com/sun/star/rendering/CompositeOperation.hpp>
using namespace ::com::sun::star;
class CanvasTest : public test::BootstrapFixture
{
VclPtr<VirtualDevice> mVclDevice;
uno::Reference<rendering::XCanvas> mCanvas;
rendering::ViewState mViewState;
rendering::RenderState mRenderState;
uno::Sequence<double> mColorBlack;
// if enabled - check the result images with:
// "xdg-open ./workdir/CppunitTest/canvas_test.test.core/"
static constexpr const bool mbExportBitmap = false;
void exportDevice(const OUString& filename, const VclPtr<VirtualDevice>& device)
{
if (mbExportBitmap)
{
BitmapEx aBitmapEx(device->GetBitmapEx(Point(0, 0), device->GetOutputSizePixel()));
SvFileStream aStream(filename, StreamMode::WRITE | StreamMode::TRUNC);
GraphicFilter::GetGraphicFilter().compressAsPNG(aBitmapEx, aStream);
}
}
public:
CanvasTest()
: BootstrapFixture(true, false)
{
}
virtual void setUp() override
{
BootstrapFixture::setUp();
mColorBlack = vcl::unotools::colorToStdColorSpaceSequence(COL_BLACK);
// Geometry init
geometry::AffineMatrix2D aUnit(1, 0, 0, 0, 1, 0);
mViewState.AffineTransform = aUnit;
mRenderState.AffineTransform = aUnit;
mRenderState.DeviceColor = mColorBlack;
mRenderState.CompositeOperation = rendering::CompositeOperation::OVER;
}
virtual void tearDown() override
{
mVclDevice.clear();
mCanvas = uno::Reference<rendering::XCanvas>();
BootstrapFixture::tearDown();
}
void setupCanvas(const Size& size, Color backgroundColor = COL_WHITE, bool alpha = false)
{
mVclDevice
= alpha ? VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT, DeviceFormat::DEFAULT)
: VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT);
mVclDevice->SetOutputSizePixel(size);
mVclDevice->SetBackground(Wallpaper(backgroundColor));
mVclDevice->Erase();
mCanvas = mVclDevice->GetCanvas();
CPPUNIT_ASSERT(mCanvas.is());
}
void testDrawLine()
{
setupCanvas(Size(10, 10));
mCanvas->drawLine(geometry::RealPoint2D(1, 1), geometry::RealPoint2D(9, 1), mViewState,
mRenderState);
exportDevice("test-draw-line.png", mVclDevice);
Bitmap bitmap = mVclDevice->GetBitmap(Point(), Size(10, 10));
Bitmap::ScopedReadAccess access(bitmap);
// Canvas uses AA, which blurs the line, and it cannot be turned off,
// so do not check the end points.
CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE), access->GetPixel(0, 0));
CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK), access->GetPixel(1, 2));
CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK), access->GetPixel(1, 8));
}
CPPUNIT_TEST_SUITE(CanvasTest);
CPPUNIT_TEST(testDrawLine);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(CanvasTest);
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View file

@ -19,40 +19,19 @@
#include <sal/config.h>
#include "canvas.hxx"
#include <com/sun/star/lang/NoSupportException.hpp>
#include <sal/log.hxx>
#include <tools/diagnose_ex.h>
#include <vcl/outdev.hxx>
#include "canvas.hxx"
#include "outdevholder.hxx"
using namespace ::com::sun::star;
namespace vclcanvas
{
namespace
{
class OutDevHolder : public OutDevProvider
{
public:
OutDevHolder(const OutDevHolder&) = delete;
const OutDevHolder& operator=(const OutDevHolder&) = delete;
explicit OutDevHolder( OutputDevice& rOutDev ) :
mrOutDev(rOutDev)
{}
private:
virtual OutputDevice& getOutDev() override { return mrOutDev; }
virtual const OutputDevice& getOutDev() const override { return mrOutDev; }
// TODO(Q2): Lifetime issue. This _only_ works reliably,
// if disposing the Canvas correctly disposes all
// entities which hold this pointer.
OutputDevice& mrOutDev;
};
}
Canvas::Canvas( const uno::Sequence< uno::Any >& aArguments,
const uno::Reference< uno::XComponentContext >& /*rxContext*/ ) :
maArguments(aArguments)

View file

@ -0,0 +1,51 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#pragma once
#include <com/sun/star/uno/Reference.hxx>
#include <vcl/outdev.hxx>
#include "outdevprovider.hxx"
namespace vclcanvas
{
class OutDevHolder : public OutDevProvider
{
public:
OutDevHolder(const OutDevHolder&) = delete;
const OutDevHolder& operator=(const OutDevHolder&) = delete;
explicit OutDevHolder(OutputDevice& rOutDev)
: mrOutDev(rOutDev)
{
}
private:
virtual OutputDevice& getOutDev() override { return mrOutDev; }
virtual const OutputDevice& getOutDev() const override { return mrOutDev; }
// TODO(Q2): Lifetime issue. This _only_ works reliably,
// if disposing the Canvas correctly disposes all
// entities which hold this pointer.
OutputDevice& mrOutDev;
};
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View file

@ -21,10 +21,12 @@
#include <sal/log.hxx>
#include <com/sun/star/awt/XTopWindow.hpp>
#include <com/sun/star/lang/NoSupportException.hpp>
#include <cppuhelper/supportsservice.hxx>
#include <tools/diagnose_ex.h>
#include "spritecanvas.hxx"
#include "outdevholder.hxx"
#include "windowoutdevholder.hxx"
@ -75,14 +77,27 @@ namespace vclcanvas
maArguments[3].getValueTypeClass() == uno::TypeClass_INTERFACE,
"VCLSpriteCanvas::initialize: wrong number of arguments, or wrong types" );
sal_Int64 nPtr = 0;
maArguments[0] >>= nPtr;
OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
if( !pOutDev )
throw lang::NoSupportException("Passed OutDev invalid!", nullptr);
uno::Reference< awt::XWindow > xParentWindow;
maArguments[3] >>= xParentWindow;
OutDevProviderSharedPtr pOutDev = std::make_shared<WindowOutDevHolder>(xParentWindow);
OutDevProviderSharedPtr pOutDevProvider;
if( xParentWindow.is())
pOutDevProvider = std::make_shared<WindowOutDevHolder>(xParentWindow);
else
pOutDevProvider = std::make_shared<OutDevHolder>(*pOutDev);
// setup helper
maDeviceHelper.init( pOutDev );
setWindow(uno::Reference<awt::XWindow2>(xParentWindow, uno::UNO_QUERY_THROW));
maDeviceHelper.init( pOutDevProvider );
setWindow( xParentWindow.is()
? uno::Reference<awt::XWindow2>(xParentWindow, uno::UNO_QUERY_THROW)
: uno::Reference<awt::XWindow2>());
maCanvasHelper.init( maDeviceHelper.getBackBuffer(),
*this,
maRedrawManager,

View file

@ -45,6 +45,7 @@
#include <basegfx/polygon/b2dpolypolygon.hxx>
#include <unotools/fontdefs.hxx>
#include <cppuhelper/weakref.hxx>
#include <com/sun/star/drawing/LineCap.hpp>
#include <com/sun/star/uno/Reference.h>
@ -110,6 +111,11 @@ namespace com::sun::star::awt {
class XGraphics;
}
namespace com::sun::star::rendering {
class XCanvas;
class XSpriteCanvas;
}
#if defined UNX
#define GLYPH_FONT_HEIGHT 128
#else
@ -304,6 +310,8 @@ private:
std::unique_ptr<ImplOutDevData> mpOutDevData;
std::vector< VCLXGraphics* >* mpUnoGraphicsList;
vcl::ExtOutDevData* mpExtOutDevData;
// The canvas interface for this output device. Is persistent after the first GetCanvas() call
mutable css::uno::WeakReference< css::rendering::XCanvas > mxCanvas;
// TEMP TEMP TEMP
VclPtr<VirtualDevice> mpAlphaVDev;
@ -440,6 +448,11 @@ public:
virtual size_t GetSyncCount() const { return 0xffffffff; }
/// request XCanvas render interface
css::uno::Reference< css::rendering::XCanvas > GetCanvas() const;
/// request XSpriteCanvas render interface
css::uno::Reference< css::rendering::XSpriteCanvas > GetSpriteCanvas() const;
protected:
/** Acquire a graphics device that the output device uses to draw on.
@ -557,6 +570,9 @@ protected:
SAL_DLLPRIVATE bool is_double_buffered_window() const;
virtual css::uno::Reference< css::rendering::XCanvas > ImplGetCanvas( bool bSpriteCanvas ) const;
SAL_DLLPRIVATE void ImplDisposeCanvas();
private:
// not implemented; to detect misuses of DrawOutDev(...OutputDevice&);

View file

@ -595,6 +595,8 @@ protected:
SAL_DLLPRIVATE void PushPaintHelper(PaintHelper* pHelper, vcl::RenderContext& rRenderContext);
SAL_DLLPRIVATE void PopPaintHelper(PaintHelper const * pHelper);
virtual css::uno::Reference< css::rendering::XCanvas > ImplGetCanvas( bool bSpriteCanvas ) const override;
private:
SAL_DLLPRIVATE void ImplSetFrameParent( const vcl::Window* pParent );
@ -706,9 +708,6 @@ private:
virtual void ImplPaintToDevice( ::OutputDevice* pTargetOutDev, const Point& rPos );
SAL_DLLPRIVATE css::uno::Reference< css::rendering::XCanvas >
ImplGetCanvas( bool bSpriteCanvas ) const;
public:
virtual vcl::Region GetActiveClipRegion() const override;
virtual vcl::Region GetOutputBoundsClipRegion() const override;
@ -1252,13 +1251,6 @@ private:
*/
Size get_ungrouped_preferred_size() const;
public:
/// request XCanvas render interface for this window
css::uno::Reference< css::rendering::XCanvas >
GetCanvas() const;
/// request XSpriteCanvas render interface for this window
css::uno::Reference< css::rendering::XSpriteCanvas >
GetSpriteCanvas() const;
/* records all DrawText operations within the passed rectangle;
* a synchronous paint is sent to achieve this
*/

View file

@ -29,7 +29,6 @@
#include <vcl/window.hxx>
#include <vcl/settings.hxx>
#include <o3tl/typed_flags_set.hxx>
#include <cppuhelper/weakref.hxx>
#include <optional>
#include <list>
@ -61,10 +60,6 @@ namespace com::sun::star {
class XAccessibleEditableText;
}
namespace rendering {
class XCanvas;
}
namespace awt {
class XWindowPeer;
class XWindow;
@ -249,9 +244,6 @@ public:
Link<vcl::Window&, bool> maMnemonicActivateHdl;
Link<tools::JsonWriter&, void> maDumpAsPropertyTreeHdl;
// The canvas interface for this VCL window. Is persistent after the first GetCanvas() call
css::uno::WeakReference< css::rendering::XCanvas > mxCanvas;
vcl::Cursor* mpCursor;
PointerStyle maPointer;
Fraction maZoom;

View file

@ -29,12 +29,17 @@
#include <vcl/toolkit/unowrap.hxx>
#include <vcl/svapp.hxx>
#include <vcl/sysdata.hxx>
#include <vcl/lazydelete.hxx>
#include <comphelper/processfactory.hxx>
#include <salgdi.hxx>
#include <window.h>
#include <outdev.h>
#include <com/sun/star/awt/DeviceCapability.hpp>
#include <com/sun/star/awt/XWindow.hpp>
#include <com/sun/star/rendering/CanvasFactory.hpp>
#include <com/sun/star/rendering/XSpriteCanvas.hpp>
#ifdef DISABLE_DYNLOADING
// Linking all needed LO code into one .so/executable, these already
@ -46,6 +51,8 @@ namespace {
}
#endif
using namespace ::com::sun::star::uno;
// Begin initializer and accessor public functions
OutputDevice::OutputDevice(OutDevType eOutDevType) :
@ -738,4 +745,77 @@ css::awt::DeviceInfo OutputDevice::GetDeviceInfo() const
return aInfo;
}
Reference< css::rendering::XCanvas > OutputDevice::GetCanvas() const
{
// try to retrieve hard reference from weak member
Reference< css::rendering::XCanvas > xCanvas( mxCanvas );
// canvas still valid? Then we're done.
if( xCanvas.is() )
return xCanvas;
xCanvas = ImplGetCanvas( false );
mxCanvas = xCanvas;
return xCanvas;
}
Reference< css::rendering::XSpriteCanvas > OutputDevice::GetSpriteCanvas() const
{
Reference< css::rendering::XCanvas > xCanvas( mxCanvas );
Reference< css::rendering::XSpriteCanvas > xSpriteCanvas( xCanvas, UNO_QUERY );
if( xSpriteCanvas.is() )
return xSpriteCanvas;
xCanvas = ImplGetCanvas( true );
mxCanvas = xCanvas;
return Reference< css::rendering::XSpriteCanvas >( xCanvas, UNO_QUERY );
}
// Generic implementation, Window will override.
com::sun::star::uno::Reference< css::rendering::XCanvas > OutputDevice::ImplGetCanvas( bool bSpriteCanvas ) const
{
/* Arguments:
0: ptr to creating instance (Window or VirtualDevice)
1: current bounds of creating instance
2: bool, denoting always on top state for Window (always false for VirtualDevice)
3: XWindow for creating Window (or empty for VirtualDevice)
4: SystemGraphicsData as a streamed Any
*/
Sequence< Any > aArg(5);
aArg[ 0 ] <<= reinterpret_cast<sal_Int64>(this);
aArg[ 1 ] <<= css::awt::Rectangle( mnOutOffX, mnOutOffY, mnOutWidth, mnOutHeight );
aArg[ 2 ] <<= false;
aArg[ 3 ] <<= Reference< css::awt::XWindow >();
aArg[ 4 ] = GetSystemGfxDataAny();
Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
static vcl::DeleteUnoReferenceOnDeinit<css::lang::XMultiComponentFactory> xStaticCanvasFactory(
css::rendering::CanvasFactory::create( xContext ) );
Reference<css::lang::XMultiComponentFactory> xCanvasFactory(xStaticCanvasFactory.get());
Reference< css::rendering::XCanvas > xCanvas;
if(xCanvasFactory.is())
{
xCanvas.set( xCanvasFactory->createInstanceWithArgumentsAndContext(
bSpriteCanvas ?
OUString( "com.sun.star.rendering.SpriteCanvas" ) :
OUString( "com.sun.star.rendering.Canvas" ),
aArg,
xContext ),
UNO_QUERY );
}
// no factory??? Empty reference, then.
return xCanvas;
}
void OutputDevice::ImplDisposeCanvas()
{
css::uno::Reference< css::rendering::XCanvas > xCanvas( mxCanvas );
if( xCanvas.is() )
{
css::uno::Reference< css::lang::XComponent > xCanvasComponent( xCanvas, css::uno::UNO_QUERY );
if( xCanvasComponent.is() )
xCanvasComponent->dispose();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View file

@ -154,13 +154,7 @@ void Window::dispose()
// Dispose of the canvas implementation (which, currently, has an
// own wrapper window as a child to this one.
Reference< css::rendering::XCanvas > xCanvas( mpWindowImpl->mxCanvas );
if( xCanvas.is() )
{
Reference < XComponent > xCanvasComponent( xCanvas, UNO_QUERY );
if( xCanvasComponent.is() )
xCanvasComponent->dispose();
}
ImplDisposeCanvas();
mpWindowImpl->mbInDispose = true;
@ -3680,13 +3674,6 @@ bool Window::IsNativeWidgetEnabled() const
Reference< css::rendering::XCanvas > Window::ImplGetCanvas( bool bSpriteCanvas ) const
{
// try to retrieve hard reference from weak member
Reference< css::rendering::XCanvas > xCanvas( mpWindowImpl->mxCanvas );
// canvas still valid? Then we're done.
if( xCanvas.is() )
return xCanvas;
Sequence< Any > aArg(5);
// Feed any with operating system's window handle
@ -3707,6 +3694,7 @@ Reference< css::rendering::XCanvas > Window::ImplGetCanvas( bool bSpriteCanvas )
static vcl::DeleteUnoReferenceOnDeinit<XMultiComponentFactory> xStaticCanvasFactory(
css::rendering::CanvasFactory::create( xContext ) );
Reference<XMultiComponentFactory> xCanvasFactory(xStaticCanvasFactory.get());
Reference< css::rendering::XCanvas > xCanvas;
if(xCanvasFactory.is())
{
@ -3740,25 +3728,12 @@ Reference< css::rendering::XCanvas > Window::ImplGetCanvas( bool bSpriteCanvas )
UNO_QUERY );
}
mpWindowImpl->mxCanvas = xCanvas;
}
// no factory??? Empty reference, then.
return xCanvas;
}
Reference< css::rendering::XCanvas > Window::GetCanvas() const
{
return ImplGetCanvas( false );
}
Reference< css::rendering::XSpriteCanvas > Window::GetSpriteCanvas() const
{
Reference< css::rendering::XSpriteCanvas > xSpriteCanvas(
ImplGetCanvas( true ), UNO_QUERY );
return xSpriteCanvas;
}
OUString Window::GetSurroundingText() const
{
return OUString();

View file

@ -145,13 +145,7 @@ void WorkWindow::ShowFullScreenMode( bool bFullScreenMode, sal_Int32 nDisplayScr
// Dispose of the canvas implementation, which might rely on
// screen-specific system data.
css::uno::Reference< css::rendering::XCanvas > xCanvas( mpWindowImpl->mxCanvas );
if( xCanvas.is() )
{
css::uno::Reference< css::lang::XComponent > xCanvasComponent( xCanvas, css::uno::UNO_QUERY );
if( xCanvasComponent.is() )
xCanvasComponent->dispose();
}
ImplDisposeCanvas();
mpWindowImpl->mpFrameWindow->mpWindowImpl->mbWaitSystemResize = true;
ImplGetFrame()->ShowFullScreen( bFullScreenMode, nDisplayScreen );