Add new SkSourceGammaTreatment enum, used in situations like mipmap construction...
[platform/upstream/libSkiaSharp.git] / src / image / SkImage_Base.h
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 #ifndef SkImage_Base_DEFINED
9 #define SkImage_Base_DEFINED
10
11 #include "SkAtomics.h"
12 #include "SkImage.h"
13 #include "SkSurface.h"
14
15 #include <new>
16
17 class GrTextureParams;
18 class SkImageCacherator;
19
20 enum {
21     kNeedNewImageUniqueID = 0
22 };
23
24 class SkImage_Base : public SkImage {
25 public:
26     SkImage_Base(int width, int height, uint32_t uniqueID);
27     virtual ~SkImage_Base();
28
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;
33
34     virtual bool onPeekPixels(SkPixmap*) const { return false; }
35
36     // Default impl calls onDraw
37     virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
38                               int srcX, int srcY, CachingHint) const;
39
40     virtual GrTexture* peekTexture() const { return nullptr; }
41     virtual SkImageCacherator* peekCacherator() const { return nullptr; }
42
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;
46
47     virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo& info) const {
48         return SkSurface::MakeRaster(info);
49     }
50
51     // Caller must call unref when they are done.
52     virtual GrTexture* asTextureRef(GrContext*, const GrTextureParams&,
53                                     SkSourceGammaTreatment) const = 0;
54
55     virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
56
57     // If a ctx is specified, then only gpu-specific formats are requested.
58     virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
59
60     virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
61
62     virtual bool onIsLazyGenerated() const { return false; }
63
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);
68     }
69
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);
74     }
75
76 private:
77     // Set true by caches when they cache content that's derived from the current pixels.
78     mutable SkAtomic<bool> fAddedToCache;
79
80     typedef SkImage INHERITED;
81 };
82
83 static inline SkImage_Base* as_IB(SkImage* image) {
84     return static_cast<SkImage_Base*>(image);
85 }
86
87 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
88     return static_cast<SkImage_Base*>(image.get());
89 }
90
91 static inline const SkImage_Base* as_IB(const SkImage* image) {
92     return static_cast<const SkImage_Base*>(image);
93 }
94
95 #endif