1/* 2 * Copyright 2015 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 "../include/core/SkCanvas.h" 9#include "../include/core/SkData.h" 10#include "../include/core/SkSurface.h" 11#include "../include/effects/SkGradientShader.h" 12#include "../include/gpu/GrContext.h" 13 14// These headers are just handy for writing this example file. Nothing Skia specific. 15#include <cstdlib> 16#include <ctime> 17#include <fstream> 18#include <iostream> 19#include <memory> 20 21// These setup_gl_context() are not meant to represent good form. 22// They are just quick hacks to get us going. 23#if defined(__APPLE__) 24 #include <OpenGL/OpenGL.h> 25 static bool setup_gl_context() { 26 CGLPixelFormatAttribute attributes[] = { (CGLPixelFormatAttribute)0 }; 27 CGLPixelFormatObj format; 28 GLint npix; 29 CGLChoosePixelFormat(attributes, &format, &npix); 30 CGLContextObj context; 31 CGLCreateContext(format, nullptr, &context); 32 CGLSetCurrentContext(context); 33 CGLReleasePixelFormat(format); 34 return true; 35 } 36#else 37 static bool setup_gl_context() { 38 return false; 39 } 40#endif 41 42// Most pointers returned by Skia are derived from SkRefCnt, 43// meaning we need to call ->unref() on them when done rather than delete them. 44template <typename T> std::shared_ptr<T> adopt(T* ptr) { 45 return std::shared_ptr<T>(ptr, [](T* p) { p->unref(); }); 46} 47 48static std::shared_ptr<SkSurface> create_raster_surface(int w, int h) { 49 std::cout << "Using raster surface" << std::endl; 50 return adopt(SkSurface::NewRasterN32Premul(w, h)); 51} 52 53static std::shared_ptr<SkSurface> create_opengl_surface(int w, int h) { 54 std::cout << "Using opengl surface" << std::endl; 55 std::shared_ptr<GrContext> grContext = adopt(GrContext::Create(kOpenGL_GrBackend, 0)); 56 return adopt(SkSurface::NewRenderTarget(grContext.get(), 57 SkBudgeted::kNo, 58 SkImageInfo::MakeN32Premul(w,h))); 59} 60 61int main(int, char**) { 62 bool gl_ok = setup_gl_context(); 63 srand(time(nullptr)); 64 std::shared_ptr<SkSurface> surface = (gl_ok && rand() % 2) ? create_opengl_surface(320, 240) 65 : create_raster_surface(320, 240); 66 67 // Create a left-to-right green-to-purple gradient shader. 68 SkPoint pts[] = { {0,0}, {320,240} }; 69 SkColor colors[] = { 0xFF00FF00, 0xFFFF00FF }; 70 std::shared_ptr<SkShader> shader = adopt( 71 SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kRepeat_TileMode)); 72 73 // Our text will draw with this paint: size 24, antialiased, with the shader. 74 SkPaint paint; 75 paint.setTextSize(24); 76 paint.setAntiAlias(true); 77 paint.setShader(shader.get()); 78 79 // Draw to the surface via its SkCanvas. 80 SkCanvas* canvas = surface->getCanvas(); // We don't manage this pointer's lifetime. 81 static const char* msg = "Hello world!"; 82 canvas->clear(SK_ColorWHITE); 83 canvas->drawText(msg, strlen(msg), 90,120, paint); 84 85 // Grab a snapshot of the surface as an immutable SkImage. 86 std::shared_ptr<SkImage> image = adopt(surface->newImageSnapshot()); 87 // Encode that image as a .png into a blob in memory. 88 std::shared_ptr<SkData> png = adopt(image->encode(SkImageEncoder::kPNG_Type, 100)); 89 90 // This code is no longer Skia-specific. We just dump the .png to disk. Any way works. 91 static const char* path = "example.png"; 92 std::ofstream(path, std::ios::out | std::ios::binary) 93 .write((const char*)png->data(), png->size()); 94 std::cout << "Wrote " << path << std::endl; 95 96 return 0; 97} 98