Home | History | Annotate | Download | only in example
      1 /*
      2  * Copyright 2015 Google Inc.
      3  *
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  *
      8  */
      9 
     10 #include "HelloWorld.h"
     11 
     12 #include "gl/GrGLInterface.h"
     13 #include "GrContext.h"
     14 #include "SkApplication.h"
     15 #include "SkCanvas.h"
     16 #include "SkGradientShader.h"
     17 #include "SkGraphics.h"
     18 #include "SkGr.h"
     19 
     20 void application_init() {
     21     SkGraphics::Init();
     22     SkEvent::Init();
     23 }
     24 
     25 void application_term() {
     26     SkEvent::Term();
     27 }
     28 
     29 HelloWorldWindow::HelloWorldWindow(void* hwnd)
     30     : INHERITED(hwnd) {
     31     fType = kGPU_DeviceType;
     32     fRotationAngle = 0;
     33     this->setTitle();
     34     this->setUpBackend();
     35 }
     36 
     37 HelloWorldWindow::~HelloWorldWindow() {
     38     tearDownBackend();
     39 }
     40 
     41 void HelloWorldWindow::tearDownBackend() {
     42     SkSafeUnref(fContext);
     43     fContext = NULL;
     44 
     45     SkSafeUnref(fInterface);
     46     fInterface = NULL;
     47 
     48     fGpuSurface = nullptr;
     49 
     50     INHERITED::release();
     51 }
     52 
     53 void HelloWorldWindow::setTitle() {
     54     SkString title("Hello World ");
     55     title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl");
     56     INHERITED::setTitle(title.c_str());
     57 }
     58 
     59 bool HelloWorldWindow::setUpBackend() {
     60     this->setVisibleP(true);
     61     this->setClipToBounds(false);
     62 
     63     bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, false, &fAttachmentInfo);
     64     if (false == result) {
     65         SkDebugf("Not possible to create backend.\n");
     66         release();
     67         return false;
     68     }
     69 
     70     fInterface = GrGLCreateNativeInterface();
     71     SkASSERT(NULL != fInterface);
     72 
     73     fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface);
     74     SkASSERT(NULL != fContext);
     75 
     76     this->setUpGpuBackedSurface();
     77     return true;
     78 }
     79 
     80 void HelloWorldWindow::setUpGpuBackedSurface() {
     81     fGpuSurface = this->makeGpuBackedSurface(fAttachmentInfo, fInterface, fContext);
     82 }
     83 
     84 void HelloWorldWindow::drawContents(SkCanvas* canvas) {
     85     // Clear background
     86     canvas->drawColor(SK_ColorWHITE);
     87 
     88     SkPaint paint;
     89     paint.setColor(SK_ColorRED);
     90 
     91     // Draw a rectangle with red paint
     92     SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
     93     canvas->drawRect(rect, paint);
     94 
     95     // Set up a linear gradient and draw a circle
     96     {
     97         SkPoint linearPoints[] = {
     98                 {0, 0},
     99                 {300, 300}
    100         };
    101         SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK};
    102 
    103         paint.setShader(SkGradientShader::MakeLinear(
    104                 linearPoints, linearColors, nullptr, 2,
    105                 SkShader::kMirror_TileMode));
    106         paint.setFlags(SkPaint::kAntiAlias_Flag);
    107 
    108         canvas->drawCircle(200, 200, 64, paint);
    109 
    110         // Detach shader
    111         paint.setShader(nullptr);
    112     }
    113 
    114     // Draw a message with a nice black paint.
    115     paint.setFlags(
    116             SkPaint::kAntiAlias_Flag |
    117             SkPaint::kSubpixelText_Flag |  // ... avoid waggly text when rotating.
    118             SkPaint::kUnderlineText_Flag);
    119     paint.setColor(SK_ColorBLACK);
    120     paint.setTextSize(20);
    121 
    122     canvas->save();
    123 
    124     static const char message[] = "Hello World";
    125 
    126     // Translate and rotate
    127     canvas->translate(300, 300);
    128     fRotationAngle += 0.2f;
    129     if (fRotationAngle > 360) {
    130         fRotationAngle -= 360;
    131     }
    132     canvas->rotate(fRotationAngle);
    133 
    134     // Draw the text:
    135     canvas->drawText(message, strlen(message), 0, 0, paint);
    136 
    137     canvas->restore();
    138 }
    139 
    140 void HelloWorldWindow::draw(SkCanvas* canvas) {
    141     this->drawContents(canvas);
    142     // Invalidate the window to force a redraw. Poor man's animation mechanism.
    143     this->inval(NULL);
    144 
    145     if (kRaster_DeviceType == fType) {
    146         fRasterSurface->draw(fGpuSurface->getCanvas(), 0, 0, nullptr);
    147     }
    148     fGpuSurface->getCanvas()->flush();
    149     INHERITED::present();
    150 }
    151 
    152 void HelloWorldWindow::onSizeChange() {
    153     this->setUpGpuBackedSurface();
    154 }
    155 
    156 bool HelloWorldWindow::onHandleChar(SkUnichar unichar) {
    157     if (' ' == unichar) {
    158         fType = fType == kRaster_DeviceType ? kGPU_DeviceType: kRaster_DeviceType;
    159         tearDownBackend();
    160         setUpBackend();
    161         this->setTitle();
    162         this->inval(NULL);
    163     }
    164     return true;
    165 }
    166 
    167 SkOSWindow* create_sk_window(void* hwnd, int , char** ) {
    168     return new HelloWorldWindow(hwnd);
    169 }
    170