From 96ffd0f9c67f2be3f77c630d11c5d24fa32db736 Mon Sep 17 00:00:00 2001 From: "erg@google.com" Date: Thu, 28 Aug 2008 22:34:22 +0000 Subject: [PATCH] Provide what looks like the minimal platform specific interface for skia. This will undoubtedly grow in the future. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1513 0039d316-1c4b-4281-b951-d872f2087c98 --- base/gfx/SConscript | 13 ++++-- base/gfx/bitmap_platform_device.h | 6 +++ base/gfx/bitmap_platform_device_linux.cc | 44 ++++++++++++++++++++ base/gfx/bitmap_platform_device_linux.h | 31 ++++++++++++++ base/gfx/platform_canvas.h | 3 ++ base/gfx/platform_canvas_linux.cc | 46 ++++++++++++++++++++ base/gfx/platform_canvas_linux.h | 53 ++++++++++++++++++++++++ base/gfx/platform_device.h | 6 +++ base/gfx/platform_device_linux.cc | 18 ++++++++ base/gfx/platform_device_linux.h | 21 ++++++++++ 10 files changed, 237 insertions(+), 4 deletions(-) create mode 100644 base/gfx/bitmap_platform_device_linux.cc create mode 100644 base/gfx/bitmap_platform_device_linux.h create mode 100644 base/gfx/platform_canvas_linux.cc create mode 100644 base/gfx/platform_canvas_linux.h create mode 100644 base/gfx/platform_device_linux.cc create mode 100644 base/gfx/platform_device_linux.h diff --git a/base/gfx/SConscript b/base/gfx/SConscript index 37939f87eeab9f..e7687a36fc84e6 100644 --- a/base/gfx/SConscript +++ b/base/gfx/SConscript @@ -87,10 +87,15 @@ if env['PLATFORM'] == 'win32': if env['PLATFORM'] == 'win32': input_files.extend([ - 'bitmap_platform_device_win.cc', - 'platform_canvas_win.cc', - 'platform_device_win.cc', - + 'bitmap_platform_device_win.cc', + 'platform_canvas_win.cc', + 'platform_device_win.cc', + ]) +elif env['PLATFORM'] == 'posix': + input_files.extend([ + 'bitmap_platform_device_linux.cc', + 'platform_canvas_linux.cc', + 'platform_device_linux.cc', ]) env.ChromeStaticLibrary('base_gfx', input_files) diff --git a/base/gfx/bitmap_platform_device.h b/base/gfx/bitmap_platform_device.h index 9b4ecd78372df5..83b74df0442307 100644 --- a/base/gfx/bitmap_platform_device.h +++ b/base/gfx/bitmap_platform_device.h @@ -6,11 +6,17 @@ // that can be used by upper-level classes that just need to pass a reference // around. +namespace gfx { + #if defined(OS_WIN) class BitmapPlatformDeviceWin; typedef BitmapPlatformDeviceWin BitmapPlatformDevice; #elif defined(OS_MACOSX) class BitmapPlatformDeviceMac; typedef BitmapPlatformDeviceMac BitmapPlatformDevice; +#elif defined(OS_LINUX) +class BitmapPlatformDeviceLinux; +typedef BitmapPlatformDeviceLinux BitmapPlatformDevice; #endif +} // namespace gfx diff --git a/base/gfx/bitmap_platform_device_linux.cc b/base/gfx/bitmap_platform_device_linux.cc new file mode 100644 index 00000000000000..1211a1536d5bb7 --- /dev/null +++ b/base/gfx/bitmap_platform_device_linux.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/gfx/bitmap_platform_device_linux.h" + +#include "base/logging.h" + +#include + +namespace gfx { + +// We use this static factory function instead of the regular constructor so +// that we can create the pixel data before calling the constructor. This is +// required so that we can call the base class' constructor with the pixel +// data. +BitmapPlatformDeviceLinux* BitmapPlatformDeviceLinux::Create( + int width, int height, bool is_opaque) { + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); + bitmap.setIsOpaque(is_opaque); + + if (is_opaque) { +#ifndef NDEBUG + // To aid in finding bugs, we set the background color to something + // obviously wrong so it will be noticable when it is not cleared + bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green +#endif + } + + // The device object will take ownership of the graphics context. + return new BitmapPlatformDeviceLinux(bitmap); +} + +// The device will own the bitmap, which corresponds to also owning the pixel +// data. Therefore, we do not transfer ownership to the SkDevice's bitmap. +BitmapPlatformDeviceLinux::BitmapPlatformDeviceLinux(const SkBitmap& bitmap) + : PlatformDeviceLinux(bitmap) { +} + +BitmapPlatformDeviceLinux::~BitmapPlatformDeviceLinux() { +} + +} // namespace gfx diff --git a/base/gfx/bitmap_platform_device_linux.h b/base/gfx/bitmap_platform_device_linux.h new file mode 100644 index 00000000000000..d8504dc6dc0e8a --- /dev/null +++ b/base/gfx/bitmap_platform_device_linux.h @@ -0,0 +1,31 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_GFX_BITMAP_PLATFORM_DEVICE_LINUX_H_ +#define BASE_GFX_BITMAP_PLATFORM_DEVICE_LINUX_H_ + +#include "base/gfx/platform_device_linux.h" +#include "base/ref_counted.h" + +namespace gfx { + +// I'm trying to get away with defining as little as possible on this. Right +// now, we don't do anything. +class BitmapPlatformDeviceLinux : public PlatformDeviceLinux { + public: + /// Static constructor. I don't understand this, it's just a copy of the mac + static BitmapPlatformDeviceLinux* Create(int width, int height, + bool is_opaque); + + /// Create a BitmapPlatformDeviceLinux from an already constructed bitmap; + /// you should probably be using Create(). This may become private later if + /// we ever have to share state between some native drawing UI and Skia, like + /// the Windows and Mac versions of this class do. + BitmapPlatformDeviceLinux(const SkBitmap& other); + virtual ~BitmapPlatformDeviceLinux(); +}; + +} // namespace gfx + +#endif // BASE_GFX_BITMAP_PLATFORM_DEVICE_LINUX_H_ diff --git a/base/gfx/platform_canvas.h b/base/gfx/platform_canvas.h index 3219a7a722493a..d7433cd386d0b7 100644 --- a/base/gfx/platform_canvas.h +++ b/base/gfx/platform_canvas.h @@ -16,6 +16,9 @@ typedef PlatformCanvasWin PlatformCanvas; #elif defined(OS_MACOSX) class PlatformCanvasMac; typedef PlatformCanvasMac PlatformCanvas; +#elif defined(OS_LINUX) +class PlatformCanvasLinux; +typedef PlatformCanvasLinux PlatformCanvas; #endif } // namespace gfx diff --git a/base/gfx/platform_canvas_linux.cc b/base/gfx/platform_canvas_linux.cc new file mode 100644 index 00000000000000..71bc8f08699aeb --- /dev/null +++ b/base/gfx/platform_canvas_linux.cc @@ -0,0 +1,46 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/gfx/platform_canvas_linux.h" + +#include "base/gfx/bitmap_platform_device_linux.h" +#include "base/logging.h" + +namespace gfx { + +PlatformCanvasLinux::PlatformCanvasLinux() : SkCanvas() { +} + +PlatformCanvasLinux::PlatformCanvasLinux(int width, int height, bool is_opaque) + : SkCanvas() { + initialize(width, height, is_opaque); +} + +PlatformCanvasLinux::~PlatformCanvasLinux() { +} + +void PlatformCanvasLinux::initialize(int width, int height, bool is_opaque) { + SkDevice* device = createPlatformDevice(width, height, is_opaque); + setDevice(device); + device->unref(); // was created with refcount 1, and setDevice also refs +} + +PlatformDeviceLinux& PlatformCanvasLinux::getTopPlatformDevice() const { + // All of our devices should be our special PlatformDevice. + SkCanvas::LayerIter iter(const_cast(this), false); + return *static_cast(iter.device()); +} + +SkDevice* PlatformCanvasLinux::createDevice(SkBitmap::Config, int width, + int height, bool is_opaque) { + return createPlatformDevice(width, height, is_opaque); +} + +SkDevice* PlatformCanvasLinux::createPlatformDevice(int width, + int height, + bool is_opaque) { + return BitmapPlatformDeviceLinux::Create(width, height, is_opaque); +} + +} // namespace gfx diff --git a/base/gfx/platform_canvas_linux.h b/base/gfx/platform_canvas_linux.h new file mode 100644 index 00000000000000..4030a55b2c738d --- /dev/null +++ b/base/gfx/platform_canvas_linux.h @@ -0,0 +1,53 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_GFX_PLATFORM_CANVAS_LINUX_H_ +#define BASE_GFX_PLATFORM_CANVAS_LINUX_H_ + +#include "base/gfx/platform_device_linux.h" +#include "base/basictypes.h" + +#import "SkCanvas.h" + +namespace gfx { + +// This class is a specialization of the regular SkCanvas that is designed to +// work with a gfx::PlatformDevice to manage platform-specific drawing. It +// allows using both Skia operations and platform-specific operations. +class PlatformCanvasLinux : public SkCanvas { + public: + // Set is_opaque if you are going to erase the bitmap and not use + // tranparency: this will enable some optimizations. The shared_section + // parameter is passed to gfx::PlatformDevice::create. See it for details. + // + // If you use the version with no arguments, you MUST call initialize() + PlatformCanvasLinux(); + PlatformCanvasLinux(int width, int height, bool is_opaque); + virtual ~PlatformCanvasLinux(); + + // For two-part init, call if you use the no-argument constructor above + void initialize(int width, int height, bool is_opaque); + + // Returns the platform device pointer of the topmost rect with a non-empty + // clip. Both the windows and mac versions have an equivalent of this method; + // a Linux version is added for compatibility. + PlatformDeviceLinux& getTopPlatformDevice() const; + + protected: + // Creates a device store for use by the canvas. We override this so that + // the device is always our own so we know that we can use GDI operations + // on it. Simply calls into createPlatformDevice(). + virtual SkDevice* createDevice(SkBitmap::Config, int width, int height, + bool is_opaque); + + // Creates a device store for use by the canvas. By default, it creates a + // BitmapPlatformDevice object. Can be overridden to change the object type. + virtual SkDevice* createPlatformDevice(int width, int height, bool is_opaque); + + DISALLOW_COPY_AND_ASSIGN(PlatformCanvasLinux); +}; + +} // namespace gfx + +#endif // BASE_GFX_PLATFORM_CANVAS_MAC_H_ diff --git a/base/gfx/platform_device.h b/base/gfx/platform_device.h index 644e89d90384bb..796eec8bd685d1 100644 --- a/base/gfx/platform_device.h +++ b/base/gfx/platform_device.h @@ -6,11 +6,17 @@ // that can be used by upper-level classes that just need to pass a reference // around. +namespace gfx { + #if defined(OS_WIN) class PlatformDeviceWin; typedef PlatformDeviceWin PlatformDevice; #elif defined(OS_MACOSX) class PlatformDeviceMac; typedef PlatformDeviceMac PlatformDevice; +#elif defined(OS_LINUX) +class PlatformDeviceLinux; +typedef PlatformDeviceLinux PlatformDevice; #endif +} // namespace gfx diff --git a/base/gfx/platform_device_linux.cc b/base/gfx/platform_device_linux.cc new file mode 100644 index 00000000000000..1f948b1b361fc4 --- /dev/null +++ b/base/gfx/platform_device_linux.cc @@ -0,0 +1,18 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/gfx/platform_device_linux.h" + +#include "base/logging.h" +#include "SkMatrix.h" +#include "SkPath.h" +#include "SkUtils.h" + +namespace gfx { + +PlatformDeviceLinux::PlatformDeviceLinux(const SkBitmap& bitmap) + : SkDevice(bitmap) { +} + +} // namespace gfx diff --git a/base/gfx/platform_device_linux.h b/base/gfx/platform_device_linux.h new file mode 100644 index 00000000000000..52fbfae8abc09d --- /dev/null +++ b/base/gfx/platform_device_linux.h @@ -0,0 +1,21 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_GFX_PLATFORM_DEVICE_LINUX_H_ +#define BASE_GFX_PLATFORM_DEVICE_LINUX_H_ + +#include "SkDevice.h" + +namespace gfx { + +// Blindly copying the mac hierarchy. +class PlatformDeviceLinux : public SkDevice { + protected: + // Forwards |bitmap| to SkDevice's constructor. + PlatformDeviceLinux(const SkBitmap& bitmap); +}; + +} // namespace gfx + +#endif // BASE_GFX_PLATFORM_DEVICE_LINUX_H_