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