blob: 37dcb40d254fbce41d6605cb12de4fcea4bbae62 [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
reed80c772b2015-07-30 18:58:23 -070014enum {
15 kNeedNewImageUniqueID = 0
16};
17
reed4af267b2014-11-21 08:46:37 -080018static SkSurfaceProps copy_or_safe_defaults(const SkSurfaceProps* props) {
19 return props ? *props : SkSurfaceProps(0, kUnknown_SkPixelGeometry);
20}
reed@google.com58b21ec2012-07-30 18:20:12 +000021
22class SkImage_Base : public SkImage {
23public:
reed80c772b2015-07-30 18:58:23 -070024 SkImage_Base(int width, int height, uint32_t uniqueID, const SkSurfaceProps* props)
25 : INHERITED(width, height, uniqueID)
reed4af267b2014-11-21 08:46:37 -080026 , 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.com58b21ec2012-07-30 18:20:12 +000042
reed4af267b2014-11-21 08:46:37 -080043 virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const = 0;
reed@google.com4f7c6152014-02-06 14:11:56 +000044
reed@google.com4f7c6152014-02-06 14:11:56 +000045 virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const {
46 return NULL;
47 }
48
reed96472de2014-12-10 09:53:42 -080049 // Default impl calls onDraw
50 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
51 int srcX, int srcY) const;
52
bsalomon55812362015-06-10 08:49:28 -070053 virtual GrTexture* getTexture() const { return NULL; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000054
reed@google.com4b0757b2013-05-20 16:33:41 +000055 // 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.com58b21ec2012-07-30 18:20:12 +000058
piotaixr76d5b472014-07-22 15:02:05 -070059 virtual SkShader* onNewShader(SkShader::TileMode,
60 SkShader::TileMode,
61 const SkMatrix* localMatrix) const { return NULL; };
reed4af267b2014-11-21 08:46:37 -080062
reedf803da12015-01-23 05:58:07 -080063 // 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;
reed871872f2015-06-22 12:48:26 -070066 virtual SkData* onRefEncoded() const { return NULL; }
reedf803da12015-01-23 05:58:07 -080067
reed3c065112015-07-08 12:46:22 -070068 virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
69
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070private:
reed4af267b2014-11-21 08:46:37 -080071 const SkSurfaceProps fProps;
72
reed@google.com58b21ec2012-07-30 18:20:12 +000073 typedef SkImage INHERITED;
74};
75
reed4af267b2014-11-21 08:46:37 -080076static inline SkImage_Base* as_IB(SkImage* image) {
77 return static_cast<SkImage_Base*>(image);
78}
79
80static inline const SkImage_Base* as_IB(const SkImage* image) {
81 return static_cast<const SkImage_Base*>(image);
82}
83
reed@google.com58b21ec2012-07-30 18:20:12 +000084#endif