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