michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef GFX_DRAWABLE_H michael@0: #define GFX_DRAWABLE_H michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "gfxRect.h" michael@0: #include "gfxMatrix.h" michael@0: #include "GraphicsFilter.h" michael@0: #include "mozilla/gfx/2D.h" michael@0: michael@0: class gfxASurface; michael@0: class gfxImageSurface; michael@0: class gfxContext; michael@0: class gfxPattern; michael@0: michael@0: /** michael@0: * gfxDrawable michael@0: * An Interface representing something that has an intrinsic size and can draw michael@0: * itself repeatedly. michael@0: */ michael@0: class gfxDrawable { michael@0: NS_INLINE_DECL_REFCOUNTING(gfxDrawable) michael@0: public: michael@0: gfxDrawable(const gfxIntSize aSize) michael@0: : mSize(aSize) {} michael@0: michael@0: /** michael@0: * Draw into aContext filling aFillRect, possibly repeating, using aFilter. michael@0: * aTransform is a userspace to "image"space matrix. For example, if Draw michael@0: * draws using a gfxPattern, this is the matrix that should be set on the michael@0: * pattern prior to rendering it. michael@0: * @return whether drawing was successful michael@0: */ michael@0: virtual bool Draw(gfxContext* aContext, michael@0: const gfxRect& aFillRect, michael@0: bool aRepeat, michael@0: const GraphicsFilter& aFilter, michael@0: const gfxMatrix& aTransform = gfxMatrix()) = 0; michael@0: virtual already_AddRefed GetAsImageSurface() { return nullptr; } michael@0: virtual gfxIntSize Size() { return mSize; } michael@0: michael@0: protected: michael@0: // Protected destructor, to discourage deletion outside of Release(): michael@0: virtual ~gfxDrawable() {} michael@0: michael@0: const gfxIntSize mSize; michael@0: }; michael@0: michael@0: /** michael@0: * gfxSurfaceDrawable michael@0: * A convenience implementation of gfxDrawable for surfaces. michael@0: */ michael@0: class gfxSurfaceDrawable : public gfxDrawable { michael@0: public: michael@0: gfxSurfaceDrawable(gfxASurface* aSurface, const gfxIntSize aSize, michael@0: const gfxMatrix aTransform = gfxMatrix()); michael@0: gfxSurfaceDrawable(mozilla::gfx::DrawTarget* aDT, const gfxIntSize aSize, michael@0: const gfxMatrix aTransform = gfxMatrix()); michael@0: gfxSurfaceDrawable(mozilla::gfx::SourceSurface* aSurface, const gfxIntSize aSize, michael@0: const gfxMatrix aTransform = gfxMatrix()); michael@0: virtual ~gfxSurfaceDrawable() {} michael@0: michael@0: virtual bool Draw(gfxContext* aContext, michael@0: const gfxRect& aFillRect, michael@0: bool aRepeat, michael@0: const GraphicsFilter& aFilter, michael@0: const gfxMatrix& aTransform = gfxMatrix()); michael@0: michael@0: virtual already_AddRefed GetAsImageSurface(); michael@0: michael@0: protected: michael@0: nsRefPtr mSurface; michael@0: mozilla::RefPtr mDrawTarget; michael@0: mozilla::RefPtr mSourceSurface; michael@0: const gfxMatrix mTransform; michael@0: }; michael@0: michael@0: /** michael@0: * gfxDrawingCallback michael@0: * A simple drawing functor. michael@0: */ michael@0: class gfxDrawingCallback { michael@0: NS_INLINE_DECL_REFCOUNTING(gfxDrawingCallback) michael@0: protected: michael@0: // Protected destructor, to discourage deletion outside of Release(): michael@0: virtual ~gfxDrawingCallback() {} michael@0: michael@0: public: michael@0: /** michael@0: * Draw into aContext filling aFillRect using aFilter. michael@0: * aTransform is a userspace to "image"space matrix. For example, if Draw michael@0: * draws using a gfxPattern, this is the matrix that should be set on the michael@0: * pattern prior to rendering it. michael@0: * @return whether drawing was successful michael@0: */ michael@0: virtual bool operator()(gfxContext* aContext, michael@0: const gfxRect& aFillRect, michael@0: const GraphicsFilter& aFilter, michael@0: const gfxMatrix& aTransform = gfxMatrix()) = 0; michael@0: michael@0: }; michael@0: michael@0: /** michael@0: * gfxCallbackDrawable michael@0: * A convenience implementation of gfxDrawable for callbacks. michael@0: */ michael@0: class gfxCallbackDrawable : public gfxDrawable { michael@0: public: michael@0: gfxCallbackDrawable(gfxDrawingCallback* aCallback, const gfxIntSize aSize); michael@0: virtual ~gfxCallbackDrawable() {} michael@0: michael@0: virtual bool Draw(gfxContext* aContext, michael@0: const gfxRect& aFillRect, michael@0: bool aRepeat, michael@0: const GraphicsFilter& aFilter, michael@0: const gfxMatrix& aTransform = gfxMatrix()); michael@0: michael@0: protected: michael@0: already_AddRefed MakeSurfaceDrawable(const GraphicsFilter aFilter = GraphicsFilter::FILTER_FAST); michael@0: michael@0: nsRefPtr mCallback; michael@0: nsRefPtr mSurfaceDrawable; michael@0: }; michael@0: michael@0: /** michael@0: * gfxPatternDrawable michael@0: * A convenience implementation of gfxDrawable for patterns. michael@0: */ michael@0: class gfxPatternDrawable : public gfxDrawable { michael@0: public: michael@0: gfxPatternDrawable(gfxPattern* aPattern, michael@0: const gfxIntSize aSize); michael@0: virtual ~gfxPatternDrawable(); michael@0: michael@0: virtual bool Draw(gfxContext* aContext, michael@0: const gfxRect& aFillRect, michael@0: bool aRepeat, michael@0: const GraphicsFilter& aFilter, michael@0: const gfxMatrix& aTransform = gfxMatrix()); michael@0: michael@0: protected: michael@0: already_AddRefed MakeCallbackDrawable(); michael@0: michael@0: nsRefPtr mPattern; michael@0: }; michael@0: michael@0: #endif /* GFX_DRAWABLE_H */