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" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 14 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/gpu/GrTypes.h" |
| 16 | #include "src/core/SkDevice.h" |
| 17 | #include "src/core/SkSpecialImage.h" |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "tests/Test.h" |
| 20 | #include "tools/gpu/GrContextFactory.h" |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 21 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 22 | class SkColorSpace; |
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) { |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 80 | auto dContext = ctxInfo.directContext(); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 81 | |
| 82 | static const int kWidth = 100; |
| 83 | static const int kHeight = 90; |
| 84 | |
| 85 | SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight); |
| 86 | |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 87 | auto device = dContext->priv().createDevice(SkBudgeted::kNo, ii, SkBackingFit::kExact, |
| 88 | 1, GrMipmapped::kNo, GrProtected::kNo, |
| 89 | kBottomLeft_GrSurfaceOrigin, SkSurfaceProps(), |
Robert Phillips | cc44feb | 2021-07-06 12:21:37 -0400 | [diff] [blame] | 90 | skgpu::BaseDevice::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 |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 96 | sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(device.get(), bm); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 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 |
Mike Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame] | 104 | sk_sp<SkImage> image(bm.asImage()); |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 105 | special = DeviceTestingAccess::MakeSpecial(device.get(), image.get()); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 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 |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 114 | image = image->makeTextureImage(dContext); |
| 115 | special = DeviceTestingAccess::MakeSpecial(device.get(), image.get()); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 116 | SkASSERT(special->isTextureBacked()); |
| 117 | SkASSERT(kWidth == special->width()); |
| 118 | SkASSERT(kHeight == special->height()); |
| 119 | SkASSERT(image->uniqueID() == special->uniqueID()); |
| 120 | SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset()); |
| 121 | |
| 122 | // Snap the device as a gpu-backed special image |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 123 | special = DeviceTestingAccess::SnapSpecial(device.get()); |
robertphillips | 6451a0c | 2016-07-18 08:31:31 -0700 | [diff] [blame] | 124 | SkASSERT(special->isTextureBacked()); |
| 125 | SkASSERT(2*kWidth == special->width()); |
| 126 | SkASSERT(2*kHeight == special->height()); |
| 127 | SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset()); |
| 128 | } |