blob: a1da0fa559f2f9f4bfdceb3486a086984017352b [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;
37
reed6ceeebd2016-03-09 14:26:26 -080038 virtual bool onPeekPixels(SkPixmap*) const { return false; }
reed@google.com4f7c6152014-02-06 14:11:56 +000039
reedf1ac1822016-08-01 11:24:14 -070040 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
41
reed96472de2014-12-10 09:53:42 -080042 // Default impl calls onDraw
43 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080044 int srcX, int srcY, CachingHint) const;
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).
reed09553032015-11-23 12:32:16 -080054 virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070055
reede8f30622016-03-23 18:59:25 -070056 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo& info) const {
57 return SkSurface::MakeRaster(info);
reed88d064d2015-10-12 11:30:02 -070058 }
59
reed746f31f2015-09-16 12:53:29 -070060 // Caller must call unref when they are done.
brianosman982eb7f2016-06-06 13:10:58 -070061 virtual GrTexture* asTextureRef(GrContext*, const GrTextureParams&,
62 SkSourceGammaTreatment) const = 0;
reed@google.com58b21ec2012-07-30 18:20:12 +000063
reed7fb4f8b2016-03-11 04:33:52 -080064 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
reed856e9d92015-09-30 12:21:45 -070065
reed05dd2512016-01-05 09:16:19 -080066 // If a ctx is specified, then only gpu-specific formats are requested.
67 virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
reedf803da12015-01-23 05:58:07 -080068
reed3c065112015-07-08 12:46:22 -070069 virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
70
fmalitaddbbdda2015-08-20 08:47:26 -070071 virtual bool onIsLazyGenerated() const { return false; }
72
fmalita3b0d5322015-09-18 08:07:31 -070073 // Call when this image is part of the key to a resourcecache entry. This allows the cache
74 // to know automatically those entries can be purged when this SkImage deleted.
75 void notifyAddedToCache() const {
76 fAddedToCache.store(true);
77 }
78
reed2d5b7142016-08-17 11:12:33 -070079 virtual void onPinAsTexture(GrContext*) const {}
80 virtual void onUnpinAsTexture(GrContext*) const {}
81
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082private:
fmalita3b0d5322015-09-18 08:07:31 -070083 // Set true by caches when they cache content that's derived from the current pixels.
84 mutable SkAtomic<bool> fAddedToCache;
85
reed@google.com58b21ec2012-07-30 18:20:12 +000086 typedef SkImage INHERITED;
87};
88
reed6ceeebd2016-03-09 14:26:26 -080089static inline SkImage_Base* as_IB(SkImage* image) {
reed4af267b2014-11-21 08:46:37 -080090 return static_cast<SkImage_Base*>(image);
91}
92
reed9ce9d672016-03-17 10:51:11 -070093static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
94 return static_cast<SkImage_Base*>(image.get());
95}
96
reed4af267b2014-11-21 08:46:37 -080097static inline const SkImage_Base* as_IB(const SkImage* image) {
98 return static_cast<const SkImage_Base*>(image);
99}
100
reed@google.com58b21ec2012-07-30 18:20:12 +0000101#endif