| 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/codec/SkCodec.h" |
| 9 |
#include "include/codec/SkJpegDecoder.h" |
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkPicture.h" |
| 12 |
#include "include/encode/SkPngEncoder.h" |
| 13 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 14 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 15 |
#include "include/private/base/SkLog.h" |
| 16 |
#include "src/core/SkAutoPixmapStorage.h" |
| 17 |
#include "src/core/SkMemset.h" |
| 18 |
#include "src/core/SkMipmap.h" |
| 19 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 20 |
#include "src/gpu/ganesh/GrGpu.h" |
| 21 |
#include "src/gpu/ganesh/GrRenderTarget.h" |
| 22 |
#include "src/gpu/ganesh/GrResourceProvider.h" |
| 23 |
#include "src/gpu/ganesh/GrTexture.h" |
| 24 |
|
| 25 |
#include "tools/flags/CommandLineFlags.h" |
| 26 |
#include "tools/ganesh/gl/GLTestContext.h" |
| 27 |
#include "tools/gpu/ManagedBackendTexture.h" |
| 28 |
|
| 29 |
#if defined(SK_CODEC_DECODES_PNG_WITH_LIBPNG) |
| 30 |
#include "include/codec/SkPngDecoder.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#if defined(SK_SUPPORT_PDF) |
| 34 |
#if !defined(SK_CODEC_DECODES_JPEG) || !defined(SK_CODEC_ENCODES_JPEG) |
| 35 |
#error "Need jpeg for PDF backend" |
| 36 |
#endif |
| 37 |
#include "include/docs/SkPDFDocument.h" |
| 38 |
#include "include/docs/SkPDFJpegHelpers.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE) |
| 42 |
#include "include/ports/SkFontMgr_fontconfig.h" |
| 43 |
#include "include/ports/SkFontScanner_FreeType.h" |
| 44 |
#endif |
| 45 |
|
| 46 |
#include <cstdio> |
| 47 |
#include <cstdlib> |
| 48 |
#include <sstream> |
| 49 |
#include <string> |
| 50 |
|
| 51 |
// fiddle_main.h (purposefully) pollutes the global namespace with very generic identifiers like |
| 52 |
// "image", "duration", "frame", and "fontMgr". As such it is something of an |
| 53 |
// "implementation header" and should be included last to avoid name shadowing warnings. |
| 54 |
#include "tools/fiddle/fiddle_main.h" |
| 55 |
|
| 56 |
using namespace skia_private; |
| 57 |
|
| 58 |
static DEFINE_double(duration, 1.0, |
| 59 |
"The total duration, in seconds, of the animation we are drawing."); |
| 60 |
static DEFINE_double(frame, 1.0, |
| 61 |
"A double value in [0, 1] that specifies the point in animation to draw."); |
| 62 |
|
| 63 |
// Globals externed in fiddle_main.h |
| 64 |
GrBackendTexture backEndTexture; |
| 65 |
GrBackendRenderTarget backEndRenderTarget; |
| 66 |
GrBackendTexture backEndTextureRenderTarget; |
| 67 |
SkBitmap source; |
| 68 |
sk_sp<SkImage> image; |
| 69 |
double duration; // The total duration of the animation in seconds. |
| 70 |
double frame; // A value in [0, 1] of where we are in the animation. |
| 71 |
sk_sp<SkFontMgr> fontMgr; |
| 72 |
|
| 73 |
// Global used by the local impl of SkLogVAList. |
| 74 |
std::ostringstream gTextOutput; |
| 75 |
|
| 76 |
// Global to record the GL driver info via create_direct_context(). |
| 77 |
std::ostringstream gGLDriverInfo; |
| 78 |
|
| 79 |
sk_sp<sk_gpu_test::ManagedBackendTexture> managedBackendTextureRenderTarget; |
| 80 |
sk_sp<sk_gpu_test::ManagedBackendTexture> managedBackendTexture; |
| 81 |
sk_sp<GrRenderTarget> backingRenderTarget; |
| 82 |
|
| 83 |
void SkLogVAList(SkLogPriority priority, const char format[], va_list args) { |
| 84 |
char formatbuffer[1024]; |
| 85 |
int n = vsnprintf(formatbuffer, sizeof(formatbuffer), format, args); |
| 86 |
if (n>=0 && n<=int(sizeof(formatbuffer))) { |
| 87 |
gTextOutput.write(formatbuffer, n); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static void encode_to_base64(const void* data, size_t size, FILE* out) { |
| 92 |
const uint8_t* input = reinterpret_cast<const uint8_t*>(data); |
| 93 |
const uint8_t* end = &input[size]; |
| 94 |
static const char codes[] = |
| 95 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 96 |
"abcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 97 |
while (input != end) { |
| 98 |
uint8_t b = (*input & 0xFC) >> 2; |
| 99 |
fputc(codes[b], out); |
| 100 |
b = (*input & 0x03) << 4; |
| 101 |
++input; |
| 102 |
if (input == end) { |
| 103 |
fputc(codes[b], out); |
| 104 |
fputs("==", out); |
| 105 |
return; |
| 106 |
} |
| 107 |
b |= (*input & 0xF0) >> 4; |
| 108 |
fputc(codes[b], out); |
| 109 |
b = (*input & 0x0F) << 2; |
| 110 |
++input; |
| 111 |
if (input == end) { |
| 112 |
fputc(codes[b], out); |
| 113 |
fputc('=', out); |
| 114 |
return; |
| 115 |
} |
| 116 |
b |= (*input & 0xC0) >> 6; |
| 117 |
fputc(codes[b], out); |
| 118 |
b = *input & 0x3F; |
| 119 |
fputc(codes[b], out); |
| 120 |
++input; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
static void dump_output(const void* data, size_t size, |
| 126 |
const char* name, bool last = true) { |
| 127 |
printf("\t\"%s\": \"", name); |
| 128 |
encode_to_base64(data, size, stdout); |
| 129 |
fputs(last ? "\"\n" : "\",\n", stdout); |
| 130 |
} |
| 131 |
|
| 132 |
static void dump_output(const sk_sp<SkData>& data, |
| 133 |
const char* name, bool last = true) { |
| 134 |
if (data) { |
| 135 |
dump_output(data->data(), data->size(), name, last); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
static sk_sp<SkData> encode_snapshot(GrDirectContext* ctx, const sk_sp<SkSurface>& surface) { |
| 140 |
sk_sp<SkImage> img(surface->makeImageSnapshot()); |
| 141 |
return SkPngEncoder::Encode(ctx, img.get(), {}); |
| 142 |
} |
| 143 |
|
| 144 |
static SkCanvas* prepare_canvas(SkCanvas * canvas) { |
| 145 |
canvas->clear(SK_ColorWHITE); |
| 146 |
return canvas; |
| 147 |
} |
| 148 |
|
| 149 |
#ifdef SK_GL |
| 150 |
static bool setup_backend_objects(GrDirectContext* dContext, |
| 151 |
const SkBitmap& bm, |
| 152 |
const DrawOptions& options) { |
| 153 |
if (!dContext) { |
| 154 |
fputs("Context is null.\n", stderr); |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
// This config must match the SkColorType used in draw.cpp in the SkImage and Surface factories |
| 159 |
GrBackendFormat renderableFormat = dContext->defaultBackendFormat(kRGBA_8888_SkColorType, |
| 160 |
GrRenderable::kYes); |
| 161 |
|
| 162 |
if (!bm.empty()) { |
| 163 |
SkPixmap originalPixmap; |
| 164 |
SkPixmap* pixmap = &originalPixmap; |
| 165 |
if (!bm.peekPixels(&originalPixmap)) { |
| 166 |
fputs("Unable to peekPixels.\n", stderr); |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
SkAutoPixmapStorage rgbaPixmap; |
| 171 |
constexpr bool kRGBAIsNative = kN32_SkColorType == kRGBA_8888_SkColorType; |
| 172 |
if ((!kRGBAIsNative)) { |
| 173 |
if (!rgbaPixmap.tryAlloc(bm.info().makeColorType(kRGBA_8888_SkColorType))) { |
| 174 |
fputs("Unable to alloc rgbaPixmap.\n", stderr); |
| 175 |
return false; |
| 176 |
} |
| 177 |
if (!bm.readPixels(rgbaPixmap)) { |
| 178 |
fputs("Unable to read rgbaPixmap.\n", stderr); |
| 179 |
return false; |
| 180 |
} |
| 181 |
pixmap = &rgbaPixmap; |
| 182 |
} |
| 183 |
|
| 184 |
managedBackendTexture = sk_gpu_test::ManagedBackendTexture::MakeFromPixmap( |
| 185 |
dContext, |
| 186 |
*pixmap, |
| 187 |
options.fMipMapping, |
| 188 |
GrRenderable::kNo, |
| 189 |
GrProtected::kNo); |
| 190 |
if (!managedBackendTexture) { |
| 191 |
fputs("Failed to create backEndTexture.\n", stderr); |
| 192 |
return false; |
| 193 |
} |
| 194 |
backEndTexture = managedBackendTexture->texture(); |
| 195 |
} |
| 196 |
|
| 197 |
{ |
| 198 |
auto resourceProvider = dContext->priv().resourceProvider(); |
| 199 |
|
| 200 |
SkISize offscreenDims = {options.fOffScreenWidth, options.fOffScreenHeight}; |
| 201 |
AutoTMalloc<uint32_t> data(offscreenDims.area()); |
| 202 |
SkOpts::memset32(data.get(), 0, offscreenDims.area()); |
| 203 |
|
| 204 |
// This backend object should be renderable but not textureable. Given the limitations |
| 205 |
// of how we're creating it though it will wind up being secretly textureable. |
| 206 |
// We use this fact to initialize it with data but don't allow mipmaps |
| 207 |
GrMipLevel level0 = {data.get(), offscreenDims.width()*sizeof(uint32_t), nullptr}; |
| 208 |
|
| 209 |
constexpr int kSampleCnt = 1; |
| 210 |
sk_sp<GrTexture> tmp = |
| 211 |
resourceProvider->createTexture(offscreenDims, |
| 212 |
renderableFormat, |
| 213 |
GrTextureType::k2D, |
| 214 |
GrColorType::kRGBA_8888, |
| 215 |
GrRenderable::kYes, |
| 216 |
kSampleCnt, |
| 217 |
skgpu::Budgeted::kNo, |
| 218 |
skgpu::Mipmapped::kNo, |
| 219 |
GrProtected::kNo, |
| 220 |
&level0, |
| 221 |
/*label=*/"Fiddle_SetupBackendObjects"); |
| 222 |
if (!tmp || !tmp->asRenderTarget()) { |
| 223 |
fputs("GrTexture is invalid.\n", stderr); |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
backingRenderTarget = sk_ref_sp(tmp->asRenderTarget()); |
| 228 |
|
| 229 |
backEndRenderTarget = backingRenderTarget->getBackendRenderTarget(); |
| 230 |
if (!backEndRenderTarget.isValid()) { |
| 231 |
fputs("BackEndRenderTarget is invalid.\n", stderr); |
| 232 |
return false; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
{ |
| 237 |
managedBackendTextureRenderTarget = sk_gpu_test::ManagedBackendTexture::MakeWithData( |
| 238 |
dContext, |
| 239 |
options.fOffScreenWidth, |
| 240 |
options.fOffScreenHeight, |
| 241 |
renderableFormat, |
| 242 |
SkColors::kTransparent, |
| 243 |
options.fOffScreenMipMapping, |
| 244 |
GrRenderable::kYes, |
| 245 |
GrProtected::kNo); |
| 246 |
if (!managedBackendTextureRenderTarget) { |
| 247 |
fputs("Failed to create backendTextureRenderTarget.\n", stderr); |
| 248 |
return false; |
| 249 |
} |
| 250 |
backEndTextureRenderTarget = managedBackendTextureRenderTarget->texture(); |
| 251 |
} |
| 252 |
|
| 253 |
return true; |
| 254 |
} |
| 255 |
#endif |
| 256 |
|
| 257 |
int main(int argc, char** argv) { |
| 258 |
CommandLineFlags::Parse(argc, argv); |
| 259 |
duration = FLAGS_duration; |
| 260 |
frame = FLAGS_frame; |
| 261 |
DrawOptions options = GetDrawOptions(); |
| 262 |
// If textOnly then only do one type of image, otherwise the text |
| 263 |
// output is duplicated for each type. |
| 264 |
if (options.textOnly) { |
| 265 |
options.raster = true; |
| 266 |
options.gpu = false; |
| 267 |
options.pdf = false; |
| 268 |
options.skp = false; |
| 269 |
} |
| 270 |
#if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE) |
| 271 |
fontMgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType()); |
| 272 |
#else |
| 273 |
fontMgr = SkFontMgr::RefEmpty(); |
| 274 |
#endif |
| 275 |
if (options.source) { |
| 276 |
sk_sp<SkData> data(SkData::MakeFromFileName(options.source)); |
| 277 |
if (!data) { |
| 278 |
perror(options.source); |
| 279 |
return 1; |
| 280 |
} |
| 281 |
std::unique_ptr<SkCodec> codec = nullptr; |
| 282 |
#if defined(SK_CODEC_DECODES_PNG_WITH_LIBPNG) |
| 283 |
if (SkPngDecoder::IsPng(data->data(), data->size())) { |
| 284 |
codec = SkPngDecoder::Decode(data, nullptr); |
| 285 |
} else |
| 286 |
#endif |
| 287 |
if (SkJpegDecoder::IsJpeg(data->data(), data->size())) { |
| 288 |
codec = SkJpegDecoder::Decode(data, nullptr); |
| 289 |
} else { |
| 290 |
perror("Unsupported file format\n"); |
| 291 |
return 1; |
| 292 |
} |
| 293 |
if (!codec) { |
| 294 |
perror("Corrupt source image file\n"); |
| 295 |
return 1; |
| 296 |
} |
| 297 |
image = std::get<0>(codec->getImage()); |
| 298 |
if (!image) { |
| 299 |
perror("Unable to decode the source image.\n"); |
| 300 |
return 1; |
| 301 |
} |
| 302 |
SkAssertResult(image->asLegacyBitmap(&source)); |
| 303 |
} |
| 304 |
sk_sp<SkData> rasterData, gpuData, pdfData, skpData; |
| 305 |
SkColorType colorType = kN32_SkColorType; |
| 306 |
sk_sp<SkColorSpace> colorSpace = nullptr; |
| 307 |
if (options.f16) { |
| 308 |
SkASSERT(options.srgb); |
| 309 |
colorType = kRGBA_F16_SkColorType; |
| 310 |
colorSpace = SkColorSpace::MakeSRGBLinear(); |
| 311 |
} else if (options.srgb) { |
| 312 |
colorSpace = SkColorSpace::MakeSRGB(); |
| 313 |
} |
| 314 |
SkImageInfo info = SkImageInfo::Make(options.size.width(), options.size.height(), colorType, |
| 315 |
kPremul_SkAlphaType, colorSpace); |
| 316 |
if (options.raster) { |
| 317 |
auto rasterSurface = SkSurfaces::Raster(info); |
| 318 |
srand(0); |
| 319 |
draw(prepare_canvas(rasterSurface->getCanvas())); |
| 320 |
rasterData = encode_snapshot(nullptr, rasterSurface); |
| 321 |
} |
| 322 |
#ifdef SK_GL |
| 323 |
if (options.gpu) { |
| 324 |
std::unique_ptr<sk_gpu_test::GLTestContext> glContext; |
| 325 |
sk_sp<GrDirectContext> direct = create_direct_context(gGLDriverInfo, &glContext); |
| 326 |
if (!direct) { |
| 327 |
fputs("Unable to get GrContext.\n", stderr); |
| 328 |
} else { |
| 329 |
if (!setup_backend_objects(direct.get(), source, options)) { |
| 330 |
fputs("Unable to create backend objects.\n", stderr); |
| 331 |
exit(1); |
| 332 |
} |
| 333 |
|
| 334 |
auto surface = SkSurfaces::RenderTarget(direct.get(), skgpu::Budgeted::kNo, info); |
| 335 |
if (!surface) { |
| 336 |
fputs("Unable to get render surface.\n", stderr); |
| 337 |
exit(1); |
| 338 |
} |
| 339 |
srand(0); |
| 340 |
draw(prepare_canvas(surface->getCanvas())); |
| 341 |
gpuData = encode_snapshot(direct.get(), surface); |
| 342 |
} |
| 343 |
} |
| 344 |
#endif |
| 345 |
|
| 346 |
#if defined(SK_SUPPORT_PDF) |
| 347 |
if (options.pdf) { |
| 348 |
SkDynamicMemoryWStream pdfStream; |
| 349 |
auto document = SkPDF::MakeDocument(&pdfStream, SkPDF::JPEG::MetadataWithCallbacks()); |
| 350 |
if (document) { |
| 351 |
srand(0); |
| 352 |
draw(prepare_canvas(document->beginPage(options.size.width(), options.size.height()))); |
| 353 |
document->close(); |
| 354 |
pdfData = pdfStream.detachAsData(); |
| 355 |
} |
| 356 |
} |
| 357 |
#endif |
| 358 |
|
| 359 |
if (options.skp) { |
| 360 |
auto size = SkSize::Make(options.size); |
| 361 |
SkPictureRecorder recorder; |
| 362 |
srand(0); |
| 363 |
draw(prepare_canvas(recorder.beginRecording(size.width(), size.height()))); |
| 364 |
auto picture = recorder.finishRecordingAsPicture(); |
| 365 |
SkDynamicMemoryWStream skpStream; |
| 366 |
picture->serialize(&skpStream); |
| 367 |
skpData = skpStream.detachAsData(); |
| 368 |
} |
| 369 |
|
| 370 |
printf("{\n"); |
| 371 |
if (!options.textOnly) { |
| 372 |
dump_output(rasterData, "Raster", false); |
| 373 |
dump_output(gpuData, "Gpu", false); |
| 374 |
dump_output(pdfData, "Pdf", false); |
| 375 |
dump_output(skpData, "Skp", false); |
| 376 |
} else { |
| 377 |
std::string textoutput = gTextOutput.str(); |
| 378 |
dump_output(textoutput.c_str(), textoutput.length(), "Text", false); |
| 379 |
} |
| 380 |
std::string glinfo = gGLDriverInfo.str(); |
| 381 |
dump_output(glinfo.c_str(), glinfo.length(), "GLInfo", true); |
| 382 |
printf("}\n"); |
| 383 |
|
| 384 |
return 0; |
| 385 |
} |