blob: 339341a8e8d93745d20ad4e576b83eb6b71a4ca6 [file] [log] [blame]
robertphillipsb6c65e92016-02-04 10:52:42 -08001/*
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 */
reedad7604b2016-07-20 16:13:32 -07007
robertphillips83c17fa2016-03-18 08:14:27 -07008#include "SkSpecialImage.h"
reedad7604b2016-07-20 16:13:32 -07009#include "SkBitmap.h"
10#include "SkImage.h"
robertphillips64612512016-04-08 12:10:42 -070011#include "SkBitmapCache.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080012#include "SkCanvas.h"
bsalomon84a4e5a2016-02-29 11:41:52 -080013#include "SkImage_Base.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080014#include "SkSpecialSurface.h"
brianosman898235c2016-04-06 07:38:23 -070015#include "SkSurfacePriv.h"
reedad7604b2016-07-20 16:13:32 -070016#include "SkPixelRef.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
Robert Phillipsb66b42f2017-03-14 08:53:02 -040020#include "GrResourceProvider.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050021#include "GrSurfaceContext.h"
Robert Phillipsa5fdc972017-02-18 16:58:09 -050022#include "GrSurfaceProxyPriv.h"
reedad7604b2016-07-20 16:13:32 -070023#include "GrTexture.h"
Brian Salomon514baff2016-11-17 15:17:07 -050024#include "GrSamplerParams.h"
Robert Phillips8bc06d02016-11-01 17:28:40 -040025#include "GrTextureProxy.h"
reedad7604b2016-07-20 16:13:32 -070026#include "SkGr.h"
Robert Phillips6de99042017-01-31 11:31:39 -050027#include "SkImage_Gpu.h"
reedad7604b2016-07-20 16:13:32 -070028#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080029
reeda2217ef2016-07-20 06:04:34 -070030// Currently the raster imagefilters can only handle certain imageinfos. Call this to know if
31// a given info is supported.
32static bool valid_for_imagefilters(const SkImageInfo& info) {
33 // no support for other swizzles/depths yet
34 return info.colorType() == kN32_SkColorType;
35}
36
robertphillipsb6c65e92016-02-04 10:52:42 -080037///////////////////////////////////////////////////////////////////////////////
38class SkSpecialImage_Base : public SkSpecialImage {
39public:
robertphillips3e302272016-04-20 11:48:36 -070040 SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps* props)
41 : INHERITED(subset, uniqueID, props) {
robertphillips3b087f42016-02-18 08:48:03 -080042 }
robertphillips3e302272016-04-20 11:48:36 -070043 ~SkSpecialImage_Base() override { }
robertphillipsb6c65e92016-02-04 10:52:42 -080044
robertphillipse8c34972016-02-16 12:09:36 -080045 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080046
robertphillips64612512016-04-08 12:10:42 -070047 virtual bool onGetROPixels(SkBitmap*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080048
Robert Phillips8bc06d02016-11-01 17:28:40 -040049 virtual GrContext* onGetContext() const { return nullptr; }
robertphillipsb6c65e92016-02-04 10:52:42 -080050
brianosmanafbf71d2016-07-21 07:15:37 -070051 virtual SkColorSpace* onGetColorSpace() const = 0;
52
robertphillipsc91fd342016-04-25 12:32:54 -070053#if SK_SUPPORT_GPU
Robert Phillips8e1c4e62017-02-19 12:27:01 -050054 virtual sk_sp<GrTextureProxy> onAsTextureProxyRef(GrContext* context) const = 0;
robertphillipsc91fd342016-04-25 12:32:54 -070055#endif
robertphillips4418dba2016-03-07 12:45:14 -080056
robertphillipsb4bd11e2016-03-21 13:44:18 -070057 virtual sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const = 0;
58
brianosmaneed6b0e2016-09-23 13:04:05 -070059 virtual sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
60 const SkISize& size, SkAlphaType at) const = 0;
robertphillipsc5035e72016-03-17 06:58:39 -070061
Robert Phillipsa5fdc972017-02-18 16:58:09 -050062 virtual sk_sp<SkImage> onAsImage(const SkIRect* subset) const = 0;
robertphillipsb4bd11e2016-03-21 13:44:18 -070063
brianosmaneed6b0e2016-09-23 13:04:05 -070064 virtual sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
65 const SkISize& size, SkAlphaType at) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080066
67private:
68 typedef SkSpecialImage INHERITED;
69};
70
71///////////////////////////////////////////////////////////////////////////////
bsalomon84a4e5a2016-02-29 11:41:52 -080072static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) {
robertphillipsb6c65e92016-02-04 10:52:42 -080073 return static_cast<const SkSpecialImage_Base*>(image);
74}
75
robertphillips3e302272016-04-20 11:48:36 -070076SkSpecialImage::SkSpecialImage(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -070077 uint32_t uniqueID,
78 const SkSurfaceProps* props)
79 : fProps(SkSurfacePropsCopyOrDefault(props))
80 , fSubset(subset)
robertphillips3e302272016-04-20 11:48:36 -070081 , fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID) {
brianosman898235c2016-04-06 07:38:23 -070082}
83
robertphillips3e302272016-04-20 11:48:36 -070084sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(GrContext* context) {
robertphillips83c17fa2016-03-18 08:14:27 -070085#if SK_SUPPORT_GPU
86 if (!context) {
87 return nullptr;
88 }
Robert Phillips8bc06d02016-11-01 17:28:40 -040089 if (GrContext* curContext = as_SIB(this)->onGetContext()) {
90 return curContext == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr;
robertphillips83c17fa2016-03-18 08:14:27 -070091 }
92
93 SkBitmap bmp;
reedcf5c8462016-07-20 12:28:40 -070094 // At this point, we are definitely not texture-backed, so we must be raster or generator
95 // backed. If we remove the special-wrapping-an-image subclass, we may be able to assert that
96 // we are strictly raster-backed (i.e. generator images become raster when they are specialized)
97 // in which case getROPixels could turn into peekPixels...
98 if (!this->getROPixels(&bmp)) {
robertphillips83c17fa2016-03-18 08:14:27 -070099 return nullptr;
100 }
101
robertphillips83f2e5a2016-03-24 06:31:25 -0700102 if (bmp.empty()) {
robertphillips3e302272016-04-20 11:48:36 -0700103 return SkSpecialImage::MakeFromRaster(SkIRect::MakeEmpty(), bmp, &this->props());
robertphillips83f2e5a2016-03-24 06:31:25 -0700104 }
105
Robert Phillipse14d3052017-02-15 13:18:21 -0500106 // TODO: this is a tight copy of 'bmp' but it doesn't have to be (given SkSpecialImage's
107 // semantics). Since this is cached though we would have to bake the fit into the cache key.
Robert Phillips26c90e02017-03-14 14:39:29 -0400108 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(context->resourceProvider(), bmp);
Robert Phillipse14d3052017-02-15 13:18:21 -0500109 if (!proxy) {
robertphillips83c17fa2016-03-18 08:14:27 -0700110 return nullptr;
111 }
112
Robert Phillipse14d3052017-02-15 13:18:21 -0500113 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
114
115 // GrMakeCachedBitmapProxy has uploaded only the specified subset of 'bmp' so we need not
116 // bother with SkBitmap::getSubset
117 return SkSpecialImage::MakeDeferredFromGpu(context,
118 rect,
119 this->uniqueID(),
120 std::move(proxy),
121 sk_ref_sp(this->getColorSpace()),
122 &this->props(),
123 this->alphaType());
robertphillips83c17fa2016-03-18 08:14:27 -0700124#else
125 return nullptr;
126#endif
127}
128
robertphillipse8c34972016-02-16 12:09:36 -0800129void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
bsalomon84a4e5a2016-02-29 11:41:52 -0800130 return as_SIB(this)->onDraw(canvas, x, y, paint);
robertphillipsb6c65e92016-02-04 10:52:42 -0800131}
132
robertphillips64612512016-04-08 12:10:42 -0700133bool SkSpecialImage::getROPixels(SkBitmap* bm) const {
134 return as_SIB(this)->onGetROPixels(bm);
robertphillipsb6c65e92016-02-04 10:52:42 -0800135}
136
robertphillips64612512016-04-08 12:10:42 -0700137bool SkSpecialImage::isTextureBacked() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400138 return SkToBool(as_SIB(this)->onGetContext());
robertphillipsb6c65e92016-02-04 10:52:42 -0800139}
140
robertphillips64612512016-04-08 12:10:42 -0700141GrContext* SkSpecialImage::getContext() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400142 return as_SIB(this)->onGetContext();
robertphillips64612512016-04-08 12:10:42 -0700143}
144
brianosmanafbf71d2016-07-21 07:15:37 -0700145SkColorSpace* SkSpecialImage::getColorSpace() const {
146 return as_SIB(this)->onGetColorSpace();
147}
148
robertphillipsc91fd342016-04-25 12:32:54 -0700149#if SK_SUPPORT_GPU
Robert Phillips8e1c4e62017-02-19 12:27:01 -0500150sk_sp<GrTextureProxy> SkSpecialImage::asTextureProxyRef(GrContext* context) const {
151 return as_SIB(this)->onAsTextureProxyRef(context);
Robert Phillips8bc06d02016-11-01 17:28:40 -0400152}
robertphillipsc91fd342016-04-25 12:32:54 -0700153#endif
robertphillips4418dba2016-03-07 12:45:14 -0800154
brianosmaneed6b0e2016-09-23 13:04:05 -0700155sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageFilter::OutputProperties& outProps,
156 const SkISize& size, SkAlphaType at) const {
157 return as_SIB(this)->onMakeSurface(outProps, size, at);
robertphillipsb6c65e92016-02-04 10:52:42 -0800158}
159
brianosmaneed6b0e2016-09-23 13:04:05 -0700160sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageFilter::OutputProperties& outProps,
161 const SkISize& size, SkAlphaType at) const {
162 return as_SIB(this)->onMakeTightSurface(outProps, size, at);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700163}
164
robertphillips37bd7c32016-03-17 14:31:39 -0700165sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const {
166 return as_SIB(this)->onMakeSubset(subset);
robertphillipsc5035e72016-03-17 06:58:39 -0700167}
168
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500169sk_sp<SkImage> SkSpecialImage::asImage(const SkIRect* subset) const {
170 return as_SIB(this)->onAsImage(subset);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700171}
172
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500173
robertphillipsb6c65e92016-02-04 10:52:42 -0800174#ifdef SK_DEBUG
175static bool rect_fits(const SkIRect& rect, int width, int height) {
robertphillips4418dba2016-03-07 12:45:14 -0800176 if (0 == width && 0 == height) {
177 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom);
178 return true;
179 }
180
robertphillipsb6c65e92016-02-04 10:52:42 -0800181 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight &&
182 rect.fRight >= 0 && rect.fRight <= width &&
183 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom &&
184 rect.fBottom >= 0 && rect.fBottom <= height;
185}
186#endif
187
robertphillips3e302272016-04-20 11:48:36 -0700188sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -0700189 sk_sp<SkImage> image,
Brian Osman61624f02016-12-09 14:51:59 -0500190 SkColorSpace* dstColorSpace,
brianosman898235c2016-04-06 07:38:23 -0700191 const SkSurfaceProps* props) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800192 SkASSERT(rect_fits(subset, image->width(), image->height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700193
reedad7604b2016-07-20 16:13:32 -0700194#if SK_SUPPORT_GPU
Robert Phillips6de99042017-01-31 11:31:39 -0500195 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef()) {
196 GrContext* context = ((SkImage_Gpu*) as_IB(image))->context();
197
198 return MakeDeferredFromGpu(context, subset, image->uniqueID(), std::move(proxy),
Robert Phillips256c37b2017-03-01 14:32:46 -0500199 as_IB(image)->onImageInfo().refColorSpace(), props);
reedad7604b2016-07-20 16:13:32 -0700200 } else
201#endif
202 {
203 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -0500204 if (as_IB(image)->getROPixels(&bm, dstColorSpace)) {
reedad7604b2016-07-20 16:13:32 -0700205 return MakeFromRaster(subset, bm, props);
206 }
reeda2217ef2016-07-20 06:04:34 -0700207 }
reedad7604b2016-07-20 16:13:32 -0700208 return nullptr;
robertphillipsb6c65e92016-02-04 10:52:42 -0800209}
210
211///////////////////////////////////////////////////////////////////////////////
robertphillipsb6c65e92016-02-04 10:52:42 -0800212
213class SkSpecialImage_Raster : public SkSpecialImage_Base {
214public:
robertphillips3e302272016-04-20 11:48:36 -0700215 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props)
216 : INHERITED(subset, bm.getGenerationID(), props)
reedbd2bd5c2016-07-25 14:26:02 -0700217 , fBitmap(bm)
218 {
219 SkASSERT(bm.pixelRef());
220
221 // We have to lock now, while bm is still in scope, since it may have come from our
222 // cache, which means we need to keep it locked until we (the special) are done, since
223 // we cannot re-generate the cache entry (if bm came from a generator).
224 fBitmap.lockPixels();
225 SkASSERT(fBitmap.getPixels());
robertphillipsb6c65e92016-02-04 10:52:42 -0800226 }
227
brianosman80e96082016-08-16 07:09:47 -0700228 SkAlphaType alphaType() const override { return fBitmap.alphaType(); }
robertphillips3b087f42016-02-18 08:48:03 -0800229
230 size_t getSize() const override { return fBitmap.getSize(); }
231
robertphillipse8c34972016-02-16 12:09:36 -0800232 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800233 SkRect dst = SkRect::MakeXYWH(x, y,
234 this->subset().width(), this->subset().height());
235
236 canvas->drawBitmapRect(fBitmap, this->subset(),
237 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
238 }
239
robertphillips64612512016-04-08 12:10:42 -0700240 bool onGetROPixels(SkBitmap* bm) const override {
241 *bm = fBitmap;
robertphillips3b087f42016-02-18 08:48:03 -0800242 return true;
243 }
244
brianosmanafbf71d2016-07-21 07:15:37 -0700245 SkColorSpace* onGetColorSpace() const override {
246 return fBitmap.colorSpace();
247 }
248
robertphillips64612512016-04-08 12:10:42 -0700249#if SK_SUPPORT_GPU
Robert Phillips8e1c4e62017-02-19 12:27:01 -0500250 sk_sp<GrTextureProxy> onAsTextureProxyRef(GrContext* context) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400251 if (context) {
Robert Phillips26c90e02017-03-14 14:39:29 -0400252 return GrMakeCachedBitmapProxy(context->resourceProvider(), fBitmap);
Robert Phillips8bc06d02016-11-01 17:28:40 -0400253 }
254
255 return nullptr;
256 }
robertphillipsc91fd342016-04-25 12:32:54 -0700257#endif
robertphillips64612512016-04-08 12:10:42 -0700258
brianosmaneed6b0e2016-09-23 13:04:05 -0700259// TODO: The raster implementations of image filters all currently assume that the pixels are
260// legacy N32. Until they actually check the format and operate on sRGB or F16 data appropriately,
261// we can't enable this. (They will continue to produce incorrect results, but less-so).
262#define RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 0
263
264 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
265 const SkISize& size, SkAlphaType at) const override {
266#if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16
267 SkColorSpace* colorSpace = outProps.colorSpace();
268#else
269 SkColorSpace* colorSpace = nullptr;
270#endif
271 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
272 ? kRGBA_F16_SkColorType : kN32_SkColorType;
273 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
274 sk_ref_sp(colorSpace));
robertphillips3e302272016-04-20 11:48:36 -0700275 return SkSpecialSurface::MakeRaster(info, nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -0800276 }
277
robertphillips37bd7c32016-03-17 14:31:39 -0700278 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
robertphillipsc5035e72016-03-17 06:58:39 -0700279 SkBitmap subsetBM;
halcanary9d524f22016-03-29 09:03:52 -0700280
robertphillipsc5035e72016-03-17 06:58:39 -0700281 if (!fBitmap.extractSubset(&subsetBM, subset)) {
282 return nullptr;
283 }
284
robertphillips3e302272016-04-20 11:48:36 -0700285 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()),
brianosman898235c2016-04-06 07:38:23 -0700286 subsetBM,
287 &this->props());
robertphillipsc5035e72016-03-17 06:58:39 -0700288 }
289
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500290 sk_sp<SkImage> onAsImage(const SkIRect* subset) const override {
291 if (subset) {
292 SkBitmap subsetBM;
robertphillipsb4bd11e2016-03-21 13:44:18 -0700293
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500294 if (!fBitmap.extractSubset(&subsetBM, *subset)) {
295 return nullptr;
296 }
297
298 return SkImage::MakeFromBitmap(subsetBM);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700299 }
300
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500301 return SkImage::MakeFromBitmap(fBitmap);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700302 }
303
brianosmaneed6b0e2016-09-23 13:04:05 -0700304 sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
305 const SkISize& size, SkAlphaType at) const override {
306#if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16
307 SkColorSpace* colorSpace = outProps.colorSpace();
308#else
309 SkColorSpace* colorSpace = nullptr;
310#endif
311 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
312 ? kRGBA_F16_SkColorType : kN32_SkColorType;
313 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
314 sk_ref_sp(colorSpace));
reede8f30622016-03-23 18:59:25 -0700315 return SkSurface::MakeRaster(info);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700316 }
317
robertphillipsb6c65e92016-02-04 10:52:42 -0800318private:
319 SkBitmap fBitmap;
320
321 typedef SkSpecialImage_Base INHERITED;
322};
323
robertphillips3e302272016-04-20 11:48:36 -0700324sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -0700325 const SkBitmap& bm,
326 const SkSurfaceProps* props) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800327 SkASSERT(rect_fits(subset, bm.width(), bm.height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700328
reedbd2bd5c2016-07-25 14:26:02 -0700329 if (!bm.pixelRef()) {
330 return nullptr;
331 }
332
reeda2217ef2016-07-20 06:04:34 -0700333 const SkBitmap* srcBM = &bm;
334 SkBitmap tmpStorage;
335 // ImageFilters only handle N32 at the moment, so force our src to be that
336 if (!valid_for_imagefilters(bm.info())) {
337 if (!bm.copyTo(&tmpStorage, kN32_SkColorType)) {
338 return nullptr;
339 }
340 srcBM = &tmpStorage;
341 }
342 return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props);
robertphillipsb6c65e92016-02-04 10:52:42 -0800343}
344
345#if SK_SUPPORT_GPU
346///////////////////////////////////////////////////////////////////////////////
robertphillipsed086ca2016-04-26 15:02:25 -0700347#include "GrTexture.h"
robertphillipsb6c65e92016-02-04 10:52:42 -0800348
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400349static sk_sp<SkImage> wrap_proxy_in_image(GrContext* context, sk_sp<GrTextureProxy> proxy,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500350 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400351 return sk_make_sp<SkImage_Gpu>(context, kNeedNewImageUniqueID, alphaType,
352 std::move(proxy), std::move(colorSpace), SkBudgeted::kYes);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500353}
354
robertphillipsed086ca2016-04-26 15:02:25 -0700355class SkSpecialImage_Gpu : public SkSpecialImage_Base {
356public:
Robert Phillips8bc06d02016-11-01 17:28:40 -0400357 SkSpecialImage_Gpu(GrContext* context, const SkIRect& subset,
Robert Phillips63c67462017-02-15 14:19:01 -0500358 uint32_t uniqueID, sk_sp<GrTextureProxy> proxy, SkAlphaType at,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400359 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props)
360 : INHERITED(subset, uniqueID, props)
361 , fContext(context)
Robert Phillips63c67462017-02-15 14:19:01 -0500362 , fTextureProxy(std::move(proxy))
robertphillipsed086ca2016-04-26 15:02:25 -0700363 , fAlphaType(at)
brianosmanafbf71d2016-07-21 07:15:37 -0700364 , fColorSpace(std::move(colorSpace))
robertphillipsed086ca2016-04-26 15:02:25 -0700365 , fAddedRasterVersionToCache(false) {
366 }
367
368 ~SkSpecialImage_Gpu() override {
369 if (fAddedRasterVersionToCache.load()) {
370 SkNotifyBitmapGenIDIsStale(this->uniqueID());
371 }
372 }
373
brianosman80e96082016-08-16 07:09:47 -0700374 SkAlphaType alphaType() const override { return fAlphaType; }
robertphillipsed086ca2016-04-26 15:02:25 -0700375
Robert Phillips63c67462017-02-15 14:19:01 -0500376 size_t getSize() const override { return fTextureProxy->gpuMemorySize(); }
robertphillipsed086ca2016-04-26 15:02:25 -0700377
378 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
379 SkRect dst = SkRect::MakeXYWH(x, y,
380 this->subset().width(), this->subset().height());
381
Robert Phillips7f6cd902016-11-10 17:03:43 -0500382 // TODO: In this instance we know we're going to draw a sub-portion of the backing
383 // texture into the canvas so it is okay to wrap it in an SkImage. This poses
384 // some problems for full deferral however in that when the deferred SkImage_Gpu
385 // instantiates itself it is going to have to either be okay with having a larger
Robert Phillips63c67462017-02-15 14:19:01 -0500386 // than expected backing texture (unlikely) or the 'fit' of the SurfaceProxy needs
Robert Phillips7f6cd902016-11-10 17:03:43 -0500387 // to be tightened (if it is deferred).
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400388 sk_sp<SkImage> img = sk_sp<SkImage>(new SkImage_Gpu(canvas->getGrContext(),
389 this->uniqueID(), fAlphaType,
390 fTextureProxy,
391 fColorSpace, SkBudgeted::kNo));
robertphillipsed086ca2016-04-26 15:02:25 -0700392
reed77d6f7d2016-07-13 12:24:48 -0700393 canvas->drawImageRect(img, this->subset(),
Robert Phillips7f6cd902016-11-10 17:03:43 -0500394 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
robertphillipsed086ca2016-04-26 15:02:25 -0700395 }
396
Robert Phillips8bc06d02016-11-01 17:28:40 -0400397 GrContext* onGetContext() const override { return fContext; }
robertphillipsed086ca2016-04-26 15:02:25 -0700398
Robert Phillips8e1c4e62017-02-19 12:27:01 -0500399 sk_sp<GrTextureProxy> onAsTextureProxyRef(GrContext*) const override {
Robert Phillips63c67462017-02-15 14:19:01 -0500400 return fTextureProxy;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400401 }
robertphillipsed086ca2016-04-26 15:02:25 -0700402
403 bool onGetROPixels(SkBitmap* dst) const override {
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400404 const auto desc = SkBitmapCacheDesc::Make(this->uniqueID(), this->width(), this->height());
405 if (SkBitmapCache::Find(desc, dst)) {
robertphillipsed086ca2016-04-26 15:02:25 -0700406 SkASSERT(dst->getGenerationID() == this->uniqueID());
407 SkASSERT(dst->isImmutable());
408 SkASSERT(dst->getPixels());
409 return true;
410 }
411
412 SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(),
brianosman80e96082016-08-16 07:09:47 -0700413 this->alphaType(), fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700414
415 if (!dst->tryAllocPixels(info)) {
416 return false;
417 }
418
Robert Phillips8bc06d02016-11-01 17:28:40 -0400419 // Reading back to an SkBitmap ends deferral
Brian Osman32342f02017-03-04 08:12:46 -0500420 GrTexture* texture = fTextureProxy->instantiate(fContext->resourceProvider());
Robert Phillips63c67462017-02-15 14:19:01 -0500421 if (!texture) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500422 return false;
423 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400424
Robert Phillips63c67462017-02-15 14:19:01 -0500425 if (!texture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400426 dst->getPixels(), dst->rowBytes())) {
robertphillipsed086ca2016-04-26 15:02:25 -0700427 return false;
428 }
429
430 dst->pixelRef()->setImmutableWithID(this->uniqueID());
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400431 SkBitmapCache::Add(desc, *dst);
robertphillipsed086ca2016-04-26 15:02:25 -0700432 fAddedRasterVersionToCache.store(true);
433 return true;
434 }
435
brianosmanafbf71d2016-07-21 07:15:37 -0700436 SkColorSpace* onGetColorSpace() const override {
437 return fColorSpace.get();
438 }
439
brianosmaneed6b0e2016-09-23 13:04:05 -0700440 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
441 const SkISize& size, SkAlphaType at) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400442 if (!fContext) {
robertphillipsed086ca2016-04-26 15:02:25 -0700443 return nullptr;
444 }
445
brianosmaneed6b0e2016-09-23 13:04:05 -0700446 SkColorSpace* colorSpace = outProps.colorSpace();
447 return SkSpecialSurface::MakeRenderTarget(
Robert Phillips8bc06d02016-11-01 17:28:40 -0400448 fContext, size.width(), size.height(),
brianosmaneed6b0e2016-09-23 13:04:05 -0700449 GrRenderableConfigForColorSpace(colorSpace), sk_ref_sp(colorSpace));
robertphillipsed086ca2016-04-26 15:02:25 -0700450 }
451
452 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400453 return SkSpecialImage::MakeDeferredFromGpu(fContext,
454 subset,
455 this->uniqueID(),
Robert Phillips63c67462017-02-15 14:19:01 -0500456 fTextureProxy,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400457 fColorSpace,
458 &this->props(),
459 fAlphaType);
robertphillipsed086ca2016-04-26 15:02:25 -0700460 }
461
Robert Phillipse2f7d182016-12-15 09:23:05 -0500462 // TODO: move all the logic here into the subset-flavor GrSurfaceProxy::copy?
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500463 sk_sp<SkImage> onAsImage(const SkIRect* subset) const override {
464 if (subset) {
465 // TODO: if this becomes a bottle neck we could base this logic on what the size
466 // will be when it is finally instantiated - but that is more fraught.
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400467 if (GrResourceProvider::IsFunctionallyExact(fTextureProxy.get()) &&
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500468 0 == subset->fLeft && 0 == subset->fTop &&
469 fTextureProxy->width() == subset->width() &&
470 fTextureProxy->height() == subset->height()) {
471 // The existing GrTexture is already tight so reuse it in the SkImage
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400472 return wrap_proxy_in_image(fContext, fTextureProxy, fAlphaType, fColorSpace);
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500473 }
474
475 sk_sp<GrTextureProxy> subsetProxy(GrSurfaceProxy::Copy(fContext, fTextureProxy.get(),
476 *subset, SkBudgeted::kYes));
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400477 if (!subsetProxy) {
478 return nullptr;
479 }
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500480
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400481 SkASSERT(subsetProxy->priv().isExact());
482 // MDB: this is acceptable (wrapping subsetProxy in an SkImage) bc Copy will
483 // return a kExact-backed proxy
484 return wrap_proxy_in_image(fContext, std::move(subsetProxy), fAlphaType, fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700485 }
486
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400487 fTextureProxy->priv().exactify();
488
489 return wrap_proxy_in_image(fContext, fTextureProxy, fAlphaType, fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700490 }
491
brianosmaneed6b0e2016-09-23 13:04:05 -0700492 sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
493 const SkISize& size, SkAlphaType at) const override {
494 SkColorSpace* colorSpace = outProps.colorSpace();
495 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
496 ? kRGBA_F16_SkColorType : kRGBA_8888_SkColorType;
497 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
498 sk_ref_sp(colorSpace));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400499 return SkSurface::MakeRenderTarget(fContext, SkBudgeted::kYes, info);
robertphillipsed086ca2016-04-26 15:02:25 -0700500 }
501
502private:
Robert Phillips8bc06d02016-11-01 17:28:40 -0400503 GrContext* fContext;
Robert Phillips63c67462017-02-15 14:19:01 -0500504 sk_sp<GrTextureProxy> fTextureProxy;
robertphillipsed086ca2016-04-26 15:02:25 -0700505 const SkAlphaType fAlphaType;
brianosmanafbf71d2016-07-21 07:15:37 -0700506 sk_sp<SkColorSpace> fColorSpace;
robertphillipsed086ca2016-04-26 15:02:25 -0700507 mutable SkAtomic<bool> fAddedRasterVersionToCache;
508
509 typedef SkSpecialImage_Base INHERITED;
510};
511
Robert Phillips8bc06d02016-11-01 17:28:40 -0400512sk_sp<SkSpecialImage> SkSpecialImage::MakeDeferredFromGpu(GrContext* context,
513 const SkIRect& subset,
514 uint32_t uniqueID,
Robert Phillips63c67462017-02-15 14:19:01 -0500515 sk_sp<GrTextureProxy> proxy,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400516 sk_sp<SkColorSpace> colorSpace,
517 const SkSurfaceProps* props,
518 SkAlphaType at) {
519 SkASSERT(rect_fits(subset, proxy->width(), proxy->height()));
520 return sk_make_sp<SkSpecialImage_Gpu>(context, subset, uniqueID, std::move(proxy), at,
521 std::move(colorSpace), props);
522}
robertphillipsb6c65e92016-02-04 10:52:42 -0800523#endif