| 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 |
#include "src/image/SkImage_Base.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkColorType.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkPixmap.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "include/private/base/SkDebug.h" |
| 20 |
#include "src/core/SkBitmapCache.h" |
| 21 |
#include "src/image/SkRescaleAndReadPixels.h" |
| 22 |
|
| 23 |
#include <atomic> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
SkImage_Base::SkImage_Base(const SkImageInfo& info, uint32_t uniqueID) |
| 27 |
: SkImage(info, uniqueID), fAddedToRasterCache(false) {} |
| 28 |
|
| 29 |
SkImage_Base::~SkImage_Base() { |
| 30 |
if (fAddedToRasterCache.load()) { |
| 31 |
SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
void SkImage_Base::onAsyncRescaleAndReadPixels(const SkImageInfo& info, |
| 36 |
SkIRect origSrcRect, |
| 37 |
RescaleGamma rescaleGamma, |
| 38 |
RescaleMode rescaleMode, |
| 39 |
ReadPixelsCallback callback, |
| 40 |
ReadPixelsContext context) const { |
| 41 |
SkBitmap src; |
| 42 |
SkPixmap peek; |
| 43 |
SkIRect srcRect; |
| 44 |
if (this->peekPixels(&peek)) { |
| 45 |
src.installPixels(peek); |
| 46 |
srcRect = origSrcRect; |
| 47 |
} else { |
| 48 |
// Context TODO: Elevate GrDirectContext requirement to public API. |
| 49 |
auto dContext = as_IB(this)->directContext(); |
| 50 |
src.setInfo(this->imageInfo().makeDimensions(origSrcRect.size())); |
| 51 |
src.allocPixels(); |
| 52 |
if (!this->readPixels(dContext, src.pixmap(), origSrcRect.x(), origSrcRect.y())) { |
| 53 |
callback(context, nullptr); |
| 54 |
return; |
| 55 |
} |
| 56 |
srcRect = SkIRect::MakeSize(src.dimensions()); |
| 57 |
} |
| 58 |
return SkRescaleAndReadPixels(src, info, srcRect, rescaleGamma, rescaleMode, callback, context); |
| 59 |
} |
| 60 |
|
| 61 |
bool SkImage_Base::onAsLegacyBitmap(GrDirectContext* dContext, SkBitmap* bitmap) const { |
| 62 |
// As the base-class, all we can do is make a copy (regardless of mode). |
| 63 |
// Subclasses that want to be more optimal should override. |
| 64 |
SkImageInfo info = fInfo.makeColorType(kN32_SkColorType).makeColorSpace(nullptr); |
| 65 |
if (!bitmap->tryAllocPixels(info)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
if (!this->readPixels( |
| 70 |
dContext, bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
| 71 |
bitmap->reset(); |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
bitmap->setImmutable(); |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
sk_sp<SkImage> SkImage_Base::makeSubset(SkRecorder* recorder, |
| 80 |
const SkIRect& subset, |
| 81 |
RequiredProperties requiredProps) const { |
| 82 |
if (subset.isEmpty()) { |
| 83 |
return nullptr; |
| 84 |
} |
| 85 |
|
| 86 |
const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 87 |
if (!bounds.contains(subset)) { |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
return this->onMakeSubset(recorder, subset, requiredProps); |
| 92 |
} |
| 93 |
|
| 94 |
void SkImage_Base::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, |
| 95 |
bool readAlpha, |
| 96 |
sk_sp<SkColorSpace> dstColorSpace, |
| 97 |
SkIRect srcRect, |
| 98 |
SkISize dstSize, |
| 99 |
RescaleGamma, |
| 100 |
RescaleMode, |
| 101 |
ReadPixelsCallback callback, |
| 102 |
ReadPixelsContext context) const { |
| 103 |
// TODO: Call non-YUV asyncRescaleAndReadPixels and then make our callback convert to YUV and |
| 104 |
// call client's callback. |
| 105 |
callback(context, nullptr); |
| 106 |
} |
| 107 |
|
| 108 |
sk_sp<SkImage> SkImage_Base::makeColorSpace(SkRecorder* recorder, |
| 109 |
sk_sp<SkColorSpace> target, |
| 110 |
RequiredProperties props) const { |
| 111 |
return this->makeColorTypeAndColorSpace(recorder, this->colorType(), std::move(target), props); |
| 112 |
} |