blob: 9e2efe98b5bd8c6a92ac5f2ae1478172ec44d055 [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
bungemanf3c15b72015-08-19 11:56:48 -070015#include <new>
16
bsalomonafa95e22015-10-12 10:39:46 -070017class GrTextureParams;
bsalomon1cf6f9b2015-12-08 10:53:43 -080018class SkImageCacherator;
bsalomonafa95e22015-10-12 10:39:46 -070019
reed80c772b2015-07-30 18:58:23 -070020enum {
21 kNeedNewImageUniqueID = 0
22};
23
reed@google.com58b21ec2012-07-30 18:20:12 +000024class SkImage_Base : public SkImage {
25public:
reedaf3fbfc2015-10-04 11:28:36 -070026 SkImage_Base(int width, int height, uint32_t uniqueID);
fmalita3b0d5322015-09-18 08:07:31 -070027 virtual ~SkImage_Base();
reed4af267b2014-11-21 08:46:37 -080028
reed6ceeebd2016-03-09 14:26:26 -080029 virtual bool onPeekPixels(SkPixmap*) const { return false; }
reed@google.com4f7c6152014-02-06 14:11:56 +000030
reed96472de2014-12-10 09:53:42 -080031 // Default impl calls onDraw
32 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080033 int srcX, int srcY, CachingHint) const;
reed86e90fa2015-09-01 12:22:32 -070034
reed85d91782015-09-10 14:33:38 -070035 virtual GrTexture* peekTexture() const { return nullptr; }
bsalomon1cf6f9b2015-12-08 10:53:43 -080036 virtual SkImageCacherator* peekCacherator() const { return nullptr; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000037
reed@google.com4b0757b2013-05-20 16:33:41 +000038 // return a read-only copy of the pixels. We promise to not modify them,
39 // but only inspect them (or encode them).
reed09553032015-11-23 12:32:16 -080040 virtual bool getROPixels(SkBitmap*, CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070041
reed88d064d2015-10-12 11:30:02 -070042 virtual SkSurface* onNewSurface(const SkImageInfo& info) const {
43 return SkSurface::NewRaster(info);
44 }
45
reed746f31f2015-09-16 12:53:29 -070046 // Caller must call unref when they are done.
bsalomonafa95e22015-10-12 10:39:46 -070047 virtual GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const = 0;
reed@google.com58b21ec2012-07-30 18:20:12 +000048
reed476506d2015-09-28 10:26:51 -070049 virtual SkImage* onNewSubset(const SkIRect&) const = 0;
reed856e9d92015-09-30 12:21:45 -070050
reed05dd2512016-01-05 09:16:19 -080051 // If a ctx is specified, then only gpu-specific formats are requested.
52 virtual SkData* onRefEncoded(GrContext*) const { return nullptr; }
reedf803da12015-01-23 05:58:07 -080053
reed3c065112015-07-08 12:46:22 -070054 virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
55
fmalitaddbbdda2015-08-20 08:47:26 -070056 virtual bool onIsLazyGenerated() const { return false; }
57
reed262a71b2015-12-05 13:07:27 -080058 // Return a bitmap suitable for passing to image-filters
59 // For now, that means wrapping textures into SkGrPixelRefs...
60 virtual bool asBitmapForImageFilters(SkBitmap* bitmap) const {
61 return this->getROPixels(bitmap, kAllow_CachingHint);
62 }
63
fmalita3b0d5322015-09-18 08:07:31 -070064 // Call when this image is part of the key to a resourcecache entry. This allows the cache
65 // to know automatically those entries can be purged when this SkImage deleted.
66 void notifyAddedToCache() const {
67 fAddedToCache.store(true);
68 }
69
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070private:
fmalita3b0d5322015-09-18 08:07:31 -070071 // Set true by caches when they cache content that's derived from the current pixels.
72 mutable SkAtomic<bool> fAddedToCache;
73
reed@google.com58b21ec2012-07-30 18:20:12 +000074 typedef SkImage INHERITED;
75};
76
reed6ceeebd2016-03-09 14:26:26 -080077static inline SkImage_Base* as_IB(SkImage* image) {
reed4af267b2014-11-21 08:46:37 -080078 return static_cast<SkImage_Base*>(image);
79}
80
81static inline const SkImage_Base* as_IB(const SkImage* image) {
82 return static_cast<const SkImage_Base*>(image);
83}
84
reed@google.com58b21ec2012-07-30 18:20:12 +000085#endif