• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
isValid(GrRecordingContext * context)44     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;
onPeekBitmap()50     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 
getPixelRef()58     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 
notifyAddedToRasterCache()67     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         SkASSERT(fBitmap.pixelRef());
72         fBitmap.pixelRef()->notifyAddedToCache();
73     }
74 
onHasMipmaps()75     bool onHasMipmaps() const override { return SkToBool(fBitmap.fMips); }
onIsProtected()76     bool onIsProtected() const override { return false; }
77 
onPeekMips()78     SkMipmap* onPeekMips() const override { return fBitmap.fMips.get(); }
79 
onMakeWithMipmaps(sk_sp<SkMipmap> mips)80     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         static auto constexpr kCopyMode = SkCopyPixelsMode::kAlways_SkCopyPixelsMode;
87         sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(fBitmap, kCopyMode);
88         auto imgRaster = static_cast<SkImage_Raster*>(img.get());
89         if (mips) {
90             imgRaster->fBitmap.fMips = std::move(mips);
91         } else {
92             imgRaster->fBitmap.fMips.reset(SkMipmap::Build(fBitmap.pixmap(), nullptr));
93         }
94         return img;
95     }
96 
type()97     SkImage_Base::Type type() const override { return SkImage_Base::Type::kRaster; }
98 
bitmap()99     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
108