Bug 1480620. Add DrawTargetOffset. r=Bas draft
authorJeff Muizelaar <jmuizelaar@mozilla.com>
Fri, 03 Aug 2018 10:48:37 -0400
changeset 826324 a5165f1d9ee39236eafa78cb052b204b28404fad
parent 823465 87bcafe428a4ad6017e59b915581ae00aa863407
child 826325 9c2088485400c21f882f3141bd9e20156331ad8b
child 826410 c4b1d5359b1e24f523924b1a5ee9ad97bda80bc0
child 826429 e3b182c0824ffaba13f6b8c9499c92e439956e45
push id118291
push userbmo:jmuizelaar@mozilla.com
push dateFri, 03 Aug 2018 15:03:55 +0000
reviewersBas
bugs1480620
milestone63.0a1
Bug 1480620. Add DrawTargetOffset. r=Bas This adds a DrawTargetOffset which is basically a simplified DrawTargetTiled that only supports one tile. It useful for situations where Cairo's device offset was used previously. This also replaces WebRender's use of DrawTargetTiled which was just trying to apply offset. MozReview-Commit-ID: I33PB6CnHh0
gfx/2d/2D.h
gfx/2d/DrawTargetOffset.cpp
gfx/2d/DrawTargetOffset.h
gfx/2d/Factory.cpp
gfx/2d/moz.build
--- a/gfx/2d/2D.h
+++ b/gfx/2d/2D.h
@@ -1752,16 +1752,17 @@ public:
 
   /*
    * This creates a new tiled DrawTarget. When a tiled drawtarget is used the
    * drawing is distributed over number of tiles which may each hold an
    * individual offset. The tiles in the set must each have the same backend
    * and format.
    */
   static already_AddRefed<DrawTarget> CreateTiledDrawTarget(const TileSet& aTileSet);
+  static already_AddRefed<DrawTarget> CreateOffsetDrawTarget(DrawTarget *aDrawTarget, IntPoint aTileOrigin);
 
   static bool DoesBackendSupportDataDrawtarget(BackendType aType);
 
 #ifdef USE_SKIA
   static already_AddRefed<DrawTarget> CreateDrawTargetWithSkCanvas(SkCanvas* aCanvas);
 #endif
 
 #ifdef MOZ_ENABLE_FREETYPE
