mark all SkImage methods const, so we can make it thread-safe
BUG=skia:
R=mtklein@google.com, halcanary@google.com
Author: reed@google.com
Review URL: https://codereview.chromium.org/453613003
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index c812f58..795b842 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -29,13 +29,12 @@
return id;
}
-void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y,
- const SkPaint* paint) {
+void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
as_IB(this)->onDraw(canvas, x, y, paint);
}
void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
- const SkPaint* paint) {
+ const SkPaint* paint) const {
as_IB(this)->onDrawRectToRect(canvas, src, dst, paint);
}
diff --git a/src/image/SkImage_Base.h b/src/image/SkImage_Base.h
index 752afed..9542ba3 100644
--- a/src/image/SkImage_Base.h
+++ b/src/image/SkImage_Base.h
@@ -14,9 +14,9 @@
public:
SkImage_Base(int width, int height) : INHERITED(width, height) {}
- virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) = 0;
+ virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0;
virtual void onDrawRectToRect(SkCanvas*, const SkRect* src,
- const SkRect& dst, const SkPaint*) = 0;
+ const SkRect& dst, const SkPaint*) const = 0;
// Default impl calls onDraw
virtual bool onReadPixels(SkBitmap*, const SkIRect& subset) const;
@@ -25,7 +25,7 @@
return NULL;
}
- virtual GrTexture* onGetTexture() { return NULL; }
+ virtual GrTexture* onGetTexture() const { return NULL; }
// return a read-only copy of the pixels. We promise to not modify them,
// but only inspect them (or encode them).
diff --git a/src/image/SkImage_Codec.cpp b/src/image/SkImage_Codec.cpp
index 3d815ce..21c844d 100644
--- a/src/image/SkImage_Codec.cpp
+++ b/src/image/SkImage_Codec.cpp
@@ -18,8 +18,9 @@
SkImage_Codec(SkData* encodedData, int width, int height);
virtual ~SkImage_Codec();
- virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
- virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE;
+ virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_OVERRIDE;
+ virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&,
+ const SkPaint*) const SK_OVERRIDE;
private:
SkData* fEncodedData;
@@ -39,19 +40,23 @@
fEncodedData->unref();
}
-void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) {
+void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
if (!fBitmap.pixelRef()) {
- if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), &fBitmap)) {
+ // todo: this needs to be thread-safe
+ SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap);
+ if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), bitmap)) {
return;
}
}
canvas->drawBitmap(fBitmap, x, y, paint);
}
-void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src,
- const SkRect& dst, const SkPaint* paint) {
+void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint) const {
if (!fBitmap.pixelRef()) {
- if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), &fBitmap)) {
+ // todo: this needs to be thread-safe
+ SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap);
+ if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), bitmap)) {
return;
}
}
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index 81114b0..0918412 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -20,12 +20,13 @@
explicit SkImage_Gpu(const SkBitmap&);
virtual ~SkImage_Gpu();
- virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) SK_OVERRIDE;
- virtual void onDrawRectToRect(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*) SK_OVERRIDE;
- virtual GrTexture* onGetTexture() SK_OVERRIDE;
+ virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const SK_OVERRIDE;
+ virtual void onDrawRectToRect(SkCanvas*, const SkRect* src, const SkRect& dst,
+ const SkPaint*) const SK_OVERRIDE;
+ virtual GrTexture* onGetTexture() const SK_OVERRIDE;
virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE;
- GrTexture* getTexture() { return fBitmap.getTexture(); }
+ GrTexture* getTexture() const { return fBitmap.getTexture(); }
virtual SkShader* onNewShader(SkShader::TileMode,
SkShader::TileMode,
@@ -54,17 +55,16 @@
return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
}
-void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
- const SkPaint* paint) {
+void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
canvas->drawBitmap(fBitmap, x, y, paint);
}
void SkImage_Gpu::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
- const SkPaint* paint) {
+ const SkPaint* paint) const {
canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
}
-GrTexture* SkImage_Gpu::onGetTexture() {
+GrTexture* SkImage_Gpu::onGetTexture() const {
return fBitmap.getTexture();
}
diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp
index 828d802..e4768af 100644
--- a/src/image/SkImage_Raster.cpp
+++ b/src/image/SkImage_Raster.cpp
@@ -53,8 +53,9 @@
SkImage_Raster(const SkImageInfo&, SkData*, size_t rb);
virtual ~SkImage_Raster();
- virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
- virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE;
+ virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_OVERRIDE;
+ virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&,
+ const SkPaint*) const SK_OVERRIDE;
virtual bool onReadPixels(SkBitmap*, const SkIRect&) const SK_OVERRIDE;
virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const SK_OVERRIDE;
virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE;
@@ -115,19 +116,17 @@
SkImage_Raster::~SkImage_Raster() {}
-SkShader* SkImage_Raster::onNewShader(SkShader::TileMode tileX,
- SkShader::TileMode tileY,
- const SkMatrix* localMatrix) const
-{
+SkShader* SkImage_Raster::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
+ const SkMatrix* localMatrix) const {
return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
}
-void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) {
+void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
canvas->drawBitmap(fBitmap, x, y, paint);
}
-void SkImage_Raster::onDrawRectToRect(SkCanvas* canvas, const SkRect* src,
- const SkRect& dst, const SkPaint* paint) {
+void SkImage_Raster::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint) const {
canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
}
@@ -143,8 +142,7 @@
}
}
-const void* SkImage_Raster::onPeekPixels(SkImageInfo* infoPtr,
- size_t* rowBytesPtr) const {
+const void* SkImage_Raster::onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytesPtr) const {
const SkImageInfo info = fBitmap.info();
if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) {
return NULL;