Debian Codegraph @

gm/crosscontextimage.cpp

raw
1 /*
2 * Copyright 2017 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 "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypes.h"
19 #include "include/gpu/ganesh/GrDirectContext.h"
20 #include "include/gpu/ganesh/GrRecordingContext.h"
21 #include "include/gpu/ganesh/SkImageGanesh.h"
22 #include "tools/Resources.h"
23
24 DEF_SIMPLE_GPU_GM_CAN_FAIL(cross_context_image, rContext, canvas, errorMsg,
25 3 * 256 + 40, 256 + 128 + 30) {
26 sk_sp<SkData> encodedData = GetResourceAsData("images/mandrill_256.png");
27 if (!encodedData) {
28 *errorMsg = "Could not load mandrill_256.png. Did you forget to set the resourcePath?";
29 return skiagm::DrawResult::kFail;
30 }
31
32 auto dContext = rContext->asDirectContext();
33 if (!dContext) {
34 *errorMsg = "CrossContext image creation requires a direct context.";
35 return skiagm::DrawResult::kSkip;
36 }
37
38 sk_sp<SkImage> images[3];
39 images[0] = SkImages::DeferredFromEncodedData(encodedData);
40
41 SkBitmap bmp;
42 SkPixmap pixmap;
43 SkAssertResult(images[0]->asLegacyBitmap(&bmp) &&
44 bmp.peekPixels(&pixmap));
45
46 images[1] = SkImages::CrossContextTextureFromPixmap(dContext, pixmap, false);
47 images[2] = SkImages::CrossContextTextureFromPixmap(dContext, pixmap, true);
48
49 canvas->translate(10, 10);
50
51 for (size_t i = 0; i < std::size(images); ++i) {
52 canvas->save();
53
54 canvas->drawImage(images[i], 0, 0);
55 canvas->translate(0, 256 + 10);
56
57 auto subset = images[i]->makeSubset(
58 dContext->asRecorder(), SkIRect::MakeXYWH(64, 64, 128, 128), {});
59 SkASSERTF(subset, "subset was null");
60 canvas->drawImage(subset, 0, 0);
61 canvas->translate(128, 0);
62
63 canvas->drawImageRect(images[i], SkRect::MakeWH(128, 128),
64 SkSamplingOptions(SkFilterMode::kLinear,
65 SkMipmapMode::kLinear));
66
67 canvas->restore();
68 canvas->translate(256 + 10, 0);
69 }
70 return skiagm::DrawResult::kOk;
71 }