blob: 46cd3de3dbdde6b608da9425172831fcf85135d0 [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
bungemanf3c15b72015-08-19 11:56:48 -070014#include <new>
15
reed80c772b2015-07-30 18:58:23 -070016enum {
17 kNeedNewImageUniqueID = 0
18};
19
reed4af267b2014-11-21 08:46:37 -080020static SkSurfaceProps copy_or_safe_defaults(const SkSurfaceProps* props) {
21 return props ? *props : SkSurfaceProps(0, kUnknown_SkPixelGeometry);
22}
reed@google.com58b21ec2012-07-30 18:20:12 +000023
24class SkImage_Base : public SkImage {
25public:
reed80c772b2015-07-30 18:58:23 -070026 SkImage_Base(int width, int height, uint32_t uniqueID, const SkSurfaceProps* props)
27 : INHERITED(width, height, uniqueID)
reed4af267b2014-11-21 08:46:37 -080028 , 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.com58b21ec2012-07-30 18:20:12 +000044
reed4af267b2014-11-21 08:46:37 -080045 virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const = 0;
reed@google.com4f7c6152014-02-06 14:11:56 +000046
reed@google.com4f7c6152014-02-06 14:11:56 +000047 virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const {
48 return NULL;
49 }
50
reed96472de2014-12-10 09:53:42 -080051 // Default impl calls onDraw
52 virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
53 int srcX, int srcY) const;
54
bsalomon55812362015-06-10 08:49:28 -070055 virtual GrTexture* getTexture() const { return NULL; }
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +000056
reed@google.com4b0757b2013-05-20 16:33:41 +000057 // 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.com58b21ec2012-07-30 18:20:12 +000060
piotaixr76d5b472014-07-22 15:02:05 -070061 virtual SkShader* onNewShader(SkShader::TileMode,
62 SkShader::TileMode,
fmalitaddbbdda2015-08-20 08:47:26 -070063 const SkMatrix* localMatrix) const { return NULL; }
reed4af267b2014-11-21 08:46:37 -080064
reedf803da12015-01-23 05:58:07 -080065 // 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;
reed871872f2015-06-22 12:48:26 -070068 virtual SkData* onRefEncoded() const { return NULL; }
reedf803da12015-01-23 05:58:07 -080069
reed3c065112015-07-08 12:46:22 -070070 virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
71
fmalitaddbbdda2015-08-20 08:47:26 -070072 virtual bool onIsLazyGenerated() const { return false; }
73
rmistry@google.comfbfcd562012-08-23 18:09:54 +000074private:
reed4af267b2014-11-21 08:46:37 -080075 const SkSurfaceProps fProps;
76
reed@google.com58b21ec2012-07-30 18:20:12 +000077 typedef SkImage INHERITED;
78};
79
reed4af267b2014-11-21 08:46:37 -080080static inline SkImage_Base* as_IB(SkImage* image) {
81 return static_cast<SkImage_Base*>(image);
82}
83
84static inline const SkImage_Base* as_IB(const SkImage* image) {
85 return static_cast<const SkImage_Base*>(image);
86}
87
reed@google.com58b21ec2012-07-30 18:20:12 +000088#endif