blob: c97eb012571127022a3bf4e0fb6b457e6c1d90b2 [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"
20#include "GrTexture.h"
21#include "GrTextureParams.h"
Robert Phillips8bc06d02016-11-01 17:28:40 -040022#include "GrTextureProxy.h"
reedad7604b2016-07-20 16:13:32 -070023#include "SkGr.h"
reedad7604b2016-07-20 16:13:32 -070024#include "SkGrPriv.h"
25#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080026
reeda2217ef2016-07-20 06:04:34 -070027// Currently the raster imagefilters can only handle certain imageinfos. Call this to know if
28// a given info is supported.
29static bool valid_for_imagefilters(const SkImageInfo& info) {
30 // no support for other swizzles/depths yet
31 return info.colorType() == kN32_SkColorType;
32}
33
robertphillipsb6c65e92016-02-04 10:52:42 -080034///////////////////////////////////////////////////////////////////////////////
35class SkSpecialImage_Base : public SkSpecialImage {
36public:
robertphillips3e302272016-04-20 11:48:36 -070037 SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps* props)
38 : INHERITED(subset, uniqueID, props) {
robertphillips3b087f42016-02-18 08:48:03 -080039 }
robertphillips3e302272016-04-20 11:48:36 -070040 ~SkSpecialImage_Base() override { }
robertphillipsb6c65e92016-02-04 10:52:42 -080041
robertphillipse8c34972016-02-16 12:09:36 -080042 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080043
robertphillips64612512016-04-08 12:10:42 -070044 virtual bool onGetROPixels(SkBitmap*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080045
Robert Phillips8bc06d02016-11-01 17:28:40 -040046 virtual GrContext* onGetContext() const { return nullptr; }
robertphillipsb6c65e92016-02-04 10:52:42 -080047
brianosmanafbf71d2016-07-21 07:15:37 -070048 virtual SkColorSpace* onGetColorSpace() const = 0;
49
robertphillipsc91fd342016-04-25 12:32:54 -070050#if SK_SUPPORT_GPU
51 virtual sk_sp<GrTexture> onAsTextureRef(GrContext* context) const = 0;
Robert Phillips8bc06d02016-11-01 17:28:40 -040052 virtual sk_sp<GrTextureProxy> onAsTextureProxy(GrContext* context) const = 0;
robertphillipsc91fd342016-04-25 12:32:54 -070053#endif
robertphillips4418dba2016-03-07 12:45:14 -080054
robertphillipsb4bd11e2016-03-21 13:44:18 -070055 virtual sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const = 0;
56
brianosmaneed6b0e2016-09-23 13:04:05 -070057 virtual sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
58 const SkISize& size, SkAlphaType at) const = 0;
robertphillipsc5035e72016-03-17 06:58:39 -070059
robertphillipsb4bd11e2016-03-21 13:44:18 -070060 virtual sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const = 0;
61
brianosmaneed6b0e2016-09-23 13:04:05 -070062 virtual sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
63 const SkISize& size, SkAlphaType at) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080064
65private:
66 typedef SkSpecialImage INHERITED;
67};
68
69///////////////////////////////////////////////////////////////////////////////
bsalomon84a4e5a2016-02-29 11:41:52 -080070static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) {
robertphillipsb6c65e92016-02-04 10:52:42 -080071 return static_cast<const SkSpecialImage_Base*>(image);
72}
73
robertphillips3e302272016-04-20 11:48:36 -070074SkSpecialImage::SkSpecialImage(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -070075 uint32_t uniqueID,
76 const SkSurfaceProps* props)
77 : fProps(SkSurfacePropsCopyOrDefault(props))
78 , fSubset(subset)
robertphillips3e302272016-04-20 11:48:36 -070079 , fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID) {
brianosman898235c2016-04-06 07:38:23 -070080}
81
robertphillips3e302272016-04-20 11:48:36 -070082sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(GrContext* context) {
robertphillips83c17fa2016-03-18 08:14:27 -070083#if SK_SUPPORT_GPU
84 if (!context) {
85 return nullptr;
86 }
Robert Phillips8bc06d02016-11-01 17:28:40 -040087 if (GrContext* curContext = as_SIB(this)->onGetContext()) {
88 return curContext == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr;
robertphillips83c17fa2016-03-18 08:14:27 -070089 }
90
91 SkBitmap bmp;
reedcf5c8462016-07-20 12:28:40 -070092 // At this point, we are definitely not texture-backed, so we must be raster or generator
93 // backed. If we remove the special-wrapping-an-image subclass, we may be able to assert that
94 // we are strictly raster-backed (i.e. generator images become raster when they are specialized)
95 // in which case getROPixels could turn into peekPixels...
96 if (!this->getROPixels(&bmp)) {
robertphillips83c17fa2016-03-18 08:14:27 -070097 return nullptr;
98 }
99
robertphillips83f2e5a2016-03-24 06:31:25 -0700100 if (bmp.empty()) {
robertphillips3e302272016-04-20 11:48:36 -0700101 return SkSpecialImage::MakeFromRaster(SkIRect::MakeEmpty(), bmp, &this->props());
robertphillips83f2e5a2016-03-24 06:31:25 -0700102 }
103
Brian Osman7b8400d2016-11-08 17:08:54 -0500104 sk_sp<GrTexture> resultTex(
105 GrRefCachedBitmapTexture(context, bmp, GrTextureParams::ClampNoFilter(),
106 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
robertphillips83c17fa2016-03-18 08:14:27 -0700107 if (!resultTex) {
108 return nullptr;
109 }
110
robertphillips3e302272016-04-20 11:48:36 -0700111 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(resultTex->width(), resultTex->height()),
robertphillips83c17fa2016-03-18 08:14:27 -0700112 this->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -0700113 resultTex, sk_ref_sp(this->getColorSpace()), &this->props(),
brianosman80e96082016-08-16 07:09:47 -0700114 this->alphaType());
robertphillips83c17fa2016-03-18 08:14:27 -0700115#else
116 return nullptr;
117#endif
118}
119
robertphillipse8c34972016-02-16 12:09:36 -0800120void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
bsalomon84a4e5a2016-02-29 11:41:52 -0800121 return as_SIB(this)->onDraw(canvas, x, y, paint);
robertphillipsb6c65e92016-02-04 10:52:42 -0800122}
123
robertphillips64612512016-04-08 12:10:42 -0700124bool SkSpecialImage::getROPixels(SkBitmap* bm) const {
125 return as_SIB(this)->onGetROPixels(bm);
robertphillipsb6c65e92016-02-04 10:52:42 -0800126}
127
robertphillips64612512016-04-08 12:10:42 -0700128bool SkSpecialImage::isTextureBacked() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400129 return SkToBool(as_SIB(this)->onGetContext());
robertphillipsb6c65e92016-02-04 10:52:42 -0800130}
131
robertphillips64612512016-04-08 12:10:42 -0700132GrContext* SkSpecialImage::getContext() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400133 return as_SIB(this)->onGetContext();
robertphillips64612512016-04-08 12:10:42 -0700134}
135
brianosmanafbf71d2016-07-21 07:15:37 -0700136SkColorSpace* SkSpecialImage::getColorSpace() const {
137 return as_SIB(this)->onGetColorSpace();
138}
139
robertphillipsc91fd342016-04-25 12:32:54 -0700140#if SK_SUPPORT_GPU
141sk_sp<GrTexture> SkSpecialImage::asTextureRef(GrContext* context) const {
robertphillips64612512016-04-08 12:10:42 -0700142 return as_SIB(this)->onAsTextureRef(context);
robertphillips4418dba2016-03-07 12:45:14 -0800143}
Robert Phillips8bc06d02016-11-01 17:28:40 -0400144
145sk_sp<GrTextureProxy> SkSpecialImage::asTextureProxy(GrContext* context) const {
146 return as_SIB(this)->onAsTextureProxy(context);
147}
robertphillipsc91fd342016-04-25 12:32:54 -0700148#endif
robertphillips4418dba2016-03-07 12:45:14 -0800149
brianosmaneed6b0e2016-09-23 13:04:05 -0700150sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageFilter::OutputProperties& outProps,
151 const SkISize& size, SkAlphaType at) const {
152 return as_SIB(this)->onMakeSurface(outProps, size, at);
robertphillipsb6c65e92016-02-04 10:52:42 -0800153}
154
brianosmaneed6b0e2016-09-23 13:04:05 -0700155sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageFilter::OutputProperties& outProps,
156 const SkISize& size, SkAlphaType at) const {
157 return as_SIB(this)->onMakeTightSurface(outProps, size, at);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700158}
159
robertphillips37bd7c32016-03-17 14:31:39 -0700160sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const {
161 return as_SIB(this)->onMakeSubset(subset);
robertphillipsc5035e72016-03-17 06:58:39 -0700162}
163
robertphillipsb4bd11e2016-03-21 13:44:18 -0700164sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const {
165 return as_SIB(this)->onMakeTightSubset(subset);
166}
167
robertphillipsb6c65e92016-02-04 10:52:42 -0800168#ifdef SK_DEBUG
169static bool rect_fits(const SkIRect& rect, int width, int height) {
robertphillips4418dba2016-03-07 12:45:14 -0800170 if (0 == width && 0 == height) {
171 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom);
172 return true;
173 }
174
robertphillipsb6c65e92016-02-04 10:52:42 -0800175 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight &&
176 rect.fRight >= 0 && rect.fRight <= width &&
177 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom &&
178 rect.fBottom >= 0 && rect.fBottom <= height;
179}
180#endif
181
robertphillips3e302272016-04-20 11:48:36 -0700182sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -0700183 sk_sp<SkImage> image,
184 const SkSurfaceProps* props) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800185 SkASSERT(rect_fits(subset, image->width(), image->height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700186
reedad7604b2016-07-20 16:13:32 -0700187#if SK_SUPPORT_GPU
188 if (GrTexture* texture = as_IB(image)->peekTexture()) {
brianosmanafbf71d2016-07-21 07:15:37 -0700189 return MakeFromGpu(subset, image->uniqueID(), sk_ref_sp(texture),
190 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()), props);
reedad7604b2016-07-20 16:13:32 -0700191 } else
192#endif
193 {
194 SkBitmap bm;
Brian Osman57ae6cf2016-11-15 18:07:26 +0000195 if (as_IB(image)->getROPixels(&bm)) {
reedad7604b2016-07-20 16:13:32 -0700196 return MakeFromRaster(subset, bm, props);
197 }
reeda2217ef2016-07-20 06:04:34 -0700198 }
reedad7604b2016-07-20 16:13:32 -0700199 return nullptr;
robertphillipsb6c65e92016-02-04 10:52:42 -0800200}
201
202///////////////////////////////////////////////////////////////////////////////
robertphillipsb6c65e92016-02-04 10:52:42 -0800203
204class SkSpecialImage_Raster : public SkSpecialImage_Base {
205public:
robertphillips3e302272016-04-20 11:48:36 -0700206 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props)
207 : INHERITED(subset, bm.getGenerationID(), props)
reedbd2bd5c2016-07-25 14:26:02 -0700208 , fBitmap(bm)
209 {
210 SkASSERT(bm.pixelRef());
211
212 // We have to lock now, while bm is still in scope, since it may have come from our
213 // cache, which means we need to keep it locked until we (the special) are done, since
214 // we cannot re-generate the cache entry (if bm came from a generator).
215 fBitmap.lockPixels();
216 SkASSERT(fBitmap.getPixels());
robertphillipsb6c65e92016-02-04 10:52:42 -0800217 }
218
brianosman80e96082016-08-16 07:09:47 -0700219 SkAlphaType alphaType() const override { return fBitmap.alphaType(); }
robertphillips3b087f42016-02-18 08:48:03 -0800220
221 size_t getSize() const override { return fBitmap.getSize(); }
222
robertphillipse8c34972016-02-16 12:09:36 -0800223 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800224 SkRect dst = SkRect::MakeXYWH(x, y,
225 this->subset().width(), this->subset().height());
226
227 canvas->drawBitmapRect(fBitmap, this->subset(),
228 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
229 }
230
robertphillips64612512016-04-08 12:10:42 -0700231 bool onGetROPixels(SkBitmap* bm) const override {
232 *bm = fBitmap;
robertphillips3b087f42016-02-18 08:48:03 -0800233 return true;
234 }
235
brianosmanafbf71d2016-07-21 07:15:37 -0700236 SkColorSpace* onGetColorSpace() const override {
237 return fBitmap.colorSpace();
238 }
239
robertphillips64612512016-04-08 12:10:42 -0700240#if SK_SUPPORT_GPU
robertphillipsc91fd342016-04-25 12:32:54 -0700241 sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override {
robertphillips64612512016-04-08 12:10:42 -0700242 if (context) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500243 return sk_ref_sp(
244 GrRefCachedBitmapTexture(context, fBitmap, GrTextureParams::ClampNoFilter(),
245 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
robertphillips64612512016-04-08 12:10:42 -0700246 }
robertphillips64612512016-04-08 12:10:42 -0700247
248 return nullptr;
249 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400250
251 sk_sp<GrTextureProxy> onAsTextureProxy(GrContext* context) const override {
252 if (context) {
Robert Phillips37430132016-11-09 06:50:43 -0500253 sk_sp<GrTexture> tex(sk_ref_sp(GrRefCachedBitmapTexture(
254 context,
255 fBitmap,
256 GrTextureParams::ClampNoFilter(),
257 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware)));
258 sk_sp<GrSurfaceProxy> sProxy = GrSurfaceProxy::MakeWrapped(std::move(tex));
259 return sk_ref_sp(sProxy->asTextureProxy());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400260 }
261
262 return nullptr;
263 }
robertphillipsc91fd342016-04-25 12:32:54 -0700264#endif
robertphillips64612512016-04-08 12:10:42 -0700265
brianosmaneed6b0e2016-09-23 13:04:05 -0700266// TODO: The raster implementations of image filters all currently assume that the pixels are
267// legacy N32. Until they actually check the format and operate on sRGB or F16 data appropriately,
268// we can't enable this. (They will continue to produce incorrect results, but less-so).
269#define RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 0
270
271 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
272 const SkISize& size, SkAlphaType at) const override {
273#if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16
274 SkColorSpace* colorSpace = outProps.colorSpace();
275#else
276 SkColorSpace* colorSpace = nullptr;
277#endif
278 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
279 ? kRGBA_F16_SkColorType : kN32_SkColorType;
280 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
281 sk_ref_sp(colorSpace));
robertphillips3e302272016-04-20 11:48:36 -0700282 return SkSpecialSurface::MakeRaster(info, nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -0800283 }
284
robertphillips37bd7c32016-03-17 14:31:39 -0700285 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
robertphillipsc5035e72016-03-17 06:58:39 -0700286 SkBitmap subsetBM;
halcanary9d524f22016-03-29 09:03:52 -0700287
robertphillipsc5035e72016-03-17 06:58:39 -0700288 if (!fBitmap.extractSubset(&subsetBM, subset)) {
289 return nullptr;
290 }
291
robertphillips3e302272016-04-20 11:48:36 -0700292 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()),
brianosman898235c2016-04-06 07:38:23 -0700293 subsetBM,
294 &this->props());
robertphillipsc5035e72016-03-17 06:58:39 -0700295 }
296
robertphillipsb4bd11e2016-03-21 13:44:18 -0700297 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override {
298 SkBitmap subsetBM;
299
300 if (!fBitmap.extractSubset(&subsetBM, subset)) {
301 return nullptr;
302 }
303
304 return SkImage::MakeFromBitmap(subsetBM);
305 }
306
brianosmaneed6b0e2016-09-23 13:04:05 -0700307 sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
308 const SkISize& size, SkAlphaType at) const override {
309#if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16
310 SkColorSpace* colorSpace = outProps.colorSpace();
311#else
312 SkColorSpace* colorSpace = nullptr;
313#endif
314 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
315 ? kRGBA_F16_SkColorType : kN32_SkColorType;
316 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
317 sk_ref_sp(colorSpace));
reede8f30622016-03-23 18:59:25 -0700318 return SkSurface::MakeRaster(info);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700319 }
320
robertphillipsb6c65e92016-02-04 10:52:42 -0800321private:
322 SkBitmap fBitmap;
323
324 typedef SkSpecialImage_Base INHERITED;
325};
326
robertphillips3e302272016-04-20 11:48:36 -0700327sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -0700328 const SkBitmap& bm,
329 const SkSurfaceProps* props) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800330 SkASSERT(rect_fits(subset, bm.width(), bm.height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700331
reedbd2bd5c2016-07-25 14:26:02 -0700332 if (!bm.pixelRef()) {
333 return nullptr;
334 }
335
reeda2217ef2016-07-20 06:04:34 -0700336 const SkBitmap* srcBM = &bm;
337 SkBitmap tmpStorage;
338 // ImageFilters only handle N32 at the moment, so force our src to be that
339 if (!valid_for_imagefilters(bm.info())) {
340 if (!bm.copyTo(&tmpStorage, kN32_SkColorType)) {
341 return nullptr;
342 }
343 srcBM = &tmpStorage;
344 }
345 return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props);
robertphillipsb6c65e92016-02-04 10:52:42 -0800346}
347
348#if SK_SUPPORT_GPU
349///////////////////////////////////////////////////////////////////////////////
robertphillipsed086ca2016-04-26 15:02:25 -0700350#include "GrTexture.h"
robertphillipsb4bd11e2016-03-21 13:44:18 -0700351#include "SkImage_Gpu.h"
robertphillipsb6c65e92016-02-04 10:52:42 -0800352
robertphillipsed086ca2016-04-26 15:02:25 -0700353class SkSpecialImage_Gpu : public SkSpecialImage_Base {
354public:
355 SkSpecialImage_Gpu(const SkIRect& subset,
356 uint32_t uniqueID, sk_sp<GrTexture> tex, SkAlphaType at,
brianosmanafbf71d2016-07-21 07:15:37 -0700357 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props)
robertphillipsed086ca2016-04-26 15:02:25 -0700358 : INHERITED(subset, uniqueID, props)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400359 , fContext(tex->getContext())
360 , fAlphaType(at)
361 , fColorSpace(std::move(colorSpace))
362 , fAddedRasterVersionToCache(false) {
Robert Phillips37430132016-11-09 06:50:43 -0500363 fSurfaceProxy = GrSurfaceProxy::MakeWrapped(std::move(tex));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400364 }
365
366 SkSpecialImage_Gpu(GrContext* context, const SkIRect& subset,
Robert Phillips37430132016-11-09 06:50:43 -0500367 uint32_t uniqueID, sk_sp<GrSurfaceProxy> proxy, SkAlphaType at,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400368 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props)
369 : INHERITED(subset, uniqueID, props)
370 , fContext(context)
Robert Phillips37430132016-11-09 06:50:43 -0500371 , fSurfaceProxy(std::move(proxy))
robertphillipsed086ca2016-04-26 15:02:25 -0700372 , fAlphaType(at)
brianosmanafbf71d2016-07-21 07:15:37 -0700373 , fColorSpace(std::move(colorSpace))
robertphillipsed086ca2016-04-26 15:02:25 -0700374 , fAddedRasterVersionToCache(false) {
375 }
376
377 ~SkSpecialImage_Gpu() override {
378 if (fAddedRasterVersionToCache.load()) {
379 SkNotifyBitmapGenIDIsStale(this->uniqueID());
380 }
381 }
382
brianosman80e96082016-08-16 07:09:47 -0700383 SkAlphaType alphaType() const override { return fAlphaType; }
robertphillipsed086ca2016-04-26 15:02:25 -0700384
Robert Phillips37430132016-11-09 06:50:43 -0500385 size_t getSize() const override { return fSurfaceProxy->gpuMemorySize(); }
robertphillipsed086ca2016-04-26 15:02:25 -0700386
387 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
388 SkRect dst = SkRect::MakeXYWH(x, y,
389 this->subset().width(), this->subset().height());
390
Robert Phillips8bc06d02016-11-01 17:28:40 -0400391 // TODO: add GrTextureProxy-backed SkImage_Gpus
Robert Phillips37430132016-11-09 06:50:43 -0500392 GrSurface* surf = fSurfaceProxy->instantiate(fContext->textureProvider());
Robert Phillipse60ad622016-11-17 10:22:48 -0500393 if (!surf) {
394 return;
395 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400396
Robert Phillips7f6cd902016-11-10 17:03:43 -0500397 // TODO: In this instance we know we're going to draw a sub-portion of the backing
398 // texture into the canvas so it is okay to wrap it in an SkImage. This poses
399 // some problems for full deferral however in that when the deferred SkImage_Gpu
400 // instantiates itself it is going to have to either be okay with having a larger
401 // than expected backing texture (unlikely) or the 'fit' of the SurfaceProxy needs
402 // to be tightened (if it is deferred).
403 auto img = sk_sp<SkImage>(new SkImage_Gpu(surf->width(), surf->height(),
Robert Phillips37430132016-11-09 06:50:43 -0500404 this->uniqueID(), fAlphaType,
405 sk_ref_sp(surf->asTexture()),
brianosmanafbf71d2016-07-21 07:15:37 -0700406 fColorSpace, SkBudgeted::kNo));
robertphillipsed086ca2016-04-26 15:02:25 -0700407
reed77d6f7d2016-07-13 12:24:48 -0700408 canvas->drawImageRect(img, this->subset(),
Robert Phillips7f6cd902016-11-10 17:03:43 -0500409 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
robertphillipsed086ca2016-04-26 15:02:25 -0700410 }
411
Robert Phillips8bc06d02016-11-01 17:28:40 -0400412 GrContext* onGetContext() const override { return fContext; }
robertphillipsed086ca2016-04-26 15:02:25 -0700413
Robert Phillips8bc06d02016-11-01 17:28:40 -0400414 // This entry point should go away in favor of asTextureProxy
415 sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override {
Robert Phillips37430132016-11-09 06:50:43 -0500416 GrSurface* surf = fSurfaceProxy->instantiate(context->textureProvider());
Robert Phillipse60ad622016-11-17 10:22:48 -0500417 if (!surf) {
418 return nullptr;
419 }
Robert Phillips37430132016-11-09 06:50:43 -0500420 return sk_ref_sp(surf->asTexture());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400421 }
422
423 sk_sp<GrTextureProxy> onAsTextureProxy(GrContext*) const override {
Robert Phillips37430132016-11-09 06:50:43 -0500424 return sk_ref_sp(fSurfaceProxy->asTextureProxy());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400425 }
robertphillipsed086ca2016-04-26 15:02:25 -0700426
427 bool onGetROPixels(SkBitmap* dst) const override {
428 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
429 SkASSERT(dst->getGenerationID() == this->uniqueID());
430 SkASSERT(dst->isImmutable());
431 SkASSERT(dst->getPixels());
432 return true;
433 }
434
435 SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(),
brianosman80e96082016-08-16 07:09:47 -0700436 this->alphaType(), fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700437
438 if (!dst->tryAllocPixels(info)) {
439 return false;
440 }
441
Robert Phillips8bc06d02016-11-01 17:28:40 -0400442 // Reading back to an SkBitmap ends deferral
Robert Phillips37430132016-11-09 06:50:43 -0500443 GrSurface* surface = fSurfaceProxy->instantiate(fContext->textureProvider());
Robert Phillipse60ad622016-11-17 10:22:48 -0500444 if (!surface) {
445 return false;
446 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400447
Robert Phillips37430132016-11-09 06:50:43 -0500448 if (!surface->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400449 dst->getPixels(), dst->rowBytes())) {
robertphillipsed086ca2016-04-26 15:02:25 -0700450 return false;
451 }
452
453 dst->pixelRef()->setImmutableWithID(this->uniqueID());
454 SkBitmapCache::Add(this->uniqueID(), *dst);
455 fAddedRasterVersionToCache.store(true);
456 return true;
457 }
458
brianosmanafbf71d2016-07-21 07:15:37 -0700459 SkColorSpace* onGetColorSpace() const override {
460 return fColorSpace.get();
461 }
462
brianosmaneed6b0e2016-09-23 13:04:05 -0700463 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
464 const SkISize& size, SkAlphaType at) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400465 if (!fContext) {
robertphillipsed086ca2016-04-26 15:02:25 -0700466 return nullptr;
467 }
468
brianosmaneed6b0e2016-09-23 13:04:05 -0700469 SkColorSpace* colorSpace = outProps.colorSpace();
470 return SkSpecialSurface::MakeRenderTarget(
Robert Phillips8bc06d02016-11-01 17:28:40 -0400471 fContext, size.width(), size.height(),
brianosmaneed6b0e2016-09-23 13:04:05 -0700472 GrRenderableConfigForColorSpace(colorSpace), sk_ref_sp(colorSpace));
robertphillipsed086ca2016-04-26 15:02:25 -0700473 }
474
475 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400476 return SkSpecialImage::MakeDeferredFromGpu(fContext,
477 subset,
478 this->uniqueID(),
Robert Phillips37430132016-11-09 06:50:43 -0500479 fSurfaceProxy,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400480 fColorSpace,
481 &this->props(),
482 fAlphaType);
robertphillipsed086ca2016-04-26 15:02:25 -0700483 }
484
485 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400486 // TODO: add GrTextureProxy-backed SkImage_Gpus
Robert Phillips37430132016-11-09 06:50:43 -0500487 GrSurface* surf = fSurfaceProxy->instantiate(fContext->textureProvider());
Robert Phillipse60ad622016-11-17 10:22:48 -0500488 if (!surf) {
489 return nullptr;
490 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400491
robertphillipsed086ca2016-04-26 15:02:25 -0700492 if (0 == subset.fLeft && 0 == subset.fTop &&
Robert Phillips37430132016-11-09 06:50:43 -0500493 fSurfaceProxy->width() == subset.width() &&
494 fSurfaceProxy->height() == subset.height()) {
robertphillipsed086ca2016-04-26 15:02:25 -0700495 // The existing GrTexture is already tight so reuse it in the SkImage
Robert Phillips37430132016-11-09 06:50:43 -0500496 return sk_make_sp<SkImage_Gpu>(surf->width(), surf->height(),
497 kNeedNewImageUniqueID, fAlphaType,
498 sk_ref_sp(surf->asTexture()),
499 fColorSpace, SkBudgeted::kYes);
robertphillipsed086ca2016-04-26 15:02:25 -0700500 }
501
Robert Phillips37430132016-11-09 06:50:43 -0500502 GrSurfaceDesc desc = fSurfaceProxy->desc();
robertphillipsed086ca2016-04-26 15:02:25 -0700503 desc.fWidth = subset.width();
504 desc.fHeight = subset.height();
505
Robert Phillips8bc06d02016-11-01 17:28:40 -0400506 sk_sp<GrTexture> subTx(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
robertphillipsed086ca2016-04-26 15:02:25 -0700507 if (!subTx) {
508 return nullptr;
509 }
Robert Phillips37430132016-11-09 06:50:43 -0500510 fContext->copySurface(subTx.get(), surf, subset, SkIPoint::Make(0, 0));
robertphillipsed086ca2016-04-26 15:02:25 -0700511 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700512 fAlphaType, std::move(subTx), fColorSpace, SkBudgeted::kYes);
robertphillipsed086ca2016-04-26 15:02:25 -0700513 }
514
brianosmaneed6b0e2016-09-23 13:04:05 -0700515 sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
516 const SkISize& size, SkAlphaType at) const override {
517 SkColorSpace* colorSpace = outProps.colorSpace();
518 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
519 ? kRGBA_F16_SkColorType : kRGBA_8888_SkColorType;
520 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
521 sk_ref_sp(colorSpace));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400522 return SkSurface::MakeRenderTarget(fContext, SkBudgeted::kYes, info);
robertphillipsed086ca2016-04-26 15:02:25 -0700523 }
524
525private:
Robert Phillips8bc06d02016-11-01 17:28:40 -0400526 GrContext* fContext;
Robert Phillips37430132016-11-09 06:50:43 -0500527 sk_sp<GrSurfaceProxy> fSurfaceProxy;
robertphillipsed086ca2016-04-26 15:02:25 -0700528 const SkAlphaType fAlphaType;
brianosmanafbf71d2016-07-21 07:15:37 -0700529 sk_sp<SkColorSpace> fColorSpace;
robertphillipsed086ca2016-04-26 15:02:25 -0700530 mutable SkAtomic<bool> fAddedRasterVersionToCache;
531
532 typedef SkSpecialImage_Base INHERITED;
533};
534
robertphillips3e302272016-04-20 11:48:36 -0700535sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset,
robertphillips37bd7c32016-03-17 14:31:39 -0700536 uint32_t uniqueID,
robertphillipsc91fd342016-04-25 12:32:54 -0700537 sk_sp<GrTexture> tex,
brianosmanafbf71d2016-07-21 07:15:37 -0700538 sk_sp<SkColorSpace> colorSpace,
brianosman898235c2016-04-06 07:38:23 -0700539 const SkSurfaceProps* props,
robertphillips37bd7c32016-03-17 14:31:39 -0700540 SkAlphaType at) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800541 SkASSERT(rect_fits(subset, tex->width(), tex->height()));
brianosmanafbf71d2016-07-21 07:15:37 -0700542 return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at,
543 std::move(colorSpace), props);
robertphillipsb6c65e92016-02-04 10:52:42 -0800544}
545
Robert Phillips8bc06d02016-11-01 17:28:40 -0400546sk_sp<SkSpecialImage> SkSpecialImage::MakeDeferredFromGpu(GrContext* context,
547 const SkIRect& subset,
548 uint32_t uniqueID,
Robert Phillips37430132016-11-09 06:50:43 -0500549 sk_sp<GrSurfaceProxy> proxy,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400550 sk_sp<SkColorSpace> colorSpace,
551 const SkSurfaceProps* props,
552 SkAlphaType at) {
553 SkASSERT(rect_fits(subset, proxy->width(), proxy->height()));
554 return sk_make_sp<SkSpecialImage_Gpu>(context, subset, uniqueID, std::move(proxy), at,
555 std::move(colorSpace), props);
556}
robertphillipsb6c65e92016-02-04 10:52:42 -0800557#endif