#ifndef DrawingBuffer_h
#define DrawingBuffer_h
#include "platform/PlatformExport.h"
#include "platform/geometry/IntSize.h"
#include "platform/graphics/GraphicsTypes3D.h"
#include "platform/graphics/gpu/WebGLImageConversion.h"
#include "public/platform/WebExternalTextureLayerClient.h"
#include "public/platform/WebExternalTextureMailbox.h"
#include "public/platform/WebGraphicsContext3D.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "wtf/Noncopyable.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
namespace blink {
class WebExternalBitmap;
class WebExternalTextureLayer;
class WebGraphicsContext3D;
class WebLayer;
}
namespace WebCore {
class ImageData;
class ImageBuffer;
class PLATFORM_EXPORT ContextEvictionManager : public RefCounted<ContextEvictionManager> {
public:
virtual ~ContextEvictionManager() {};
virtual void forciblyLoseOldestContext(const String& reason) = 0;
virtual IntSize oldestContextSize() = 0;
};
class PLATFORM_EXPORT DrawingBuffer : public RefCounted<DrawingBuffer>, public blink::WebExternalTextureLayerClient {
struct MailboxInfo : public RefCounted<MailboxInfo> {
blink::WebExternalTextureMailbox mailbox;
unsigned textureId;
IntSize size;
};
public:
enum PreserveDrawingBuffer {
Preserve,
Discard
};
static PassRefPtr<DrawingBuffer> create(blink::WebGraphicsContext3D*, const IntSize&, PreserveDrawingBuffer, PassRefPtr<ContextEvictionManager>);
virtual ~DrawingBuffer();
void releaseResources();
void clearFramebuffers(GLbitfield clearMask);
IntSize adjustSize(const IntSize&);
void reset(const IntSize&);
void bind();
IntSize size() const { return m_size; }
bool isZeroSized() const { return m_size.isEmpty(); }
void commit(long x = 0, long y = 0, long width = -1, long height = -1);
void setScissorEnabled(bool scissorEnabled) { m_scissorEnabled = scissorEnabled; }
void setTexture2DBinding(Platform3DObject texture) { m_texture2DBinding = texture; }
void setFramebufferBinding(Platform3DObject fbo) { m_framebufferBinding = fbo; }
void setActiveTextureUnit(GLint textureUnit) { m_activeTextureUnit = textureUnit; }
bool multisample() const;
Platform3DObject framebuffer() const;
void markContentsChanged();
void markLayerComposited();
bool layerComposited() const;
blink::WebLayer* platformLayer();
void paintCompositedResultsToCanvas(ImageBuffer*);
virtual blink::WebGraphicsContext3D* context() OVERRIDE;
virtual bool prepareMailbox(blink::WebExternalTextureMailbox*, blink::WebExternalBitmap*) OVERRIDE;
virtual void mailboxReleased(const blink::WebExternalTextureMailbox&) OVERRIDE;
bool copyToPlatformTexture(blink::WebGraphicsContext3D*, Platform3DObject texture, GLenum internalFormat,
GLenum destType, GLint level, bool premultiplyAlpha, bool flipY);
void setPackAlignment(GLint param);
void paintRenderingResultsToCanvas(ImageBuffer*);
PassRefPtr<Uint8ClampedArray> paintRenderingResultsToImageData(int&, int&);
private:
DrawingBuffer(blink::WebGraphicsContext3D*, const IntSize&, bool multisampleExtensionSupported,
bool packedDepthStencilExtensionSupported, PreserveDrawingBuffer, PassRefPtr<ContextEvictionManager>);
void initialize(const IntSize&);
unsigned createColorTexture(const IntSize& size = IntSize());
void createSecondaryBuffers();
bool resizeFramebuffer(const IntSize&);
bool resizeMultisampleFramebuffer(const IntSize&);
void resizeDepthStencil(const IntSize&);
void restoreFramebufferBinding();
void clearPlatformLayer();
PassRefPtr<MailboxInfo> recycledMailbox();
PassRefPtr<MailboxInfo> createNewMailbox(unsigned);
void setSize(const IntSize& size);
int pixelDelta(const IntSize& size);
IntSize adjustSizeWithContextEviction(const IntSize&, bool& evictContext);
void paintFramebufferToCanvas(int framebuffer, int width, int height, bool premultiplyAlpha, ImageBuffer*);
enum ReadbackOrder {
ReadbackRGBA,
ReadbackSkia
};
void readBackFramebuffer(unsigned char* pixels, int width, int height, ReadbackOrder, WebGLImageConversion::AlphaOp);
void flipVertically(uint8_t* data, int width, int height);
void texImage2DResourceSafe(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLint alignment = 4);
PreserveDrawingBuffer m_preserveDrawingBuffer;
bool m_scissorEnabled;
Platform3DObject m_texture2DBinding;
Platform3DObject m_framebufferBinding;
GLenum m_activeTextureUnit;
blink::WebGraphicsContext3D* m_context;
IntSize m_size;
bool m_multisampleExtensionSupported;
bool m_packedDepthStencilExtensionSupported;
Platform3DObject m_fbo;
Platform3DObject m_colorBuffer;
Platform3DObject m_frontColorBuffer;
Platform3DObject m_depthStencilBuffer;
Platform3DObject m_depthBuffer;
Platform3DObject m_stencilBuffer;
Platform3DObject m_multisampleFBO;
Platform3DObject m_multisampleColorBuffer;
bool m_contentsChanged;
bool m_contentsChangeCommitted;
bool m_layerComposited;
enum MultisampleMode {
None,
ImplicitResolve,
ExplicitResolve,
};
MultisampleMode m_multisampleMode;
blink::WebGraphicsContext3D::Attributes m_attributes;
unsigned m_internalColorFormat;
unsigned m_colorFormat;
unsigned m_internalRenderbufferFormat;
int m_maxTextureSize;
int m_sampleCount;
int m_packAlignment;
OwnPtr<blink::WebExternalTextureLayer> m_layer;
Vector<RefPtr<MailboxInfo> > m_textureMailboxes;
Vector<RefPtr<MailboxInfo> > m_recycledMailboxes;
RefPtr<ContextEvictionManager> m_contextEvictionManager;
SkBitmap m_resizingBitmap;
Vector<uint8_t> m_scanline;
};
}
#endif