layout/generic/nsCanvasFrame.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /* rendering object that goes directly inside the document's scrollbars */
     8 #ifndef nsCanvasFrame_h___
     9 #define nsCanvasFrame_h___
    11 #include "mozilla/Attributes.h"
    12 #include "mozilla/EventForwards.h"
    13 #include "nsContainerFrame.h"
    14 #include "nsIScrollPositionListener.h"
    15 #include "nsDisplayList.h"
    17 class nsPresContext;
    18 class nsRenderingContext;
    20 /**
    21  * Root frame class.
    22  *
    23  * The root frame is the parent frame for the document element's frame.
    24  * It only supports having a single child frame which must be an area
    25  * frame
    26  */
    27 class nsCanvasFrame : public nsContainerFrame,
    28                       public nsIScrollPositionListener
    29 {
    30 public:
    31   nsCanvasFrame(nsStyleContext* aContext)
    32   : nsContainerFrame(aContext),
    33     mDoPaintFocus(false),
    34     mAddedScrollPositionListener(false) {}
    36   NS_DECL_QUERYFRAME_TARGET(nsCanvasFrame)
    37   NS_DECL_QUERYFRAME
    38   NS_DECL_FRAMEARENA_HELPERS
    41   virtual void DestroyFrom(nsIFrame* aDestructRoot) MOZ_OVERRIDE;
    43   virtual nsresult SetInitialChildList(ChildListID     aListID,
    44                                        nsFrameList&    aChildList) MOZ_OVERRIDE;
    45   virtual nsresult AppendFrames(ChildListID     aListID,
    46                                 nsFrameList&    aFrameList) MOZ_OVERRIDE;
    47   virtual nsresult InsertFrames(ChildListID     aListID,
    48                                 nsIFrame*       aPrevFrame,
    49                                 nsFrameList&    aFrameList) MOZ_OVERRIDE;
    50   virtual nsresult RemoveFrame(ChildListID     aListID,
    51                                nsIFrame*       aOldFrame) MOZ_OVERRIDE;
    53   virtual nscoord GetMinWidth(nsRenderingContext *aRenderingContext) MOZ_OVERRIDE;
    54   virtual nscoord GetPrefWidth(nsRenderingContext *aRenderingContext) MOZ_OVERRIDE;
    55   virtual nsresult Reflow(nsPresContext*           aPresContext,
    56                           nsHTMLReflowMetrics&     aDesiredSize,
    57                           const nsHTMLReflowState& aReflowState,
    58                           nsReflowStatus&          aStatus) MOZ_OVERRIDE;
    59   virtual bool IsFrameOfType(uint32_t aFlags) const MOZ_OVERRIDE
    60   {
    61     return nsContainerFrame::IsFrameOfType(aFlags &
    62              ~(nsIFrame::eCanContainOverflowContainers));
    63   }
    65   /** SetHasFocus tells the CanvasFrame to draw with focus ring
    66    *  @param aHasFocus true to show focus ring, false to hide it
    67    */
    68   NS_IMETHOD SetHasFocus(bool aHasFocus);
    70   virtual void BuildDisplayList(nsDisplayListBuilder*   aBuilder,
    71                                 const nsRect&           aDirtyRect,
    72                                 const nsDisplayListSet& aLists) MOZ_OVERRIDE;
    74   void PaintFocus(nsRenderingContext& aRenderingContext, nsPoint aPt);
    76   // nsIScrollPositionListener
    77   virtual void ScrollPositionWillChange(nscoord aX, nscoord aY) MOZ_OVERRIDE;
    78   virtual void ScrollPositionDidChange(nscoord aX, nscoord aY) MOZ_OVERRIDE {}
    80   /**
    81    * Get the "type" of the frame
    82    *
    83    * @see nsGkAtoms::canvasFrame
    84    */
    85   virtual nsIAtom* GetType() const MOZ_OVERRIDE;
    87   virtual nsresult StealFrame(nsIFrame* aChild, bool aForceNormal) MOZ_OVERRIDE
    88   {
    89     NS_ASSERTION(!aForceNormal, "No-one should be passing this in here");
    91     // nsCanvasFrame keeps overflow container continuations of its child
    92     // frame in main child list
    93     nsresult rv = nsContainerFrame::StealFrame(aChild, true);
    94     if (NS_FAILED(rv)) {
    95       rv = nsContainerFrame::StealFrame(aChild);
    96     }
    97     return rv;
    98   }
   100 #ifdef DEBUG_FRAME_DUMP
   101   virtual nsresult GetFrameName(nsAString& aResult) const MOZ_OVERRIDE;
   102 #endif
   103   virtual nsresult GetContentForEvent(mozilla::WidgetEvent* aEvent,
   104                                       nsIContent** aContent) MOZ_OVERRIDE;
   106   nsRect CanvasArea() const;
   108 protected:
   109   // Data members
   110   bool                      mDoPaintFocus;
   111   bool                      mAddedScrollPositionListener;
   112 };
   114 /**
   115  * Override nsDisplayBackground methods so that we pass aBGClipRect to
   116  * PaintBackground, covering the whole overflow area.
   117  * We can also paint an "extra background color" behind the normal
   118  * background.
   119  */
   120 class nsDisplayCanvasBackgroundColor : public nsDisplayItem {
   121 public:
   122   nsDisplayCanvasBackgroundColor(nsDisplayListBuilder* aBuilder, nsIFrame *aFrame)
   123     : nsDisplayItem(aBuilder, aFrame)
   124     , mColor(NS_RGBA(0,0,0,0))
   125   {
   126   }
   128   virtual bool ComputeVisibility(nsDisplayListBuilder* aBuilder,
   129                                  nsRegion* aVisibleRegion,
   130                                  const nsRect& aAllowVisibleRegionExpansion) MOZ_OVERRIDE
   131   {
   132     return NS_GET_A(mColor) > 0;
   133   }
   134   virtual nsRegion GetOpaqueRegion(nsDisplayListBuilder* aBuilder,
   135                                    bool* aSnap) MOZ_OVERRIDE
   136   {
   137     if (NS_GET_A(mColor) == 255) {
   138       return nsRegion(GetBounds(aBuilder, aSnap));
   139     }
   140     return nsRegion();
   141   }
   142   virtual bool IsUniform(nsDisplayListBuilder* aBuilder, nscolor* aColor) MOZ_OVERRIDE
   143   {
   144     *aColor = mColor;
   145     return true;
   146   }
   147   virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) MOZ_OVERRIDE
   148   {
   149     nsCanvasFrame* frame = static_cast<nsCanvasFrame*>(mFrame);
   150     *aSnap = true;
   151     return frame->CanvasArea() + ToReferenceFrame();
   152   }
   153   virtual void HitTest(nsDisplayListBuilder* aBuilder, const nsRect& aRect,
   154                        HitTestState* aState, nsTArray<nsIFrame*> *aOutFrames) MOZ_OVERRIDE
   155   {
   156     // We need to override so we don't consider border-radius.
   157     aOutFrames->AppendElement(mFrame);
   158   }
   160   virtual nsDisplayItemGeometry* AllocateGeometry(nsDisplayListBuilder* aBuilder) MOZ_OVERRIDE
   161   {
   162     return new nsDisplayItemBoundsGeometry(this, aBuilder);
   163   }
   165   virtual void ComputeInvalidationRegion(nsDisplayListBuilder* aBuilder,
   166                                          const nsDisplayItemGeometry* aGeometry,
   167                                          nsRegion* aInvalidRegion) MOZ_OVERRIDE
   168   {
   169     const nsDisplayItemBoundsGeometry* geometry = static_cast<const nsDisplayItemBoundsGeometry*>(aGeometry);
   170     ComputeInvalidationRegionDifference(aBuilder, geometry, aInvalidRegion);
   171   }
   173   virtual void Paint(nsDisplayListBuilder* aBuilder,
   174                      nsRenderingContext* aCtx) MOZ_OVERRIDE;
   176   void SetExtraBackgroundColor(nscolor aColor)
   177   {
   178     mColor = aColor;
   179   }
   181   NS_DISPLAY_DECL_NAME("CanvasBackgroundColor", TYPE_CANVAS_BACKGROUND_COLOR)
   183 private:
   184   nscolor mColor;
   185 };
   187 class nsDisplayCanvasBackgroundImage : public nsDisplayBackgroundImage {
   188 public:
   189   nsDisplayCanvasBackgroundImage(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
   190                                  uint32_t aLayer, const nsStyleBackground* aBg)
   191     : nsDisplayBackgroundImage(aBuilder, aFrame, aLayer, aBg)
   192   {}
   194   virtual void Paint(nsDisplayListBuilder* aBuilder, nsRenderingContext* aCtx) MOZ_OVERRIDE;
   196   virtual void NotifyRenderingChanged() MOZ_OVERRIDE
   197   {
   198     mFrame->Properties().Delete(nsIFrame::CachedBackgroundImage());
   199     mFrame->Properties().Delete(nsIFrame::CachedBackgroundImageDT());
   200   }
   202   virtual bool ShouldFixToViewport(nsDisplayListBuilder* aBuilder) MOZ_OVERRIDE
   203   {
   204     // Put background-attachment:fixed canvas background images in their own
   205     // compositing layer. Since we know their background painting area can't
   206     // change (unless the viewport size itself changes), async scrolling
   207     // will work well.
   208     return mBackgroundStyle->mLayers[mLayer].mAttachment == NS_STYLE_BG_ATTACHMENT_FIXED &&
   209            !mBackgroundStyle->mLayers[mLayer].mImage.IsEmpty();
   210   }
   212   // We still need to paint a background color as well as an image for this item, 
   213   // so we can't support this yet.
   214   virtual bool SupportsOptimizingToImage() MOZ_OVERRIDE { return false; }
   217   NS_DISPLAY_DECL_NAME("CanvasBackgroundImage", TYPE_CANVAS_BACKGROUND_IMAGE)
   218 };
   220 class nsDisplayCanvasThemedBackground : public nsDisplayThemedBackground {
   221 public:
   222   nsDisplayCanvasThemedBackground(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame)
   223     : nsDisplayThemedBackground(aBuilder, aFrame)
   224   {}
   226   virtual void Paint(nsDisplayListBuilder* aBuilder, nsRenderingContext* aCtx) MOZ_OVERRIDE;
   228   NS_DISPLAY_DECL_NAME("CanvasThemedBackground", TYPE_CANVAS_THEMED_BACKGROUND)
   229 };
   231 #endif /* nsCanvasFrame_h___ */

mercurial