| 1 |
/* |
| 2 |
* Copyright 2012 Google Inc. |
| 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 |
#include "include/core/SkImage.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkBlendMode.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkColorSpace.h" |
| 14 |
#include "include/core/SkData.h" |
| 15 |
#include "include/core/SkPaint.h" |
| 16 |
#include "include/core/SkPixmap.h" |
| 17 |
#include "include/core/SkSurface.h" |
| 18 |
#include "include/core/SkSurfaceProps.h" |
| 19 |
#include "include/core/SkTileMode.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "src/core/SkColorSpacePriv.h" |
| 22 |
#include "src/core/SkImageInfoPriv.h" |
| 23 |
#include "src/core/SkMipmap.h" |
| 24 |
#include "src/core/SkNextID.h" |
| 25 |
#include "src/image/SkImage_Base.h" |
| 26 |
#include "src/shaders/SkImageShader.h" |
| 27 |
|
| 28 |
#include <utility> |
| 29 |
|
| 30 |
class SkShader; |
| 31 |
|
| 32 |
SkImage::SkImage(const SkImageInfo& info, uint32_t uniqueID) |
| 33 |
: fInfo(info) |
| 34 |
, fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) { |
| 35 |
SkASSERT(info.width() > 0); |
| 36 |
SkASSERT(info.height() > 0); |
| 37 |
} |
| 38 |
|
| 39 |
bool SkImage::peekPixels(SkPixmap* pm) const { |
| 40 |
SkPixmap tmp; |
| 41 |
if (!pm) { |
| 42 |
pm = &tmp; |
| 43 |
} |
| 44 |
return as_IB(this)->onPeekPixels(pm); |
| 45 |
} |
| 46 |
|
| 47 |
bool SkImage::readPixels(GrDirectContext* dContext, const SkImageInfo& dstInfo, void* dstPixels, |
| 48 |
size_t dstRowBytes, int srcX, int srcY, CachingHint chint) const { |
| 49 |
return as_IB(this)->onReadPixels(dContext, dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint); |
| 50 |
} |
| 51 |
|
| 52 |
sk_sp<SkImage> SkImage::makeScaled(const SkImageInfo& newInfo, |
| 53 |
const SkSamplingOptions& sampling) const { |
| 54 |
return makeScaled(nullptr, newInfo, sampling, SkSurfaceProps{}); |
| 55 |
} |
| 56 |
|
| 57 |
sk_sp<SkImage> SkImage::makeScaled(SkRecorder* recorder, |
| 58 |
const SkImageInfo& newInfo, |
| 59 |
const SkSamplingOptions& sampling) const { |
| 60 |
return makeScaled(recorder, newInfo, sampling, SkSurfaceProps{}); |
| 61 |
} |
| 62 |
|
| 63 |
sk_sp<SkImage> SkImage::makeScaled(SkRecorder* recorder, |
| 64 |
const SkImageInfo& newInfo, |
| 65 |
const SkSamplingOptions& sampling, |
| 66 |
const SkSurfaceProps& props) const { |
| 67 |
if (!SkImageInfoIsValid(newInfo)) { |
| 68 |
return nullptr; |
| 69 |
} |
| 70 |
if (newInfo == this->imageInfo()) { |
| 71 |
return sk_ref_sp(this); |
| 72 |
} |
| 73 |
|
| 74 |
auto surf = as_IB(this)->onMakeSurface(recorder, newInfo); |
| 75 |
if (!surf) { |
| 76 |
return nullptr; |
| 77 |
} |
| 78 |
|
| 79 |
SkPaint paint; |
| 80 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 81 |
surf->getCanvas()->drawImageRect(this, |
| 82 |
SkRect::MakeIWH(newInfo.width(), newInfo.height()), |
| 83 |
sampling, |
| 84 |
&paint); |
| 85 |
return surf->makeImageSnapshot(); |
| 86 |
} |
| 87 |
|
| 88 |
#ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API |
| 89 |
bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, |
| 90 |
size_t dstRowBytes, int srcX, int srcY, CachingHint chint) const { |
| 91 |
auto dContext = as_IB(this)->directContext(); |
| 92 |
return this->readPixels(dContext, dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint); |
| 93 |
} |
| 94 |
#endif |
| 95 |
|
| 96 |
void SkImage::asyncRescaleAndReadPixels(const SkImageInfo& info, |
| 97 |
const SkIRect& srcRect, |
| 98 |
RescaleGamma rescaleGamma, |
| 99 |
RescaleMode rescaleMode, |
| 100 |
ReadPixelsCallback callback, |
| 101 |
ReadPixelsContext context) const { |
| 102 |
if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || |
| 103 |
!SkImageInfoIsValid(info)) { |
| 104 |
callback(context, nullptr); |
| 105 |
return; |
| 106 |
} |
| 107 |
as_IB(this)->onAsyncRescaleAndReadPixels( |
| 108 |
info, srcRect, rescaleGamma, rescaleMode, callback, context); |
| 109 |
} |
| 110 |
|
| 111 |
void SkImage::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, |
| 112 |
sk_sp<SkColorSpace> dstColorSpace, |
| 113 |
const SkIRect& srcRect, |
| 114 |
const SkISize& dstSize, |
| 115 |
RescaleGamma rescaleGamma, |
| 116 |
RescaleMode rescaleMode, |
| 117 |
ReadPixelsCallback callback, |
| 118 |
ReadPixelsContext context) const { |
| 119 |
if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() || |
| 120 |
(dstSize.width() & 0b1) || (dstSize.height() & 0b1)) { |
| 121 |
callback(context, nullptr); |
| 122 |
return; |
| 123 |
} |
| 124 |
as_IB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace, |
| 125 |
/*readAlpha=*/false, |
| 126 |
std::move(dstColorSpace), |
| 127 |
srcRect, |
| 128 |
dstSize, |
| 129 |
rescaleGamma, |
| 130 |
rescaleMode, |
| 131 |
callback, |
| 132 |
context); |
| 133 |
} |
| 134 |
|
| 135 |
void SkImage::asyncRescaleAndReadPixelsYUVA420(SkYUVColorSpace yuvColorSpace, |
| 136 |
sk_sp<SkColorSpace> dstColorSpace, |
| 137 |
const SkIRect& srcRect, |
| 138 |
const SkISize& dstSize, |
| 139 |
RescaleGamma rescaleGamma, |
| 140 |
RescaleMode rescaleMode, |
| 141 |
ReadPixelsCallback callback, |
| 142 |
ReadPixelsContext context) const { |
| 143 |
if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() || |
| 144 |
(dstSize.width() & 0b1) || (dstSize.height() & 0b1)) { |
| 145 |
callback(context, nullptr); |
| 146 |
return; |
| 147 |
} |
| 148 |
as_IB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace, |
| 149 |
/*readAlpha=*/true, |
| 150 |
std::move(dstColorSpace), |
| 151 |
srcRect, |
| 152 |
dstSize, |
| 153 |
rescaleGamma, |
| 154 |
rescaleMode, |
| 155 |
callback, |
| 156 |
context); |
| 157 |
} |
| 158 |
|
| 159 |
bool SkImage::scalePixels(const SkPixmap& dst, const SkSamplingOptions& sampling, |
| 160 |
CachingHint chint) const { |
| 161 |
// Context TODO: Elevate GrDirectContext requirement to public API. |
| 162 |
auto dContext = as_IB(this)->directContext(); |
| 163 |
if (this->width() == dst.width() && this->height() == dst.height()) { |
| 164 |
return this->readPixels(dContext, dst, 0, 0, chint); |
| 165 |
} |
| 166 |
|
| 167 |
// Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself |
| 168 |
// can scale more efficiently) we should take advantage of it here. |
| 169 |
// |
| 170 |
SkBitmap bm; |
| 171 |
if (as_IB(this)->getROPixels(dContext, &bm, chint)) { |
| 172 |
SkPixmap pmap; |
| 173 |
// Note: By calling the pixmap scaler, we never cache the final result, so the chint |
| 174 |
// is (currently) only being applied to the getROPixels. If we get a request to |
| 175 |
// also attempt to cache the final (scaled) result, we would add that logic here. |
| 176 |
// |
| 177 |
return bm.peekPixels(&pmap) && pmap.scalePixels(dst, sampling); |
| 178 |
} |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 183 |
|
| 184 |
SkColorType SkImage::colorType() const { return fInfo.colorType(); } |
| 185 |
|
| 186 |
SkAlphaType SkImage::alphaType() const { return fInfo.alphaType(); } |
| 187 |
|
| 188 |
SkColorSpace* SkImage::colorSpace() const { return fInfo.colorSpace(); } |
| 189 |
|
| 190 |
sk_sp<SkColorSpace> SkImage::refColorSpace() const { return fInfo.refColorSpace(); } |
| 191 |
|
| 192 |
sk_sp<SkShader> SkImage::makeShader(const SkSamplingOptions& sampling, const SkMatrix& lm) const { |
| 193 |
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), |
| 194 |
SkTileMode::kClamp, SkTileMode::kClamp, |
| 195 |
sampling, &lm); |
| 196 |
} |
| 197 |
|
| 198 |
sk_sp<SkShader> SkImage::makeShader(const SkSamplingOptions& sampling, const SkMatrix* lm) const { |
| 199 |
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), |
| 200 |
SkTileMode::kClamp, SkTileMode::kClamp, |
| 201 |
sampling, lm); |
| 202 |
} |
| 203 |
|
| 204 |
sk_sp<SkShader> SkImage::makeShader(SkTileMode tmx, SkTileMode tmy, |
| 205 |
const SkSamplingOptions& sampling, |
| 206 |
const SkMatrix& lm) const { |
| 207 |
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy, |
| 208 |
sampling, &lm); |
| 209 |
} |
| 210 |
|
| 211 |
sk_sp<SkShader> SkImage::makeShader(SkTileMode tmx, SkTileMode tmy, |
| 212 |
const SkSamplingOptions& sampling, |
| 213 |
const SkMatrix* localMatrix) const { |
| 214 |
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy, |
| 215 |
sampling, localMatrix); |
| 216 |
} |
| 217 |
|
| 218 |
sk_sp<SkShader> SkImage::makeRawShader(SkTileMode tmx, SkTileMode tmy, |
| 219 |
const SkSamplingOptions& sampling, |
| 220 |
const SkMatrix& lm) const { |
| 221 |
return SkImageShader::MakeRaw(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy, |
| 222 |
sampling, &lm); |
| 223 |
} |
| 224 |
|
| 225 |
sk_sp<SkShader> SkImage::makeRawShader(const SkSamplingOptions& sampling, |
| 226 |
const SkMatrix& lm) const { |
| 227 |
return SkImageShader::MakeRaw(sk_ref_sp(const_cast<SkImage*>(this)), |
| 228 |
SkTileMode::kClamp, SkTileMode::kClamp, |
| 229 |
sampling, &lm); |
| 230 |
} |
| 231 |
|
| 232 |
sk_sp<SkShader> SkImage::makeRawShader(const SkSamplingOptions& sampling, |
| 233 |
const SkMatrix* localMatrix) const { |
| 234 |
return SkImageShader::MakeRaw(sk_ref_sp(const_cast<SkImage*>(this)), |
| 235 |
SkTileMode::kClamp, SkTileMode::kClamp, |
| 236 |
sampling, localMatrix); |
| 237 |
} |
| 238 |
|
| 239 |
sk_sp<SkShader> SkImage::makeRawShader(SkTileMode tmx, SkTileMode tmy, |
| 240 |
const SkSamplingOptions& sampling, |
| 241 |
const SkMatrix* localMatrix) const { |
| 242 |
return SkImageShader::MakeRaw(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy, |
| 243 |
sampling, localMatrix); |
| 244 |
} |
| 245 |
|
| 246 |
sk_sp<const SkData> SkImage::refEncodedData() const { return as_IB(this)->onRefEncoded(); } |
| 247 |
|
| 248 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 249 |
|
| 250 |
bool SkImage::readPixels(GrDirectContext* dContext, const SkPixmap& pmap, int srcX, int srcY, |
| 251 |
CachingHint chint) const { |
| 252 |
return this->readPixels(dContext, pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, |
| 253 |
srcY, chint); |
| 254 |
} |
| 255 |
|
| 256 |
#ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API |
| 257 |
bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const { |
| 258 |
auto dContext = as_IB(this)->directContext(); |
| 259 |
return this->readPixels(dContext, pmap, srcX, srcY, chint); |
| 260 |
} |
| 261 |
#endif |
| 262 |
|
| 263 |
bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode ) const { |
| 264 |
// Context TODO: Elevate GrDirectContext requirement to public API. |
| 265 |
auto dContext = as_IB(this)->directContext(); |
| 266 |
return as_IB(this)->onAsLegacyBitmap(dContext, bitmap); |
| 267 |
} |
| 268 |
|
| 269 |
bool SkImage::isAlphaOnly() const { return SkColorTypeIsAlphaOnly(fInfo.colorType()); } |
| 270 |
|
| 271 |
sk_sp<SkImage> SkImage::reinterpretColorSpace(sk_sp<SkColorSpace> target) const { |
| 272 |
if (!target) { |
| 273 |
return nullptr; |
| 274 |
} |
| 275 |
|
| 276 |
// No need to create a new image if: |
| 277 |
// (1) The color spaces are equal. |
| 278 |
// (2) The color type is kAlpha8. |
| 279 |
SkColorSpace* colorSpace = this->colorSpace(); |
| 280 |
if (!colorSpace) { |
| 281 |
colorSpace = sk_srgb_singleton(); |
| 282 |
} |
| 283 |
if (SkColorSpace::Equals(colorSpace, target.get()) || this->isAlphaOnly()) { |
| 284 |
return sk_ref_sp(const_cast<SkImage*>(this)); |
| 285 |
} |
| 286 |
|
| 287 |
return as_IB(this)->onReinterpretColorSpace(std::move(target)); |
| 288 |
} |
| 289 |
|
| 290 |
sk_sp<SkImage> SkImage::makeNonTextureImage(GrDirectContext* dContext) const { |
| 291 |
if (!this->isTextureBacked()) { |
| 292 |
return sk_ref_sp(const_cast<SkImage*>(this)); |
| 293 |
} |
| 294 |
return this->makeRasterImage(dContext, kDisallow_CachingHint); |
| 295 |
} |
| 296 |
|
| 297 |
sk_sp<SkImage> SkImage::makeRasterImage(GrDirectContext* dContext, CachingHint chint) const { |
| 298 |
SkPixmap pm; |
| 299 |
if (this->peekPixels(&pm)) { |
| 300 |
return sk_ref_sp(const_cast<SkImage*>(this)); |
| 301 |
} |
| 302 |
|
| 303 |
const size_t rowBytes = fInfo.minRowBytes(); |
| 304 |
size_t size = fInfo.computeByteSize(rowBytes); |
| 305 |
if (SkImageInfo::ByteSizeOverflowed(size)) { |
| 306 |
return nullptr; |
| 307 |
} |
| 308 |
|
| 309 |
if (!dContext) { |
| 310 |
// Try to get the saved context if the client didn't pass it in (but they really should). |
| 311 |
dContext = as_IB(this)->directContext(); |
| 312 |
} |
| 313 |
sk_sp<SkData> data = SkData::MakeUninitialized(size); |
| 314 |
pm = {fInfo.makeColorSpace(nullptr), data->writable_data(), fInfo.minRowBytes()}; |
| 315 |
if (!this->readPixels(dContext, pm, 0, 0, chint)) { |
| 316 |
return nullptr; |
| 317 |
} |
| 318 |
|
| 319 |
return SkImages::RasterFromData(fInfo, std::move(data), rowBytes); |
| 320 |
} |
| 321 |
|
| 322 |
bool SkImage::hasMipmaps() const { return as_IB(this)->onHasMipmaps(); } |
| 323 |
|
| 324 |
bool SkImage::isProtected() const { return as_IB(this)->onIsProtected(); } |
| 325 |
|
| 326 |
sk_sp<SkImage> SkImage::withMipmaps(sk_sp<SkMipmap> mips) const { |
| 327 |
if (mips == nullptr || mips->validForRootLevel(this->imageInfo())) { |
| 328 |
if (auto result = as_IB(this)->onMakeWithMipmaps(std::move(mips))) { |
| 329 |
return result; |
| 330 |
} |
| 331 |
} |
| 332 |
return sk_ref_sp((const_cast<SkImage*>(this))); |
| 333 |
} |
| 334 |
|
| 335 |
sk_sp<SkImage> SkImage::withDefaultMipmaps() const { |
| 336 |
return this->withMipmaps(nullptr); |
| 337 |
} |