1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/thebes/gfxDrawable.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef GFX_DRAWABLE_H 1.10 +#define GFX_DRAWABLE_H 1.11 + 1.12 +#include "nsAutoPtr.h" 1.13 +#include "gfxRect.h" 1.14 +#include "gfxMatrix.h" 1.15 +#include "GraphicsFilter.h" 1.16 +#include "mozilla/gfx/2D.h" 1.17 + 1.18 +class gfxASurface; 1.19 +class gfxImageSurface; 1.20 +class gfxContext; 1.21 +class gfxPattern; 1.22 + 1.23 +/** 1.24 + * gfxDrawable 1.25 + * An Interface representing something that has an intrinsic size and can draw 1.26 + * itself repeatedly. 1.27 + */ 1.28 +class gfxDrawable { 1.29 + NS_INLINE_DECL_REFCOUNTING(gfxDrawable) 1.30 +public: 1.31 + gfxDrawable(const gfxIntSize aSize) 1.32 + : mSize(aSize) {} 1.33 + 1.34 + /** 1.35 + * Draw into aContext filling aFillRect, possibly repeating, using aFilter. 1.36 + * aTransform is a userspace to "image"space matrix. For example, if Draw 1.37 + * draws using a gfxPattern, this is the matrix that should be set on the 1.38 + * pattern prior to rendering it. 1.39 + * @return whether drawing was successful 1.40 + */ 1.41 + virtual bool Draw(gfxContext* aContext, 1.42 + const gfxRect& aFillRect, 1.43 + bool aRepeat, 1.44 + const GraphicsFilter& aFilter, 1.45 + const gfxMatrix& aTransform = gfxMatrix()) = 0; 1.46 + virtual already_AddRefed<gfxImageSurface> GetAsImageSurface() { return nullptr; } 1.47 + virtual gfxIntSize Size() { return mSize; } 1.48 + 1.49 +protected: 1.50 + // Protected destructor, to discourage deletion outside of Release(): 1.51 + virtual ~gfxDrawable() {} 1.52 + 1.53 + const gfxIntSize mSize; 1.54 +}; 1.55 + 1.56 +/** 1.57 + * gfxSurfaceDrawable 1.58 + * A convenience implementation of gfxDrawable for surfaces. 1.59 + */ 1.60 +class gfxSurfaceDrawable : public gfxDrawable { 1.61 +public: 1.62 + gfxSurfaceDrawable(gfxASurface* aSurface, const gfxIntSize aSize, 1.63 + const gfxMatrix aTransform = gfxMatrix()); 1.64 + gfxSurfaceDrawable(mozilla::gfx::DrawTarget* aDT, const gfxIntSize aSize, 1.65 + const gfxMatrix aTransform = gfxMatrix()); 1.66 + gfxSurfaceDrawable(mozilla::gfx::SourceSurface* aSurface, const gfxIntSize aSize, 1.67 + const gfxMatrix aTransform = gfxMatrix()); 1.68 + virtual ~gfxSurfaceDrawable() {} 1.69 + 1.70 + virtual bool Draw(gfxContext* aContext, 1.71 + const gfxRect& aFillRect, 1.72 + bool aRepeat, 1.73 + const GraphicsFilter& aFilter, 1.74 + const gfxMatrix& aTransform = gfxMatrix()); 1.75 + 1.76 + virtual already_AddRefed<gfxImageSurface> GetAsImageSurface(); 1.77 + 1.78 +protected: 1.79 + nsRefPtr<gfxASurface> mSurface; 1.80 + mozilla::RefPtr<mozilla::gfx::DrawTarget> mDrawTarget; 1.81 + mozilla::RefPtr<mozilla::gfx::SourceSurface> mSourceSurface; 1.82 + const gfxMatrix mTransform; 1.83 +}; 1.84 + 1.85 +/** 1.86 + * gfxDrawingCallback 1.87 + * A simple drawing functor. 1.88 + */ 1.89 +class gfxDrawingCallback { 1.90 + NS_INLINE_DECL_REFCOUNTING(gfxDrawingCallback) 1.91 +protected: 1.92 + // Protected destructor, to discourage deletion outside of Release(): 1.93 + virtual ~gfxDrawingCallback() {} 1.94 + 1.95 +public: 1.96 + /** 1.97 + * Draw into aContext filling aFillRect using aFilter. 1.98 + * aTransform is a userspace to "image"space matrix. For example, if Draw 1.99 + * draws using a gfxPattern, this is the matrix that should be set on the 1.100 + * pattern prior to rendering it. 1.101 + * @return whether drawing was successful 1.102 + */ 1.103 + virtual bool operator()(gfxContext* aContext, 1.104 + const gfxRect& aFillRect, 1.105 + const GraphicsFilter& aFilter, 1.106 + const gfxMatrix& aTransform = gfxMatrix()) = 0; 1.107 + 1.108 +}; 1.109 + 1.110 +/** 1.111 + * gfxCallbackDrawable 1.112 + * A convenience implementation of gfxDrawable for callbacks. 1.113 + */ 1.114 +class gfxCallbackDrawable : public gfxDrawable { 1.115 +public: 1.116 + gfxCallbackDrawable(gfxDrawingCallback* aCallback, const gfxIntSize aSize); 1.117 + virtual ~gfxCallbackDrawable() {} 1.118 + 1.119 + virtual bool Draw(gfxContext* aContext, 1.120 + const gfxRect& aFillRect, 1.121 + bool aRepeat, 1.122 + const GraphicsFilter& aFilter, 1.123 + const gfxMatrix& aTransform = gfxMatrix()); 1.124 + 1.125 +protected: 1.126 + already_AddRefed<gfxSurfaceDrawable> MakeSurfaceDrawable(const GraphicsFilter aFilter = GraphicsFilter::FILTER_FAST); 1.127 + 1.128 + nsRefPtr<gfxDrawingCallback> mCallback; 1.129 + nsRefPtr<gfxSurfaceDrawable> mSurfaceDrawable; 1.130 +}; 1.131 + 1.132 +/** 1.133 + * gfxPatternDrawable 1.134 + * A convenience implementation of gfxDrawable for patterns. 1.135 + */ 1.136 +class gfxPatternDrawable : public gfxDrawable { 1.137 +public: 1.138 + gfxPatternDrawable(gfxPattern* aPattern, 1.139 + const gfxIntSize aSize); 1.140 + virtual ~gfxPatternDrawable(); 1.141 + 1.142 + virtual bool Draw(gfxContext* aContext, 1.143 + const gfxRect& aFillRect, 1.144 + bool aRepeat, 1.145 + const GraphicsFilter& aFilter, 1.146 + const gfxMatrix& aTransform = gfxMatrix()); 1.147 + 1.148 +protected: 1.149 + already_AddRefed<gfxCallbackDrawable> MakeCallbackDrawable(); 1.150 + 1.151 + nsRefPtr<gfxPattern> mPattern; 1.152 +}; 1.153 + 1.154 +#endif /* GFX_DRAWABLE_H */