blob: 20259b558928bdf05dde2b53d53c75402193c976 [file] [log] [blame]
mtkleine0f06a42015-08-28 11:51:06 -07001/*
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
mtklein2409abb2015-08-31 06:59:21 -070021// These setup_gl_context() are not meant to represent good form.
22// They are just quick hacks to get us going.
mtkleine0f06a42015-08-28 11:51:06 -070023#if defined(__APPLE__)
24 #include <OpenGL/OpenGL.h>
mtklein2409abb2015-08-31 06:59:21 -070025 static bool setup_gl_context() {
mtkleine0f06a42015-08-28 11:51:06 -070026 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);
mtklein2409abb2015-08-31 06:59:21 -070034 return true;
35 }
36#else
37 static bool setup_gl_context() {
38 return false;
mtkleine0f06a42015-08-28 11:51:06 -070039 }
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;
reede8f30622016-03-23 18:59:25 -070050 return adopt(SkSurface::MakeRasterN32Premul(w, h).release());
mtkleine0f06a42015-08-28 11:51:06 -070051}
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));
reede8f30622016-03-23 18:59:25 -070056 return adopt(SkSurface::MakeRenderTarget(grContext.get(),
bsalomon5ec26ae2016-02-25 08:33:02 -080057 SkBudgeted::kNo,
reede8f30622016-03-23 18:59:25 -070058 SkImageInfo::MakeN32Premul(w,h)).release());
mtkleine0f06a42015-08-28 11:51:06 -070059}
60
61int main(int, char**) {
mtklein2409abb2015-08-31 06:59:21 -070062 bool gl_ok = setup_gl_context();
mtkleine0f06a42015-08-28 11:51:06 -070063 srand(time(nullptr));
mtklein2409abb2015-08-31 06:59:21 -070064 std::shared_ptr<SkSurface> surface = (gl_ok && rand() % 2) ? create_opengl_surface(320, 240)
65 : create_raster_surface(320, 240);
mtkleine0f06a42015-08-28 11:51:06 -070066
67 // Create a left-to-right green-to-purple gradient shader.
68 SkPoint pts[] = { {0,0}, {320,240} };
69 SkColor colors[] = { 0xFF00FF00, 0xFFFF00FF };
mtkleine0f06a42015-08-28 11:51:06 -070070 // Our text will draw with this paint: size 24, antialiased, with the shader.
71 SkPaint paint;
72 paint.setTextSize(24);
73 paint.setAntiAlias(true);
reedc6f28f72016-03-14 12:22:10 -070074 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kRepeat_TileMode));
mtkleine0f06a42015-08-28 11:51:06 -070075
76 // Draw to the surface via its SkCanvas.
77 SkCanvas* canvas = surface->getCanvas(); // We don't manage this pointer's lifetime.
78 static const char* msg = "Hello world!";
79 canvas->clear(SK_ColorWHITE);
80 canvas->drawText(msg, strlen(msg), 90,120, paint);
81
82 // Grab a snapshot of the surface as an immutable SkImage.
reed2a041042016-04-15 10:56:51 -070083 sk_sp<SkImage> image = surface->makeImageSnapshot();
mtkleine0f06a42015-08-28 11:51:06 -070084 // Encode that image as a .png into a blob in memory.
85 std::shared_ptr<SkData> png = adopt(image->encode(SkImageEncoder::kPNG_Type, 100));
86
87 // This code is no longer Skia-specific. We just dump the .png to disk. Any way works.
88 static const char* path = "example.png";
89 std::ofstream(path, std::ios::out | std::ios::binary)
90 .write((const char*)png->data(), png->size());
91 std::cout << "Wrote " << path << std::endl;
92
93 return 0;
94}