xref: /third_party/skia/gm/crosscontextimage.cpp (revision cb93a386)
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/GrDirectContext.h"
20#include "include/gpu/GrRecordingContext.h"
21#include "tools/Resources.h"
22
23DEF_SIMPLE_GPU_GM_CAN_FAIL(cross_context_image, rContext, canvas, errorMsg,
24                           3 * 256 + 40, 256 + 128 + 30) {
25    sk_sp<SkData> encodedData = GetResourceAsData("images/mandrill_256.png");
26    if (!encodedData) {
27        *errorMsg = "Could not load mandrill_256.png. Did you forget to set the resourcePath?";
28        return skiagm::DrawResult::kFail;
29    }
30
31    auto dContext = rContext->asDirectContext();
32    if (!dContext) {
33        *errorMsg = "CrossContext image creation requires a direct context.";
34        return skiagm::DrawResult::kSkip;
35    }
36
37    sk_sp<SkImage> images[3];
38    images[0] = SkImage::MakeFromEncoded(encodedData);
39
40    SkBitmap bmp;
41    SkPixmap pixmap;
42    SkAssertResult(images[0]->asLegacyBitmap(&bmp) &&
43                   bmp.peekPixels(&pixmap));
44
45    images[1] = SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
46    images[2] = SkImage::MakeCrossContextFromPixmap(dContext, pixmap, true);
47
48    canvas->translate(10, 10);
49
50    for (size_t i = 0; i < SK_ARRAY_COUNT(images); ++i) {
51        canvas->save();
52
53        canvas->drawImage(images[i], 0, 0);
54        canvas->translate(0, 256 + 10);
55
56        canvas->drawImage(images[i]->makeSubset(SkIRect::MakeXYWH(64, 64, 128, 128), dContext),
57                          0, 0);
58        canvas->translate(128, 0);
59
60        canvas->drawImageRect(images[i], SkRect::MakeWH(128, 128),
61                              SkSamplingOptions(SkFilterMode::kLinear,
62                                                SkMipmapMode::kLinear));
63
64        canvas->restore();
65        canvas->translate(256 + 10, 0);
66    }
67    return skiagm::DrawResult::kOk;
68}
69