/src/skia/src/image/SkImage_Raster.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2023 Google LLC |
3 | | * |
4 | | * Use of this source code is governed by a BSD-style license that can be |
5 | | * found in the LICENSE file. |
6 | | */ |
7 | | |
8 | | #ifndef SkImage_Raster_DEFINED |
9 | | #define SkImage_Raster_DEFINED |
10 | | |
11 | | #include "include/core/SkBitmap.h" |
12 | | #include "include/core/SkImage.h" |
13 | | #include "include/core/SkPixelRef.h" |
14 | | #include "include/core/SkRefCnt.h" |
15 | | #include "include/core/SkTypes.h" |
16 | | #include "include/private/base/SkTo.h" |
17 | | #include "src/core/SkImagePriv.h" |
18 | | #include "src/core/SkMipmap.h" |
19 | | #include "src/image/SkImage_Base.h" |
20 | | |
21 | | #include <cstddef> |
22 | | #include <cstdint> |
23 | | #include <utility> |
24 | | |
25 | | class GrDirectContext; |
26 | | class GrRecordingContext; |
27 | | class SkColorSpace; |
28 | | class SkData; |
29 | | class SkPixmap; |
30 | | enum SkColorType : int; |
31 | | struct SkIRect; |
32 | | struct SkImageInfo; |
33 | | |
34 | | namespace skgpu { namespace graphite { class Recorder; } } |
35 | | |
36 | | class SkImage_Raster : public SkImage_Base { |
37 | | public: |
38 | | SkImage_Raster(const SkImageInfo&, sk_sp<SkData>, size_t rb, |
39 | | uint32_t id = kNeedNewImageUniqueID); |
40 | | SkImage_Raster(const SkBitmap& bm, bool bitmapMayBeMutable = false); |
41 | | ~SkImage_Raster() override; |
42 | | |
43 | | // From SkImage.h |
44 | 0 | bool isValid(GrRecordingContext* context) const override { return true; } |
45 | | |
46 | | // From SkImage_Base.h |
47 | | bool onReadPixels(GrDirectContext*, const SkImageInfo&, void*, size_t, int srcX, int srcY, |
48 | | CachingHint) const override; |
49 | | bool onPeekPixels(SkPixmap*) const override; |
50 | 0 | const SkBitmap* onPeekBitmap() const override { return &fBitmap; } |
51 | | |
52 | | bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint) const override; |
53 | | sk_sp<SkImage> onMakeSubset(GrDirectContext*, const SkIRect&) const override; |
54 | | sk_sp<SkImage> onMakeSubset(skgpu::graphite::Recorder*, |
55 | | const SkIRect&, |
56 | | RequiredProperties) const override; |
57 | | |
58 | 0 | SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
59 | | |
60 | | bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const override; |
61 | | |
62 | | sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>, |
63 | | GrDirectContext*) const override; |
64 | | |
65 | | sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const override; |
66 | | |
67 | 303 | void notifyAddedToRasterCache() const override { |
68 | | // We explicitly DON'T want to call INHERITED::notifyAddedToRasterCache. That ties the |
69 | | // lifetime of derived/cached resources to the image. In this case, we only want cached |
70 | | // data (eg mips) tied to the lifetime of the underlying pixelRef. |
71 | 303 | SkASSERT(fBitmap.pixelRef()); |
72 | 303 | fBitmap.pixelRef()->notifyAddedToCache(); |
73 | 303 | } |
74 | | |
75 | 3.54k | bool onHasMipmaps() const override { return SkToBool(fBitmap.fMips); } |
76 | 0 | bool onIsProtected() const override { return false; } |
77 | | |
78 | 317 | SkMipmap* onPeekMips() const override { return fBitmap.fMips.get(); } |
79 | | |
80 | 2.77k | sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap> mips) const override { |
81 | | // It's dangerous to have two SkBitmaps that share a SkPixelRef but have different SkMipmaps |
82 | | // since various caches key on SkPixelRef's generation ID. Also, SkPixelRefs that back |
83 | | // SkSurfaces are marked "temporarily immutable" and making an image that uses the same |
84 | | // SkPixelRef can interact badly with SkSurface/SkImage copy-on-write. So we just always |
85 | | // make a copy with a new ID. |
86 | 2.77k | static auto constexpr kCopyMode = SkCopyPixelsMode::kAlways_SkCopyPixelsMode; |
87 | 2.77k | sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(fBitmap, kCopyMode); |
88 | 2.77k | auto imgRaster = static_cast<SkImage_Raster*>(img.get()); |
89 | 2.77k | if (mips) { |
90 | 1.30k | imgRaster->fBitmap.fMips = std::move(mips); |
91 | 1.47k | } else { |
92 | 1.47k | imgRaster->fBitmap.fMips.reset(SkMipmap::Build(fBitmap.pixmap(), nullptr)); |
93 | 1.47k | } |
94 | 2.77k | return img; |
95 | 2.77k | } |
96 | | |
97 | 3.48k | SkImage_Base::Type type() const override { return SkImage_Base::Type::kRaster; } |
98 | | |
99 | 681 | SkBitmap bitmap() const { return fBitmap; } |
100 | | |
101 | | private: |
102 | | SkBitmap fBitmap; |
103 | | }; |
104 | | |
105 | | sk_sp<SkImage> MakeRasterCopyPriv(const SkPixmap& pmap, uint32_t id); |
106 | | |
107 | | #endif // SkImage_Raster_DEFINED |