aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Mike Reed <reed@google.com>2017-06-28 19:57:21 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-06-29 12:14:53 +0000
commit44d04bd7ed3be8c39cc7ea02e3789cccf083bb55 (patch)
tree9fc3d15501a84bf9636b3ec5619b90c8d7f62a9a
parent7f22511c1cb4e823e17446c54aa308a2f2203899 (diff)
add null-surface
Bug:crbug.com/737726 Change-Id: Iec9094d8d7232943e90fe2d9745fc83bcdf90954 Reviewed-on: https://skia-review.googlesource.com/21190 Reviewed-by: Robert Phillips <robertphillips@google.com> Commit-Queue: Mike Reed <reed@google.com>
-rw-r--r--include/core/SkSurface.h6
-rw-r--r--src/image/SkSurface.cpp26
-rw-r--r--tests/SurfaceTest.cpp12
3 files changed, 44 insertions, 0 deletions
diff --git a/include/core/SkSurface.h b/include/core/SkSurface.h
index 08e558cac7..f6142c9e3b 100644
--- a/include/core/SkSurface.h
+++ b/include/core/SkSurface.h
@@ -176,6 +176,12 @@ public:
return MakeRenderTarget(gr, b, info, 0, kBottomLeft_GrSurfaceOrigin, nullptr);
}
+ /**
+ * Returns a surface that stores no pixels. It can be drawn to via its canvas, but that
+ * canvas does not draw anything. Calling makeImageSnapshot() will return nullptr.
+ */
+ static sk_sp<SkSurface> MakeNull(int width, int height);
+
int width() const { return fWidth; }
int height() const { return fHeight; }
diff --git a/src/image/SkSurface.cpp b/src/image/SkSurface.cpp
index 57d419c408..dcf56493b5 100644
--- a/src/image/SkSurface.cpp
+++ b/src/image/SkSurface.cpp
@@ -200,6 +200,32 @@ bool SkSurface::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores
}
//////////////////////////////////////////////////////////////////////////////////////
+#include "SkNoDrawCanvas.h"
+
+class SkNullSurface : public SkSurface_Base {
+public:
+ SkNullSurface(int width, int height) : SkSurface_Base(width, height, nullptr) {}
+
+protected:
+ SkCanvas* onNewCanvas() override {
+ return new SkNoDrawCanvas(this->width(), this->height());
+ }
+ sk_sp<SkSurface> onNewSurface(const SkImageInfo& info) override {
+ return MakeNull(info.width(), info.height());
+ }
+ sk_sp<SkImage> onNewImageSnapshot() override { return nullptr; }
+ void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override {}
+ void onCopyOnWrite(ContentChangeMode) override {}
+};
+
+sk_sp<SkSurface> SkSurface::MakeNull(int width, int height) {
+ if (width < 1 || height < 1) {
+ return nullptr;
+ }
+ return sk_sp<SkSurface>(new SkNullSurface(width, height));
+}
+
+//////////////////////////////////////////////////////////////////////////////////////
#if !SK_SUPPORT_GPU
diff --git a/tests/SurfaceTest.cpp b/tests/SurfaceTest.cpp
index dc051a54b6..5249b3d562 100644
--- a/tests/SurfaceTest.cpp
+++ b/tests/SurfaceTest.cpp
@@ -948,3 +948,15 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
test_overdraw_surface(r, surface.get());
}
#endif
+
+DEF_TEST(Surface_null, r) {
+ REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
+
+ const int w = 37;
+ const int h = 1000;
+ auto surf = SkSurface::MakeNull(w, h);
+ auto canvas = surf->getCanvas();
+
+ canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
+ REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
+}