OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "platform/graphics/OffscreenCanvasPlaceholder.h" | |
6 | |
7 #include "platform/CrossThreadFunctional.h" | |
8 #include "platform/WebTaskRunner.h" | |
9 #include "platform/graphics/Image.h" | |
10 #include "platform/graphics/OffscreenCanvasFrameDispatcher.h" | |
11 #include "wtf/HashMap.h" | |
12 | |
13 namespace { | |
14 | |
15 typedef HashMap<int, blink::OffscreenCanvasPlaceholder*> PlaceholderIdMap; | |
16 | |
17 PlaceholderIdMap& placeholderRegistry() { | |
18 DCHECK(isMainThread()); | |
19 DEFINE_STATIC_LOCAL(PlaceholderIdMap, s_placeholderRegistry, ()); | |
20 return s_placeholderRegistry; | |
21 } | |
22 | |
23 void releaseFrameToDispatcher( | |
xlai (Olivia)
2016/11/11 16:28:33
Is there any reason why you put this function in a
Justin Novosad
2016/11/11 22:09:00
I can't put it in OffscreenCanvasFrameDispatcher b
| |
24 WeakPtr<blink::OffscreenCanvasFrameDispatcher> dispatcher, | |
25 RefPtr<blink::Image> oldImage, | |
26 unsigned resourceId) { | |
27 oldImage = nullptr; // Needed to unref'ed on the right thread | |
28 if (dispatcher) { | |
29 dispatcher->reclaimResource(resourceId); | |
30 } | |
31 } | |
32 | |
33 } // unnamed namespace | |
34 | |
35 namespace blink { | |
36 | |
37 OffscreenCanvasPlaceholder::~OffscreenCanvasPlaceholder() { | |
38 unregisterPlaceholder(); | |
39 } | |
40 | |
41 OffscreenCanvasPlaceholder* OffscreenCanvasPlaceholder::getPlaceholderById( | |
42 unsigned placeholderId) { | |
43 PlaceholderIdMap::iterator it = placeholderRegistry().find(placeholderId); | |
44 if (it == placeholderRegistry().end()) | |
45 return nullptr; | |
46 return it->value; | |
47 } | |
48 | |
49 void OffscreenCanvasPlaceholder::registerPlaceholder(unsigned placeholderId) { | |
50 DCHECK(!placeholderRegistry().contains(placeholderId)); | |
51 DCHECK(!isPlaceholderRegistered()); | |
52 placeholderRegistry().add(placeholderId, this); | |
53 m_placeholderId = placeholderId; | |
54 } | |
55 | |
56 void OffscreenCanvasPlaceholder::unregisterPlaceholder() { | |
57 if (!isPlaceholderRegistered()) | |
58 return; | |
59 DCHECK(placeholderRegistry().find(m_placeholderId)->value == this); | |
60 placeholderRegistry().remove(m_placeholderId); | |
61 m_placeholderId = kNoPlaceholderId; | |
62 } | |
63 | |
64 void OffscreenCanvasPlaceholder::setPlaceholderFrame( | |
65 RefPtr<Image> newFrame, | |
66 WeakPtr<OffscreenCanvasFrameDispatcher> dispatcher, | |
67 std::unique_ptr<WebTaskRunner> taskRunner, | |
68 unsigned resourceId) { | |
69 DCHECK(isPlaceholderRegistered()); | |
70 releasePlaceholderFrame(); | |
71 m_placeholderFrame = std::move(newFrame); | |
72 m_frameDispatcher = std::move(dispatcher); | |
73 m_frameDispatcherTaskRunner = std::move(taskRunner); | |
74 m_placeholderFrameResourceId = resourceId; | |
75 } | |
76 | |
77 void OffscreenCanvasPlaceholder::releasePlaceholderFrame() { | |
78 DCHECK(isPlaceholderRegistered()); | |
79 if (m_placeholderFrame) { | |
80 m_frameDispatcherTaskRunner->postTask( | |
81 BLINK_FROM_HERE, | |
82 crossThreadBind(releaseFrameToDispatcher, std::move(m_frameDispatcher), | |
83 std::move(m_placeholderFrame), | |
84 m_placeholderFrameResourceId)); | |
85 } | |
86 } | |
87 | |
88 } // blink | |
OLD | NEW |