blob: ebb38d47312517242d5dc75504e9ecb23a750d53 [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"
17#endif
18
bungemanf3c15b72015-08-19 11:56:48 -070019#include <new>
20
Brian Salomon514baff2016-11-17 15:17:07 -050021class GrSamplerParams;
bsalomon1cf6f9b2015-12-08 10:53:43 -080022class SkImageCacherator;
bsalomonafa95e22015-10-12 10:39:46 -070023
reed80c772b2015-07-30 18:58:23 -070024enum {
25 kNeedNewImageUniqueID = 0
26};
27
reed@google.com58b21ec2012-07-30 18:20:12 +000028class SkImage_Base : public SkImage {
29public:
reedaf3fbfc2015-10-04 11:28:36 -070030 SkImage_Base(int width, int height, uint32_t uniqueID);
fmalita3b0d5322015-09-18 08:07:31 -070031 virtual ~SkImage_Base();
reed4af267b2014-11-21 08:46:37 -080032
herba7c9d632016-04-19 12:30:22 -070033 // User: returns image info for this SkImage.
34 // Implementors: if you can not return the value, return an invalid ImageInfo with w=0 & h=0
35 // & unknown color space.
36 virtual SkImageInfo onImageInfo() const = 0;
brianosman69c166d2016-08-17 14:01:05 -070037 virtual SkAlphaType onAlphaType() const = 0;
herba7c9d632016-04-19 12:30:22 -070038
reed6ceeebd2016-03-09 14:26:26 -080039 virtual bool onPeekPixels(SkPixmap*) const { return false; }
reed@google.com4f7c6152014-02-06 14:11:56 +000040
reedf1ac1822016-08-01 11:24:14 -070041 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
42
reed96472de2014-12-10 09:53:42 -080043 // Default impl calls onDraw
44 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080045 int srcX, int srcY, CachingHint) const;
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
reed2d5b7142016-08-17 11:12:33 -070049 virtual sk_sp<GrTexture> refPinnedTexture(uint32_t* uniqueID) const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070050#endif
bsalomon1cf6f9b2015-12-08 10:53:43 -080051 virtual SkImageCacherator* peekCacherator() const { return nullptr; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000052
reed@google.com4b0757b2013-05-20 16:33:41 +000053 // return a read-only copy of the pixels. We promise to not modify them,
54 // but only inspect them (or encode them).
Brian Osman61624f02016-12-09 14:51:59 -050055 virtual bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050056 CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070057
58 // Caller must call unref when they are done.
Brian Osman61624f02016-12-09 14:51:59 -050059 virtual GrTexture* asTextureRef(GrContext*, const GrSamplerParams&, SkColorSpace*,
60 sk_sp<SkColorSpace>*) const = 0;
reed@google.com58b21ec2012-07-30 18:20:12 +000061
reed7fb4f8b2016-03-11 04:33:52 -080062 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
reed856e9d92015-09-30 12:21:45 -070063
reed05dd2512016-01-05 09:16:19 -080064 // If a ctx is specified, then only gpu-specific formats are requested.
65 virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
reedf803da12015-01-23 05:58:07 -080066
reed3c065112015-07-08 12:46:22 -070067 virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
68
fmalitaddbbdda2015-08-20 08:47:26 -070069 virtual bool onIsLazyGenerated() const { return false; }
70
fmalita3b0d5322015-09-18 08:07:31 -070071 // Call when this image is part of the key to a resourcecache entry. This allows the cache
72 // to know automatically those entries can be purged when this SkImage deleted.
73 void notifyAddedToCache() const {
74 fAddedToCache.store(true);
75 }
76
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -050077 virtual bool onPinAsTexture(GrContext*) const { return false; }
reed2d5b7142016-08-17 11:12:33 -070078 virtual void onUnpinAsTexture(GrContext*) const {}
79
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080private:
fmalita3b0d5322015-09-18 08:07:31 -070081 // Set true by caches when they cache content that's derived from the current pixels.
82 mutable SkAtomic<bool> fAddedToCache;
83
reed@google.com58b21ec2012-07-30 18:20:12 +000084 typedef SkImage INHERITED;
85};
86
reed6ceeebd2016-03-09 14:26:26 -080087static inline SkImage_Base* as_IB(SkImage* image) {
reed4af267b2014-11-21 08:46:37 -080088 return static_cast<SkImage_Base*>(image);
89}
90
reed9ce9d672016-03-17 10:51:11 -070091static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
92 return static_cast<SkImage_Base*>(image.get());
93}
94
reed4af267b2014-11-21 08:46:37 -080095static inline const SkImage_Base* as_IB(const SkImage* image) {
96 return static_cast<const SkImage_Base*>(image);
97}
98
reed@google.com58b21ec2012-07-30 18:20:12 +000099#endif