2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkColorShader.h"
12 #include "SkSurface.h"
19 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
20 * discarding it, drawing to it, and then drawing it to the main canvas.
22 class DiscardGM : public GM {
28 virtual uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }
31 virtual SkString onShortName() SK_OVERRIDE {
32 return SkString("discard");
35 virtual SkISize onISize() SK_OVERRIDE {
36 return SkISize::Make(100, 100);
39 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
40 GrContext* context = canvas->getGrContext();
41 if (NULL == context) {
45 SkISize size = this->getISize();
48 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
49 SkSurface* surface = SkSurface::NewRenderTarget(context, info);
51 if (NULL == surface) {
55 canvas->clear(SK_ColorBLACK);
58 for (int x = 0; x < 10; ++x) {
59 for (int y = 0; y < 10; ++y) {
60 surface->getCanvas()->discard();
61 // Make something that isn't too close to the background color, black.
62 SkColor color = rand.nextU() | 0xFF404040;
63 switch (rand.nextULessThan(3)) {
65 surface->getCanvas()->drawColor(color);
68 surface->getCanvas()->clear(color);
71 SkColorShader shader(color);
73 paint.setShader(&shader);
74 surface->getCanvas()->drawPaint(paint);
77 surface->draw(canvas, 10.f*x, 10.f*y, NULL);
81 surface->getCanvas()->discard();
89 //////////////////////////////////////////////////////////////////////////////
91 DEF_GM( return SkNEW(DiscardGM); )