blob: 5b1676ca191313a5a85d2c54df78ebcf630b42db [file] [log] [blame]
robertphillips6451a0c2016-07-18 08:31:31 -07001/*
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 Canary8a001442018-09-19 11:31:27 -04008#include "GrContextFactory.h"
9#include "GrTypes.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040010#include "SkBitmap.h"
Mike Reed986480a2017-01-13 22:43:16 +000011#include "SkDevice.h"
Hal Canary8a001442018-09-19 11:31:27 -040012#include "SkGpuDevice.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040013#include "SkImage.h"
14#include "SkImageInfo.h"
15#include "SkRect.h"
16#include "SkRefCnt.h"
robertphillips6451a0c2016-07-18 08:31:31 -070017#include "SkSpecialImage.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040018#include "SkTypes.h"
19#include "Test.h"
robertphillips6451a0c2016-07-18 08:31:31 -070020
Hal Canary8a001442018-09-19 11:31:27 -040021class SkColorSpace;
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040022class GrContext;
robertphillips6451a0c2016-07-18 08:31:31 -070023
robertphillips6451a0c2016-07-18 08:31:31 -070024class DeviceTestingAccess {
25public:
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
41DEF_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 Canary342b7ac2016-11-04 11:49:42 -040047 sk_sp<SkBaseDevice> bmDev(SkBitmapDevice::Create(ii));
robertphillips6451a0c2016-07-18 08:31:31 -070048
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
robertphillips6451a0c2016-07-18 08:31:31 -070079DEF_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 Salomonbdecacf2018-02-02 20:32:49 -050088 1, kBottomLeft_GrSurfaceOrigin, nullptr,
Greg Daniele252f082017-10-23 16:05:23 -040089 GrMipMapped::kNo,
robertphillips7e922762016-07-26 11:38:17 -070090 SkGpuDevice::kClear_InitContents));
robertphillips6451a0c2016-07-18 08:31:31 -070091
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 Osman041f7df2017-02-07 11:23:28 -0500114 SkColorSpace* legacyColorSpace = nullptr;
115 image = image->makeTextureImage(context, legacyColorSpace);
robertphillips6451a0c2016-07-18 08:31:31 -0700116 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}