2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkImage_Base_DEFINED
9 #define SkImage_Base_DEFINED
11 #include "SkAtomics.h"
13 #include "SkSurface.h"
17 class GrTextureParams;
18 class SkImageCacherator;
21 kNeedNewImageUniqueID = 0
24 class SkImage_Base : public SkImage {
26 SkImage_Base(int width, int height, uint32_t uniqueID);
27 virtual ~SkImage_Base();
29 // User: returns image info for this SkImage.
30 // Implementors: if you can not return the value, return an invalid ImageInfo with w=0 & h=0
31 // & unknown color space.
32 virtual SkImageInfo onImageInfo() const = 0;
34 virtual bool onPeekPixels(SkPixmap*) const { return false; }
36 // Default impl calls onDraw
37 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
38 int srcX, int srcY, CachingHint) const;
40 virtual GrTexture* peekTexture() const { return nullptr; }
41 virtual SkImageCacherator* peekCacherator() const { return nullptr; }
43 // return a read-only copy of the pixels. We promise to not modify them,
44 // but only inspect them (or encode them).
45 virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
47 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo& info) const {
48 return SkSurface::MakeRaster(info);
51 // Caller must call unref when they are done.
52 virtual GrTexture* asTextureRef(GrContext*, const GrTextureParams&,
53 SkSourceGammaTreatment) const = 0;
55 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
57 // If a ctx is specified, then only gpu-specific formats are requested.
58 virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
60 virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
62 virtual bool onIsLazyGenerated() const { return false; }
64 // Return a bitmap suitable for passing to image-filters
65 // For now, that means wrapping textures into SkGrPixelRefs...
66 virtual bool asBitmapForImageFilters(SkBitmap* bitmap) const {
67 return this->getROPixels(bitmap, kAllow_CachingHint);
70 // Call when this image is part of the key to a resourcecache entry. This allows the cache
71 // to know automatically those entries can be purged when this SkImage deleted.
72 void notifyAddedToCache() const {
73 fAddedToCache.store(true);
77 // Set true by caches when they cache content that's derived from the current pixels.
78 mutable SkAtomic<bool> fAddedToCache;
80 typedef SkImage INHERITED;
83 static inline SkImage_Base* as_IB(SkImage* image) {
84 return static_cast<SkImage_Base*>(image);
87 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
88 return static_cast<SkImage_Base*>(image.get());
91 static inline const SkImage_Base* as_IB(const SkImage* image) {
92 return static_cast<const SkImage_Base*>(image);