blob: 7bbd81d05f01d8a0c30e13d56be41dc1e9e22831 [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;
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040025class SkCachedData;
26struct SkYUVSizeInfo;
bsalomonafa95e22015-10-12 10:39:46 -070027
reed80c772b2015-07-30 18:58:23 -070028enum {
29 kNeedNewImageUniqueID = 0
30};
31
reed@google.com58b21ec2012-07-30 18:20:12 +000032class SkImage_Base : public SkImage {
33public:
fmalita3b0d5322015-09-18 08:07:31 -070034 virtual ~SkImage_Base();
reed4af267b2014-11-21 08:46:37 -080035
herba7c9d632016-04-19 12:30:22 -070036 // User: returns image info for this SkImage.
37 // Implementors: if you can not return the value, return an invalid ImageInfo with w=0 & h=0
38 // & unknown color space.
39 virtual SkImageInfo onImageInfo() const = 0;
40
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
reed@google.com4b0757b2013-05-20 16:33:41 +000068 // return a read-only copy of the pixels. We promise to not modify them,
69 // but only inspect them (or encode them).
Brian Osman61624f02016-12-09 14:51:59 -050070 virtual bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050071 CachingHint = kAllow_CachingHint) const = 0;
reed746f31f2015-09-16 12:53:29 -070072
reed7fb4f8b2016-03-11 04:33:52 -080073 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&) const = 0;
reed856e9d92015-09-30 12:21:45 -070074
Jim Van Verth8f11e432018-10-18 14:36:59 -040075 virtual sk_sp<SkCachedData> getPlanes(SkYUVSizeInfo*, SkYUVAIndex[4],
76 SkYUVColorSpace*, const void* planes[4]);
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
Jim Van Verth21bd60d2018-10-12 15:00:20 -040084 // True for images instantiated in GPU memory
85 virtual bool onIsTextureBacked() const { return false; }
86
fmalita3b0d5322015-09-18 08:07:31 -070087 // Call when this image is part of the key to a resourcecache entry. This allows the cache
88 // to know automatically those entries can be purged when this SkImage deleted.
Brian Osman3a160732018-10-15 15:32:06 -040089 virtual void notifyAddedToRasterCache() const {
Mike Reed30301c42018-07-19 09:39:21 -040090 fAddedToRasterCache.store(true);
fmalita3b0d5322015-09-18 08:07:31 -070091 }
92
Brian Osman5bbd0762017-05-08 11:07:42 -040093 virtual bool onIsValid(GrContext*) const = 0;
94
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -050095 virtual bool onPinAsTexture(GrContext*) const { return false; }
reed2d5b7142016-08-17 11:12:33 -070096 virtual void onUnpinAsTexture(GrContext*) const {}
97
Brian Osman15f0f292018-10-01 14:14:46 -040098 virtual sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>) const = 0;
Florin Malitaf7beee72017-05-26 12:54:32 -040099protected:
100 SkImage_Base(int width, int height, uint32_t uniqueID);
Matt Sarett6de13102017-03-14 14:10:48 -0400101
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000102private:
fmalita3b0d5322015-09-18 08:07:31 -0700103 // Set true by caches when they cache content that's derived from the current pixels.
Mike Klein015c8992018-08-09 12:23:19 -0400104 mutable std::atomic<bool> fAddedToRasterCache;
fmalita3b0d5322015-09-18 08:07:31 -0700105
reed@google.com58b21ec2012-07-30 18:20:12 +0000106 typedef SkImage INHERITED;
107};
108
reed6ceeebd2016-03-09 14:26:26 -0800109static inline SkImage_Base* as_IB(SkImage* image) {
reed4af267b2014-11-21 08:46:37 -0800110 return static_cast<SkImage_Base*>(image);
111}
112
reed9ce9d672016-03-17 10:51:11 -0700113static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
114 return static_cast<SkImage_Base*>(image.get());
115}
116
reed4af267b2014-11-21 08:46:37 -0800117static inline const SkImage_Base* as_IB(const SkImage* image) {
118 return static_cast<const SkImage_Base*>(image);
119}
120
reed@google.com58b21ec2012-07-30 18:20:12 +0000121#endif