gfx/layers/basic/BasicImageLayer.cpp

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

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "BasicLayersImpl.h" // for FillRectWithMask, etc
michael@0 7 #include "ImageContainer.h" // for AutoLockImage, etc
michael@0 8 #include "ImageLayers.h" // for ImageLayer
michael@0 9 #include "Layers.h" // for Layer (ptr only), etc
michael@0 10 #include "basic/BasicImplData.h" // for BasicImplData
michael@0 11 #include "basic/BasicLayers.h" // for BasicLayerManager
michael@0 12 #include "mozilla/mozalloc.h" // for operator new
michael@0 13 #include "nsAutoPtr.h" // for nsRefPtr, getter_AddRefs, etc
michael@0 14 #include "nsCOMPtr.h" // for already_AddRefed
michael@0 15 #include "nsDebug.h" // for NS_ASSERTION
michael@0 16 #include "nsISupportsImpl.h" // for gfxPattern::Release, etc
michael@0 17 #include "nsRect.h" // for nsIntRect
michael@0 18 #include "nsRegion.h" // for nsIntRegion
michael@0 19 #include "mozilla/gfx/Point.h" // for IntSize
michael@0 20
michael@0 21 using namespace mozilla::gfx;
michael@0 22
michael@0 23 namespace mozilla {
michael@0 24 namespace layers {
michael@0 25
michael@0 26 class BasicImageLayer : public ImageLayer, public BasicImplData {
michael@0 27 public:
michael@0 28 BasicImageLayer(BasicLayerManager* aLayerManager) :
michael@0 29 ImageLayer(aLayerManager,
michael@0 30 static_cast<BasicImplData*>(MOZ_THIS_IN_INITIALIZER_LIST())),
michael@0 31 mSize(-1, -1)
michael@0 32 {
michael@0 33 MOZ_COUNT_CTOR(BasicImageLayer);
michael@0 34 }
michael@0 35 virtual ~BasicImageLayer()
michael@0 36 {
michael@0 37 MOZ_COUNT_DTOR(BasicImageLayer);
michael@0 38 }
michael@0 39
michael@0 40 virtual void SetVisibleRegion(const nsIntRegion& aRegion)
michael@0 41 {
michael@0 42 NS_ASSERTION(BasicManager()->InConstruction(),
michael@0 43 "Can only set properties in construction phase");
michael@0 44 ImageLayer::SetVisibleRegion(aRegion);
michael@0 45 }
michael@0 46
michael@0 47 virtual void Paint(DrawTarget* aDT,
michael@0 48 const gfx::Point& aDeviceOffset,
michael@0 49 Layer* aMaskLayer) MOZ_OVERRIDE;
michael@0 50
michael@0 51 virtual TemporaryRef<SourceSurface> GetAsSourceSurface() MOZ_OVERRIDE;
michael@0 52
michael@0 53 protected:
michael@0 54 BasicLayerManager* BasicManager()
michael@0 55 {
michael@0 56 return static_cast<BasicLayerManager*>(mManager);
michael@0 57 }
michael@0 58
michael@0 59 // only paints the image if aContext is non-null
michael@0 60 void
michael@0 61 GetAndPaintCurrentImage(DrawTarget* aTarget,
michael@0 62 float aOpacity,
michael@0 63 SourceSurface* aMaskSurface);
michael@0 64
michael@0 65 gfx::IntSize mSize;
michael@0 66 };
michael@0 67
michael@0 68 void
michael@0 69 BasicImageLayer::Paint(DrawTarget* aDT,
michael@0 70 const gfx::Point& aDeviceOffset,
michael@0 71 Layer* aMaskLayer)
michael@0 72 {
michael@0 73 if (IsHidden() || !mContainer) {
michael@0 74 return;
michael@0 75 }
michael@0 76
michael@0 77 mContainer->SetImageFactory(mManager->IsCompositingCheap() ? nullptr : BasicManager()->GetImageFactory());
michael@0 78
michael@0 79 RefPtr<gfx::SourceSurface> surface;
michael@0 80 AutoLockImage autoLock(mContainer, &surface);
michael@0 81 Image *image = autoLock.GetImage();
michael@0 82 gfx::IntSize size = mSize = autoLock.GetSize();
michael@0 83
michael@0 84 if (!surface || !surface->IsValid()) {
michael@0 85 return;
michael@0 86 }
michael@0 87
michael@0 88 FillRectWithMask(aDT, aDeviceOffset, Rect(0, 0, size.width, size.height),
michael@0 89 surface, ToFilter(mFilter),
michael@0 90 DrawOptions(GetEffectiveOpacity(), GetEffectiveOperator(this)),
michael@0 91 aMaskLayer);
michael@0 92
michael@0 93 GetContainer()->NotifyPaintedImage(image);
michael@0 94 }
michael@0 95
michael@0 96 void
michael@0 97 BasicImageLayer::GetAndPaintCurrentImage(DrawTarget* aTarget,
michael@0 98 float aOpacity,
michael@0 99 SourceSurface* aMaskSurface)
michael@0 100 {
michael@0 101 if (!mContainer) {
michael@0 102 return;
michael@0 103 }
michael@0 104
michael@0 105 mContainer->SetImageFactory(mManager->IsCompositingCheap() ?
michael@0 106 nullptr :
michael@0 107 BasicManager()->GetImageFactory());
michael@0 108 IntSize size;
michael@0 109 Image* image = nullptr;
michael@0 110 RefPtr<SourceSurface> surf =
michael@0 111 mContainer->LockCurrentAsSourceSurface(&size, &image);
michael@0 112
michael@0 113 if (!surf) {
michael@0 114 return;
michael@0 115 }
michael@0 116
michael@0 117 if (aTarget) {
michael@0 118 // The visible region can extend outside the image, so just draw
michael@0 119 // within the image bounds.
michael@0 120 SurfacePattern pat(surf, ExtendMode::CLAMP, Matrix(), ToFilter(mFilter));
michael@0 121 CompositionOp op = GetEffectiveOperator(this);
michael@0 122 DrawOptions opts(aOpacity, op);
michael@0 123
michael@0 124 aTarget->MaskSurface(pat, aMaskSurface, Point(0, 0), opts);
michael@0 125
michael@0 126 GetContainer()->NotifyPaintedImage(image);
michael@0 127 }
michael@0 128
michael@0 129 mContainer->UnlockCurrentImage();
michael@0 130 }
michael@0 131
michael@0 132 TemporaryRef<SourceSurface>
michael@0 133 BasicImageLayer::GetAsSourceSurface()
michael@0 134 {
michael@0 135 if (!mContainer) {
michael@0 136 return nullptr;
michael@0 137 }
michael@0 138
michael@0 139 gfx::IntSize dontCare;
michael@0 140 return mContainer->GetCurrentAsSourceSurface(&dontCare);
michael@0 141 }
michael@0 142
michael@0 143 already_AddRefed<ImageLayer>
michael@0 144 BasicLayerManager::CreateImageLayer()
michael@0 145 {
michael@0 146 NS_ASSERTION(InConstruction(), "Only allowed in construction phase");
michael@0 147 nsRefPtr<ImageLayer> layer = new BasicImageLayer(this);
michael@0 148 return layer.forget();
michael@0 149 }
michael@0 150
michael@0 151 }
michael@0 152 }

mercurial