OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "../include/core/SkCanvas.h" | 8 #include "../include/core/SkCanvas.h" |
9 #include "../include/core/SkData.h" | 9 #include "../include/core/SkData.h" |
10 #include "../include/core/SkSurface.h" | 10 #include "../include/core/SkSurface.h" |
11 #include "../include/effects/SkGradientShader.h" | 11 #include "../include/effects/SkGradientShader.h" |
12 #include "../include/gpu/GrContext.h" | 12 #include "../include/gpu/GrContext.h" |
13 | 13 |
14 // These headers are just handy for writing this example file. Nothing Skia spe
cific. | 14 // These headers are just handy for writing this example file. Nothing Skia spe
cific. |
15 #include <cstdlib> | 15 #include <cstdlib> |
16 #include <ctime> | 16 #include <ctime> |
17 #include <fstream> | 17 #include <fstream> |
18 #include <iostream> | 18 #include <iostream> |
19 #include <memory> | 19 #include <memory> |
20 | 20 |
| 21 // These setup_gl_context() are not meant to represent good form. |
| 22 // They are just quick hacks to get us going. |
21 #if defined(__APPLE__) | 23 #if defined(__APPLE__) |
22 #include <OpenGL/OpenGL.h> | 24 #include <OpenGL/OpenGL.h> |
23 static void setup_gl_context() { | 25 static bool 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 CGLPixelFormatAttribute attributes[] = { (CGLPixelFormatAttribute)0 }; |
26 CGLPixelFormatObj format; | 27 CGLPixelFormatObj format; |
27 GLint npix; | 28 GLint npix; |
28 CGLChoosePixelFormat(attributes, &format, &npix); | 29 CGLChoosePixelFormat(attributes, &format, &npix); |
29 CGLContextObj context; | 30 CGLContextObj context; |
30 CGLCreateContext(format, nullptr, &context); | 31 CGLCreateContext(format, nullptr, &context); |
31 CGLSetCurrentContext(context); | 32 CGLSetCurrentContext(context); |
32 CGLReleasePixelFormat(format); | 33 CGLReleasePixelFormat(format); |
| 34 return true; |
| 35 } |
| 36 #else |
| 37 static bool setup_gl_context() { |
| 38 return false; |
33 } | 39 } |
34 #endif | 40 #endif |
35 | 41 |
36 // Most pointers returned by Skia are derived from SkRefCnt, | 42 // Most pointers returned by Skia are derived from SkRefCnt, |
37 // meaning we need to call ->unref() on them when done rather than delete them. | 43 // 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) { | 44 template <typename T> std::shared_ptr<T> adopt(T* ptr) { |
39 return std::shared_ptr<T>(ptr, [](T* p) { p->unref(); }); | 45 return std::shared_ptr<T>(ptr, [](T* p) { p->unref(); }); |
40 } | 46 } |
41 | 47 |
42 static std::shared_ptr<SkSurface> create_raster_surface(int w, int h) { | 48 static std::shared_ptr<SkSurface> create_raster_surface(int w, int h) { |
43 std::cout << "Using raster surface" << std::endl; | 49 std::cout << "Using raster surface" << std::endl; |
44 return adopt(SkSurface::NewRasterN32Premul(w, h)); | 50 return adopt(SkSurface::NewRasterN32Premul(w, h)); |
45 } | 51 } |
46 | 52 |
47 static std::shared_ptr<SkSurface> create_opengl_surface(int w, int h) { | 53 static std::shared_ptr<SkSurface> create_opengl_surface(int w, int h) { |
48 std::cout << "Using opengl surface" << std::endl; | 54 std::cout << "Using opengl surface" << std::endl; |
49 std::shared_ptr<GrContext> grContext = adopt(GrContext::Create(kOpenGL_GrBac
kend, 0)); | 55 std::shared_ptr<GrContext> grContext = adopt(GrContext::Create(kOpenGL_GrBac
kend, 0)); |
50 return adopt(SkSurface::NewRenderTarget(grContext.get(), | 56 return adopt(SkSurface::NewRenderTarget(grContext.get(), |
51 SkSurface::kNo_Budgeted, | 57 SkSurface::kNo_Budgeted, |
52 SkImageInfo::MakeN32Premul(w,h))); | 58 SkImageInfo::MakeN32Premul(w,h))); |
53 } | 59 } |
54 | 60 |
55 int main(int, char**) { | 61 int main(int, char**) { |
56 setup_gl_context(); | 62 bool gl_ok = setup_gl_context(); |
57 srand(time(nullptr)); | 63 srand(time(nullptr)); |
58 std::shared_ptr<SkSurface> surface = (rand() % 2) ? create_raster_surface(32
0, 240) | 64 std::shared_ptr<SkSurface> surface = (gl_ok && rand() % 2) ? create_opengl_s
urface(320, 240) |
59 : create_opengl_surface(32
0, 240); | 65 : create_raster_s
urface(320, 240); |
60 | 66 |
61 // Create a left-to-right green-to-purple gradient shader. | 67 // Create a left-to-right green-to-purple gradient shader. |
62 SkPoint pts[] = { {0,0}, {320,240} }; | 68 SkPoint pts[] = { {0,0}, {320,240} }; |
63 SkColor colors[] = { 0xFF00FF00, 0xFFFF00FF }; | 69 SkColor colors[] = { 0xFF00FF00, 0xFFFF00FF }; |
64 std::shared_ptr<SkShader> shader = adopt( | 70 std::shared_ptr<SkShader> shader = adopt( |
65 SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kR
epeat_TileMode)); | 71 SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kR
epeat_TileMode)); |
66 | 72 |
67 // Our text will draw with this paint: size 24, antialiased, with the shader
. | 73 // Our text will draw with this paint: size 24, antialiased, with the shader
. |
68 SkPaint paint; | 74 SkPaint paint; |
69 paint.setTextSize(24); | 75 paint.setTextSize(24); |
(...skipping 12 matching lines...) Expand all Loading... |
82 std::shared_ptr<SkData> png = adopt(image->encode(SkImageEncoder::kPNG_Type,
100)); | 88 std::shared_ptr<SkData> png = adopt(image->encode(SkImageEncoder::kPNG_Type,
100)); |
83 | 89 |
84 // This code is no longer Skia-specific. We just dump the .png to disk. An
y way works. | 90 // 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"; | 91 static const char* path = "example.png"; |
86 std::ofstream(path, std::ios::out | std::ios::binary) | 92 std::ofstream(path, std::ios::out | std::ios::binary) |
87 .write((const char*)png->data(), png->size()); | 93 .write((const char*)png->data(), png->size()); |
88 std::cout << "Wrote " << path << std::endl; | 94 std::cout << "Wrote " << path << std::endl; |
89 | 95 |
90 return 0; | 96 return 0; |
91 } | 97 } |
OLD | NEW |