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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkImage.h" |
| 10 | #include "include/core/SkImageInfo.h" |
| 11 | #include "include/core/SkRect.h" |
| 12 | #include "include/core/SkRefCnt.h" |
| 13 | #include "include/core/SkTypes.h" |
| 14 | #include "include/gpu/GrTypes.h" |
| 15 | #include "src/core/SkDevice.h" |
| 16 | #include "src/core/SkSpecialImage.h" |
| 17 | #include "src/gpu/SkGpuDevice.h" |
| 18 | #include "tests/Test.h" |
| 19 | #include "tools/gpu/GrContextFactory.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 | } |