| 1 |
/* |
| 2 |
* Copyright 2015 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 "src/image/SkImage_Lazy.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCPURecorder.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkData.h" |
| 14 |
#include "include/core/SkImageGenerator.h" |
| 15 |
#include "include/core/SkPixmap.h" |
| 16 |
#include "include/core/SkRecorder.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkSurface.h" // IWYU pragma: keep |
| 19 |
#include "include/core/SkYUVAInfo.h" |
| 20 |
#include "src/core/SkBitmapCache.h" |
| 21 |
#include "src/core/SkCachedData.h" |
| 22 |
#include "src/core/SkNextID.h" |
| 23 |
#include "src/core/SkResourceCache.h" |
| 24 |
#include "src/core/SkYUVPlanesCache.h" |
| 25 |
|
| 26 |
#include <utility> |
| 27 |
|
| 28 |
class SkSurfaceProps; |
| 29 |
|
| 30 |
enum SkColorType : int; |
| 31 |
|
| 32 |
sk_sp<SharedGenerator> SharedGenerator::Make(std::unique_ptr<SkImageGenerator> gen) { |
| 33 |
return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr; |
| 34 |
} |
| 35 |
|
| 36 |
SharedGenerator::SharedGenerator(std::unique_ptr<SkImageGenerator> gen) |
| 37 |
: fGenerator(std::move(gen)) { |
| 38 |
SkASSERT(fGenerator); |
| 39 |
} |
| 40 |
|
| 41 |
const SkImageInfo& SharedGenerator::getInfo() const { return fGenerator->getInfo(); } |
| 42 |
|
| 43 |
bool SharedGenerator::isTextureGenerator() { return fGenerator->isTextureGenerator(); } |
| 44 |
|
| 45 |
/////////////////////////////////////////////////////////////////////////////// |
| 46 |
|
| 47 |
SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkColorType* colorType, |
| 48 |
sk_sp<SkColorSpace> colorSpace) |
| 49 |
: fSharedGenerator(std::move(gen)) { |
| 50 |
if (!fSharedGenerator) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
// The following generator accessors are safe without acquiring the mutex (const getters). |
| 55 |
// TODO: refactor to use a ScopedGenerator instead, for clarity. |
| 56 |
fInfo = fSharedGenerator->fGenerator->getInfo(); |
| 57 |
if (fInfo.isEmpty()) { |
| 58 |
fSharedGenerator.reset(); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
fUniqueID = fSharedGenerator->fGenerator->uniqueID(); |
| 63 |
|
| 64 |
if (colorType && (*colorType == fInfo.colorType())) { |
| 65 |
colorType = nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
if (colorType || colorSpace) { |
| 69 |
if (colorType) { |
| 70 |
fInfo = fInfo.makeColorType(*colorType); |
| 71 |
} |
| 72 |
if (colorSpace) { |
| 73 |
fInfo = fInfo.makeColorSpace(colorSpace); |
| 74 |
} |
| 75 |
fUniqueID = SkNextID::ImageID(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/////////////////////////////////////////////////////////////////////////////// |
| 80 |
|
| 81 |
// Helper for exclusive access to a shared generator. |
| 82 |
class SkImage_Lazy::ScopedGenerator { |
| 83 |
public: |
| 84 |
ScopedGenerator(const sk_sp<SharedGenerator>& gen) |
| 85 |
: fSharedGenerator(gen) |
| 86 |
, fAutoAcquire(gen->fMutex) {} |
| 87 |
|
| 88 |
SkImageGenerator* operator->() const { |
| 89 |
fSharedGenerator->fMutex.assertHeld(); |
| 90 |
return fSharedGenerator->fGenerator.get(); |
| 91 |
} |
| 92 |
|
| 93 |
operator SkImageGenerator*() const { |
| 94 |
fSharedGenerator->fMutex.assertHeld(); |
| 95 |
return fSharedGenerator->fGenerator.get(); |
| 96 |
} |
| 97 |
|
| 98 |
private: |
| 99 |
const sk_sp<SharedGenerator>& fSharedGenerator; |
| 100 |
SkAutoMutexExclusive fAutoAcquire; |
| 101 |
}; |
| 102 |
|
| 103 |
/////////////////////////////////////////////////////////////////////////////// |
| 104 |
|
| 105 |
SkImage_Lazy::SkImage_Lazy(Validator* validator) |
| 106 |
: SkImage_Base(validator->fInfo, validator->fUniqueID) |
| 107 |
, fSharedGenerator(std::move(validator->fSharedGenerator)) |
| 108 |
{ |
| 109 |
SkASSERT(fSharedGenerator); |
| 110 |
} |
| 111 |
|
| 112 |
bool SkImage_Lazy::getROPixels(GrDirectContext* ctx, SkBitmap* bitmap, |
| 113 |
SkImage::CachingHint chint) const { |
| 114 |
auto check_output_bitmap = [bitmap]() { |
| 115 |
SkASSERT(bitmap->isImmutable()); |
| 116 |
SkASSERT(bitmap->getPixels()); |
| 117 |
(void)bitmap; |
| 118 |
}; |
| 119 |
|
| 120 |
auto desc = SkBitmapCacheDesc::Make(this); |
| 121 |
if (SkBitmapCache::Find(desc, bitmap)) { |
| 122 |
check_output_bitmap(); |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
if (SkImage::kAllow_CachingHint == chint) { |
| 127 |
SkPixmap pmap; |
| 128 |
SkBitmapCache::RecPtr cacheRec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap); |
| 129 |
if (!cacheRec) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
bool success = false; |
| 133 |
{ // make sure ScopedGenerator goes out of scope before we try readPixelsProxy |
| 134 |
success = ScopedGenerator(fSharedGenerator)->getPixels(pmap); |
| 135 |
} |
| 136 |
if (!success && !this->readPixelsProxy(ctx, pmap)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
SkBitmapCache::Add(std::move(cacheRec), bitmap); |
| 140 |
this->notifyAddedToRasterCache(); |
| 141 |
} else { |
| 142 |
if (!bitmap->tryAllocPixels(this->imageInfo())) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
bool success = false; |
| 146 |
{ // make sure ScopedGenerator goes out of scope before we try readPixelsProxy |
| 147 |
success = ScopedGenerator(fSharedGenerator)->getPixels(bitmap->pixmap()); |
| 148 |
} |
| 149 |
if (!success && !this->readPixelsProxy(ctx, bitmap->pixmap())) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
bitmap->setImmutable(); |
| 153 |
} |
| 154 |
check_output_bitmap(); |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
sk_sp<SharedGenerator> SkImage_Lazy::generator() const { |
| 159 |
return fSharedGenerator; |
| 160 |
} |
| 161 |
|
| 162 |
bool SkImage_Lazy::onIsProtected() const { |
| 163 |
ScopedGenerator generator(fSharedGenerator); |
| 164 |
return generator->isProtected(); |
| 165 |
} |
| 166 |
|
| 167 |
bool SkImage_Lazy::onReadPixels(GrDirectContext* dContext, |
| 168 |
const SkImageInfo& dstInfo, |
| 169 |
void* dstPixels, |
| 170 |
size_t dstRB, |
| 171 |
int srcX, |
| 172 |
int srcY, |
| 173 |
CachingHint chint) const { |
| 174 |
SkBitmap bm; |
| 175 |
if (this->getROPixels(dContext, &bm, chint)) { |
| 176 |
return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 177 |
} |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
sk_sp<const SkData> SkImage_Lazy::onRefEncoded() const { |
| 182 |
// check that we aren't a subset or colortype/etc modification of the original |
| 183 |
if (fSharedGenerator->fGenerator->uniqueID() == this->uniqueID()) { |
| 184 |
ScopedGenerator generator(fSharedGenerator); |
| 185 |
return generator->refEncodedData(); |
| 186 |
} |
| 187 |
return nullptr; |
| 188 |
} |
| 189 |
|
| 190 |
bool SkImage_Lazy::isValid(SkRecorder* recorder) const { |
| 191 |
ScopedGenerator generator(fSharedGenerator); |
| 192 |
return generator->isValid(recorder); |
| 193 |
} |
| 194 |
|
| 195 |
sk_sp<SkImage> SkImage_Lazy::onMakeSubset(SkRecorder*, |
| 196 |
const SkIRect& subset, |
| 197 |
RequiredProperties props) const { |
| 198 |
// TODO: can we do this more efficiently, by telling the generator we want to |
| 199 |
// "realize" a subset? |
| 200 |
sk_sp<SkImage> nonLazyImg = this->makeRasterImage(nullptr); |
| 201 |
if (!nonLazyImg) { |
| 202 |
return nullptr; |
| 203 |
} |
| 204 |
return nonLazyImg->makeSubset(nullptr, subset, props); |
| 205 |
} |
| 206 |
|
| 207 |
sk_sp<SkSurface> SkImage_Lazy::onMakeSurface(SkRecorder* recorder, const SkImageInfo& info) const { |
| 208 |
if (!recorder) { |
| 209 |
// TODO(kjlubick) remove this after old SkImage::makeScaled(image info, sampling) API gone |
| 210 |
recorder = skcpu::Recorder::TODO(); |
| 211 |
} |
| 212 |
const SkSurfaceProps* props = nullptr; |
| 213 |
constexpr size_t rowBytes = 0; |
| 214 |
return recorder->cpuRecorder()->makeBitmapSurface(info, rowBytes, props); |
| 215 |
} |
| 216 |
|
| 217 |
sk_sp<SkImage> SkImage_Lazy::makeColorTypeAndColorSpace(SkRecorder*, |
| 218 |
SkColorType targetColorType, |
| 219 |
sk_sp<SkColorSpace> targetColorSpace, |
| 220 |
RequiredProperties) const { |
| 221 |
SkAutoMutexExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex); |
| 222 |
if (fOnMakeColorTypeAndSpaceResult && |
| 223 |
targetColorType == fOnMakeColorTypeAndSpaceResult->colorType() && |
| 224 |
SkColorSpace::Equals(targetColorSpace.get(), |
| 225 |
fOnMakeColorTypeAndSpaceResult->colorSpace())) { |
| 226 |
return fOnMakeColorTypeAndSpaceResult; |
| 227 |
} |
| 228 |
Validator validator(fSharedGenerator, &targetColorType, targetColorSpace); |
| 229 |
sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr; |
| 230 |
if (result) { |
| 231 |
fOnMakeColorTypeAndSpaceResult = result; |
| 232 |
} |
| 233 |
return result; |
| 234 |
} |
| 235 |
|
| 236 |
sk_sp<SkImage> SkImage_Lazy::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const { |
| 237 |
// TODO: The correct thing is to clone the generator, and modify its color space. That's hard, |
| 238 |
// because we don't have a clone method, and generator is public (and derived-from by clients). |
| 239 |
// So do the simple/inefficient thing here, and fallback to raster when this is called. |
| 240 |
|
| 241 |
// We allocate the bitmap with the new color space, then generate the image using the original. |
| 242 |
SkBitmap bitmap; |
| 243 |
if (bitmap.tryAllocPixels(this->imageInfo().makeColorSpace(std::move(newCS)))) { |
| 244 |
SkPixmap pixmap = bitmap.pixmap(); |
| 245 |
pixmap.setColorSpace(this->refColorSpace()); |
| 246 |
if (ScopedGenerator(fSharedGenerator)->getPixels(pixmap)) { |
| 247 |
bitmap.setImmutable(); |
| 248 |
return SkImages::RasterFromBitmap(bitmap); |
| 249 |
} |
| 250 |
} |
| 251 |
return nullptr; |
| 252 |
} |
| 253 |
|
| 254 |
sk_sp<SkCachedData> SkImage_Lazy::getPlanes( |
| 255 |
const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes, |
| 256 |
SkYUVAPixmaps* yuvaPixmaps) const { |
| 257 |
ScopedGenerator generator(fSharedGenerator); |
| 258 |
|
| 259 |
sk_sp<SkCachedData> data(SkYUVPlanesCache::FindAndRef(generator->uniqueID(), yuvaPixmaps)); |
| 260 |
|
| 261 |
if (data) { |
| 262 |
SkASSERT(yuvaPixmaps->isValid()); |
| 263 |
SkASSERT(yuvaPixmaps->yuvaInfo().dimensions() == this->dimensions()); |
| 264 |
return data; |
| 265 |
} |
| 266 |
SkYUVAPixmapInfo yuvaPixmapInfo; |
| 267 |
if (!generator->queryYUVAInfo(supportedDataTypes, &yuvaPixmapInfo) || |
| 268 |
yuvaPixmapInfo.yuvaInfo().dimensions() != this->dimensions()) { |
| 269 |
return nullptr; |
| 270 |
} |
| 271 |
data.reset(SkResourceCache::NewCachedData(yuvaPixmapInfo.computeTotalBytes())); |
| 272 |
SkYUVAPixmaps tempPixmaps = SkYUVAPixmaps::FromExternalMemory(yuvaPixmapInfo, |
| 273 |
data->writable_data()); |
| 274 |
SkASSERT(tempPixmaps.isValid()); |
| 275 |
if (!generator->getYUVAPlanes(tempPixmaps)) { |
| 276 |
return nullptr; |
| 277 |
} |
| 278 |
// Decoding is done, cache the resulting YUV planes |
| 279 |
*yuvaPixmaps = tempPixmaps; |
| 280 |
SkYUVPlanesCache::Add(this->uniqueID(), data.get(), *yuvaPixmaps); |
| 281 |
return data; |
| 282 |
} |
| 283 |
|
| 284 |
void SkImage_Lazy::addUniqueIDListener(sk_sp<SkIDChangeListener> listener) const { |
| 285 |
fUniqueIDListeners.add(std::move(listener)); |
| 286 |
} |
| 287 |
|
| 288 |
// TODO(kjlubick) move SharedGenerate to SkImage_Lazy.h and this to SkImage_LazyFactories |
| 289 |
namespace SkImages { |
| 290 |
|
| 291 |
sk_sp<SkImage> DeferredFromGenerator(std::unique_ptr<SkImageGenerator> generator) { |
| 292 |
SkImage_Lazy::Validator validator( |
| 293 |
SharedGenerator::Make(std::move(generator)), nullptr, nullptr); |
| 294 |
|
| 295 |
return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr; |
| 296 |
} |
| 297 |
|
| 298 |
} // namespace SkImages |