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 <stdio.h> | 8 #include <stdio.h> |
9 | 9 |
10 #include <GL/osmesa.h> | 10 #include <GL/osmesa.h> |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 static OSMesaContext create_osmesa_context() { | 65 static OSMesaContext create_osmesa_context() { |
66 OSMesaContext osMesaContext = | 66 OSMesaContext osMesaContext = |
67 OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr); | 67 OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr); |
68 if (osMesaContext != nullptr) { | 68 if (osMesaContext != nullptr) { |
69 static uint32_t buffer[16 * 16]; | 69 static uint32_t buffer[16 * 16]; |
70 OSMesaMakeCurrent(osMesaContext, &buffer, GL_UNSIGNED_BYTE, 16, 16); | 70 OSMesaMakeCurrent(osMesaContext, &buffer, GL_UNSIGNED_BYTE, 16, 16); |
71 } | 71 } |
72 return osMesaContext; | 72 return osMesaContext; |
73 } | 73 } |
74 | 74 |
75 static GrContext* create_mesa_grcontext() { | 75 static sk_sp<GrContext> create_mesa_grcontext() { |
76 sk_sp<const GrGLInterface> mesa(GrGLCreateMesaInterface()); | 76 if (nullptr == OSMesaGetCurrentContext()) { |
77 intptr_t backend = reinterpret_cast<intptr_t>(mesa.get()); | 77 return nullptr; |
78 return backend ? GrContext::Create(kOpenGL_GrBackend, backend) : nullptr; | 78 } |
| 79 auto osmesa_get = [](void* ctx, const char name[]) { |
| 80 SkASSERT(nullptr == ctx); |
| 81 SkASSERT(OSMesaGetCurrentContext()); |
| 82 return OSMesaGetProcAddress(name); |
| 83 }; |
| 84 sk_sp<const GrGLInterface> mesa(GrGLAssembleInterface(nullptr, osmesa_get)); |
| 85 if (!mesa) { |
| 86 return nullptr; |
| 87 } |
| 88 return sk_sp<GrContext>(GrContext::Create( |
| 89 kOpenGL_GrBackend, |
| 90 reinterpret_cast<intptr_t>(mesa.get()))); |
79 } | 91 } |
80 | 92 |
81 | |
82 int main() { | 93 int main() { |
83 const DrawOptions options = GetDrawOptions(); | 94 const DrawOptions options = GetDrawOptions(); |
84 if (options.source) { | 95 if (options.source) { |
85 sk_sp<SkData> data(SkData::NewFromFileName(options.source)); | 96 sk_sp<SkData> data(SkData::NewFromFileName(options.source)); |
86 if (!data) { | 97 if (!data) { |
87 perror(options.source); | 98 perror(options.source); |
88 return 1; | 99 return 1; |
89 } else { | 100 } else { |
90 image = SkImage::NewFromEncoded(data.get()); | 101 image = SkImage::NewFromEncoded(data.get()); |
91 if (!image) { | 102 if (!image) { |
92 perror("Unable to decode the source image."); | 103 perror("Unable to decode the source image."); |
93 return 1; | 104 return 1; |
94 } | 105 } |
95 SkAssertResult(image->asLegacyBitmap( | 106 SkAssertResult(image->asLegacyBitmap( |
96 &source, SkImage::kRO_LegacyBitmapMode)); | 107 &source, SkImage::kRO_LegacyBitmapMode)); |
97 } | 108 } |
98 } | 109 } |
99 sk_sp<SkData> rasterData, gpuData, pdfData, skpData; | 110 sk_sp<SkData> rasterData, gpuData, pdfData, skpData; |
100 if (options.raster) { | 111 if (options.raster) { |
101 auto rasterSurface = | 112 auto rasterSurface = |
102 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size)); | 113 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size)); |
103 draw(rasterSurface->getCanvas()); | 114 draw(rasterSurface->getCanvas()); |
104 rasterData.reset(encode_snapshot(rasterSurface)); | 115 rasterData.reset(encode_snapshot(rasterSurface)); |
105 } | 116 } |
106 if (options.gpu) { | 117 if (options.gpu) { |
107 OSMesaContext osMesaContext = create_osmesa_context(); | 118 OSMesaContext osMesaContext = create_osmesa_context(); |
108 sk_sp<GrContext> grContext(create_mesa_grcontext()); | 119 auto grContext = create_mesa_grcontext(); |
109 if (!grContext) { | 120 if (!grContext) { |
110 fputs("Unable to get Mesa GrContext.\n", stderr); | 121 fputs("Unable to get Mesa GrContext.\n", stderr); |
111 } else { | 122 } else { |
112 auto surface = SkSurface::MakeRenderTarget( | 123 auto surface = SkSurface::MakeRenderTarget( |
113 grContext.get(), | 124 grContext.get(), |
114 SkBudgeted::kNo, | 125 SkBudgeted::kNo, |
115 SkImageInfo::MakeN32Premul(options.size)); | 126 SkImageInfo::MakeN32Premul(options.size)); |
116 if (!surface) { | 127 if (!surface) { |
117 fputs("Unable to get render surface.\n", stderr); | 128 fputs("Unable to get render surface.\n", stderr); |
118 exit(1); | 129 exit(1); |
(...skipping 26 matching lines...) Expand all Loading... |
145 printf("{\n"); | 156 printf("{\n"); |
146 dump_output(rasterData, "Raster", !gpuData && !pdfData && !skpData); | 157 dump_output(rasterData, "Raster", !gpuData && !pdfData && !skpData); |
147 dump_output(gpuData, "Gpu", !pdfData && !skpData); | 158 dump_output(gpuData, "Gpu", !pdfData && !skpData); |
148 dump_output(pdfData, "Pdf", !skpData); | 159 dump_output(pdfData, "Pdf", !skpData); |
149 dump_output(skpData, "Skp"); | 160 dump_output(skpData, "Skp"); |
150 printf("}\n"); | 161 printf("}\n"); |
151 | 162 |
152 SkSafeSetNull(image); | 163 SkSafeSetNull(image); |
153 return 0; | 164 return 0; |
154 } | 165 } |
OLD | NEW |