xref: /aosp_15_r20/external/skia/src/image/SkImage_Raster.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 class SkSurface;
31 enum SkColorType : int;
32 struct SkIRect;
33 struct SkImageInfo;
34 
35 namespace skgpu { namespace graphite { class Recorder; } }
36 
37 class SkImage_Raster : public SkImage_Base {
38 public:
39     SkImage_Raster(const SkImageInfo&, sk_sp<SkData>, size_t rb,
40                    uint32_t id = kNeedNewImageUniqueID);
41     SkImage_Raster(const SkBitmap& bm, bool bitmapMayBeMutable = false);
42     ~SkImage_Raster() override;
43 
44     // From SkImage.h
isValid(GrRecordingContext * context)45     bool isValid(GrRecordingContext* context) const override { return true; }
46 
47     // From SkImage_Base.h
48     bool onReadPixels(GrDirectContext*, const SkImageInfo&, void*, size_t, int srcX, int srcY,
49                       CachingHint) const override;
50     bool onPeekPixels(SkPixmap*) const override;
onPeekBitmap()51     const SkBitmap* onPeekBitmap() const override { return &fBitmap; }
52 
53     bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint) const override;
54     sk_sp<SkImage> onMakeSubset(GrDirectContext*, const SkIRect&) const override;
55     sk_sp<SkImage> onMakeSubset(skgpu::graphite::Recorder*,
56                                 const SkIRect&,
57                                 RequiredProperties) const override;
58 
59     sk_sp<SkSurface> onMakeSurface(skgpu::graphite::Recorder*, const SkImageInfo&) const override;
60 
getPixelRef()61     SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
62 
63     bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const override;
64 
65     sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>,
66                                                 GrDirectContext*) const override;
67 
68     sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const override;
69 
notifyAddedToRasterCache()70     void notifyAddedToRasterCache() const override {
71         // We explicitly DON'T want to call INHERITED::notifyAddedToRasterCache. That ties the
72         // lifetime of derived/cached resources to the image. In this case, we only want cached
73         // data (eg mips) tied to the lifetime of the underlying pixelRef.
74         SkASSERT(fBitmap.pixelRef());
75         fBitmap.pixelRef()->notifyAddedToCache();
76     }
77 
onHasMipmaps()78     bool onHasMipmaps() const override { return SkToBool(fBitmap.fMips); }
onIsProtected()79     bool onIsProtected() const override { return false; }
80 
onPeekMips()81     SkMipmap* onPeekMips() const override { return fBitmap.fMips.get(); }
82 
onMakeWithMipmaps(sk_sp<SkMipmap> mips)83     sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap> mips) const override {
84         // It's dangerous to have two SkBitmaps that share a SkPixelRef but have different SkMipmaps
85         // since various caches key on SkPixelRef's generation ID. Also, SkPixelRefs that back
86         // SkSurfaces are marked "temporarily immutable" and making an image that uses the same
87         // SkPixelRef can interact badly with SkSurface/SkImage copy-on-write. So we just always
88         // make a copy with a new ID.
89         static auto constexpr kCopyMode = SkCopyPixelsMode::kAlways_SkCopyPixelsMode;
90         sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(fBitmap, kCopyMode);
91         auto imgRaster = static_cast<SkImage_Raster*>(img.get());
92         if (mips) {
93             imgRaster->fBitmap.fMips = std::move(mips);
94         } else {
95             imgRaster->fBitmap.fMips.reset(SkMipmap::Build(fBitmap.pixmap(), nullptr));
96         }
97         return img;
98     }
99 
type()100     SkImage_Base::Type type() const override { return SkImage_Base::Type::kRaster; }
101 
bitmap()102     SkBitmap bitmap() const { return fBitmap; }
103 
104 private:
105     SkBitmap fBitmap;
106 };
107 
108 sk_sp<SkImage> MakeRasterCopyPriv(const SkPixmap& pmap, uint32_t id);
109 
110 #endif // SkImage_Raster_DEFINED
111