• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkFilterQuality.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPixmap.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypes.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "include/gpu/GrRecordingContext.h"
22 #include "tools/Resources.h"
23 
24 class GrSurfaceDrawContext;
25 
26 DEF_SIMPLE_GPU_GM_CAN_FAIL(cross_context_image, context, rtc, canvas, errorMsg,
27                            3 * 256 + 40, 256 + 128 + 30) {
28     sk_sp<SkData> encodedData = GetResourceAsData("images/mandrill_256.png");
29     if (!encodedData) {
30         *errorMsg = "Could not load mandrill_256.png. Did you forget to set the resourcePath?";
31         return skiagm::DrawResult::kFail;
32     }
33 
34     auto dContext = context->asDirectContext();
35     if (!dContext) {
36         *errorMsg = "CrossContext image creation requires a direct context.";
37         return skiagm::DrawResult::kSkip;
38     }
39 
40     sk_sp<SkImage> images[3];
41     images[0] = SkImage::MakeFromEncoded(encodedData);
42 
43     SkBitmap bmp;
44     SkPixmap pixmap;
45     SkAssertResult(images[0]->asLegacyBitmap(&bmp) &&
46                    bmp.peekPixels(&pixmap));
47 
48     images[1] = SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
49     images[2] = SkImage::MakeCrossContextFromPixmap(dContext, pixmap, true);
50 
51     canvas->translate(10, 10);
52 
53     for (size_t i = 0; i < SK_ARRAY_COUNT(images); ++i) {
54         canvas->save();
55 
56         canvas->drawImage(images[i], 0, 0);
57         canvas->translate(0, 256 + 10);
58 
59         canvas->drawImage(images[i]->makeSubset(SkIRect::MakeXYWH(64, 64, 128, 128), dContext),
60                           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 }
72