blob: 3a4aabd4b68a14bd593f3c37fc0792dfe8b0203d [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
bsalomonafa95e22015-10-12 10:39:46 -070021class GrTextureParams;
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).
reed09553032015-11-23 12:32:16 -080055 virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070056
57 // Caller must call unref when they are done.
brianosman982eb7f2016-06-06 13:10:58 -070058 virtual GrTexture* asTextureRef(GrContext*, const GrTextureParams&,
Brian Osman7b8400d2016-11-08 17:08:54 -050059 SkDestinationSurfaceColorMode) 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
reed2d5b7142016-08-17 11:12:33 -070076 virtual void onPinAsTexture(GrContext*) const {}
77 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