Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkImage_Base.cpp
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
9
16#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
23
24#include <atomic>
25#include <utility>
26
28 : SkImage(info, uniqueID), fAddedToRasterCache(false) {}
29
31 if (fAddedToRasterCache.load()) {
33 }
34}
35
37 SkIRect origSrcRect,
38 RescaleGamma rescaleGamma,
39 RescaleMode rescaleMode,
41 ReadPixelsContext context) const {
42 SkBitmap src;
43 SkPixmap peek;
44 SkIRect srcRect;
45 if (this->peekPixels(&peek)) {
46 src.installPixels(peek);
47 srcRect = origSrcRect;
48 } else {
49 // Context TODO: Elevate GrDirectContext requirement to public API.
50 auto dContext = as_IB(this)->directContext();
51 src.setInfo(this->imageInfo().makeDimensions(origSrcRect.size()));
52 src.allocPixels();
53 if (!this->readPixels(dContext, src.pixmap(), origSrcRect.x(), origSrcRect.y())) {
54 callback(context, nullptr);
55 return;
56 }
57 srcRect = SkIRect::MakeSize(src.dimensions());
58 }
59 return SkRescaleAndReadPixels(src, info, srcRect, rescaleGamma, rescaleMode, callback, context);
60}
61
63 // As the base-class, all we can do is make a copy (regardless of mode).
64 // Subclasses that want to be more optimal should override.
65 SkImageInfo info = fInfo.makeColorType(kN32_SkColorType).makeColorSpace(nullptr);
66 if (!bitmap->tryAllocPixels(info)) {
67 return false;
68 }
69
70 if (!this->readPixels(
71 dContext, bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
72 bitmap->reset();
73 return false;
74 }
75
76 bitmap->setImmutable();
77 return true;
78}
79
81 if (subset.isEmpty()) {
82 return nullptr;
83 }
84
85 const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
86 if (!bounds.contains(subset)) {
87 return nullptr;
88 }
89
90 // optimization : return self if the subset == our bounds
91 if (bounds == subset) {
92 return sk_ref_sp(const_cast<SkImage_Base*>(this));
93 }
94
95 return this->onMakeSubset(direct, subset);
96}
97
99 const SkIRect& subset,
100 RequiredProperties requiredProps) const {
101 if (subset.isEmpty()) {
102 return nullptr;
103 }
104
105 const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
106 if (!bounds.contains(subset)) {
107 return nullptr;
108 }
109
110 return this->onMakeSubset(recorder, subset, requiredProps);
111}
112
114 bool readAlpha,
115 sk_sp<SkColorSpace> dstColorSpace,
116 SkIRect srcRect,
117 SkISize dstSize,
121 ReadPixelsContext context) const {
122 // TODO: Call non-YUV asyncRescaleAndReadPixels and then make our callback convert to YUV and
123 // call client's callback.
124 callback(context, nullptr);
125}
126
131
134 RequiredProperties props) const {
135 return this->makeColorTypeAndColorSpace(recorder, this->colorType(), std::move(target), props);
136}
137
139 SkColorType targetColorType,
140 sk_sp<SkColorSpace> targetCS) const {
141 if (kUnknown_SkColorType == targetColorType || !targetCS) {
142 return nullptr;
143 }
144
147 if (!colorSpace) {
149 }
150 if (colorType == targetColorType &&
151 (SkColorSpace::Equals(colorSpace, targetCS.get()) || this->isAlphaOnly())) {
152 return sk_ref_sp(const_cast<SkImage_Base*>(this));
153 }
154
155 return this->onMakeColorTypeAndColorSpace(targetColorType, std::move(targetCS), dContext);
156}
157
159 SkColorType ct,
161 RequiredProperties) const {
162 // Default to the ganesh version which should be backend agnostic if this
163 // image is, for example, a raster backed image. The graphite subclass overrides
164 // this method and things work correctly.
165 return this->makeColorTypeAndColorSpace(nullptr, ct, std::move(cs));
166}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID)
SkColorSpace * sk_srgb_singleton()
SkColorType
Definition SkColorType.h:19
@ kUnknown_SkColorType
uninitialized
Definition SkColorType.h:20
SkYUVColorSpace
Definition SkImageInfo.h:68
static SkImage_Base * as_IB(SkImage *image)
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
void SkRescaleAndReadPixels(SkBitmap bmp, const SkImageInfo &resultInfo, const SkIRect &srcRect, SkImage::RescaleGamma rescaleGamma, SkImage::RescaleMode rescaleMode, SkImage::ReadPixelsCallback callback, SkImage::ReadPixelsContext context)
static bool Equals(const SkColorSpace *, const SkColorSpace *)
virtual bool onAsLegacyBitmap(GrDirectContext *, SkBitmap *) const
~SkImage_Base() override
virtual GrDirectContext * directContext() const
virtual sk_sp< SkImage > onMakeSubset(GrDirectContext *, const SkIRect &) const =0
sk_sp< SkImage > makeSubset(GrDirectContext *direct, const SkIRect &subset) const override
virtual GrImageContext * context() const
sk_sp< SkImage > makeColorTypeAndColorSpace(GrDirectContext *dContext, SkColorType targetColorType, sk_sp< SkColorSpace > targetCS) const override
virtual sk_sp< SkImage > onMakeColorTypeAndColorSpace(SkColorType, sk_sp< SkColorSpace >, GrDirectContext *) const =0
sk_sp< SkImage > makeColorSpace(GrDirectContext *, sk_sp< SkColorSpace >) const override
virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, bool readAlpha, sk_sp< SkColorSpace > dstColorSpace, SkIRect srcRect, SkISize dstSize, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext) const
virtual void onAsyncRescaleAndReadPixels(const SkImageInfo &, SkIRect srcRect, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext) const
const SkImageInfo & imageInfo() const
Definition SkImage.h:279
SkColorSpace * colorSpace() const
Definition SkImage.cpp:156
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Definition SkImage.cpp:42
uint32_t uniqueID() const
Definition SkImage.h:311
void * ReadPixelsContext
Definition SkImage.h:578
RescaleMode
Definition SkImage.h:587
bool peekPixels(SkPixmap *pixmap) const
Definition SkImage.cpp:34
int width() const
Definition SkImage.h:285
RescaleGamma
Definition SkImage.h:585
friend class SkImage_Base
Definition SkImage.h:936
SkColorType colorType() const
Definition SkImage.cpp:152
int height() const
Definition SkImage.h:291
SkIRect bounds() const
Definition SkImage.h:303
void(ReadPixelsContext, std::unique_ptr< const AsyncReadResult >) ReadPixelsCallback
Definition SkImage.h:583
T * get() const
Definition SkRefCnt.h:303
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
uint32_t * target
constexpr int32_t x() const
Definition SkRect.h:141
constexpr int32_t y() const
Definition SkRect.h:148
constexpr SkISize size() const
Definition SkRect.h:172
static constexpr SkIRect MakeSize(const SkISize &size)
Definition SkRect.h:66
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56
bool isEmpty() const
Definition SkRect.h:202
bool contains(int32_t x, int32_t y) const
Definition SkRect.h:463
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
SkImageInfo makeColorType(SkColorType newColorType) const