Flutter Engine
The Flutter Engine
SkImage_Raster.h
Go to the documentation of this file.
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
18#include "src/core/SkMipmap.h"
20
21#include <cstddef>
22#include <cstdint>
23#include <utility>
24
25class GrDirectContext;
27class SkColorSpace;
28class SkData;
29class SkPixmap;
30enum SkColorType : int;
31struct SkIRect;
32struct SkImageInfo;
33
34namespace skgpu { namespace graphite { class Recorder; } }
35
37public:
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 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 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;
55 const SkIRect&,
56 RequiredProperties) const override;
57
58 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
59
60 bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const override;
61
63 GrDirectContext*) const override;
64
66
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
75 bool onHasMipmaps() const override { return SkToBool(fBitmap.fMips); }
76 bool onIsProtected() const override { return false; }
77
78 SkMipmap* onPeekMips() const override { return fBitmap.fMips.get(); }
79
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
98
99 SkBitmap bitmap() const { return fBitmap; }
100
101private:
102 SkBitmap fBitmap;
103};
104
105sk_sp<SkImage> MakeRasterCopyPriv(const SkPixmap& pmap, uint32_t id);
106
107#endif // SkImage_Raster_DEFINED
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
SK_SPI sk_sp< SkImage > SkMakeImageFromRasterBitmap(const SkBitmap &, SkCopyPixelsMode)
@ kAlways_SkCopyPixelsMode
always copy src pixels (even if they are marked immutable)
Definition: SkImagePriv.h:19
@ kNeedNewImageUniqueID
Definition: SkImage_Base.h:33
sk_sp< SkImage > MakeRasterCopyPriv(const SkPixmap &pmap, uint32_t id)
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
SkPixelRef * pixelRef() const
Definition: SkBitmap.h:720
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
Definition: SkData.h:25
virtual GrImageContext * context() const
Definition: SkImage_Base.h:112
bool onHasMipmaps() const override
void notifyAddedToRasterCache() const override
bool getROPixels(GrDirectContext *, SkBitmap *, CachingHint) const override
sk_sp< SkImage > onMakeSubset(GrDirectContext *, const SkIRect &) const override
bool onPeekPixels(SkPixmap *) const override
bool onAsLegacyBitmap(GrDirectContext *, SkBitmap *) const override
bool onIsProtected() const override
SkImage_Base::Type type() const override
const SkBitmap * onPeekBitmap() const override
sk_sp< SkImage > onMakeWithMipmaps(sk_sp< SkMipmap > mips) const override
~SkImage_Raster() override
bool onReadPixels(GrDirectContext *, const SkImageInfo &, void *, size_t, int srcX, int srcY, CachingHint) const override
SkPixelRef * getPixelRef() const
sk_sp< SkImage > onReinterpretColorSpace(sk_sp< SkColorSpace >) const override
SkMipmap * onPeekMips() const override
bool isValid(GrRecordingContext *context) const override
SkBitmap bitmap() const
sk_sp< SkImage > onMakeColorTypeAndColorSpace(SkColorType, sk_sp< SkColorSpace >, GrDirectContext *) const override
CachingHint
Definition: SkImage.h:463
friend class SkImage_Raster
Definition: SkImage.h:937
static SkMipmap * Build(const SkPixmap &src, SkDiscardableFactoryProc, bool computeContents=true)
Definition: SkMipmap.cpp:45
void notifyAddedToCache()
Definition: SkPixelRef.h:76
T * get() const
Definition: SkRefCnt.h:303
Definition: GpuTools.h:21
Definition: SkRect.h:32