• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if SK_SUPPORT_GPU
16     #include "GrTexture.h"
17     #include "GrTextureProxy.h"
18 #endif
19 
20 #include <new>
21 
22 class GrSamplerParams;
23 class SkImageCacherator;
24 
25 enum {
26     kNeedNewImageUniqueID = 0
27 };
28 
29 class SkImage_Base : public SkImage {
30 public:
31     SkImage_Base(int width, int height, uint32_t uniqueID);
32     virtual ~SkImage_Base();
33 
34     // User: returns image info for this SkImage.
35     // Implementors: if you can not return the value, return an invalid ImageInfo with w=0 & h=0
36     // & unknown color space.
37     virtual SkImageInfo onImageInfo() const = 0;
38     virtual SkAlphaType onAlphaType() const = 0;
39 
onPeekPixels(SkPixmap *)40     virtual bool onPeekPixels(SkPixmap*) const { return false; }
41 
onPeekBitmap()42     virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
43 
44     virtual bool onReadYUV8Planes(const SkISize sizes[3], void* const planes[3],
45                                   const size_t rowBytes[3], SkYUVColorSpace colorSpace) const;
46 
47     virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
48                               int srcX, int srcY, CachingHint) const = 0;
49 
50     // MDB TODO: this entry point needs to go away
peekTexture()51     virtual GrTexture* peekTexture() const { return nullptr; }
52 #if SK_SUPPORT_GPU
peekProxy()53     virtual GrTextureProxy* peekProxy() const { return nullptr; }
asTextureProxyRef()54     virtual sk_sp<GrTextureProxy> asTextureProxyRef() const { return nullptr; }
55     virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, const GrSamplerParams&,
56                                                     SkColorSpace*, sk_sp<SkColorSpace>*,
57                                                     SkScalar scaleAdjust[2]) const = 0;
refPinnedTextureProxy(uint32_t * uniqueID)58     virtual sk_sp<GrTextureProxy> refPinnedTextureProxy(uint32_t* uniqueID) const {
59         return nullptr;
60     }
onGetTextureHandle(bool flushPendingGrContextIO,GrSurfaceOrigin * origin)61     virtual GrBackendObject onGetTextureHandle(bool flushPendingGrContextIO,
62                                                GrSurfaceOrigin* origin) const {
63         return 0;
64     }
onGetTexture()65     virtual GrTexture* onGetTexture() const { return nullptr; }
66 #endif
peekCacherator()67     virtual SkImageCacherator* peekCacherator() const { return nullptr; }
68 
69     // return a read-only copy of the pixels. We promise to not modify them,
70     // but only inspect them (or encode them).
71     virtual bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace,
72                              CachingHint = kAllow_CachingHint) const = 0;
73 
74     virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
75 
76     // If a ctx is specified, then only gpu-specific formats are requested.
onRefEncoded(GrContext *)77     virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
78 
79     virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
80 
onIsLazyGenerated()81     virtual bool onIsLazyGenerated() const { return false; }
82 
83     // Call when this image is part of the key to a resourcecache entry. This allows the cache
84     // to know automatically those entries can be purged when this SkImage deleted.
notifyAddedToCache()85     void notifyAddedToCache() const {
86         fAddedToCache.store(true);
87     }
88 
89     // Transforms image into the input color space.
90     sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target) const;
91 
onPinAsTexture(GrContext *)92     virtual bool onPinAsTexture(GrContext*) const { return false; }
onUnpinAsTexture(GrContext *)93     virtual void onUnpinAsTexture(GrContext*) const {}
94 
95 protected:
onMakeColorSpace(sk_sp<SkColorSpace>)96     virtual sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>) const {
97         // TODO: Make this pure virtual.
98         return sk_ref_sp(const_cast<SkImage_Base*>(this));
99     }
100 
101 private:
102     // Set true by caches when they cache content that's derived from the current pixels.
103     mutable SkAtomic<bool> fAddedToCache;
104 
105     typedef SkImage INHERITED;
106 };
107 
as_IB(SkImage * image)108 static inline SkImage_Base* as_IB(SkImage* image) {
109     return static_cast<SkImage_Base*>(image);
110 }
111 
as_IB(const sk_sp<SkImage> & image)112 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
113     return static_cast<SkImage_Base*>(image.get());
114 }
115 
as_IB(const SkImage * image)116 static inline const SkImage_Base* as_IB(const SkImage* image) {
117     return static_cast<const SkImage_Base*>(image);
118 }
119 
120 #endif
121