Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2014 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 #if SK_SUPPORT_GPU
      9 
     10 #include "gm.h"
     11 #include "SkCanvas.h"
     12 #include "SkColorShader.h"
     13 #include "SkPaint.h"
     14 #include "SkSurface.h"
     15 
     16 namespace skiagm {
     17 
     18 /*
     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.
     21  */
     22 class DiscardGM : public GM {
     23 
     24 public:
     25     DiscardGM() {
     26     }
     27 
     28     virtual uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }
     29 
     30 protected:
     31     virtual SkString onShortName() SK_OVERRIDE {
     32         return SkString("discard");
     33     }
     34 
     35     virtual SkISize onISize() SK_OVERRIDE {
     36         return SkISize::Make(100, 100);
     37     }
     38 
     39     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     40         GrContext* context = canvas->getGrContext();
     41         if (NULL == context) {
     42             return;
     43         }
     44 
     45         SkISize size = this->getISize();
     46         size.fWidth /= 10;
     47         size.fHeight /= 10;
     48         SkImageInfo info = SkImageInfo::MakeN32Premul(size);
     49         SkSurface* surface = SkSurface::NewRenderTarget(context, info);
     50 
     51         if (NULL == surface) {
     52             return;
     53         }
     54 
     55         canvas->clear(SK_ColorBLACK);
     56 
     57         SkRandom rand;
     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)) {
     64                   case 0:
     65                       surface->getCanvas()->drawColor(color);
     66                       break;
     67                   case 1:
     68                       surface->getCanvas()->clear(color);
     69                       break;
     70                   case 2:
     71                       SkColorShader shader(color);
     72                       SkPaint paint;
     73                       paint.setShader(&shader);
     74                       surface->getCanvas()->drawPaint(paint);
     75                       break;
     76               }
     77               surface->draw(canvas, 10.f*x, 10.f*y, NULL);
     78             }
     79         }
     80 
     81         surface->getCanvas()->discard();
     82         surface->unref();
     83     }
     84 
     85 private:
     86     typedef GM INHERITED;
     87 };
     88 
     89 //////////////////////////////////////////////////////////////////////////////
     90 
     91 DEF_GM( return SkNEW(DiscardGM); )
     92 
     93 } // end namespace
     94 
     95 #endif
     96