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