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.h" 9 #include "Resources.h" 10 11 #include "GrContext.h" 12 #include "SkImage.h" 13 14 DEF_SIMPLE_GM(cross_context_image, canvas, 512 * 3 + 60, 512 + 128 + 30) { 15 GrContext* context = canvas->getGrContext(); 16 if (!context) { 17 skiagm::GM::DrawGpuOnlyMessage(canvas); 18 return; 19 } 20 21 sk_sp<SkData> encodedData = GetResourceAsData("images/mandrill_512.png"); 22 if (!encodedData) { 23 return; 24 } 25 26 sk_sp<SkImage> encodedImage = SkImage::MakeFromEncoded(encodedData); 27 canvas->drawImage(encodedImage, 10, 10); 28 29 sk_sp<SkImage> crossContextImage = SkImage::MakeCrossContextFromEncoded( 30 context, encodedData, false, canvas->imageInfo().colorSpace()); 31 canvas->drawImage(crossContextImage, 512 + 30, 10); 32 33 SkBitmap bmp; 34 SkPixmap pixmap; 35 SkAssertResult(encodedImage->asLegacyBitmap(&bmp) && 36 bmp.peekPixels(&pixmap)); 37 38 sk_sp<SkImage> crossContextRaster = SkImage::MakeCrossContextFromPixmap( 39 context, pixmap, false, canvas->imageInfo().colorSpace()); 40 canvas->drawImage(crossContextRaster, 512 + 512 + 60, 10); 41 42 SkIRect subset = SkIRect::MakeXYWH(256 - 64, 256 - 64, 128, 128); 43 sk_sp<SkImage> encodedSubset = encodedImage->makeSubset(subset); 44 sk_sp<SkImage> crossContextSubset = crossContextImage->makeSubset(subset); 45 sk_sp<SkImage> crossContextRasterSubset = crossContextRaster->makeSubset(subset); 46 47 canvas->drawImage(encodedSubset, 10, 512 + 30); 48 canvas->drawImage(crossContextSubset, 512 + 30, 512 + 30); 49 canvas->drawImage(crossContextRasterSubset, 512 + 512 + 60, 512 + 30); 50 } 51