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 30 matching lines...) Expand all Loading... |
41 return; | 41 return; |
42 } | 42 } |
43 b |= (*input & 0xC0) >> 6; | 43 b |= (*input & 0xC0) >> 6; |
44 fputc(codes[b], out); | 44 fputc(codes[b], out); |
45 b = *input & 0x3F; | 45 b = *input & 0x3F; |
46 fputc(codes[b], out); | 46 fputc(codes[b], out); |
47 ++input; | 47 ++input; |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 static void dump_output(SkData* data, const char* name, bool last = true) { | 51 static void dump_output(const sk_sp<SkData>& data, |
| 52 const char* name, bool last = true) { |
52 if (data) { | 53 if (data) { |
53 printf("\t\"%s\": \"", name); | 54 printf("\t\"%s\": \"", name); |
54 encode_to_base64(data->data(), data->size(), stdout); | 55 encode_to_base64(data->data(), data->size(), stdout); |
55 fputs(last ? "\"\n" : "\",\n", stdout); | 56 fputs(last ? "\"\n" : "\",\n", stdout); |
56 } | 57 } |
57 } | 58 } |
58 | 59 |
59 static SkData* encode_snapshot(SkSurface* surface) { | 60 static SkData* encode_snapshot(const sk_sp<SkSurface>& surface) { |
60 SkAutoTUnref<SkImage> img(surface->newImageSnapshot()); | 61 sk_sp<SkImage> img(surface->newImageSnapshot()); |
61 return img ? img->encode() : nullptr; | 62 return img ? img->encode() : nullptr; |
62 } | 63 } |
63 | 64 |
64 static OSMesaContext create_osmesa_context() { | 65 static OSMesaContext create_osmesa_context() { |
65 OSMesaContext osMesaContext = | 66 OSMesaContext osMesaContext = |
66 OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr); | 67 OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr); |
67 if (osMesaContext != nullptr) { | 68 if (osMesaContext != nullptr) { |
68 static uint32_t buffer[16 * 16]; | 69 static uint32_t buffer[16 * 16]; |
69 OSMesaMakeCurrent(osMesaContext, &buffer, GL_UNSIGNED_BYTE, 16, 16); | 70 OSMesaMakeCurrent(osMesaContext, &buffer, GL_UNSIGNED_BYTE, 16, 16); |
70 } | 71 } |
71 return osMesaContext; | 72 return osMesaContext; |
72 } | 73 } |
73 | 74 |
74 static GrContext* create_mesa_grcontext() { | 75 static GrContext* create_mesa_grcontext() { |
75 SkAutoTUnref<const GrGLInterface> mesa(GrGLCreateMesaInterface()); | 76 sk_sp<const GrGLInterface> mesa(GrGLCreateMesaInterface()); |
76 intptr_t backend = reinterpret_cast<intptr_t>(mesa.get()); | 77 intptr_t backend = reinterpret_cast<intptr_t>(mesa.get()); |
77 return backend ? GrContext::Create(kOpenGL_GrBackend, backend) : nullptr; | 78 return backend ? GrContext::Create(kOpenGL_GrBackend, backend) : nullptr; |
78 } | 79 } |
79 | 80 |
80 | 81 |
81 int main() { | 82 int main() { |
82 const DrawOptions options = GetDrawOptions(); | 83 const DrawOptions options = GetDrawOptions(); |
83 fprintf(stderr, "%s\n", options.source); | 84 fprintf(stderr, "%s\n", options.source); |
84 if (options.source) { | 85 if (options.source) { |
85 SkAutoTUnref<SkData> data(SkData::NewFromFileName(options.source)); | 86 sk_sp<SkData> data(SkData::NewFromFileName(options.source)); |
86 if (!data) { | 87 if (!data) { |
87 perror(options.source); | 88 perror(options.source); |
88 return 1; | 89 return 1; |
89 } else { | 90 } else { |
90 image = SkImage::NewFromEncoded(data); | 91 image = SkImage::NewFromEncoded(data.get()); |
91 if (!image) { | 92 if (!image) { |
92 perror("Unable to decode the source image."); | 93 perror("Unable to decode the source image."); |
93 return 1; | 94 return 1; |
94 } | 95 } |
95 SkAssertResult(image->asLegacyBitmap( | 96 SkAssertResult(image->asLegacyBitmap( |
96 &source, SkImage::kRO_LegacyBitmapMode)); | 97 &source, SkImage::kRO_LegacyBitmapMode)); |
97 } | 98 } |
98 } | 99 } |
99 SkAutoTUnref<SkData> rasterData, gpuData, pdfData, skpData; | 100 sk_sp<SkData> rasterData, gpuData, pdfData, skpData; |
100 if (options.raster) { | 101 if (options.raster) { |
101 SkAutoTUnref<SkSurface> rasterSurface( | 102 auto rasterSurface = |
102 SkSurface::NewRaster(SkImageInfo::MakeN32Premul(options.size))); | 103 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size)); |
103 draw(rasterSurface->getCanvas()); | 104 draw(rasterSurface->getCanvas()); |
104 rasterData.reset(encode_snapshot(rasterSurface)); | 105 rasterData.reset(encode_snapshot(rasterSurface)); |
105 } | 106 } |
106 if (options.gpu) { | 107 if (options.gpu) { |
107 OSMesaContext osMesaContext = create_osmesa_context(); | 108 OSMesaContext osMesaContext = create_osmesa_context(); |
108 SkAutoTUnref<GrContext> grContext(create_mesa_grcontext()); | 109 sk_sp<GrContext> grContext(create_mesa_grcontext()); |
109 if (!grContext) { | 110 if (!grContext) { |
110 fputs("Unable to get Mesa GrContext.\n", stderr); | 111 fputs("Unable to get Mesa GrContext.\n", stderr); |
111 } else { | 112 } else { |
112 SkAutoTUnref<SkSurface> surface( | 113 auto surface = SkSurface::MakeRenderTarget( |
113 SkSurface::NewRenderTarget( | 114 grContext.get(), |
114 grContext, | 115 SkBudgeted::kNo, |
115 SkBudgeted::kNo, | 116 SkImageInfo::MakeN32Premul(options.size)); |
116 SkImageInfo::MakeN32Premul(options.size))); | |
117 if (!surface) { | 117 if (!surface) { |
118 fputs("Unable to get render surface.\n", stderr); | 118 fputs("Unable to get render surface.\n", stderr); |
119 exit(1); | 119 exit(1); |
120 } | 120 } |
121 draw(surface->getCanvas()); | 121 draw(surface->getCanvas()); |
122 gpuData.reset(encode_snapshot(surface)); | 122 gpuData.reset(encode_snapshot(surface)); |
123 } | 123 } |
124 if (osMesaContext) { | 124 if (osMesaContext) { |
125 OSMesaDestroyContext(osMesaContext); | 125 OSMesaDestroyContext(osMesaContext); |
126 } | 126 } |
127 } | 127 } |
128 if (options.pdf) { | 128 if (options.pdf) { |
129 SkDynamicMemoryWStream pdfStream; | 129 SkDynamicMemoryWStream pdfStream; |
130 SkAutoTUnref<SkDocument> document(SkDocument::CreatePDF(&pdfStream)); | 130 sk_sp<SkDocument> document(SkDocument::CreatePDF(&pdfStream)); |
131 draw(document->beginPage(options.size.width(), options.size.height())); | 131 draw(document->beginPage(options.size.width(), options.size.height())); |
132 document->close(); | 132 document->close(); |
133 pdfData.reset(pdfStream.copyToData()); | 133 pdfData.reset(pdfStream.copyToData()); |
134 } | 134 } |
135 if (options.skp) { | 135 if (options.skp) { |
136 SkSize size; | 136 SkSize size; |
137 size = options.size; | 137 size = options.size; |
138 SkPictureRecorder recorder; | 138 SkPictureRecorder recorder; |
139 draw(recorder.beginRecording(size.width(), size.height())); | 139 draw(recorder.beginRecording(size.width(), size.height())); |
140 SkAutoTUnref<SkPicture> picture(recorder.endRecordingAsPicture()); | 140 auto picture = recorder.finishRecordingAsPicture(); |
141 SkDynamicMemoryWStream skpStream; | 141 SkDynamicMemoryWStream skpStream; |
142 picture->serialize(&skpStream); | 142 picture->serialize(&skpStream); |
143 skpData.reset(skpStream.copyToData()); | 143 skpData.reset(skpStream.copyToData()); |
144 } | 144 } |
145 | 145 |
146 printf("{\n"); | 146 printf("{\n"); |
147 dump_output(rasterData, "Raster", !gpuData && !pdfData && !skpData); | 147 dump_output(rasterData, "Raster", !gpuData && !pdfData && !skpData); |
148 dump_output(gpuData, "Gpu", !pdfData && !skpData); | 148 dump_output(gpuData, "Gpu", !pdfData && !skpData); |
149 dump_output(pdfData, "Pdf", !skpData); | 149 dump_output(pdfData, "Pdf", !skpData); |
150 dump_output(skpData, "Skp"); | 150 dump_output(skpData, "Skp"); |
151 printf("}\n"); | 151 printf("}\n"); |
152 | 152 |
153 SkSafeSetNull(image); | 153 SkSafeSetNull(image); |
154 return 0; | 154 return 0; |
155 } | 155 } |
OLD | NEW |