blob: 8c1dfada638db398c6b645c7e0c264c54a3b470c [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"
13
14static SkSurfaceProps copy_or_safe_defaults(const SkSurfaceProps* props) {
15 return props ? *props : SkSurfaceProps(0, kUnknown_SkPixelGeometry);
16}
reed@google.com58b21ec2012-07-30 18:20:12 +000017
18class SkImage_Base : public SkImage {
19public:
reed4af267b2014-11-21 08:46:37 -080020 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.com58b21ec2012-07-30 18:20:12 +000038
reed8572fc02014-08-11 13:03:55 -070039 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0;
piotaixr5ceff912014-09-26 07:36:26 -070040 virtual void onDrawRect(SkCanvas*, const SkRect* src,
reed8572fc02014-08-11 13:03:55 -070041 const SkRect& dst, const SkPaint*) const = 0;
reed4af267b2014-11-21 08:46:37 -080042 virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const = 0;
reed@google.com4f7c6152014-02-06 14:11:56 +000043
reed@google.com4f7c6152014-02-06 14:11:56 +000044 virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const {
45 return NULL;
46 }
47
reed96472de2014-12-10 09:53:42 -080048 // Default impl calls onDraw
49 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
50 int srcX, int srcY) const;
51
reed8572fc02014-08-11 13:03:55 -070052 virtual GrTexture* onGetTexture() const { return NULL; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000053
reed@google.com4b0757b2013-05-20 16:33:41 +000054 // 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.com58b21ec2012-07-30 18:20:12 +000057
piotaixr76d5b472014-07-22 15:02:05 -070058 virtual SkShader* onNewShader(SkShader::TileMode,
59 SkShader::TileMode,
60 const SkMatrix* localMatrix) const { return NULL; };
reed4af267b2014-11-21 08:46:37 -080061
rmistry@google.comfbfcd562012-08-23 18:09:54 +000062private:
reed4af267b2014-11-21 08:46:37 -080063 const SkSurfaceProps fProps;
64
reed@google.com58b21ec2012-07-30 18:20:12 +000065 typedef SkImage INHERITED;
66};
67
reed4af267b2014-11-21 08:46:37 -080068static inline SkImage_Base* as_IB(SkImage* image) {
69 return static_cast<SkImage_Base*>(image);
70}
71
72static inline const SkImage_Base* as_IB(const SkImage* image) {
73 return static_cast<const SkImage_Base*>(image);
74}
75
reed@google.com58b21ec2012-07-30 18:20:12 +000076#endif