Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / Canvas2DLayerBridge.h
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef Canvas2DLayerBridge_h
27 #define Canvas2DLayerBridge_h
28
29 #include "SkDeferredCanvas.h"
30 #include "SkImage.h"
31 #include "platform/PlatformExport.h"
32 #include "platform/geometry/IntSize.h"
33 #include "platform/graphics/ImageBufferSurface.h"
34 #include "public/platform/WebExternalTextureLayer.h"
35 #include "public/platform/WebExternalTextureLayerClient.h"
36 #include "public/platform/WebExternalTextureMailbox.h"
37 #include "third_party/khronos/GLES2/gl2.h"
38 #include "wtf/DoublyLinkedList.h"
39 #include "wtf/PassOwnPtr.h"
40 #include "wtf/RefCounted.h"
41 #include "wtf/RefPtr.h"
42
43 namespace blink {
44 class WebGraphicsContext3D;
45 class WebGraphicsContext3DProvider;
46 }
47
48 class Canvas2DLayerBridgeTest;
49
50 namespace WebCore {
51
52 class ImageBuffer;
53
54 class PLATFORM_EXPORT Canvas2DLayerBridge : public blink::WebExternalTextureLayerClient, public SkDeferredCanvas::NotificationClient, public DoublyLinkedListNode<Canvas2DLayerBridge>, public RefCounted<Canvas2DLayerBridge> {
55     WTF_MAKE_NONCOPYABLE(Canvas2DLayerBridge);
56 public:
57     static PassRefPtr<Canvas2DLayerBridge> create(const IntSize&, OpacityMode, int msaaSampleCount);
58
59     virtual ~Canvas2DLayerBridge();
60
61     // blink::WebExternalTextureLayerClient implementation.
62     virtual bool prepareMailbox(blink::WebExternalTextureMailbox*, blink::WebExternalBitmap*) OVERRIDE;
63     virtual void mailboxReleased(const blink::WebExternalTextureMailbox&) OVERRIDE;
64
65     // SkDeferredCanvas::NotificationClient implementation
66     virtual void prepareForDraw() OVERRIDE;
67     virtual void storageAllocatedForRecordingChanged(size_t) OVERRIDE;
68     virtual void flushedDrawCommands() OVERRIDE;
69     virtual void skippedPendingDrawCommands() OVERRIDE;
70
71     // ImageBufferSurface implementation
72     void willUse();
73     SkCanvas* canvas() const { return m_canvas.get(); }
74     bool checkSurfaceValid();
75     bool restoreSurface();
76     blink::WebLayer* layer() const;
77     Platform3DObject getBackingTexture();
78     bool isAccelerated() const { return true; }
79     void setIsHidden(bool);
80     void setImageBuffer(ImageBuffer* imageBuffer) { m_imageBuffer = imageBuffer; }
81
82     // Methods used by Canvas2DLayerManager
83     virtual size_t freeMemoryIfPossible(size_t); // virtual for mocking
84     virtual void flush(); // virtual for mocking
85     virtual size_t storageAllocatedForRecording(); // virtual for faking
86     size_t bytesAllocated() const { return m_bytesAllocated; }
87     void limitPendingFrames();
88     void freeReleasedMailbox();
89     bool hasReleasedMailbox() const;
90     void freeTransientResources();
91     bool hasTransientResources() const;
92     bool isHidden() { return m_isHidden; }
93
94     void beginDestruction();
95
96 protected:
97     Canvas2DLayerBridge(PassOwnPtr<blink::WebGraphicsContext3DProvider>, PassOwnPtr<SkDeferredCanvas>, int, OpacityMode);
98     void setRateLimitingEnabled(bool);
99     bool releasedMailboxHasExpired();
100     blink::WebGraphicsContext3D* context();
101
102     OwnPtr<SkDeferredCanvas> m_canvas;
103     OwnPtr<blink::WebExternalTextureLayer> m_layer;
104     OwnPtr<blink::WebGraphicsContext3DProvider> m_contextProvider;
105     ImageBuffer* m_imageBuffer;
106     int m_msaaSampleCount;
107     size_t m_bytesAllocated;
108     bool m_didRecordDrawCommand;
109     bool m_isSurfaceValid;
110     int m_framesPending;
111     int m_framesSinceMailboxRelease;
112     bool m_destructionInProgress;
113     bool m_rateLimitingEnabled;
114     bool m_isHidden;
115
116     friend class WTF::DoublyLinkedListNode<Canvas2DLayerBridge>;
117     friend class ::Canvas2DLayerBridgeTest;
118     Canvas2DLayerBridge* m_next;
119     Canvas2DLayerBridge* m_prev;
120
121     enum MailboxStatus {
122         MailboxInUse,
123         MailboxReleased,
124         MailboxAvailable,
125     };
126
127     struct MailboxInfo {
128         blink::WebExternalTextureMailbox m_mailbox;
129         RefPtr<SkImage> m_image;
130         MailboxStatus m_status;
131         RefPtr<Canvas2DLayerBridge> m_parentLayerBridge;
132
133         MailboxInfo(const MailboxInfo&);
134         MailboxInfo() {}
135     };
136     MailboxInfo* createMailboxInfo();
137     MailboxInfo* releasedMailboxInfo();
138
139     uint32_t m_lastImageId;
140     Vector<MailboxInfo> m_mailboxes;
141     int m_releasedMailboxInfoIndex;
142 };
143 }
144 #endif