blob: e6598f8d06fd9ba8cb7165423ba19ee2a8ad62ba [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
Greg Daniel2ad08202018-09-07 09:13:36 -040016#include "GrTextureProxy.h"
17#include "SkTDArray.h"
Robert Phillips646e4292017-06-13 12:44:56 -040018
Greg Daniel2ad08202018-09-07 09:13:36 -040019class GrTexture;
reed0e7bddf2016-08-17 13:24:05 -070020#endif
21
bungemanf3c15b72015-08-19 11:56:48 -070022#include <new>
23
Brian Salomon2bbdcc42017-09-07 12:36:34 -040024class GrSamplerState;
bsalomon1cf6f9b2015-12-08 10:53:43 -080025class SkImageCacherator;
bsalomonafa95e22015-10-12 10:39:46 -070026
reed80c772b2015-07-30 18:58:23 -070027enum {
28 kNeedNewImageUniqueID = 0
29};
30
reed@google.com58b21ec2012-07-30 18:20:12 +000031class SkImage_Base : public SkImage {
32public:
fmalita3b0d5322015-09-18 08:07:31 -070033 virtual ~SkImage_Base();
reed4af267b2014-11-21 08:46:37 -080034
herba7c9d632016-04-19 12:30:22 -070035 // User: returns image info for this SkImage.
36 // Implementors: if you can not return the value, return an invalid ImageInfo with w=0 & h=0
37 // & unknown color space.
38 virtual SkImageInfo onImageInfo() const = 0;
Greg Daniel56008aa2018-03-14 15:33:42 -040039 virtual SkColorType onColorType() const = 0;
brianosman69c166d2016-08-17 14:01:05 -070040 virtual SkAlphaType onAlphaType() const = 0;
herba7c9d632016-04-19 12:30:22 -070041
Mike Reedf2c73642018-05-29 15:41:27 -040042 virtual SkIRect onGetSubset() const {
43 return { 0, 0, this->width(), this->height() };
44 }
45
reed6ceeebd2016-03-09 14:26:26 -080046 virtual bool onPeekPixels(SkPixmap*) const { return false; }
reed@google.com4f7c6152014-02-06 14:11:56 +000047
reedf1ac1822016-08-01 11:24:14 -070048 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
49
reed96472de2014-12-10 09:53:42 -080050 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
Matt Sarette2609592017-01-10 09:29:04 -050051 int srcX, int srcY, CachingHint) const = 0;
reed86e90fa2015-09-01 12:22:32 -070052
Robert Phillips87444052017-06-23 14:09:30 -040053 virtual GrContext* context() const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070054#if SK_SUPPORT_GPU
Robert Phillips0ae6faa2017-03-21 16:22:00 -040055 virtual GrTextureProxy* peekProxy() const { return nullptr; }
Robert Phillips6de99042017-01-31 11:31:39 -050056 virtual sk_sp<GrTextureProxy> asTextureProxyRef() const { return nullptr; }
Brian Salomon2bbdcc42017-09-07 12:36:34 -040057 virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, const GrSamplerState&,
Robert Phillipsb726d582017-03-09 16:36:32 -050058 SkColorSpace*, sk_sp<SkColorSpace>*,
59 SkScalar scaleAdjust[2]) const = 0;
Robert Phillips3798c862017-03-27 11:08:16 -040060 virtual sk_sp<GrTextureProxy> refPinnedTextureProxy(uint32_t* uniqueID) const {
61 return nullptr;
62 }
Robert Phillipsc5509952018-04-04 15:54:55 -040063
Robert Phillips0ae6faa2017-03-21 16:22:00 -040064 virtual GrTexture* onGetTexture() const { return nullptr; }
reed0e7bddf2016-08-17 13:24:05 -070065#endif
Robert Phillips8caf85f2018-04-05 09:30:38 -040066 virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
67 GrSurfaceOrigin* origin) const;
68
bsalomon1cf6f9b2015-12-08 10:53:43 -080069 virtual SkImageCacherator* peekCacherator() const { return nullptr; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000070
reed@google.com4b0757b2013-05-20 16:33:41 +000071 // return a read-only copy of the pixels. We promise to not modify them,
72 // but only inspect them (or encode them).
Brian Osman61624f02016-12-09 14:51:59 -050073 virtual bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050074 CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070075
reed7fb4f8b2016-03-11 04:33:52 -080076 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
reed856e9d92015-09-30 12:21:45 -070077
Ben Wagnerbdf54332018-05-15 14:12:14 -040078 virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
reedf803da12015-01-23 05:58:07 -080079
Cary Clark4f5a79c2018-02-07 15:51:00 -050080 virtual bool onAsLegacyBitmap(SkBitmap*) const;
reed3c065112015-07-08 12:46:22 -070081
Mike Reed7f1d0202017-05-08 16:13:39 -040082 // True for picture-backed and codec-backed
fmalitaddbbdda2015-08-20 08:47:26 -070083 virtual bool onIsLazyGenerated() const { return false; }
84
fmalita3b0d5322015-09-18 08:07:31 -070085 // Call when this image is part of the key to a resourcecache entry. This allows the cache
86 // to know automatically those entries can be purged when this SkImage deleted.
Mike Reed30301c42018-07-19 09:39:21 -040087 void notifyAddedToRasterCache() const {
88 fAddedToRasterCache.store(true);
fmalita3b0d5322015-09-18 08:07:31 -070089 }
90
Brian Osman5bbd0762017-05-08 11:07:42 -040091 virtual bool onIsValid(GrContext*) const = 0;
92
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -050093 virtual bool onPinAsTexture(GrContext*) const { return false; }
reed2d5b7142016-08-17 11:12:33 -070094 virtual void onUnpinAsTexture(GrContext*) const {}
95
Brian Osmanb62f50c2018-07-12 14:44:27 -040096 virtual sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType) const = 0;
Florin Malitaf7beee72017-05-26 12:54:32 -040097protected:
98 SkImage_Base(int width, int height, uint32_t uniqueID);
Matt Sarett6de13102017-03-14 14:10:48 -040099
Greg Daniel2ad08202018-09-07 09:13:36 -0400100#if SK_SUPPORT_GPU
101 // When the SkImage_Base goes away, we will iterate over all the unique keys we've used and
102 // send messages to the GrContexts to say the unique keys are no longer valid. The GrContexts
103 // can then release the resources, conntected with the those unique keys, from their caches.
104 SkTDArray<GrUniqueKeyInvalidatedMessage*> fUniqueKeyInvalidatedMessages;
105#endif
106
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000107private:
fmalita3b0d5322015-09-18 08:07:31 -0700108 // Set true by caches when they cache content that's derived from the current pixels.
Mike Klein015c8992018-08-09 12:23:19 -0400109 mutable std::atomic<bool> fAddedToRasterCache;
fmalita3b0d5322015-09-18 08:07:31 -0700110
reed@google.com58b21ec2012-07-30 18:20:12 +0000111 typedef SkImage INHERITED;
112};
113
reed6ceeebd2016-03-09 14:26:26 -0800114static inline SkImage_Base* as_IB(SkImage* image) {
reed4af267b2014-11-21 08:46:37 -0800115 return static_cast<SkImage_Base*>(image);
116}
117
reed9ce9d672016-03-17 10:51:11 -0700118static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
119 return static_cast<SkImage_Base*>(image.get());
120}
121
reed4af267b2014-11-21 08:46:37 -0800122static inline const SkImage_Base* as_IB(const SkImage* image) {
123 return static_cast<const SkImage_Base*>(image);
124}
125
reed@google.com58b21ec2012-07-30 18:20:12 +0000126#endif