2010-07-21 22:06:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2010-03-04 00:37:04 +03:00
|
|
|
|
|
|
|
#ifndef THEBESLAYERBUFFER_H_
|
|
|
|
#define THEBESLAYERBUFFER_H_
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "nsRegion.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
class ThebesLayer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class encapsulates the buffer used to retain ThebesLayer contents,
|
|
|
|
* i.e., the contents of the layer's GetVisibleRegion().
|
|
|
|
*
|
|
|
|
* This is a cairo/Thebes surface, but with a literal twist. Scrolling
|
|
|
|
* causes the layer's visible region to move. We want to keep
|
|
|
|
* reusing the same surface if the region size hasn't changed, but we don't
|
|
|
|
* want to keep moving the contents of the surface around in memory. So
|
|
|
|
* we use a trick.
|
|
|
|
* Consider just the vertical case, and suppose the buffer is H pixels
|
|
|
|
* high and we're scrolling down by N pixels. Instead of copying the
|
|
|
|
* buffer contents up by N pixels, we leave the buffer contents in place,
|
|
|
|
* and paint content rows H to H+N-1 into rows 0 to N-1 of the buffer.
|
|
|
|
* Then we can refresh the screen by painting rows N to H-1 of the buffer
|
|
|
|
* at row 0 on the screen, and then painting rows 0 to N-1 of the buffer
|
|
|
|
* at row H-N on the screen.
|
|
|
|
* mBufferRotation.y would be N in this example.
|
|
|
|
*/
|
|
|
|
class ThebesLayerBuffer {
|
|
|
|
public:
|
2010-07-21 22:06:33 +04:00
|
|
|
typedef gfxASurface::gfxContentType ContentType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controls the size of the backing buffer of this.
|
|
|
|
* - SizedToVisibleBounds: the backing buffer is exactly the same
|
|
|
|
* size as the bounds of ThebesLayer's visible region
|
|
|
|
* - ContainsVisibleBounds: the backing buffer is large enough to
|
|
|
|
* fit visible bounds. May be larger.
|
|
|
|
*/
|
|
|
|
enum BufferSizePolicy {
|
|
|
|
SizedToVisibleBounds,
|
|
|
|
ContainsVisibleBounds
|
|
|
|
};
|
|
|
|
|
|
|
|
ThebesLayerBuffer(BufferSizePolicy aBufferSizePolicy)
|
2011-06-22 16:11:28 +04:00
|
|
|
: mBufferRotation(0,0)
|
2010-07-21 22:06:33 +04:00
|
|
|
, mBufferSizePolicy(aBufferSizePolicy)
|
2010-03-04 00:37:04 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ThebesLayerBuffer);
|
|
|
|
}
|
2010-07-21 22:06:33 +04:00
|
|
|
virtual ~ThebesLayerBuffer()
|
2010-03-04 00:37:04 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ThebesLayerBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wipe out all retained contents. Call this when the entire
|
|
|
|
* buffer becomes invalid.
|
|
|
|
*/
|
|
|
|
void Clear()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mBuffer = nullptr;
|
2011-04-19 07:07:23 +04:00
|
|
|
mBufferRect.SetEmpty();
|
2010-03-04 00:37:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is returned by BeginPaint. The caller should draw into mContext.
|
|
|
|
* mRegionToDraw must be drawn. mRegionToInvalidate has been invalidated
|
|
|
|
* by ThebesLayerBuffer and must be redrawn on the screen.
|
|
|
|
* mRegionToInvalidate is set when the buffer has changed from
|
|
|
|
* opaque to transparent or vice versa, since the details of rendering can
|
2011-03-12 08:22:39 +03:00
|
|
|
* depend on the buffer type. mDidSelfCopy is true if we kept our buffer
|
|
|
|
* but used MovePixels() to shift its content.
|
2010-03-04 00:37:04 +03:00
|
|
|
*/
|
|
|
|
struct PaintState {
|
2011-08-18 01:52:48 +04:00
|
|
|
PaintState()
|
2011-10-17 18:59:28 +04:00
|
|
|
: mDidSelfCopy(false)
|
2011-08-18 01:52:48 +04:00
|
|
|
{}
|
|
|
|
|
2010-03-04 00:37:04 +03:00
|
|
|
nsRefPtr<gfxContext> mContext;
|
|
|
|
nsIntRegion mRegionToDraw;
|
|
|
|
nsIntRegion mRegionToInvalidate;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mDidSelfCopy;
|
2010-03-04 00:37:04 +03:00
|
|
|
};
|
2010-07-21 22:06:33 +04:00
|
|
|
|
2011-03-01 23:30:16 +03:00
|
|
|
enum {
|
2012-03-19 04:01:47 +04:00
|
|
|
PAINT_WILL_RESAMPLE = 0x01,
|
|
|
|
PAINT_NO_ROTATION = 0x02
|
2011-03-01 23:30:16 +03:00
|
|
|
};
|
2010-03-04 00:37:04 +03:00
|
|
|
/**
|
2010-07-16 01:08:10 +04:00
|
|
|
* Start a drawing operation. This returns a PaintState describing what
|
2010-03-04 00:37:04 +03:00
|
|
|
* needs to be drawn to bring the buffer up to date in the visible region.
|
|
|
|
* This queries aLayer to get the currently valid and visible regions.
|
|
|
|
* The returned mContext may be null if mRegionToDraw is empty.
|
|
|
|
* Otherwise it must not be null.
|
|
|
|
* mRegionToInvalidate will contain mRegionToDraw.
|
2011-03-01 23:30:16 +03:00
|
|
|
* @param aFlags when PAINT_WILL_RESAMPLE is passed, this indicates that
|
|
|
|
* buffer will be resampled when rendering (i.e the effective transform
|
|
|
|
* combined with the scale for the resolution is not just an integer
|
|
|
|
* translation). This will disable buffer rotation (since we don't want
|
|
|
|
* to resample across the rotation boundary) and will ensure that we
|
|
|
|
* make the entire buffer contents valid (since we don't want to sample
|
|
|
|
* invalid pixels outside the visible region, if the visible region doesn't
|
|
|
|
* fill the buffer bounds).
|
2010-03-04 00:37:04 +03:00
|
|
|
*/
|
2010-09-04 00:10:46 +04:00
|
|
|
PaintState BeginPaint(ThebesLayer* aLayer, ContentType aContentType,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags);
|
2010-07-21 22:06:33 +04:00
|
|
|
|
2011-04-01 01:33:46 +04:00
|
|
|
enum {
|
|
|
|
ALLOW_REPEAT = 0x01
|
|
|
|
};
|
2010-03-04 00:37:04 +03:00
|
|
|
/**
|
2010-07-21 22:06:33 +04:00
|
|
|
* Return a new surface of |aSize| and |aType|.
|
2011-04-01 01:33:46 +04:00
|
|
|
* @param aFlags if ALLOW_REPEAT is set, then the buffer should be configured
|
|
|
|
* to allow repeat-mode, otherwise it should be in pad (clamp) mode
|
2010-03-04 00:37:04 +03:00
|
|
|
*/
|
2010-07-21 22:06:33 +04:00
|
|
|
virtual already_AddRefed<gfxASurface>
|
2012-08-22 19:56:38 +04:00
|
|
|
CreateBuffer(ContentType aType, const nsIntSize& aSize, uint32_t aFlags) = 0;
|
2010-03-04 00:37:04 +03:00
|
|
|
|
2010-07-16 01:08:10 +04:00
|
|
|
/**
|
|
|
|
* Get the underlying buffer, if any. This is useful because we can pass
|
|
|
|
* in the buffer as the default "reference surface" if there is one.
|
|
|
|
* Don't use it for anything else!
|
|
|
|
*/
|
|
|
|
gfxASurface* GetBuffer() { return mBuffer; }
|
|
|
|
|
2010-03-04 00:37:04 +03:00
|
|
|
protected:
|
|
|
|
enum XSide {
|
|
|
|
LEFT, RIGHT
|
|
|
|
};
|
|
|
|
enum YSide {
|
|
|
|
TOP, BOTTOM
|
|
|
|
};
|
|
|
|
nsIntRect GetQuadrantRectangle(XSide aXSide, YSide aYSide);
|
2012-02-08 02:27:21 +04:00
|
|
|
/*
|
|
|
|
* If aMask is non-null, then it is used as an alpha mask for rendering this
|
|
|
|
* buffer. aMaskTransform must be non-null if aMask is non-null, and is used
|
|
|
|
* to adjust the coordinate space of the mask.
|
|
|
|
*/
|
2010-09-04 00:10:46 +04:00
|
|
|
void DrawBufferQuadrant(gfxContext* aTarget, XSide aXSide, YSide aYSide,
|
2012-02-08 02:27:21 +04:00
|
|
|
float aOpacity,
|
|
|
|
gfxASurface* aMask,
|
|
|
|
const gfxMatrix* aMaskTransform);
|
|
|
|
void DrawBufferWithRotation(gfxContext* aTarget, float aOpacity,
|
2012-07-30 18:20:58 +04:00
|
|
|
gfxASurface* aMask = nullptr,
|
|
|
|
const gfxMatrix* aMaskTransform = nullptr);
|
2010-03-04 00:37:04 +03:00
|
|
|
|
2010-09-14 09:23:08 +04:00
|
|
|
/**
|
|
|
|
* |BufferRect()| is the rect of device pixels that this
|
|
|
|
* ThebesLayerBuffer covers. That is what DrawBufferWithRotation()
|
|
|
|
* will paint when it's called.
|
|
|
|
*/
|
2010-07-21 22:06:33 +04:00
|
|
|
const nsIntRect& BufferRect() const { return mBufferRect; }
|
|
|
|
const nsIntPoint& BufferRotation() const { return mBufferRotation; }
|
|
|
|
|
2010-07-22 01:17:33 +04:00
|
|
|
already_AddRefed<gfxASurface>
|
2011-06-22 16:11:28 +04:00
|
|
|
SetBuffer(gfxASurface* aBuffer,
|
2010-07-22 01:17:33 +04:00
|
|
|
const nsIntRect& aBufferRect, const nsIntPoint& aBufferRotation)
|
|
|
|
{
|
2010-09-14 09:23:08 +04:00
|
|
|
nsRefPtr<gfxASurface> tmp = mBuffer.forget();
|
2010-07-22 01:17:33 +04:00
|
|
|
mBuffer = aBuffer;
|
|
|
|
mBufferRect = aBufferRect;
|
|
|
|
mBufferRotation = aBufferRotation;
|
2010-09-14 09:23:08 +04:00
|
|
|
return tmp.forget();
|
2010-07-22 01:17:33 +04:00
|
|
|
}
|
|
|
|
|
2012-07-12 16:51:58 +04:00
|
|
|
/**
|
|
|
|
* Set the buffer only. This is intended to be used with the
|
|
|
|
* shadow-layer Open/CloseDescriptor interface, to ensure we don't
|
|
|
|
* accidentally touch a buffer when it's not mapped.
|
|
|
|
*/
|
|
|
|
void SetBuffer(gfxASurface* aBuffer)
|
|
|
|
{
|
|
|
|
mBuffer = aBuffer;
|
|
|
|
}
|
|
|
|
|
2010-11-05 10:17:07 +03:00
|
|
|
/**
|
|
|
|
* Get a context at the specified resolution for updating |aBounds|,
|
|
|
|
* which must be contained within a single quadrant.
|
|
|
|
*/
|
|
|
|
already_AddRefed<gfxContext>
|
2011-06-22 16:11:28 +04:00
|
|
|
GetContextForQuadrantUpdate(const nsIntRect& aBounds);
|
2010-11-05 10:17:07 +03:00
|
|
|
|
2010-03-04 00:37:04 +03:00
|
|
|
private:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool BufferSizeOkFor(const nsIntSize& aSize)
|
2010-07-21 22:06:33 +04:00
|
|
|
{
|
2011-06-22 16:11:28 +04:00
|
|
|
return (aSize == mBufferRect.Size() ||
|
2010-07-21 22:06:33 +04:00
|
|
|
(SizedToVisibleBounds != mBufferSizePolicy &&
|
2011-06-22 16:11:28 +04:00
|
|
|
aSize < mBufferRect.Size()));
|
2010-07-21 22:06:33 +04:00
|
|
|
}
|
|
|
|
|
2010-03-04 00:37:04 +03:00
|
|
|
nsRefPtr<gfxASurface> mBuffer;
|
|
|
|
/** The area of the ThebesLayer that is covered by the buffer as a whole */
|
|
|
|
nsIntRect mBufferRect;
|
|
|
|
/**
|
|
|
|
* The x and y rotation of the buffer. Conceptually the buffer
|
|
|
|
* has its origin translated to mBufferRect.TopLeft() - mBufferRotation,
|
|
|
|
* is tiled to fill the plane, and the result is clipped to mBufferRect.
|
|
|
|
* So the pixel at mBufferRotation within the buffer is what gets painted at
|
|
|
|
* mBufferRect.TopLeft().
|
|
|
|
* This is "rotation" in the sense of rotating items in a linear buffer,
|
|
|
|
* where items falling off the end of the buffer are returned to the
|
|
|
|
* buffer at the other end, not 2D rotation!
|
|
|
|
*/
|
|
|
|
nsIntPoint mBufferRotation;
|
2010-07-21 22:06:33 +04:00
|
|
|
BufferSizePolicy mBufferSizePolicy;
|
2010-03-04 00:37:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* THEBESLAYERBUFFER_H_ */
|