robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
| 9 | #include "SkDevice.h" |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 10 | #include "SkSpecialImage.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | #include "SkGpuDevice.h" |
| 14 | #endif |
| 15 | |
| 16 | #include "Test.h" |
| 17 | |
| 18 | class DeviceTestingAccess { |
| 19 | public: |
| 20 | static sk_sp<SkSpecialImage> MakeSpecial(SkBaseDevice* dev, const SkBitmap& bm) { |
| 21 | return dev->makeSpecial(bm); |
| 22 | } |
| 23 | |
| 24 | static sk_sp<SkSpecialImage> MakeSpecial(SkBaseDevice* dev, SkImage* img) { |
| 25 | return dev->makeSpecial(img); |
| 26 | } |
| 27 | |
| 28 | static sk_sp<SkSpecialImage> SnapSpecial(SkBaseDevice* dev) { |
| 29 | return dev->snapSpecial(); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | // TODO: re-enable this when Raster methods are implemented |
| 34 | #if 0 |
| 35 | DEF_TEST(SpecialImage_BitmapDevice, reporter) { |
| 36 | static const int kWidth = 100; |
| 37 | static const int kHeight = 90; |
| 38 | |
| 39 | SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight); |
| 40 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 41 | sk_sp<SkBaseDevice> bmDev(SkBitmapDevice::Create(ii)); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 42 | |
| 43 | SkBitmap bm; |
| 44 | bm.tryAllocN32Pixels(kWidth, kHeight); |
| 45 | |
| 46 | // Create a raster-backed special image from a raster-backed SkBitmap |
| 47 | sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(bmDev.get(), bm); |
| 48 | SkASSERT(!special->isTextureBacked()); |
| 49 | SkASSERT(kWidth == special->width()); |
| 50 | SkASSERT(kHeight == special->height()); |
| 51 | SkASSERT(bm.getGenerationID() == special->uniqueID()); |
| 52 | SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset()); |
| 53 | |
| 54 | // Create a raster-backed special image from a raster-backed SkImage |
| 55 | sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm)); |
| 56 | special = DeviceTestingAccess::MakeSpecial(bmDev.get(), image.get()); |
| 57 | SkASSERT(!special->isTextureBacked()); |
| 58 | SkASSERT(kWidth == special->width()); |
| 59 | SkASSERT(kHeight == special->height()); |
| 60 | SkASSERT(bm.getGenerationID() == special->uniqueID()); |
| 61 | SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset()); |
| 62 | |
| 63 | // Snap the device as a raster-backed special image |
| 64 | special = DeviceTestingAccess::SnapSpecial(bmDev.get()); |
| 65 | SkASSERT(!special->isTextureBacked()); |
| 66 | SkASSERT(2*kWidth == special->width()); |
| 67 | SkASSERT(2*kHeight == special->height()); |
| 68 | SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset()); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | |
| 73 | #if SK_SUPPORT_GPU |
| 74 | |
| 75 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_GPUDevice, reporter, ctxInfo) { |
| 76 | GrContext* context = ctxInfo.grContext(); |
| 77 | |
| 78 | static const int kWidth = 100; |
| 79 | static const int kHeight = 90; |
| 80 | |
| 81 | SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight); |
| 82 | |
| 83 | sk_sp<SkBaseDevice> gpuDev(SkGpuDevice::Make(context, SkBudgeted::kNo, ii, |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 84 | 0, kBottomLeft_GrSurfaceOrigin, nullptr, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 85 | GrMipMapped::kNo, |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 86 | SkGpuDevice::kClear_InitContents)); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 87 | |
| 88 | SkBitmap bm; |
| 89 | SkAssertResult(bm.tryAllocN32Pixels(kWidth, kHeight)); |
| 90 | |
| 91 | // Create a gpu-backed special image from a raster-backed SkBitmap |
| 92 | sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(gpuDev.get(), bm); |
| 93 | SkASSERT(special->isTextureBacked()); |
| 94 | SkASSERT(kWidth == special->width()); |
| 95 | SkASSERT(kHeight == special->height()); |
| 96 | SkASSERT(bm.getGenerationID() == special->uniqueID()); |
| 97 | SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset()); |
| 98 | |
| 99 | // Create a gpu-backed special image from a raster-backed SkImage |
| 100 | sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm)); |
| 101 | special = DeviceTestingAccess::MakeSpecial(gpuDev.get(), image.get()); |
| 102 | SkASSERT(special->isTextureBacked()); |
| 103 | SkASSERT(kWidth == special->width()); |
| 104 | SkASSERT(kHeight == special->height()); |
| 105 | // TODO: Hmmm, this is a bit unexpected |
| 106 | SkASSERT(image->uniqueID() != special->uniqueID()); |
| 107 | SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset()); |
| 108 | |
| 109 | // Create a gpu-backed special image from a gpu-backed SkImage |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 110 | SkColorSpace* legacyColorSpace = nullptr; |
| 111 | image = image->makeTextureImage(context, legacyColorSpace); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 112 | special = DeviceTestingAccess::MakeSpecial(gpuDev.get(), image.get()); |
| 113 | SkASSERT(special->isTextureBacked()); |
| 114 | SkASSERT(kWidth == special->width()); |
| 115 | SkASSERT(kHeight == special->height()); |
| 116 | SkASSERT(image->uniqueID() == special->uniqueID()); |
| 117 | SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset()); |
| 118 | |
| 119 | // Snap the device as a gpu-backed special image |
| 120 | special = DeviceTestingAccess::SnapSpecial(gpuDev.get()); |
| 121 | SkASSERT(special->isTextureBacked()); |
| 122 | SkASSERT(2*kWidth == special->width()); |
| 123 | SkASSERT(2*kHeight == special->height()); |
| 124 | SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset()); |
| 125 | } |
| 126 | |
| 127 | #endif |