Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: cmake/example.cpp

Issue 1319543003: Example CMake build for Skia. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: sort Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« cmake/CMakeLists.txt ('K') | « cmake/README.md ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 spe cific.
15 #include <cstdlib>
16 #include <ctime>
17 #include <fstream>
18 #include <iostream>
19 #include <memory>
20
21 #if defined(__APPLE__)
22 #include <OpenGL/OpenGL.h>
23 static void setup_gl_context() {
24 // This is not meant to represent good form. It's just a quick hack to get us going.
25 CGLPixelFormatAttribute attributes[] = { (CGLPixelFormatAttribute)0 };
26 CGLPixelFormatObj format;
27 GLint npix;
28 CGLChoosePixelFormat(attributes, &format, &npix);
29 CGLContextObj context;
30 CGLCreateContext(format, nullptr, &context);
31 CGLSetCurrentContext(context);
32 CGLReleasePixelFormat(format);
33 }
34 #endif
35
36 // Most pointers returned by Skia are derived from SkRefCnt,
37 // meaning we need to call ->unref() on them when done rather than delete them.
38 template <typename T> std::shared_ptr<T> adopt(T* ptr) {
39 return std::shared_ptr<T>(ptr, [](T* p) { p->unref(); });
40 }
41
42 static std::shared_ptr<SkSurface> create_raster_surface(int w, int h) {
43 std::cout << "Using raster surface" << std::endl;
44 return adopt(SkSurface::NewRasterN32Premul(w, h));
45 }
46
47 static std::shared_ptr<SkSurface> create_opengl_surface(int w, int h) {
48 std::cout << "Using opengl surface" << std::endl;
49 std::shared_ptr<GrContext> grContext = adopt(GrContext::Create(kOpenGL_GrBac kend, 0));
50 return adopt(SkSurface::NewRenderTarget(grContext.get(),
51 SkSurface::kNo_Budgeted,
52 SkImageInfo::MakeN32Premul(w,h)));
53 }
54
55 int main(int, char**) {
56 setup_gl_context();
57 srand(time(nullptr));
58 std::shared_ptr<SkSurface> surface = (rand() % 2) ? create_raster_surface(32 0, 240)
59 : create_opengl_surface(32 0, 240);
60
61 // Create a left-to-right green-to-purple gradient shader.
62 SkPoint pts[] = { {0,0}, {320,240} };
63 SkColor colors[] = { 0xFF00FF00, 0xFFFF00FF };
64 std::shared_ptr<SkShader> shader = adopt(
65 SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kR epeat_TileMode));
66
67 // Our text will draw with this paint: size 24, antialiased, with the shader .
68 SkPaint paint;
69 paint.setTextSize(24);
70 paint.setAntiAlias(true);
71 paint.setShader(shader.get());
72
73 // Draw to the surface via its SkCanvas.
74 SkCanvas* canvas = surface->getCanvas(); // We don't manage this pointer's lifetime.
75 static const char* msg = "Hello world!";
76 canvas->clear(SK_ColorWHITE);
77 canvas->drawText(msg, strlen(msg), 90,120, paint);
78
79 // Grab a snapshot of the surface as an immutable SkImage.
80 std::shared_ptr<SkImage> image = adopt(surface->newImageSnapshot());
81 // Encode that image as a .png into a blob in memory.
82 std::shared_ptr<SkData> png = adopt(image->encode(SkImageEncoder::kPNG_Type, 100));
83
84 // This code is no longer Skia-specific. We just dump the .png to disk. An y way works.
85 static const char* path = "example.png";
86 std::ofstream(path, std::ios::out | std::ios::binary)
87 .write((const char*)png->data(), png->size());
88 std::cout << "Wrote " << path << std::endl;
89
90 return 0;
91 }
OLDNEW
« cmake/CMakeLists.txt ('K') | « cmake/README.md ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698