gfx/2d/DrawTargetCG.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/DrawTargetCG.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,190 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     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 mozilla_gfx_DrawTargetCG_h
    1.10 +#define mozilla_gfx_DrawTargetCG_h
    1.11 +
    1.12 +#include <ApplicationServices/ApplicationServices.h>
    1.13 +
    1.14 +#include "2D.h"
    1.15 +#include "Rect.h"
    1.16 +#include "PathCG.h"
    1.17 +#include "SourceSurfaceCG.h"
    1.18 +#include "GLDefs.h"
    1.19 +#include "Tools.h"
    1.20 +
    1.21 +namespace mozilla {
    1.22 +namespace gfx {
    1.23 +
    1.24 +static inline CGAffineTransform
    1.25 +GfxMatrixToCGAffineTransform(Matrix m)
    1.26 +{
    1.27 +  CGAffineTransform t;
    1.28 +  t.a = m._11;
    1.29 +  t.b = m._12;
    1.30 +  t.c = m._21;
    1.31 +  t.d = m._22;
    1.32 +  t.tx = m._31;
    1.33 +  t.ty = m._32;
    1.34 +  return t;
    1.35 +}
    1.36 +
    1.37 +static inline Rect
    1.38 +CGRectToRect(CGRect rect)
    1.39 +{
    1.40 +  return Rect(rect.origin.x,
    1.41 +              rect.origin.y,
    1.42 +              rect.size.width,
    1.43 +              rect.size.height);
    1.44 +}
    1.45 +
    1.46 +static inline Point
    1.47 +CGPointToPoint(CGPoint point)
    1.48 +{
    1.49 +  return Point(point.x, point.y);
    1.50 +}
    1.51 +
    1.52 +static inline void
    1.53 +SetStrokeOptions(CGContextRef cg, const StrokeOptions &aStrokeOptions)
    1.54 +{
    1.55 +  switch (aStrokeOptions.mLineCap)
    1.56 +  {
    1.57 +    case CapStyle::BUTT:
    1.58 +      CGContextSetLineCap(cg, kCGLineCapButt);
    1.59 +      break;
    1.60 +    case CapStyle::ROUND:
    1.61 +      CGContextSetLineCap(cg, kCGLineCapRound);
    1.62 +      break;
    1.63 +    case CapStyle::SQUARE:
    1.64 +      CGContextSetLineCap(cg, kCGLineCapSquare);
    1.65 +      break;
    1.66 +  }
    1.67 +
    1.68 +  switch (aStrokeOptions.mLineJoin)
    1.69 +  {
    1.70 +    case JoinStyle::BEVEL:
    1.71 +      CGContextSetLineJoin(cg, kCGLineJoinBevel);
    1.72 +      break;
    1.73 +    case JoinStyle::ROUND:
    1.74 +      CGContextSetLineJoin(cg, kCGLineJoinRound);
    1.75 +      break;
    1.76 +    case JoinStyle::MITER:
    1.77 +    case JoinStyle::MITER_OR_BEVEL:
    1.78 +      CGContextSetLineJoin(cg, kCGLineJoinMiter);
    1.79 +      break;
    1.80 +  }
    1.81 +
    1.82 +  CGContextSetLineWidth(cg, aStrokeOptions.mLineWidth);
    1.83 +  CGContextSetMiterLimit(cg, aStrokeOptions.mMiterLimit);
    1.84 +
    1.85 +  // XXX: rename mDashLength to dashLength
    1.86 +  if (aStrokeOptions.mDashLength > 0) {
    1.87 +    // we use a regular array instead of a std::vector here because we don't want to leak the <vector> include
    1.88 +    CGFloat *dashes = new CGFloat[aStrokeOptions.mDashLength];
    1.89 +    for (size_t i=0; i<aStrokeOptions.mDashLength; i++) {
    1.90 +      dashes[i] = aStrokeOptions.mDashPattern[i];
    1.91 +    }
    1.92 +    CGContextSetLineDash(cg, aStrokeOptions.mDashOffset, dashes, aStrokeOptions.mDashLength);
    1.93 +    delete[] dashes;
    1.94 +  }
    1.95 +}
    1.96 +
    1.97 +
    1.98 +class DrawTargetCG : public DrawTarget
    1.99 +{
   1.100 +public:
   1.101 +  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetCG)
   1.102 +  friend class BorrowedCGContext;
   1.103 +  DrawTargetCG();
   1.104 +  virtual ~DrawTargetCG();
   1.105 +
   1.106 +  virtual BackendType GetType() const;
   1.107 +  virtual TemporaryRef<SourceSurface> Snapshot();
   1.108 +
   1.109 +  virtual void DrawSurface(SourceSurface *aSurface,
   1.110 +                           const Rect &aDest,
   1.111 +                           const Rect &aSource,
   1.112 +                           const DrawSurfaceOptions &aSurfOptions = DrawSurfaceOptions(),
   1.113 +                           const DrawOptions &aOptions = DrawOptions());
   1.114 +  virtual void DrawFilter(FilterNode *aNode,
   1.115 +                          const Rect &aSourceRect,
   1.116 +                          const Point &aDestPoint,
   1.117 +                          const DrawOptions &aOptions = DrawOptions());
   1.118 +  virtual void MaskSurface(const Pattern &aSource,
   1.119 +                           SourceSurface *aMask,
   1.120 +                           Point aOffset,
   1.121 +                           const DrawOptions &aOptions = DrawOptions());
   1.122 +
   1.123 +  virtual void FillRect(const Rect &aRect,
   1.124 +                        const Pattern &aPattern,
   1.125 +                        const DrawOptions &aOptions = DrawOptions());
   1.126 +
   1.127 +
   1.128 +  //XXX: why do we take a reference to SurfaceFormat?
   1.129 +  bool Init(BackendType aType, const IntSize &aSize, SurfaceFormat&);
   1.130 +  bool Init(BackendType aType, unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat);
   1.131 +  bool Init(CGContextRef cgContext, const IntSize &aSize);
   1.132 +
   1.133 +  // Flush if using IOSurface context
   1.134 +  virtual void Flush();
   1.135 +
   1.136 +  virtual void DrawSurfaceWithShadow(SourceSurface *, const Point &, const Color &, const Point &, Float, CompositionOp);
   1.137 +  virtual void ClearRect(const Rect &);
   1.138 +  virtual void CopySurface(SourceSurface *, const IntRect&, const IntPoint&);
   1.139 +  virtual void StrokeRect(const Rect &, const Pattern &, const StrokeOptions&, const DrawOptions&);
   1.140 +  virtual void StrokeLine(const Point &, const Point &, const Pattern &, const StrokeOptions &, const DrawOptions &);
   1.141 +  virtual void Stroke(const Path *, const Pattern &, const StrokeOptions &, const DrawOptions &);
   1.142 +  virtual void Fill(const Path *, const Pattern &, const DrawOptions &);
   1.143 +  virtual void FillGlyphs(ScaledFont *, const GlyphBuffer&, const Pattern &, const DrawOptions &, const GlyphRenderingOptions *);
   1.144 +  virtual void Mask(const Pattern &aSource,
   1.145 +                    const Pattern &aMask,
   1.146 +                    const DrawOptions &aOptions = DrawOptions());
   1.147 +  virtual void PushClip(const Path *);
   1.148 +  virtual void PushClipRect(const Rect &aRect);
   1.149 +  virtual void PopClip();
   1.150 +  virtual TemporaryRef<SourceSurface> CreateSourceSurfaceFromNativeSurface(const NativeSurface&) const { return nullptr;}
   1.151 +  virtual TemporaryRef<DrawTarget> CreateSimilarDrawTarget(const IntSize &, SurfaceFormat) const;
   1.152 +  virtual TemporaryRef<PathBuilder> CreatePathBuilder(FillRule) const;
   1.153 +  virtual TemporaryRef<GradientStops> CreateGradientStops(GradientStop *, uint32_t,
   1.154 +                                                          ExtendMode aExtendMode = ExtendMode::CLAMP) const;
   1.155 +  virtual TemporaryRef<FilterNode> CreateFilter(FilterType aType);
   1.156 +
   1.157 +  virtual void *GetNativeSurface(NativeSurfaceType);
   1.158 +
   1.159 +  virtual IntSize GetSize() { return mSize; }
   1.160 +
   1.161 +
   1.162 +  /* This is for creating good compatible surfaces */
   1.163 +  virtual TemporaryRef<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData,
   1.164 +                                                            const IntSize &aSize,
   1.165 +                                                            int32_t aStride,
   1.166 +                                                            SurfaceFormat aFormat) const;
   1.167 +  virtual TemporaryRef<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const;
   1.168 +  CGContextRef GetCGContext() {
   1.169 +      return mCg;
   1.170 +  }
   1.171 +private:
   1.172 +  void MarkChanged();
   1.173 +
   1.174 +  IntSize mSize;
   1.175 +  CGColorSpaceRef mColorSpace;
   1.176 +  CGContextRef mCg;
   1.177 +
   1.178 +  /**
   1.179 +   * The image buffer, if the buffer is owned by this class.
   1.180 +   * If the DrawTarget was created for a pre-existing buffer or if the buffer's
   1.181 +   * lifetime is managed by CoreGraphics, mData will be null.
   1.182 +   * Data owned by DrawTargetCG will be deallocated in the destructor.
   1.183 +   */
   1.184 +  AlignedArray<uint8_t> mData;
   1.185 +
   1.186 +  RefPtr<SourceSurfaceCGContext> mSnapshot;
   1.187 +};
   1.188 +
   1.189 +}
   1.190 +}
   1.191 +
   1.192 +#endif
   1.193 +

mercurial