• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdio>
9 #include <cstdlib>
10 #include <sstream>
11 #include <string>
12 
13 #include "src/core/SkAutoPixmapStorage.h"
14 #include "src/core/SkMipMap.h"
15 #include "src/core/SkUtils.h"
16 #include "tools/flags/CommandLineFlags.h"
17 
18 #include "tools/fiddle/fiddle_main.h"
19 
20 static DEFINE_double(duration, 1.0,
21                      "The total duration, in seconds, of the animation we are drawing.");
22 static DEFINE_double(frame, 1.0,
23                      "A double value in [0, 1] that specifies the point in animation to draw.");
24 
25 #include "include/gpu/GrBackendSurface.h"
26 #include "src/gpu/GrContextPriv.h"
27 #include "src/gpu/GrGpu.h"
28 #include "src/gpu/GrRenderTarget.h"
29 #include "tools/gpu/gl/GLTestContext.h"
30 
31 // Globals externed in fiddle_main.h
32 sk_sp<GrTexture>      backingTexture;  // not externed
33 GrBackendTexture      backEndTexture;
34 
35 sk_sp<GrRenderTarget> backingRenderTarget; // not externed
36 GrBackendRenderTarget backEndRenderTarget;
37 
38 sk_sp<GrTexture>      backingTextureRenderTarget;  // not externed
39 GrBackendTexture      backEndTextureRenderTarget;
40 
41 SkBitmap source;
42 sk_sp<SkImage> image;
43 double duration; // The total duration of the animation in seconds.
44 double frame;    // A value in [0, 1] of where we are in the animation.
45 
46 // Global used by the local impl of SkDebugf.
47 std::ostringstream gTextOutput;
48 
49 // Global to record the GL driver info via create_grcontext().
50 std::ostringstream gGLDriverInfo;
51 
SkDebugf(const char * fmt,...)52 void SkDebugf(const char * fmt, ...) {
53     va_list args;
54     va_start(args, fmt);
55     char formatbuffer[1024];
56     int n = vsnprintf(formatbuffer, sizeof(formatbuffer), fmt, args);
57     va_end(args);
58     if (n>=0 && n<=int(sizeof(formatbuffer))) {
59         gTextOutput.write(formatbuffer, n);
60     }
61 }
62 
encode_to_base64(const void * data,size_t size,FILE * out)63 static void encode_to_base64(const void* data, size_t size, FILE* out) {
64     const uint8_t* input = reinterpret_cast<const uint8_t*>(data);
65     const uint8_t* end = &input[size];
66     static const char codes[] =
67             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
68             "abcdefghijklmnopqrstuvwxyz0123456789+/";
69     while (input != end) {
70         uint8_t b = (*input & 0xFC) >> 2;
71         fputc(codes[b], out);
72         b = (*input & 0x03) << 4;
73         ++input;
74         if (input == end) {
75             fputc(codes[b], out);
76             fputs("==", out);
77             return;
78         }
79         b |= (*input & 0xF0) >> 4;
80         fputc(codes[b], out);
81         b = (*input & 0x0F) << 2;
82         ++input;
83         if (input == end) {
84             fputc(codes[b], out);
85             fputc('=', out);
86             return;
87         }
88         b |= (*input & 0xC0) >> 6;
89         fputc(codes[b], out);
90         b = *input & 0x3F;
91         fputc(codes[b], out);
92         ++input;
93     }
94 }
95 
96 
dump_output(const void * data,size_t size,const char * name,bool last=true)97 static void dump_output(const void* data, size_t size,
98                         const char* name, bool last = true) {
99     printf("\t\"%s\": \"", name);
100     encode_to_base64(data, size, stdout);
101     fputs(last ? "\"\n" : "\",\n", stdout);
102 }
103 
dump_output(const sk_sp<SkData> & data,const char * name,bool last=true)104 static void dump_output(const sk_sp<SkData>& data,
105                         const char* name, bool last = true) {
106     if (data) {
107         dump_output(data->data(), data->size(), name, last);
108     }
109 }
110 
encode_snapshot(const sk_sp<SkSurface> & surface)111 static sk_sp<SkData> encode_snapshot(const sk_sp<SkSurface>& surface) {
112     sk_sp<SkImage> img(surface->makeImageSnapshot());
113     return img ? img->encodeToData() : nullptr;
114 }
115 
prepare_canvas(SkCanvas * canvas)116 static SkCanvas* prepare_canvas(SkCanvas * canvas) {
117     canvas->clear(SK_ColorWHITE);
118     return canvas;
119 }
120 
setup_backend_objects(GrContext * context,const SkBitmap & bm,const DrawOptions & options)121 static bool setup_backend_objects(GrContext* context,
122                                   const SkBitmap& bm,
123                                   const DrawOptions& options) {
124     if (!context) {
125         return false;
126     }
127 
128     auto resourceProvider = context->priv().resourceProvider();
129 
130     GrSurfaceDesc backingDesc;
131     backingDesc.fWidth = bm.width();
132     backingDesc.fHeight = bm.height();
133     // This config must match the SkColorType used in draw.cpp in the SkImage and Surface factories
134     backingDesc.fConfig = kRGBA_8888_GrPixelConfig;
135     auto format = resourceProvider->caps()->getDefaultBackendFormat(
136             SkColorTypeToGrColorType(kRGBA_8888_SkColorType), GrRenderable::kNo);
137     auto renderableFormat = resourceProvider->caps()->getDefaultBackendFormat(
138             SkColorTypeToGrColorType(kRGBA_8888_SkColorType), GrRenderable::kYes);
139 
140     if (!bm.empty()) {
141         SkPixmap originalPixmap;
142         SkPixmap* pixmap = &originalPixmap;
143         if (!bm.peekPixels(&originalPixmap)) {
144             return false;
145         }
146 
147         SkAutoPixmapStorage rgbaPixmap;
148         if (kN32_SkColorType != kRGBA_8888_SkColorType) {
149             if (!rgbaPixmap.tryAlloc(bm.info().makeColorType(kRGBA_8888_SkColorType))) {
150                 return false;
151             }
152             if (!bm.readPixels(rgbaPixmap)) {
153                 return false;
154             }
155             pixmap = &rgbaPixmap;
156         }
157         int mipLevelCount = GrMipMapped::kYes == options.fMipMapping
158                                     ? SkMipMap::ComputeLevelCount(bm.width(), bm.height())
159                                     : 1;
160         std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
161 
162         texels[0].fPixels = pixmap->addr();
163         texels[0].fRowBytes = pixmap->rowBytes();
164 
165         for (int i = 1; i < mipLevelCount; i++) {
166             texels[i].fPixels = nullptr;
167             texels[i].fRowBytes = 0;
168         }
169 
170         backingTexture = resourceProvider->createTexture(backingDesc, format, GrRenderable::kNo, 1,
171                                                          SkBudgeted::kNo, GrProtected::kNo,
172                                                          texels.get(), mipLevelCount);
173         if (!backingTexture) {
174             return false;
175         }
176 
177         backEndTexture = backingTexture->getBackendTexture();
178         if (!backEndTexture.isValid()) {
179             return false;
180         }
181     }
182 
183     backingDesc.fWidth = options.fOffScreenWidth;
184     backingDesc.fHeight = options.fOffScreenHeight;
185 
186     SkAutoTMalloc<uint32_t> data(backingDesc.fWidth * backingDesc.fHeight);
187     sk_memset32(data.get(), 0, backingDesc.fWidth * backingDesc.fHeight);
188 
189 
190     {
191         // This backend object should be renderable but not textureable. Given the limitations
192         // of how we're creating it though it will wind up being secretly textureable.
193         // We use this fact to initialize it with data but don't allow mipmaps
194         GrMipLevel level0 = { data.get(), backingDesc.fWidth*sizeof(uint32_t) };
195 
196         sk_sp<GrTexture> tmp = resourceProvider->createTexture(
197                 backingDesc, renderableFormat, GrRenderable::kYes, options.fOffScreenSampleCount,
198                 SkBudgeted::kNo, GrProtected::kNo, &level0, 1);
199         if (!tmp || !tmp->asRenderTarget()) {
200             return false;
201         }
202 
203         backingRenderTarget = sk_ref_sp(tmp->asRenderTarget());
204 
205         backEndRenderTarget = backingRenderTarget->getBackendRenderTarget();
206         if (!backEndRenderTarget.isValid()) {
207             return false;
208         }
209     }
210 
211     {
212         int mipLevelCount = GrMipMapped::kYes == options.fOffScreenMipMapping
213                             ? SkMipMap::ComputeLevelCount(backingDesc.fWidth, backingDesc.fHeight)
214                             : 1;
215         std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
216 
217         texels[0].fPixels = data.get();
218         texels[0].fRowBytes = backingDesc.fWidth*sizeof(uint32_t);
219 
220         for (int i = 1; i < mipLevelCount; i++) {
221             texels[i].fPixels = nullptr;
222             texels[i].fRowBytes = 0;
223         }
224 
225         backingTextureRenderTarget = resourceProvider->createTexture(
226                 backingDesc, renderableFormat, GrRenderable::kYes, options.fOffScreenSampleCount,
227                 SkBudgeted::kNo, GrProtected::kNo, texels.get(), mipLevelCount);
228         if (!backingTextureRenderTarget || !backingTextureRenderTarget->asRenderTarget()) {
229             return false;
230         }
231 
232         backEndTextureRenderTarget = backingTextureRenderTarget->getBackendTexture();
233         if (!backEndTextureRenderTarget.isValid()) {
234             return false;
235         }
236     }
237 
238 
239     return true;
240 }
241 
main(int argc,char ** argv)242 int main(int argc, char** argv) {
243     CommandLineFlags::Parse(argc, argv);
244     duration = FLAGS_duration;
245     frame = FLAGS_frame;
246     DrawOptions options = GetDrawOptions();
247     // If textOnly then only do one type of image, otherwise the text
248     // output is duplicated for each type.
249     if (options.textOnly) {
250         options.raster = true;
251         options.gpu = false;
252         options.pdf = false;
253         options.skp = false;
254     }
255     if (options.source) {
256         sk_sp<SkData> data(SkData::MakeFromFileName(options.source));
257         if (!data) {
258             perror(options.source);
259             return 1;
260         } else {
261             image = SkImage::MakeFromEncoded(std::move(data));
262             if (!image) {
263                 perror("Unable to decode the source image.");
264                 return 1;
265             }
266             SkAssertResult(image->asLegacyBitmap(&source));
267         }
268     }
269     sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
270     SkColorType colorType = kN32_SkColorType;
271     sk_sp<SkColorSpace> colorSpace = nullptr;
272     if (options.f16) {
273         SkASSERT(options.srgb);
274         colorType = kRGBA_F16_SkColorType;
275         colorSpace = SkColorSpace::MakeSRGBLinear();
276     } else if (options.srgb) {
277         colorSpace = SkColorSpace::MakeSRGB();
278     }
279     SkImageInfo info = SkImageInfo::Make(options.size.width(), options.size.height(), colorType,
280                                          kPremul_SkAlphaType, colorSpace);
281     if (options.raster) {
282         auto rasterSurface = SkSurface::MakeRaster(info);
283         srand(0);
284         draw(prepare_canvas(rasterSurface->getCanvas()));
285         rasterData = encode_snapshot(rasterSurface);
286     }
287     if (options.gpu) {
288         std::unique_ptr<sk_gpu_test::GLTestContext> glContext;
289         sk_sp<GrContext> grContext = create_grcontext(gGLDriverInfo, &glContext);
290         if (!grContext) {
291             fputs("Unable to get GrContext.\n", stderr);
292         } else {
293             if (!setup_backend_objects(grContext.get(), source, options)) {
294                 fputs("Unable to create backend objects.\n", stderr);
295                 exit(1);
296             }
297 
298             auto surface = SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kNo, info);
299             if (!surface) {
300                 fputs("Unable to get render surface.\n", stderr);
301                 exit(1);
302             }
303             srand(0);
304             draw(prepare_canvas(surface->getCanvas()));
305             gpuData = encode_snapshot(surface);
306         }
307     }
308     if (options.pdf) {
309         SkDynamicMemoryWStream pdfStream;
310         auto document = SkPDF::MakeDocument(&pdfStream);
311         if (document) {
312             srand(0);
313             draw(prepare_canvas(document->beginPage(options.size.width(), options.size.height())));
314             document->close();
315             pdfData = pdfStream.detachAsData();
316         }
317     }
318     if (options.skp) {
319         SkSize size;
320         size = options.size;
321         SkPictureRecorder recorder;
322         srand(0);
323         draw(prepare_canvas(recorder.beginRecording(size.width(), size.height())));
324         auto picture = recorder.finishRecordingAsPicture();
325         SkDynamicMemoryWStream skpStream;
326         picture->serialize(&skpStream);
327         skpData = skpStream.detachAsData();
328     }
329 
330     printf("{\n");
331     if (!options.textOnly) {
332         dump_output(rasterData, "Raster", false);
333         dump_output(gpuData, "Gpu", false);
334         dump_output(pdfData, "Pdf", false);
335         dump_output(skpData, "Skp", false);
336     } else {
337         std::string textoutput = gTextOutput.str();
338         dump_output(textoutput.c_str(), textoutput.length(), "Text", false);
339     }
340     std::string glinfo = gGLDriverInfo.str();
341     dump_output(glinfo.c_str(), glinfo.length(), "GLInfo", true);
342     printf("}\n");
343 
344     return 0;
345 }
346