blob: 33f3caabff78697ab36bfcda908b0b3b6669866b [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
11#include "SkImage.h"
reed4af267b2014-11-21 08:46:37 -080012#include "SkSurface.h"
Mike Klein015c8992018-08-09 12:23:19 -040013#include <atomic>
reed4af267b2014-11-21 08:46:37 -080014
reed0e7bddf2016-08-17 13:24:05 -070015#if SK_SUPPORT_GPU
Robert Phillips6de99042017-01-31 11:31:39 -050016 #include "GrTextureProxy.h"
Robert Phillips646e4292017-06-13 12:44:56 -040017
18 class GrTexture;
reed0e7bddf2016-08-17 13:24:05 -070019#endif
20
bungemanf3c15b72015-08-19 11:56:48 -070021#include <new>
22
Brian Salomon2bbdcc42017-09-07 12:36:34 -040023class GrSamplerState;
bsalomon1cf6f9b2015-12-08 10:53:43 -080024class SkImageCacherator;
bsalomonafa95e22015-10-12 10:39:46 -070025
reed80c772b2015-07-30 18:58:23 -070026enum {
27 kNeedNewImageUniqueID = 0
28};
29
reed@google.com58b21ec2012-07-30 18:20:12 +000030class SkImage_Base : public SkImage {
31public:
fmalita3b0d5322015-09-18 08:07:31 -070032 virtual ~SkImage_Base();
reed4af267b2014-11-21 08:46:37 -080033
herba7c9d632016-04-19 12:30:22 -070034 // User: returns image info for this SkImage.
35 // Implementors: if you can not return the value, return an invalid ImageInfo with w=0 & h=0
36 // & unknown color space.
37 virtual SkImageInfo onImageInfo() const = 0;
Greg Daniel56008aa2018-03-14 15:33:42 -040038 virtual SkColorType onColorType() const = 0;
brianosman69c166d2016-08-17 14:01:05 -070039 virtual SkAlphaType onAlphaType() const = 0;
herba7c9d632016-04-19 12:30:22 -070040
Mike Reedf2c73642018-05-29 15:41:27 -040041 virtual SkIRect onGetSubset() const {
42 return { 0, 0, this->width(), this->height() };
43 }
44
reed6ceeebd2016-03-09 14:26:26 -080045 virtual bool onPeekPixels(SkPixmap*) const { return false; }
reed@google.com4f7c6152014-02-06 14:11:56 +000046
reedf1ac1822016-08-01 11:24:14 -070047 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
48
reed96472de2014-12-10 09:53:42 -080049 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
Matt Sarette2609592017-01-10 09:29:04 -050050 int srcX, int srcY, CachingHint) const = 0;
reed86e90fa2015-09-01 12:22:32 -070051
Robert Phillips87444052017-06-23 14:09:30 -040052 virtual GrContext* context() const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070053#if SK_SUPPORT_GPU
Robert Phillips0ae6faa2017-03-21 16:22:00 -040054 virtual GrTextureProxy* peekProxy() const { return nullptr; }
Robert Phillips6de99042017-01-31 11:31:39 -050055 virtual sk_sp<GrTextureProxy> asTextureProxyRef() const { return nullptr; }
Brian Salomon2bbdcc42017-09-07 12:36:34 -040056 virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, const GrSamplerState&,
Robert Phillipsb726d582017-03-09 16:36:32 -050057 SkColorSpace*, sk_sp<SkColorSpace>*,
58 SkScalar scaleAdjust[2]) const = 0;
Robert Phillips3798c862017-03-27 11:08:16 -040059 virtual sk_sp<GrTextureProxy> refPinnedTextureProxy(uint32_t* uniqueID) const {
60 return nullptr;
61 }
Robert Phillipsc5509952018-04-04 15:54:55 -040062
Robert Phillips0ae6faa2017-03-21 16:22:00 -040063 virtual GrTexture* onGetTexture() const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070064#endif
Robert Phillips8caf85f2018-04-05 09:30:38 -040065 virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
66 GrSurfaceOrigin* origin) const;
67
bsalomon1cf6f9b2015-12-08 10:53:43 -080068 virtual SkImageCacherator* peekCacherator() const { return nullptr; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000069
reed@google.com4b0757b2013-05-20 16:33:41 +000070 // return a read-only copy of the pixels. We promise to not modify them,
71 // but only inspect them (or encode them).
Brian Osman61624f02016-12-09 14:51:59 -050072 virtual bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050073 CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070074
reed7fb4f8b2016-03-11 04:33:52 -080075 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
reed856e9d92015-09-30 12:21:45 -070076
Ben Wagnerbdf54332018-05-15 14:12:14 -040077 virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
reedf803da12015-01-23 05:58:07 -080078
Cary Clark4f5a79c2018-02-07 15:51:00 -050079 virtual bool onAsLegacyBitmap(SkBitmap*) const;
reed3c065112015-07-08 12:46:22 -070080
Mike Reed7f1d0202017-05-08 16:13:39 -040081 // True for picture-backed and codec-backed
fmalitaddbbdda2015-08-20 08:47:26 -070082 virtual bool onIsLazyGenerated() const { return false; }
83
fmalita3b0d5322015-09-18 08:07:31 -070084 // Call when this image is part of the key to a resourcecache entry. This allows the cache
85 // to know automatically those entries can be purged when this SkImage deleted.
Mike Reed30301c42018-07-19 09:39:21 -040086 void notifyAddedToRasterCache() const {
87 fAddedToRasterCache.store(true);
fmalita3b0d5322015-09-18 08:07:31 -070088 }
89
Brian Osman5bbd0762017-05-08 11:07:42 -040090 virtual bool onIsValid(GrContext*) const = 0;
91
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -050092 virtual bool onPinAsTexture(GrContext*) const { return false; }
reed2d5b7142016-08-17 11:12:33 -070093 virtual void onUnpinAsTexture(GrContext*) const {}
94
Brian Osmanb62f50c2018-07-12 14:44:27 -040095 virtual sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType) const = 0;
Florin Malitaf7beee72017-05-26 12:54:32 -040096protected:
97 SkImage_Base(int width, int height, uint32_t uniqueID);
Matt Sarett6de13102017-03-14 14:10:48 -040098
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099private:
fmalita3b0d5322015-09-18 08:07:31 -0700100 // Set true by caches when they cache content that's derived from the current pixels.
Mike Klein015c8992018-08-09 12:23:19 -0400101 mutable std::atomic<bool> fAddedToRasterCache;
fmalita3b0d5322015-09-18 08:07:31 -0700102
reed@google.com58b21ec2012-07-30 18:20:12 +0000103 typedef SkImage INHERITED;
104};
105
reed6ceeebd2016-03-09 14:26:26 -0800106static inline SkImage_Base* as_IB(SkImage* image) {
reed4af267b2014-11-21 08:46:37 -0800107 return static_cast<SkImage_Base*>(image);
108}
109
reed9ce9d672016-03-17 10:51:11 -0700110static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
111 return static_cast<SkImage_Base*>(image.get());
112}
113
reed4af267b2014-11-21 08:46:37 -0800114static inline const SkImage_Base* as_IB(const SkImage* image) {
115 return static_cast<const SkImage_Base*>(image);
116}
117
reed@google.com58b21ec2012-07-30 18:20:12 +0000118#endif