aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/discard.cpp
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-04-24 21:03:00 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-04-24 21:03:00 +0000
commit04f03d1fad46776a0d4231f77f1a70a904e66dec (patch)
treed1d3fe0ef2de354769b594f1d6b8d817aeb2170f /gm/discard.cpp
parentd0306a15938a971e10dd8648d3e17b001c4b0014 (diff)
Add GM that exercises SkCanvas::discard()
R=robertphillips@google.com Author: bsalomon@google.com Review URL: https://codereview.chromium.org/252443007 git-svn-id: http://skia.googlecode.com/svn/trunk@14365 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'gm/discard.cpp')
-rw-r--r--gm/discard.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/gm/discard.cpp b/gm/discard.cpp
new file mode 100644
index 0000000000..ae3947dfe4
--- /dev/null
+++ b/gm/discard.cpp
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#if SK_SUPPORT_GPU
+
+#include "gm.h"
+#include "SkCanvas.h"
+#include "SkColorShader.h"
+#include "SkPaint.h"
+#include "SkSurface.h"
+
+namespace skiagm {
+
+/*
+ * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
+ * discarding it, drawing to it, and then drawing it to the main canvas.
+ */
+class DiscardGM : public GM {
+
+public:
+ DiscardGM() {
+ }
+
+ virtual uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }
+
+protected:
+ virtual SkString onShortName() SK_OVERRIDE {
+ return SkString("discard");
+ }
+
+ virtual SkISize onISize() SK_OVERRIDE {
+ return SkISize::Make(100, 100);
+ }
+
+ virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+ GrContext* context = canvas->getGrContext();
+ if (NULL == context) {
+ return;
+ }
+
+ SkISize size = this->getISize();
+ size.fWidth /= 10;
+ size.fHeight /= 10;
+ SkImageInfo info = SkImageInfo::MakeN32Premul(size);
+ SkSurface* surface = SkSurface::NewRenderTarget(context, info);
+
+ if (NULL == surface) {
+ return;
+ }
+
+ canvas->clear(SK_ColorBLACK);
+
+ SkRandom rand;
+ for (int x = 0; x < 10; ++x) {
+ for (int y = 0; y < 10; ++y) {
+ surface->getCanvas()->discard();
+ // Make something that isn't too close to the background color, black.
+ SkColor color = rand.nextU() | 0xFF404040;
+ switch (rand.nextULessThan(3)) {
+ case 0:
+ surface->getCanvas()->drawColor(color);
+ break;
+ case 1:
+ surface->getCanvas()->clear(color);
+ break;
+ case 2:
+ SkColorShader shader(color);
+ SkPaint paint;
+ paint.setShader(&shader);
+ surface->getCanvas()->drawPaint(paint);
+ break;
+ }
+ surface->draw(canvas, 10*x, 10*y, NULL);
+ }
+ }
+
+ surface->getCanvas()->discard();
+ surface->unref();
+ }
+
+private:
+ typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+DEF_GM( return SkNEW(DiscardGM); )
+
+} // end namespace
+
+#endif