forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster_cache.h
89 lines (66 loc) · 2.36 KB
/
raster_cache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_FLOW_RASTER_CACHE_H_
#define FLUTTER_FLOW_RASTER_CACHE_H_
#include <memory>
#include <unordered_map>
#include "flutter/flow/instrumentation.h"
#include "flutter/flow/raster_cache_key.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/weak_ptr.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSize.h"
namespace flow {
class RasterCacheResult {
public:
RasterCacheResult() {}
RasterCacheResult(sk_sp<SkImage> image, const SkRect& logical_rect)
: image_(std::move(image)), logical_rect_(logical_rect) {}
operator bool() const { return static_cast<bool>(image_); }
bool is_valid() const { return static_cast<bool>(image_); };
void draw(SkCanvas& canvas) const;
private:
sk_sp<SkImage> image_;
SkRect logical_rect_;
};
class RasterCache {
public:
explicit RasterCache(size_t threshold = 3);
~RasterCache();
static SkIRect GetDeviceBounds(const SkRect& rect, const SkMatrix& ctm) {
SkRect device_rect;
ctm.mapRect(&device_rect, rect);
SkIRect bounds;
device_rect.roundOut(&bounds);
return bounds;
}
static SkMatrix GetIntegralTransCTM(const SkMatrix& ctm) {
SkMatrix result = ctm;
result[SkMatrix::kMTransX] = SkScalarRoundToScalar(ctm.getTranslateX());
result[SkMatrix::kMTransY] = SkScalarRoundToScalar(ctm.getTranslateY());
return result;
}
RasterCacheResult GetPrerolledImage(GrContext* context,
SkPicture* picture,
const SkMatrix& transformation_matrix,
SkColorSpace* dst_color_space,
bool is_complex,
bool will_change);
void SweepAfterFrame();
void Clear();
void SetCheckboardCacheImages(bool checkerboard);
private:
struct Entry {
bool used_this_frame = false;
size_t access_count = 0;
RasterCacheResult image;
};
const size_t threshold_;
RasterCacheKey::Map<Entry> cache_;
bool checkerboard_images_;
fml::WeakPtrFactory<RasterCache> weak_factory_;
FML_DISALLOW_COPY_AND_ASSIGN(RasterCache);
};
} // namespace flow
#endif // FLUTTER_FLOW_RASTER_CACHE_H_