blob: e839a05cd211ddf1d24b5e6767ff6bd027a5ec3f [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 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
Matt Sarette2609592017-01-10 09:29:04 -050044 int srcX, int srcY, CachingHint) const = 0;
reed86e90fa2015-09-01 12:22:32 -070045
reed85d91782015-09-10 14:33:38 -070046 virtual GrTexture* peekTexture() const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070047#if SK_SUPPORT_GPU
reed2d5b7142016-08-17 11:12:33 -070048 virtual sk_sp<GrTexture> refPinnedTexture(uint32_t* uniqueID) const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070049#endif
bsalomon1cf6f9b2015-12-08 10:53:43 -080050 virtual SkImageCacherator* peekCacherator() const { return nullptr; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000051
reed@google.com4b0757b2013-05-20 16:33:41 +000052 // return a read-only copy of the pixels. We promise to not modify them,
53 // but only inspect them (or encode them).
Brian Osman61624f02016-12-09 14:51:59 -050054 virtual bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050055 CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070056
57 // Caller must call unref when they are done.
Brian Osman61624f02016-12-09 14:51:59 -050058 virtual GrTexture* asTextureRef(GrContext*, const GrSamplerParams&, SkColorSpace*,
59 sk_sp<SkColorSpace>*) const = 0;
reed@google.com58b21ec2012-07-30 18:20:12 +000060
reed7fb4f8b2016-03-11 04:33:52 -080061 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
reed856e9d92015-09-30 12:21:45 -070062
reed05dd2512016-01-05 09:16:19 -080063 // If a ctx is specified, then only gpu-specific formats are requested.
64 virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
reedf803da12015-01-23 05:58:07 -080065
reed3c065112015-07-08 12:46:22 -070066 virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
67
fmalitaddbbdda2015-08-20 08:47:26 -070068 virtual bool onIsLazyGenerated() const { return false; }
69
fmalita3b0d5322015-09-18 08:07:31 -070070 // 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
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -050076 virtual bool onPinAsTexture(GrContext*) const { return false; }
reed2d5b7142016-08-17 11:12:33 -070077 virtual void onUnpinAsTexture(GrContext*) const {}
78
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079private:
fmalita3b0d5322015-09-18 08:07:31 -070080 // Set true by caches when they cache content that's derived from the current pixels.
81 mutable SkAtomic<bool> fAddedToCache;
82
reed@google.com58b21ec2012-07-30 18:20:12 +000083 typedef SkImage INHERITED;
84};
85
reed6ceeebd2016-03-09 14:26:26 -080086static inline SkImage_Base* as_IB(SkImage* image) {
reed4af267b2014-11-21 08:46:37 -080087 return static_cast<SkImage_Base*>(image);
88}
89
reed9ce9d672016-03-17 10:51:11 -070090static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
91 return static_cast<SkImage_Base*>(image.get());
92}
93
reed4af267b2014-11-21 08:46:37 -080094static inline const SkImage_Base* as_IB(const SkImage* image) {
95 return static_cast<const SkImage_Base*>(image);
96}
97
reed@google.com58b21ec2012-07-30 18:20:12 +000098#endif