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 "core/platform/graphics/GraphicsContext3D.h" 32 #include "core/platform/graphics/IntSize.h" 33 #include "public/platform/WebExternalTextureLayer.h" 34 #include "public/platform/WebExternalTextureLayerClient.h" 35 #include "public/platform/WebExternalTextureMailbox.h" 36 #include "wtf/DoublyLinkedList.h" 37 #include "wtf/PassOwnPtr.h" 38 #include "wtf/RefPtr.h" 39 40 namespace WebKit { 41 class WebGraphicsContext3D; 42 } 43 44 namespace WebCore { 45 46 class Canvas2DLayerBridge : public WebKit::WebExternalTextureLayerClient, public SkDeferredCanvas::NotificationClient, public DoublyLinkedListNode<Canvas2DLayerBridge> { 47 WTF_MAKE_NONCOPYABLE(Canvas2DLayerBridge); 48 public: 49 enum OpacityMode { 50 Opaque, 51 NonOpaque 52 }; 53 54 static PassOwnPtr<Canvas2DLayerBridge> create(PassRefPtr<GraphicsContext3D>, const IntSize&, OpacityMode); 55 56 virtual ~Canvas2DLayerBridge(); 57 58 // WebKit::WebExternalTextureLayerClient implementation. 59 virtual WebKit::WebGraphicsContext3D* context() OVERRIDE; 60 virtual bool prepareMailbox(WebKit::WebExternalTextureMailbox*, WebKit::WebExternalBitmap*) OVERRIDE; 61 virtual void mailboxReleased(const WebKit::WebExternalTextureMailbox&) OVERRIDE; 62 63 // SkDeferredCanvas::NotificationClient implementation 64 virtual void prepareForDraw() OVERRIDE; 65 virtual void storageAllocatedForRecordingChanged(size_t) OVERRIDE; 66 virtual void flushedDrawCommands() OVERRIDE; 67 virtual void skippedPendingDrawCommands() OVERRIDE; 68 69 // Methods used by Canvas2DLayerManager 70 virtual size_t freeMemoryIfPossible(size_t); // virtual for mocking 71 virtual void flush(); // virtual for mocking 72 virtual size_t storageAllocatedForRecording(); // virtual for faking 73 size_t bytesAllocated() const {return m_bytesAllocated;} 74 void limitPendingFrames(); 75 76 WebKit::WebLayer* layer(); 77 void contextAcquired(); 78 SkCanvas* getCanvas() { return m_canvas; } 79 80 unsigned backBufferTexture(); 81 82 bool isValid(); 83 84 protected: 85 Canvas2DLayerBridge(PassRefPtr<GraphicsContext3D>, SkDeferredCanvas*, OpacityMode); 86 void setRateLimitingEnabled(bool); 87 88 SkDeferredCanvas* m_canvas; 89 OwnPtr<WebKit::WebExternalTextureLayer> m_layer; 90 RefPtr<GraphicsContext3D> m_context; 91 size_t m_bytesAllocated; 92 bool m_didRecordDrawCommand; 93 bool m_surfaceIsValid; 94 int m_framesPending; 95 bool m_rateLimitingEnabled; 96 97 friend class WTF::DoublyLinkedListNode<Canvas2DLayerBridge>; 98 Canvas2DLayerBridge* m_next; 99 Canvas2DLayerBridge* m_prev; 100 101 enum MailboxStatus { 102 MailboxInUse, 103 MailboxReleased, 104 MailboxAvailable, 105 }; 106 107 struct MailboxInfo { 108 WebKit::WebExternalTextureMailbox m_mailbox; 109 SkAutoTUnref<SkImage> m_image; 110 MailboxStatus m_status; 111 112 MailboxInfo(const MailboxInfo&); 113 MailboxInfo() {} 114 }; 115 MailboxInfo* createMailboxInfo(); 116 117 uint32_t m_lastImageId; 118 Vector<MailboxInfo> m_mailboxes; 119 }; 120 121 } 122 123 #endif 124