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 #include "gm.h"
      9 #include "sk_tool_utils.h"
     10 #include "SkCanvas.h"
     11 #include "SkPaint.h"
     12 #include "SkRandom.h"
     13 #include "SkSurface.h"
     14 
     15 #if SK_SUPPORT_GPU
     16 
     17 namespace skiagm {
     18 
     19 /*
     20  * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
     21  * discarding it, drawing to it, and then drawing it to the main canvas.
     22  */
     23 class DiscardGM : public GM {
     24 
     25 public:
     26     DiscardGM() {
     27     }
     28 
     29 protected:
     30     SkString onShortName() override {
     31         return SkString("discard");
     32     }
     33 
     34     SkISize onISize() override {
     35         return SkISize::Make(100, 100);
     36     }
     37 
     38     void onDraw(SkCanvas* canvas) override {
     39         GrContext* context = canvas->getGrContext();
     40         if (nullptr == context) {
     41             return;
     42         }
     43 
     44         SkISize size = this->getISize();
     45         size.fWidth /= 10;
     46         size.fHeight /= 10;
     47         SkImageInfo info = SkImageInfo::MakeN32Premul(size);
     48         auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
     49         if (nullptr == surface) {
     50             return;
     51         }
     52 
     53         canvas->clear(SK_ColorBLACK);
     54 
     55         SkRandom rand;
     56         for (int x = 0; x < 10; ++x) {
     57             for (int y = 0; y < 10; ++y) {
     58               surface->getCanvas()->discard();
     59               // Make something that isn't too close to the background color, black.
     60               SkColor color = sk_tool_utils::color_to_565(rand.nextU() | 0xFF404040);
     61               switch (rand.nextULessThan(3)) {
     62                   case 0:
     63                       surface->getCanvas()->drawColor(color);
     64                       break;
     65                   case 1:
     66                       surface->getCanvas()->clear(color);
     67                       break;
     68                   case 2:
     69                       SkPaint paint;
     70                       paint.setShader(SkShader::MakeColorShader(color));
     71                       surface->getCanvas()->drawPaint(paint);
     72                       break;
     73               }
     74               surface->draw(canvas, 10.f*x, 10.f*y, nullptr);
     75             }
     76         }
     77 
     78         surface->getCanvas()->discard();
     79     }
     80 
     81 private:
     82     typedef GM INHERITED;
     83 };
     84 
     85 //////////////////////////////////////////////////////////////////////////////
     86 
     87 DEF_GM(return new DiscardGM;)
     88 
     89 } // end namespace
     90 
     91 #endif
     92