blob: ff56a280d96063c68af2f071c3c70915cc286072 [file] [log] [blame]
Brian Osman13dddce2017-05-09 13:19:50 -04001/*
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.h"
9#include "Resources.h"
10
11#if SK_SUPPORT_GPU
12#include "GrContext.h"
13#include "SkImage.h"
14
Brian Osman63bc48d2017-11-07 10:37:00 -050015DEF_SIMPLE_GM(cross_context_image, canvas, 512 * 3 + 60, 512 + 128 + 30) {
Brian Osman13dddce2017-05-09 13:19:50 -040016 GrContext* context = canvas->getGrContext();
17 if (!context) {
18 skiagm::GM::DrawGpuOnlyMessage(canvas);
19 return;
20 }
21
22 sk_sp<SkData> encodedData = GetResourceAsData("mandrill_512.png");
23
24 sk_sp<SkImage> encodedImage = SkImage::MakeFromEncoded(encodedData);
25 canvas->drawImage(encodedImage, 10, 10);
26
27 sk_sp<SkImage> crossContextImage = SkImage::MakeCrossContextFromEncoded(
Brian Osman63bc48d2017-11-07 10:37:00 -050028 context, encodedData, false, canvas->imageInfo().colorSpace());
Brian Osman13dddce2017-05-09 13:19:50 -040029 canvas->drawImage(crossContextImage, 512 + 30, 10);
30
Brian Osman63bc48d2017-11-07 10:37:00 -050031 SkBitmap bmp;
32 SkPixmap pixmap;
33 SkAssertResult(encodedImage->asLegacyBitmap(&bmp, SkImage::kRO_LegacyBitmapMode) &&
34 bmp.peekPixels(&pixmap));
35
36 sk_sp<SkImage> crossContextRaster = SkImage::MakeCrossContextFromPixmap(
37 context, pixmap, false, canvas->imageInfo().colorSpace());
38 canvas->drawImage(crossContextRaster, 512 + 512 + 60, 10);
39
Brian Osman13dddce2017-05-09 13:19:50 -040040 SkIRect subset = SkIRect::MakeXYWH(256 - 64, 256 - 64, 128, 128);
41 sk_sp<SkImage> encodedSubset = encodedImage->makeSubset(subset);
42 sk_sp<SkImage> crossContextSubset = crossContextImage->makeSubset(subset);
Brian Osman63bc48d2017-11-07 10:37:00 -050043 sk_sp<SkImage> crossContextRasterSubset = crossContextRaster->makeSubset(subset);
Brian Osman13dddce2017-05-09 13:19:50 -040044
45 canvas->drawImage(encodedSubset, 10, 512 + 30);
46 canvas->drawImage(crossContextSubset, 512 + 30, 512 + 30);
Brian Osman63bc48d2017-11-07 10:37:00 -050047 canvas->drawImage(crossContextRasterSubset, 512 + 512 + 60, 512 + 30);
Brian Osman13dddce2017-05-09 13:19:50 -040048}
49
50#endif