new file mode 100644
--- /dev/null
+++ b/gfx/2d/DrawTargetOffset.cpp
@@ -0,0 +1,192 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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 "DrawTargetOffset.h"
+#include "Logging.h"
+#include "PathHelpers.h"
+
+using namespace std;
+
+namespace mozilla {
+namespace gfx {
+
+DrawTargetOffset::DrawTargetOffset()
+{
+}
+
+bool
+DrawTargetOffset::Init(DrawTarget *aDrawTarget, IntPoint aOrigin)
+{
+  mDrawTarget = aDrawTarget;
+  mOrigin = aOrigin;
+  mDrawTarget->SetTransform(Matrix::Translation(-mOrigin.x,
+                                                -mOrigin.y));
+  mFormat = mDrawTarget->GetFormat();
+  SetPermitSubpixelAA(IsOpaque(mFormat));
+  return true;
+}
+
+already_AddRefed<SourceSurface>
+DrawTargetOffset::Snapshot()
+{
+  return mDrawTarget->Snapshot();
+}
+
+void
+DrawTargetOffset::DetachAllSnapshots()
+{}
+
+// Skip the mClippedOut check since this is only used for Flush() which
+// should happen even if we're clipped.
+#define OFFSET_COMMAND(command) \
+  void \
+  DrawTargetOffset::command() \
+  { \
+    mDrawTarget->command(); \
+  }
+#define OFFSET_COMMAND1(command, type1) \
+  void \
+  DrawTargetOffset::command(type1 arg1) \
+  { \
+    mDrawTarget->command(arg1); \
+  }
+#define OFFSET_COMMAND3(command, type1, type2, type3) \
+  void \
+  DrawTargetOffset::command(type1 arg1, type2 arg2, type3 arg3) \
+  { \
+    mDrawTarget->command(arg1, arg2, arg3); \
+  }
+#define OFFSET_COMMAND4(command, type1, type2, type3, type4) \
+  void \
+  DrawTargetOffset::command(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
+  { \
+    mDrawTarget->command(arg1, arg2, arg3, arg4); \
+  }
+#define OFFSET_COMMAND5(command, type1, type2, type3, type4, type5) \
+  void \
+  DrawTargetOffset::command(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \
+  { \
+    mDrawTarget->command(arg1, arg2, arg3, arg4, arg5); \
+  }
+
+OFFSET_COMMAND(Flush)
+OFFSET_COMMAND4(DrawFilter, FilterNode*, const Rect&, const Point&, const DrawOptions&)
+OFFSET_COMMAND1(ClearRect, const Rect&)
+OFFSET_COMMAND4(MaskSurface, const Pattern&, SourceSurface*, Point, const DrawOptions&)
+OFFSET_COMMAND4(FillGlyphs, ScaledFont*, const GlyphBuffer&, const Pattern&, const DrawOptions&)
+OFFSET_COMMAND3(Mask, const Pattern&, const Pattern&, const DrawOptions&)
+
+void
+DrawTargetOffset::PushClip(const Path* aPath)
+{
+  mDrawTarget->PushClip(aPath);
+}
+
+void
+DrawTargetOffset::PushClipRect(const Rect& aRect)
+{
+  mDrawTarget->PushClipRect(aRect);
+}
+
+void
+DrawTargetOffset::PopClip()
+{
+  mDrawTarget->PopClip();
+}
+
+void
+DrawTargetOffset::CopySurface(SourceSurface *aSurface,
+                             const IntRect &aSourceRect,
+                             const IntPoint &aDestination)
+{
+  IntPoint tileOrigin = mOrigin;
+  // CopySurface ignores the transform, account for that here.
+  mDrawTarget->CopySurface(aSurface, aSourceRect, aDestination - tileOrigin);
+}
+
+void
+DrawTargetOffset::SetTransform(const Matrix& aTransform)
+{
+  Matrix mat = aTransform;
+  mat.PostTranslate(Float(-mOrigin.x), Float(-mOrigin.y));
+  mDrawTarget->SetTransform(mat);
+
+  DrawTarget::SetTransform(aTransform);
+}
+
+void
+DrawTargetOffset::SetPermitSubpixelAA(bool aPermitSubpixelAA)
+{
+  mDrawTarget->SetPermitSubpixelAA(aPermitSubpixelAA);
+}
+
+void
+DrawTargetOffset::DrawSurface(SourceSurface* aSurface, const Rect& aDest, const Rect& aSource, const DrawSurfaceOptions& aSurfaceOptions, const DrawOptions& aDrawOptions)
+{
+  mDrawTarget->DrawSurface(aSurface, aDest, aSource, aSurfaceOptions, aDrawOptions);
+}
+
+void
+DrawTargetOffset::FillRect(const Rect& aRect, const Pattern& aPattern, const DrawOptions& aDrawOptions)
+{
+  mDrawTarget->FillRect(aRect, aPattern, aDrawOptions);
+}
+
+void
+DrawTargetOffset::Stroke(const Path* aPath, const Pattern& aPattern, const StrokeOptions& aStrokeOptions, const DrawOptions& aDrawOptions)
+{
+  mDrawTarget->Stroke(aPath, aPattern, aStrokeOptions, aDrawOptions);
+}
+
+void
+DrawTargetOffset::StrokeRect(const Rect& aRect, const Pattern& aPattern, const StrokeOptions &aStrokeOptions, const DrawOptions& aDrawOptions)
+{
+  mDrawTarget->StrokeRect(aRect, aPattern, aStrokeOptions, aDrawOptions);
+}
+
+void
+DrawTargetOffset::StrokeLine(const Point& aStart, const Point& aEnd, const Pattern& aPattern, const StrokeOptions &aStrokeOptions, const DrawOptions& aDrawOptions)
+{
+  mDrawTarget->StrokeLine(aStart, aEnd, aPattern, aStrokeOptions, aDrawOptions);
+}
+
+void
+DrawTargetOffset::Fill(const Path* aPath, const Pattern& aPattern, const DrawOptions& aDrawOptions)
+{
+  mDrawTarget->Fill(aPath, aPattern, aDrawOptions);
+}
+
+void
+DrawTargetOffset::PushLayer(bool aOpaque, Float aOpacity, SourceSurface* aMask,
+                           const Matrix& aMaskTransform, const IntRect& aBounds,
+                           bool aCopyBackground)
+{
+  IntRect bounds = aBounds;
+  bounds.MoveBy(mOrigin);
+  mDrawTarget->PushLayer(aOpaque, aOpacity, aMask, aMaskTransform, bounds, aCopyBackground);
+}
+
+void
+DrawTargetOffset::PushLayerWithBlend(bool aOpaque, Float aOpacity,
+                                    SourceSurface* aMask,
+                                    const Matrix& aMaskTransform,
+                                    const IntRect& aBounds,
+                                    bool aCopyBackground,
+                                    CompositionOp aOp)
+{
+  IntRect bounds = aBounds;
+  bounds.MoveBy(mOrigin);
+  mDrawTarget->PushLayerWithBlend(aOpaque, aOpacity, aMask, aMaskTransform, bounds, aCopyBackground, aOp);
+}
+
+void
+DrawTargetOffset::PopLayer()
+{
+  mDrawTarget->PopLayer();
+}
+
+} // namespace gfx
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/gfx/2d/DrawTargetOffset.h
@@ -0,0 +1,167 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#ifndef MOZILLA_GFX_DRAWTARGETOFFSET_H_
+#define MOZILLA_GFX_DRAWTARGETOFFSET_H_
+
+#include "2D.h"
+
+#include "mozilla/Vector.h"
+
+#include "Filters.h"
+#include "Logging.h"
+
+#include <vector>
+
+namespace mozilla {
+namespace gfx {
+
+class DrawTargetOffset : public DrawTarget
+{
+public:
+  DrawTargetOffset();
+
+  bool Init(DrawTarget *aDrawTarget, IntPoint aOrigin);
+
+  // We'll pestimistically return true here
+  virtual bool IsTiledDrawTarget() const override { return true; }
+
+  virtual bool IsCaptureDT() const override { return mDrawTarget->IsCaptureDT(); }
+  virtual DrawTargetType GetType() const override { return mDrawTarget->GetType(); }
+  virtual BackendType GetBackendType() const override { return mDrawTarget->GetBackendType(); }
+  virtual already_AddRefed<SourceSurface> Snapshot() override;
+  virtual void DetachAllSnapshots() override;
+  virtual IntSize GetSize() const override {
+    return mDrawTarget->GetSize();
+  }
+  virtual IntRect GetRect() const override {
+    return mDrawTarget->GetRect();
+  }
+
+  virtual void Flush() override;
+  virtual void DrawSurface(SourceSurface *aSurface,
+                           const Rect &aDest,
+                           const Rect &aSource,
+                           const DrawSurfaceOptions &aSurfOptions,
+                           const DrawOptions &aOptions) override;
+  virtual void DrawFilter(FilterNode *aNode,
+                          const Rect &aSourceRect,
+                          const Point &aDestPoint,
+                          const DrawOptions &aOptions = DrawOptions()) override;
+  virtual void DrawSurfaceWithShadow(SourceSurface *aSurface,
+                                     const Point &aDest,
+                                     const Color &aColor,
+                                     const Point &aOffset,
+                                     Float aSigma,
+                                     CompositionOp aOperator) override { /* Not implemented */ MOZ_CRASH("GFX: DrawSurfaceWithShadow"); }
+
+  virtual void ClearRect(const Rect &aRect) override;
+  virtual void MaskSurface(const Pattern &aSource,
+                           SourceSurface *aMask,
+                           Point aOffset,
+                           const DrawOptions &aOptions = DrawOptions()) override;
+
+  virtual void CopySurface(SourceSurface *aSurface,
+                           const IntRect &aSourceRect,
+                           const IntPoint &aDestination) override;
+
+  virtual void FillRect(const Rect &aRect,
+                        const Pattern &aPattern,
+                        const DrawOptions &aOptions = DrawOptions()) override;
+  virtual void StrokeRect(const Rect &aRect,
+                          const Pattern &aPattern,
+                          const StrokeOptions &aStrokeOptions = StrokeOptions(),
+                          const DrawOptions &aOptions = DrawOptions()) override;
+  virtual void StrokeLine(const Point &aStart,
+                          const Point &aEnd,
+                          const Pattern &aPattern,
+                          const StrokeOptions &aStrokeOptions = StrokeOptions(),
+                          const DrawOptions &aOptions = DrawOptions()) override;
+  virtual void Stroke(const Path *aPath,
+                      const Pattern &aPattern,
+                      const StrokeOptions &aStrokeOptions = StrokeOptions(),
+                      const DrawOptions &aOptions = DrawOptions()) override;
+  virtual void Fill(const Path *aPath,
+                    const Pattern &aPattern,
+                    const DrawOptions &aOptions = DrawOptions()) override;
+  virtual void FillGlyphs(ScaledFont *aFont,
+                          const GlyphBuffer &aBuffer,
+                          const Pattern &aPattern,
+                          const DrawOptions &aOptions = DrawOptions()) override;
+  virtual void Mask(const Pattern &aSource,
+                    const Pattern &aMask,
+                    const DrawOptions &aOptions = DrawOptions()) override;
+  virtual void PushClip(const Path *aPath) override;
+  virtual void PushClipRect(const Rect &aRect) override;
+  virtual void PopClip() override;
+  virtual void PushLayer(bool aOpaque, Float aOpacity,
+                         SourceSurface* aMask,
+                         const Matrix& aMaskTransform,
+                         const IntRect& aBounds = IntRect(),
+                         bool aCopyBackground = false) override;
+  virtual void PushLayerWithBlend(bool aOpaque, Float aOpacity,
+                         SourceSurface* aMask,
+                         const Matrix& aMaskTransform,
+                         const IntRect& aBounds = IntRect(),
+                         bool aCopyBackground = false,
+                         CompositionOp = CompositionOp::OP_OVER) override;
+  virtual void PopLayer() override;
+
+
+  virtual void SetTransform(const Matrix &aTransform) override;
+
+  virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA) override;
+
+  virtual already_AddRefed<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData,
+                                                                  const IntSize &aSize,
+                                                                  int32_t aStride,
+                                                                  SurfaceFormat aFormat) const override
+  {
+    return mDrawTarget->CreateSourceSurfaceFromData(aData, aSize, aStride, aFormat);
+  }
+  virtual already_AddRefed<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const override
+  {
+    return mDrawTarget->OptimizeSourceSurface(aSurface);
+  }
+
+  virtual already_AddRefed<SourceSurface>
+    CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const override
+  {
+    return mDrawTarget->CreateSourceSurfaceFromNativeSurface(aSurface);
+  }
+
+  virtual already_AddRefed<DrawTarget>
+    CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const override
+  {
+    return mDrawTarget->CreateSimilarDrawTarget(aSize, aFormat);
+  }
+
+  virtual already_AddRefed<PathBuilder> CreatePathBuilder(FillRule aFillRule = FillRule::FILL_WINDING) const override
+  {
+    return mDrawTarget->CreatePathBuilder(aFillRule);
+  }
+
+  virtual already_AddRefed<GradientStops>
+    CreateGradientStops(GradientStop *aStops,
+                        uint32_t aNumStops,
+                        ExtendMode aExtendMode = ExtendMode::CLAMP) const override
+  {
+    return mDrawTarget->CreateGradientStops(aStops, aNumStops, aExtendMode);
+  }
+  virtual already_AddRefed<FilterNode> CreateFilter(FilterType aType) override
+  {
+    return mDrawTarget->CreateFilter(aType);
+  }
+
+private:
+  RefPtr<DrawTarget> mDrawTarget;
+  IntPoint mOrigin;
+};
+
+} // namespace gfx
+} // namespace mozilla
+
+#endif
--- a/gfx/2d/Factory.cpp
+++ b/gfx/2d/Factory.cpp
@@ -47,16 +47,17 @@
 #include "HelpersD2D.h"
 #include "HelpersWinFonts.h"
 #include "mozilla/Mutex.h"
 #endif
 
 #include "DrawTargetCapture.h"
 #include "DrawTargetDual.h"
 #include "DrawTargetTiled.h"
+#include "DrawTargetOffset.h"
 #include "DrawTargetWrapAndRecord.h"
 #include "DrawTargetRecording.h"
 
 #include "SourceSurfaceRawData.h"
 
 #include "DrawEventRecorder.h"
 
 #include "Logging.h"
@@ -538,16 +539,29 @@ Factory::CreateTiledDrawTarget(const Til
 
   if (!dt->Init(aTileSet)) {
     return nullptr;
   }
 
   return dt.forget();
 }
 
+already_AddRefed<DrawTarget>
+Factory::CreateOffsetDrawTarget(DrawTarget *aDrawTarget, IntPoint aTileOrigin)
+{
+  RefPtr<DrawTargetOffset> dt = new DrawTargetOffset();
+
+  if (!dt->Init(aDrawTarget, aTileOrigin)) {
+    return nullptr;
+  }
+
+  return dt.forget();
+}
+
+
 bool
 Factory::DoesBackendSupportDataDrawtarget(BackendType aType)
 {
   switch (aType) {
   case BackendType::DIRECT2D:
   case BackendType::DIRECT2D1_1:
   case BackendType::RECORDING:
   case BackendType::NONE:
--- a/gfx/2d/moz.build
+++ b/gfx/2d/moz.build
@@ -19,16 +19,17 @@ EXPORTS.mozilla.gfx += [
     'BaseSize.h',
     'BezierUtils.h',
     'Blur.h',
     'BorrowedContext.h',
     'Coord.h',
     'CriticalSection.h',
     'DataSurfaceHelpers.h',
     'DrawEventRecorder.h',
+    'DrawTargetOffset.h',
     'DrawTargetRecording.h',
     'DrawTargetTiled.h',
     'DrawTargetWrapAndRecord.h',
     'Filters.h',
     'FontVariation.h',
     'Helpers.h',
     'HelpersCairo.h',
     'InlineTranslator.h',
@@ -166,16 +167,17 @@ UNIFIED_SOURCES += [
     'DataSourceSurface.cpp',
     'DataSurfaceHelpers.cpp',
     'DrawEventRecorder.cpp',
     'DrawingJob.cpp',
     'DrawTarget.cpp',
     'DrawTargetCairo.cpp',
     'DrawTargetCapture.cpp',
     'DrawTargetDual.cpp',
+    'DrawTargetOffset.cpp',
     'DrawTargetRecording.cpp',
     'DrawTargetTiled.cpp',
     'DrawTargetWrapAndRecord.cpp',
     'FilterNodeCapture.cpp',
     'FilterNodeSoftware.cpp',
     'FilterProcessing.cpp',
     'FilterProcessingScalar.cpp',
     'ImageScaling.cpp',