| 1 |
/* |
| 2 |
* Copyright 2018 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 |
#ifndef SkImage_Lazy_DEFINED |
| 9 |
#define SkImage_Lazy_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkImageGenerator.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkTypes.h" |
| 17 |
#include "include/core/SkYUVAPixmaps.h" |
| 18 |
#include "include/private/SkIDChangeListener.h" |
| 19 |
#include "include/private/base/SkMutex.h" |
| 20 |
#include "src/image/SkImage_Base.h" |
| 21 |
|
| 22 |
#include <cstddef> |
| 23 |
#include <cstdint> |
| 24 |
#include <memory> |
| 25 |
|
| 26 |
class GrDirectContext; |
| 27 |
class SharedGenerator; |
| 28 |
class SkBitmap; |
| 29 |
class SkCachedData; |
| 30 |
class SkData; |
| 31 |
class SkPixmap; |
| 32 |
class SkRecorder; |
| 33 |
class SkSurface; |
| 34 |
enum SkColorType : int; |
| 35 |
struct SkIRect; |
| 36 |
|
| 37 |
class SkImage_Lazy : public SkImage_Base { |
| 38 |
public: |
| 39 |
struct Validator { |
| 40 |
Validator(sk_sp<SharedGenerator>, const SkColorType*, sk_sp<SkColorSpace>); |
| 41 |
|
| 42 |
explicit operator bool() const { return fSharedGenerator.get(); } |
| 43 |
|
| 44 |
sk_sp<SharedGenerator> fSharedGenerator; |
| 45 |
SkImageInfo fInfo; |
| 46 |
sk_sp<SkColorSpace> fColorSpace; |
| 47 |
uint32_t fUniqueID; |
| 48 |
}; |
| 49 |
|
| 50 |
explicit SkImage_Lazy(Validator* validator); |
| 51 |
|
| 52 |
// From SkImage.h |
| 53 |
bool isValid(SkRecorder*) const override; |
| 54 |
sk_sp<SkImage> makeColorTypeAndColorSpace(SkRecorder*, |
| 55 |
SkColorType targetColorType, |
| 56 |
sk_sp<SkColorSpace> targetColorSpace, |
| 57 |
RequiredProperties) const override; |
| 58 |
|
| 59 |
// From SkImage_Base.h |
| 60 |
bool onHasMipmaps() const override { |
| 61 |
// TODO: Should we defer to the generator? The generator interface currently doesn't have |
| 62 |
// a way to provide content for levels other than via SkImageGenerator::generateTexture(). |
| 63 |
return false; |
| 64 |
} |
| 65 |
bool onIsProtected() const override; |
| 66 |
|
| 67 |
bool onReadPixels(GrDirectContext*, const SkImageInfo&, void*, size_t, int srcX, int srcY, |
| 68 |
CachingHint) const override; |
| 69 |
sk_sp<const SkData> onRefEncoded() const override; |
| 70 |
|
| 71 |
sk_sp<SkImage> onMakeSubset(SkRecorder*, const SkIRect&, RequiredProperties) const override; |
| 72 |
|
| 73 |
sk_sp<SkSurface> onMakeSurface(SkRecorder*, const SkImageInfo&) const override; |
| 74 |
|
| 75 |
bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint) const override; |
| 76 |
SkImage_Base::Type type() const override { return SkImage_Base::Type::kLazy; } |
| 77 |
|
| 78 |
sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final; |
| 79 |
|
| 80 |
void addUniqueIDListener(sk_sp<SkIDChangeListener>) const; |
| 81 |
sk_sp<SkCachedData> getPlanes(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes, |
| 82 |
SkYUVAPixmaps* pixmaps) const; |
| 83 |
|
| 84 |
// Be careful with this. You need to acquire the mutex, as the generator might be shared |
| 85 |
// among several images. |
| 86 |
sk_sp<SharedGenerator> generator() const; |
| 87 |
|
| 88 |
protected: |
| 89 |
virtual bool readPixelsProxy(GrDirectContext*, const SkPixmap&) const { return false; } |
| 90 |
|
| 91 |
private: |
| 92 |
|
| 93 |
class ScopedGenerator; |
| 94 |
|
| 95 |
// Note that this->imageInfo() is not necessarily the info from the generator. It may be |
| 96 |
// cropped by onMakeSubset and its color type/space may be changed by |
| 97 |
// onMakeColorTypeAndColorSpace. |
| 98 |
sk_sp<SharedGenerator> fSharedGenerator; |
| 99 |
|
| 100 |
// Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs |
| 101 |
// and SkImage_Lazy instances. Cache the result of the last successful call. |
| 102 |
mutable SkMutex fOnMakeColorTypeAndSpaceMutex; |
| 103 |
mutable sk_sp<SkImage> fOnMakeColorTypeAndSpaceResult; |
| 104 |
// When the SkImage_Lazy goes away, we will iterate over all the listeners to inform them |
| 105 |
// of the unique ID's demise. This is used to remove cached textures from GrContext. |
| 106 |
mutable SkIDChangeListener::List fUniqueIDListeners; |
| 107 |
}; |
| 108 |
|
| 109 |
// Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images |
| 110 |
class SharedGenerator final : public SkNVRefCnt<SharedGenerator> { |
| 111 |
public: |
| 112 |
static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen); |
| 113 |
|
| 114 |
// This is thread safe. It is a const field set in the constructor. |
| 115 |
const SkImageInfo& getInfo() const; |
| 116 |
|
| 117 |
bool isTextureGenerator(); |
| 118 |
|
| 119 |
std::unique_ptr<SkImageGenerator> fGenerator; |
| 120 |
SkMutex fMutex; |
| 121 |
|
| 122 |
private: |
| 123 |
explicit SharedGenerator(std::unique_ptr<SkImageGenerator> gen); |
| 124 |
}; |
| 125 |
|
| 126 |
#endif |