| 1 |
/* |
| 2 |
* Copyright 2024 Google LLC |
| 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/core/SkCanvas.h" |
| 9 |
#include "include/core/SkRRect.h" |
| 10 |
#include "include/core/SkRect.h" |
| 11 |
#include "include/core/SkStream.h" |
| 12 |
#include "include/core/SkSurface.h" |
| 13 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 14 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 15 |
#include "include/gpu/ganesh/mtl/GrMtlDirectContext.h" |
| 16 |
#include "include/gpu/ganesh/mtl/GrMtlBackendContext.h" |
| 17 |
#include "include/encode/SkJpegEncoder.h" |
| 18 |
|
| 19 |
#include "ganesh_metal_context_helper.h" |
| 20 |
|
| 21 |
int main(int argc, char *argv[]) { |
| 22 |
if (argc != 2) { |
| 23 |
printf("Usage: %s <name.jpeg>\n", argv[0]); |
| 24 |
return 1; |
| 25 |
} |
| 26 |
|
| 27 |
SkFILEWStream output(argv[1]); |
| 28 |
if (!output.isValid()) { |
| 29 |
printf("Cannot open output file %s\n", argv[1]); |
| 30 |
return 1; |
| 31 |
} |
| 32 |
|
| 33 |
GrMtlBackendContext backendContext = GetMetalContext(); |
| 34 |
sk_sp<GrDirectContext> ctx = GrDirectContexts::MakeMetal(backendContext); |
| 35 |
if (!ctx) { |
| 36 |
printf("Could not make metal context\n"); |
| 37 |
return 1; |
| 38 |
} |
| 39 |
printf("Context made, now to make the surface\n"); |
| 40 |
|
| 41 |
SkImageInfo imageInfo = |
| 42 |
SkImageInfo::Make(200, 400, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 43 |
|
| 44 |
sk_sp<SkSurface> surface = |
| 45 |
SkSurfaces::RenderTarget(ctx.get(), skgpu::Budgeted::kYes, imageInfo); |
| 46 |
if (!surface) { |
| 47 |
printf("Could not make surface from Metal DirectContext\n"); |
| 48 |
return 1; |
| 49 |
} |
| 50 |
|
| 51 |
SkCanvas* canvas = surface->getCanvas(); |
| 52 |
canvas->clear(SK_ColorCYAN); |
| 53 |
SkRRect rrect = SkRRect::MakeRectXY(SkRect::MakeLTRB(10, 20, 50, 70), 10, 10); |
| 54 |
|
| 55 |
SkPaint paint; |
| 56 |
paint.setColor(SK_ColorMAGENTA); |
| 57 |
paint.setAntiAlias(true); |
| 58 |
|
| 59 |
canvas->drawRRect(rrect, paint); |
| 60 |
|
| 61 |
ctx->flush(); |
| 62 |
|
| 63 |
printf("Drew to surface, now doing readback\n"); |
| 64 |
sk_sp<SkImage> img = surface->makeImageSnapshot(); |
| 65 |
sk_sp<SkData> jpeg = SkJpegEncoder::Encode(ctx.get(), img.get(), {}); |
| 66 |
if (!jpeg) { |
| 67 |
printf("Readback of pixels (or encoding) failed\n"); |
| 68 |
return 1; |
| 69 |
} |
| 70 |
output.write(jpeg->data(), jpeg->size()); |
| 71 |
output.fsync(); |
| 72 |
return 0; |
| 73 |
} |