1 | /* |
2 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
3 | * Copyright (C) 2015 Igalia S.L. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
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 INC. AND ITS CONTRIBUTORS ``AS IS'' |
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
24 | * THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #include "config.h" |
28 | #include "BitmapTexturePool.h" |
29 | |
30 | #if USE(TEXTURE_MAPPER_GL) |
31 | #include "BitmapTextureGL.h" |
32 | #endif |
33 | |
34 | namespace WebCore { |
35 | |
36 | static const Seconds releaseUnusedSecondsTolerance { 3_s }; |
37 | static const Seconds releaseUnusedTexturesTimerInterval { 500_ms }; |
38 | |
39 | #if USE(TEXTURE_MAPPER_GL) |
40 | BitmapTexturePool::BitmapTexturePool(const TextureMapperContextAttributes& contextAttributes) |
41 | : m_contextAttributes(contextAttributes) |
42 | , m_releaseUnusedTexturesTimer(RunLoop::current(), this, &BitmapTexturePool::releaseUnusedTexturesTimerFired) |
43 | { |
44 | } |
45 | #endif |
46 | |
47 | RefPtr<BitmapTexture> BitmapTexturePool::acquireTexture(const IntSize& size, const BitmapTexture::Flags flags) |
48 | { |
49 | Entry* selectedEntry = std::find_if(m_textures.begin(), m_textures.end(), |
50 | [&size](Entry& entry) { return entry.m_texture->refCount() == 1 && entry.m_texture->size() == size; }); |
51 | |
52 | if (selectedEntry == m_textures.end()) { |
53 | m_textures.append(Entry(createTexture(flags))); |
54 | selectedEntry = &m_textures.last(); |
55 | } |
56 | |
57 | scheduleReleaseUnusedTextures(); |
58 | selectedEntry->markIsInUse(); |
59 | return selectedEntry->m_texture.copyRef(); |
60 | } |
61 | |
62 | void BitmapTexturePool::scheduleReleaseUnusedTextures() |
63 | { |
64 | if (m_releaseUnusedTexturesTimer.isActive()) |
65 | return; |
66 | |
67 | m_releaseUnusedTexturesTimer.startOneShot(releaseUnusedTexturesTimerInterval); |
68 | } |
69 | |
70 | void BitmapTexturePool::releaseUnusedTexturesTimerFired() |
71 | { |
72 | if (m_textures.isEmpty()) |
73 | return; |
74 | |
75 | // Delete entries, which have been unused in releaseUnusedSecondsTolerance. |
76 | MonotonicTime minUsedTime = MonotonicTime::now() - releaseUnusedSecondsTolerance; |
77 | |
78 | m_textures.removeAllMatching([&minUsedTime](const Entry& entry) { |
79 | return entry.canBeReleased(minUsedTime); |
80 | }); |
81 | |
82 | if (!m_textures.isEmpty()) |
83 | scheduleReleaseUnusedTextures(); |
84 | } |
85 | |
86 | RefPtr<BitmapTexture> BitmapTexturePool::createTexture(const BitmapTexture::Flags flags) |
87 | { |
88 | #if USE(TEXTURE_MAPPER_GL) |
89 | return BitmapTextureGL::create(m_contextAttributes, flags); |
90 | #else |
91 | UNUSED_PARAM(flags); |
92 | return nullptr; |
93 | #endif |
94 | } |
95 | |
96 | } // namespace WebCore |
97 | |