• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11 
12 #include "snapshot/TextureSaver.h"
13 
14 #include "base/CompressingStream.h"
15 #include "base/System.h"
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <iterator>
20 #include <utility>
21 
22 using android::base::CompressingStream;
23 
24 namespace android {
25 namespace snapshot {
26 
TextureSaver(android::base::StdioStream && stream)27 TextureSaver::TextureSaver(android::base::StdioStream&& stream)
28     : mStream(std::move(stream)) {
29     // Put a placeholder for the index offset right now.
30     mStream.putBe64(0);
31 }
32 
~TextureSaver()33 TextureSaver::~TextureSaver() {
34     done();
35 }
36 
saveTexture(uint32_t texId,const saver_t & saver)37 void TextureSaver::saveTexture(uint32_t texId, const saver_t& saver) {
38 
39     if (!mStartTime) {
40         mStartTime = base::getHighResTimeUs();
41     }
42 
43     assert(mIndex.textures.end() ==
44            std::find_if(mIndex.textures.begin(), mIndex.textures.end(),
45                         [texId](FileIndex::Texture& tex) {
46                             return tex.texId == texId;
47                         }));
48     mIndex.textures.push_back({texId, ftello64(mStream.get())});
49 
50     CompressingStream stream(mStream);
51     saver(&stream, &mBuffer);
52 }
53 
done()54 void TextureSaver::done() {
55     if (mFinished) {
56         return;
57     }
58     mIndex.startPosInFile = ftello64(mStream.get());
59     writeIndex();
60     mEndTime = base::getHighResTimeUs();
61 #if SNAPSHOT_PROFILE > 1
62     printf("Texture saving time: %.03f\n",
63            (mEndTime - mStartTime) / 1000.0);
64 #endif
65     mHasError = ferror(mStream.get()) != 0;
66     mFinished = true;
67     mStream.close();
68 }
69 
writeIndex()70 void TextureSaver::writeIndex() {
71 #if SNAPSHOT_PROFILE > 1
72     auto start = ftello64(mStream.get());
73 #endif
74 
75     mStream.putBe32(static_cast<uint32_t>(mIndex.version));
76     mStream.putBe32(static_cast<uint32_t>(mIndex.textures.size()));
77     for (const FileIndex::Texture& b : mIndex.textures) {
78         mStream.putBe32(b.texId);
79         mStream.putBe64(static_cast<uint64_t>(b.filePos));
80     }
81     auto end = ftello64(mStream.get());
82     mDiskSize = uint64_t(end);
83 #if SNAPSHOT_PROFILE > 1
84     printf("texture: index size: %d\n", int(end - start));
85 #endif
86 
87     fseeko64(mStream.get(), 0, SEEK_SET);
88     mStream.putBe64(static_cast<uint64_t>(mIndex.startPosInFile));
89 }
90 
91 }  // namespace snapshot
92 }  // namespace android
93