Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2011 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 "SkShader.h"
     11 #include "SkTraceEvent.h"
     12 using namespace skiagm;
     13 
     14 GM::GM() {
     15     fMode = kGM_Mode;
     16     fBGColor = SK_ColorWHITE;
     17     fCanvasIsDeferred = false;
     18     fHaveCalledOnceBeforeDraw = false;
     19 }
     20 
     21 GM::~GM() {}
     22 
     23 void GM::draw(SkCanvas* canvas) {
     24     TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName()));
     25     this->drawBackground(canvas);
     26     this->drawContent(canvas);
     27 }
     28 
     29 void GM::drawContent(SkCanvas* canvas) {
     30     TRACE_EVENT0("GM", TRACE_FUNC);
     31     if (!fHaveCalledOnceBeforeDraw) {
     32         fHaveCalledOnceBeforeDraw = true;
     33         this->onOnceBeforeDraw();
     34     }
     35     SkAutoCanvasRestore acr(canvas, true);
     36     this->onDraw(canvas);
     37 }
     38 
     39 void GM::drawBackground(SkCanvas* canvas) {
     40     TRACE_EVENT0("GM", TRACE_FUNC);
     41     if (!fHaveCalledOnceBeforeDraw) {
     42         fHaveCalledOnceBeforeDraw = true;
     43         this->onOnceBeforeDraw();
     44     }
     45     SkAutoCanvasRestore acr(canvas, true);
     46     canvas->drawColor(fBGColor, SkBlendMode::kSrc);
     47 }
     48 
     49 const char* GM::getName() {
     50     if (fShortName.size() == 0) {
     51         fShortName = this->onShortName();
     52     }
     53     return fShortName.c_str();
     54 }
     55 
     56 void GM::setBGColor(SkColor color) {
     57     fBGColor = color;
     58 }
     59 
     60 bool GM::animate(const SkAnimTimer& timer) {
     61     return this->onAnimate(timer);
     62 }
     63 
     64 /////////////////////////////////////////////////////////////////////////////////////////////
     65 
     66 void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
     67     SkISize size = this->getISize();
     68     SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
     69                               SkIntToScalar(size.height()));
     70     SkPaint paint;
     71     paint.setColor(color);
     72     canvas->drawRect(r, paint);
     73 }
     74 
     75 void GM::DrawGpuOnlyMessage(SkCanvas* canvas) {
     76     SkBitmap bmp;
     77     bmp.allocN32Pixels(128, 64);
     78     SkCanvas bmpCanvas(bmp);
     79     bmpCanvas.drawColor(SK_ColorWHITE);
     80     SkFont font(sk_tool_utils::create_portable_typeface(), 20);
     81     SkPaint paint;
     82     paint.setColor(SK_ColorRED);
     83     bmpCanvas.drawString("GPU Only", 20, 40, font, paint);
     84     SkMatrix localM;
     85     localM.setRotate(35.f);
     86     localM.postTranslate(10.f, 0.f);
     87     paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode,
     88                                                SkShader::kMirror_TileMode,
     89                                                &localM));
     90     paint.setFilterQuality(kMedium_SkFilterQuality);
     91     canvas->drawPaint(paint);
     92     return;
     93 }
     94 
     95 // need to explicitly declare this, or we get some weird infinite loop llist
     96 template GMRegistry* GMRegistry::gHead;
     97 
     98 void skiagm::SimpleGM::onDraw(SkCanvas* canvas) {
     99     fDrawProc(canvas);
    100 }
    101 
    102 SkISize skiagm::SimpleGM::onISize() {
    103     return fSize;
    104 }
    105 
    106 SkString skiagm::SimpleGM::onShortName() {
    107     return fName;
    108 }
    109 
    110 template <typename Fn>
    111 static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) {
    112     SkPaint alpha;
    113     alpha.setAlpha(0x50);
    114     canvas->saveLayer(nullptr, &alpha);
    115         canvas->translate(x,y);
    116         canvas->scale(2,2);
    117         fn();
    118     canvas->restore();
    119 }
    120 
    121 void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) {
    122     mark(canvas, x,y, [&]{
    123         SkPaint paint;
    124 
    125         // A green circle.
    126         paint.setColor(SkColorSetRGB(27, 158, 119));
    127         canvas->drawCircle(0,0, 12, paint);
    128 
    129         // Cut out a check mark.
    130         paint.setBlendMode(SkBlendMode::kSrc);
    131         paint.setColor(0x00000000);
    132         paint.setStrokeWidth(2);
    133         paint.setStyle(SkPaint::kStroke_Style);
    134         canvas->drawLine(-6, 0,
    135                          -1, 5, paint);
    136         canvas->drawLine(-1, +5,
    137                          +7, -5, paint);
    138     });
    139 }
    140 
    141 void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) {
    142     mark(canvas, x,y, [&] {
    143         SkPaint paint;
    144 
    145         // A red circle.
    146         paint.setColor(SkColorSetRGB(231, 41, 138));
    147         canvas->drawCircle(0,0, 12, paint);
    148 
    149         // Cut out an 'X'.
    150         paint.setBlendMode(SkBlendMode::kSrc);
    151         paint.setColor(0x00000000);
    152         paint.setStrokeWidth(2);
    153         paint.setStyle(SkPaint::kStroke_Style);
    154         canvas->drawLine(-5,-5,
    155                          +5,+5, paint);
    156         canvas->drawLine(+5,-5,
    157                          -5,+5, paint);
    158     });
    159 }
    160