reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 1 | /* |
| 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" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 12 | #include "SkSurface.h" |
| 13 | |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 14 | #include <new> |
| 15 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 16 | enum { |
| 17 | kNeedNewImageUniqueID = 0 |
| 18 | }; |
| 19 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 20 | static SkSurfaceProps copy_or_safe_defaults(const SkSurfaceProps* props) { |
| 21 | return props ? *props : SkSurfaceProps(0, kUnknown_SkPixelGeometry); |
| 22 | } |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 23 | |
| 24 | class SkImage_Base : public SkImage { |
| 25 | public: |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 26 | SkImage_Base(int width, int height, uint32_t uniqueID, const SkSurfaceProps* props) |
| 27 | : INHERITED(width, height, uniqueID) |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 28 | , fProps(copy_or_safe_defaults(props)) |
| 29 | {} |
| 30 | |
| 31 | /** |
| 32 | * If the props weren't know at constructor time, call this but only before the image is |
| 33 | * ever released into the wild (since the props field must appear to be immutable). |
| 34 | */ |
| 35 | void initWithProps(const SkSurfaceProps& props) { |
| 36 | SkASSERT(this->unique()); // only viewed by one thread |
| 37 | SkSurfaceProps* mutableProps = const_cast<SkSurfaceProps*>(&fProps); |
| 38 | SkASSERT(mutableProps != &props); // check for self-assignment |
| 39 | mutableProps->~SkSurfaceProps(); |
| 40 | new (mutableProps) SkSurfaceProps(props); |
| 41 | } |
| 42 | |
| 43 | const SkSurfaceProps& props() const { return fProps; } |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 44 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 45 | virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const = 0; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 46 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 47 | virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const { |
| 48 | return NULL; |
| 49 | } |
| 50 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 51 | // Default impl calls onDraw |
| 52 | virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 53 | int srcX, int srcY) const; |
| 54 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 55 | virtual GrTexture* getTexture() const { return NULL; } |
skia.committer@gmail.com | 3e50e99 | 2013-05-21 07:01:40 +0000 | [diff] [blame] | 56 | |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 57 | // return a read-only copy of the pixels. We promise to not modify them, |
| 58 | // but only inspect them (or encode them). |
| 59 | virtual bool getROPixels(SkBitmap*) const { return false; } |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 60 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 61 | virtual SkShader* onNewShader(SkShader::TileMode, |
| 62 | SkShader::TileMode, |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame^] | 63 | const SkMatrix* localMatrix) const { return NULL; } |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 64 | |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 65 | // newWidth > 0, newHeight > 0, subset either NULL or a proper subset of this bounds |
| 66 | virtual SkImage* onNewImage(int newWidth, int newHeight, const SkIRect* subset, |
| 67 | SkFilterQuality) const; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 68 | virtual SkData* onRefEncoded() const { return NULL; } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 69 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 70 | virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const; |
| 71 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame^] | 72 | virtual bool onIsLazyGenerated() const { return false; } |
| 73 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 74 | private: |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 75 | const SkSurfaceProps fProps; |
| 76 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 77 | typedef SkImage INHERITED; |
| 78 | }; |
| 79 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 80 | static inline SkImage_Base* as_IB(SkImage* image) { |
| 81 | return static_cast<SkImage_Base*>(image); |
| 82 | } |
| 83 | |
| 84 | static inline const SkImage_Base* as_IB(const SkImage* image) { |
| 85 | return static_cast<const SkImage_Base*>(image); |
| 86 | } |
| 87 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 88 | #endif |