blob: 42493b2351d3d4d801fd18743c90ac79c2adf70d [file] [log] [blame]
reed@google.com58b21ec2012-07-30 18:20:12 +00001/*
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
herbb906daf2015-09-29 09:37:59 -070011#include "SkAtomics.h"
reed@google.com58b21ec2012-07-30 18:20:12 +000012#include "SkImage.h"
reed4af267b2014-11-21 08:46:37 -080013#include "SkSurface.h"
14
reed0e7bddf2016-08-17 13:24:05 -070015#if SK_SUPPORT_GPU
16 #include "GrTexture.h"
Robert Phillips6de99042017-01-31 11:31:39 -050017 #include "GrTextureProxy.h"
reed0e7bddf2016-08-17 13:24:05 -070018#endif
19
bungemanf3c15b72015-08-19 11:56:48 -070020#include <new>
21
Brian Salomon514baff2016-11-17 15:17:07 -050022class GrSamplerParams;
bsalomon1cf6f9b2015-12-08 10:53:43 -080023class SkImageCacherator;
bsalomonafa95e22015-10-12 10:39:46 -070024
reed80c772b2015-07-30 18:58:23 -070025enum {
26 kNeedNewImageUniqueID = 0
27};
28
reed@google.com58b21ec2012-07-30 18:20:12 +000029class SkImage_Base : public SkImage {
30public:
reedaf3fbfc2015-10-04 11:28:36 -070031 SkImage_Base(int width, int height, uint32_t uniqueID);
fmalita3b0d5322015-09-18 08:07:31 -070032 virtual ~SkImage_Base();
reed4af267b2014-11-21 08:46:37 -080033
herba7c9d632016-04-19 12:30:22 -070034 // 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;
brianosman69c166d2016-08-17 14:01:05 -070038 virtual SkAlphaType onAlphaType() const = 0;
herba7c9d632016-04-19 12:30:22 -070039
reed6ceeebd2016-03-09 14:26:26 -080040 virtual bool onPeekPixels(SkPixmap*) const { return false; }
reed@google.com4f7c6152014-02-06 14:11:56 +000041
reedf1ac1822016-08-01 11:24:14 -070042 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
43
reed96472de2014-12-10 09:53:42 -080044 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
Matt Sarette2609592017-01-10 09:29:04 -050045 int srcX, int srcY, CachingHint) const = 0;
reed86e90fa2015-09-01 12:22:32 -070046
reed85d91782015-09-10 14:33:38 -070047 virtual GrTexture* peekTexture() const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070048#if SK_SUPPORT_GPU
Robert Phillips6de99042017-01-31 11:31:39 -050049 virtual sk_sp<GrTextureProxy> asTextureProxyRef() const { return nullptr; }
reed2d5b7142016-08-17 11:12:33 -070050 virtual sk_sp<GrTexture> refPinnedTexture(uint32_t* uniqueID) const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070051#endif
bsalomon1cf6f9b2015-12-08 10:53:43 -080052 virtual SkImageCacherator* peekCacherator() const { return nullptr; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000053
reed@google.com4b0757b2013-05-20 16:33:41 +000054 // return a read-only copy of the pixels. We promise to not modify them,
55 // but only inspect them (or encode them).
Brian Osman61624f02016-12-09 14:51:59 -050056 virtual bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050057 CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070058
59 // Caller must call unref when they are done.
Brian Osman61624f02016-12-09 14:51:59 -050060 virtual GrTexture* asTextureRef(GrContext*, const GrSamplerParams&, SkColorSpace*,
Robert Phillips67c18d62017-01-20 12:44:06 -050061 sk_sp<SkColorSpace>*, SkScalar scaleAdjust[2]) const = 0;
reed@google.com58b21ec2012-07-30 18:20:12 +000062
reed7fb4f8b2016-03-11 04:33:52 -080063 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
reed856e9d92015-09-30 12:21:45 -070064
reed05dd2512016-01-05 09:16:19 -080065 // If a ctx is specified, then only gpu-specific formats are requested.
66 virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
reedf803da12015-01-23 05:58:07 -080067
reed3c065112015-07-08 12:46:22 -070068 virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
69
fmalitaddbbdda2015-08-20 08:47:26 -070070 virtual bool onIsLazyGenerated() const { return false; }
71
fmalita3b0d5322015-09-18 08:07:31 -070072 // Call when this image is part of the key to a resourcecache entry. This allows the cache
73 // to know automatically those entries can be purged when this SkImage deleted.
74 void notifyAddedToCache() const {
75 fAddedToCache.store(true);
76 }
77
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -050078 virtual bool onPinAsTexture(GrContext*) const { return false; }
reed2d5b7142016-08-17 11:12:33 -070079 virtual void onUnpinAsTexture(GrContext*) const {}
80
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081private:
fmalita3b0d5322015-09-18 08:07:31 -070082 // Set true by caches when they cache content that's derived from the current pixels.
83 mutable SkAtomic<bool> fAddedToCache;
84
reed@google.com58b21ec2012-07-30 18:20:12 +000085 typedef SkImage INHERITED;
86};
87
reed6ceeebd2016-03-09 14:26:26 -080088static inline SkImage_Base* as_IB(SkImage* image) {
reed4af267b2014-11-21 08:46:37 -080089 return static_cast<SkImage_Base*>(image);
90}
91
reed9ce9d672016-03-17 10:51:11 -070092static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
93 return static_cast<SkImage_Base*>(image.get());
94}
95
reed4af267b2014-11-21 08:46:37 -080096static inline const SkImage_Base* as_IB(const SkImage* image) {
97 return static_cast<const SkImage_Base*>(image);
98}
99
reed@google.com58b21ec2012-07-30 18:20:12 +0000100#endif