• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #pragma once
18 
19 #include "aemu/base/Compiler.h"
20 #include "aemu/base/synchronization/Lock.h"
21 
22 namespace android {
23 
24 namespace base { class Stream; }
25 
26 namespace snapshot {
27 
28 // LazySnapshotObj is a base class for objects that use lazy strategy for
29 // snapshot loading. It separates heavy-weight loading / restoring operations
30 // and only triggers it when the object needs to be used.
31 // Please implement heavy-weight loading / restoring operations in restore()
32 // method and call "touch" before you need to use the object.
33 
34 // An example is for texture lazy loading. On load it only reads the data from
35 // disk but does not load them into GPU. On restore it performs the heavy-weight
36 // GPU data loading.
37 
38 template <class Derived>
39 class LazySnapshotObj {
40     DISALLOW_COPY_AND_ASSIGN(LazySnapshotObj);
41 public:
42     LazySnapshotObj() = default;
43     // Snapshot loader
LazySnapshotObj(base::Stream *)44     LazySnapshotObj(base::Stream*) : mNeedRestore(true) {}
45 
touch()46     void touch() {
47         base::AutoLock lock(mMutex);
48         if (!mNeedRestore) {
49             return;
50         }
51         static_cast<Derived*>(this)->restore();
52         mNeedRestore = false;
53     }
54 
needRestore()55     bool needRestore() const {
56         base::AutoLock lock(mMutex);
57         return mNeedRestore;
58     }
59 
60 protected:
61     ~LazySnapshotObj() = default;
62     bool mNeedRestore = false;
63 
64 private:
65     mutable base::Lock mMutex;
66 };
67 
68 } // namespace snapshot
69 } // namespace android
70