blob: f44d011c05144ec3a8b738c40551b2ff6ed514f9 [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 Phillipse2f7d182016-12-15 09:23:05 -050020#include "GrSurfaceContext.h"
reedad7604b2016-07-20 16:13:32 -070021#include "GrTexture.h"
Brian Salomon514baff2016-11-17 15:17:07 -050022#include "GrSamplerParams.h"
Robert Phillips8bc06d02016-11-01 17:28:40 -040023#include "GrTextureProxy.h"
reedad7604b2016-07-20 16:13:32 -070024#include "SkGr.h"
reedad7604b2016-07-20 16:13:32 -070025#include "SkGrPriv.h"
Robert Phillips6de99042017-01-31 11:31:39 -050026#include "SkImage_Gpu.h"
reedad7604b2016-07-20 16:13:32 -070027#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080028
reeda2217ef2016-07-20 06:04:34 -070029// Currently the raster imagefilters can only handle certain imageinfos. Call this to know if
30// a given info is supported.
31static bool valid_for_imagefilters(const SkImageInfo& info) {
32 // no support for other swizzles/depths yet
33 return info.colorType() == kN32_SkColorType;
34}
35
robertphillipsb6c65e92016-02-04 10:52:42 -080036///////////////////////////////////////////////////////////////////////////////
37class SkSpecialImage_Base : public SkSpecialImage {
38public:
robertphillips3e302272016-04-20 11:48:36 -070039 SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps* props)
40 : INHERITED(subset, uniqueID, props) {
robertphillips3b087f42016-02-18 08:48:03 -080041 }
robertphillips3e302272016-04-20 11:48:36 -070042 ~SkSpecialImage_Base() override { }
robertphillipsb6c65e92016-02-04 10:52:42 -080043
robertphillipse8c34972016-02-16 12:09:36 -080044 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080045
robertphillips64612512016-04-08 12:10:42 -070046 virtual bool onGetROPixels(SkBitmap*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080047
Robert Phillips8bc06d02016-11-01 17:28:40 -040048 virtual GrContext* onGetContext() const { return nullptr; }
robertphillipsb6c65e92016-02-04 10:52:42 -080049
brianosmanafbf71d2016-07-21 07:15:37 -070050 virtual SkColorSpace* onGetColorSpace() const = 0;
51
robertphillipsc91fd342016-04-25 12:32:54 -070052#if SK_SUPPORT_GPU
Robert Phillips696b2932017-02-08 12:49:00 +000053 virtual sk_sp<GrTexture> onAsTextureRef(GrContext* context) const = 0;
54 virtual sk_sp<GrTextureProxy> onAsTextureProxy(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
robertphillipsb4bd11e2016-03-21 13:44:18 -070062 virtual sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const = 0;
63
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.
108 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(context, bmp);
109 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 Phillips696b2932017-02-08 12:49:00 +0000150sk_sp<GrTexture> SkSpecialImage::asTextureRef(GrContext* context) const {
151 return as_SIB(this)->onAsTextureRef(context);
152}
153
154sk_sp<GrTextureProxy> SkSpecialImage::asTextureProxy(GrContext* context) const {
155 return as_SIB(this)->onAsTextureProxy(context);
Robert Phillips8bc06d02016-11-01 17:28:40 -0400156}
robertphillipsc91fd342016-04-25 12:32:54 -0700157#endif
robertphillips4418dba2016-03-07 12:45:14 -0800158
brianosmaneed6b0e2016-09-23 13:04:05 -0700159sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageFilter::OutputProperties& outProps,
160 const SkISize& size, SkAlphaType at) const {
161 return as_SIB(this)->onMakeSurface(outProps, size, at);
robertphillipsb6c65e92016-02-04 10:52:42 -0800162}
163
brianosmaneed6b0e2016-09-23 13:04:05 -0700164sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageFilter::OutputProperties& outProps,
165 const SkISize& size, SkAlphaType at) const {
166 return as_SIB(this)->onMakeTightSurface(outProps, size, at);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700167}
168
robertphillips37bd7c32016-03-17 14:31:39 -0700169sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const {
170 return as_SIB(this)->onMakeSubset(subset);
robertphillipsc5035e72016-03-17 06:58:39 -0700171}
172
robertphillipsb4bd11e2016-03-21 13:44:18 -0700173sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const {
174 return as_SIB(this)->onMakeTightSubset(subset);
175}
176
robertphillipsb6c65e92016-02-04 10:52:42 -0800177#ifdef SK_DEBUG
178static bool rect_fits(const SkIRect& rect, int width, int height) {
robertphillips4418dba2016-03-07 12:45:14 -0800179 if (0 == width && 0 == height) {
180 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom);
181 return true;
182 }
183
robertphillipsb6c65e92016-02-04 10:52:42 -0800184 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight &&
185 rect.fRight >= 0 && rect.fRight <= width &&
186 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom &&
187 rect.fBottom >= 0 && rect.fBottom <= height;
188}
189#endif
190
robertphillips3e302272016-04-20 11:48:36 -0700191sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -0700192 sk_sp<SkImage> image,
Brian Osman61624f02016-12-09 14:51:59 -0500193 SkColorSpace* dstColorSpace,
brianosman898235c2016-04-06 07:38:23 -0700194 const SkSurfaceProps* props) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800195 SkASSERT(rect_fits(subset, image->width(), image->height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700196
reedad7604b2016-07-20 16:13:32 -0700197#if SK_SUPPORT_GPU
Robert Phillips6de99042017-01-31 11:31:39 -0500198 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef()) {
199 GrContext* context = ((SkImage_Gpu*) as_IB(image))->context();
200
201 return MakeDeferredFromGpu(context, subset, image->uniqueID(), std::move(proxy),
202 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()), props);
reedad7604b2016-07-20 16:13:32 -0700203 } else
204#endif
205 {
206 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -0500207 if (as_IB(image)->getROPixels(&bm, dstColorSpace)) {
reedad7604b2016-07-20 16:13:32 -0700208 return MakeFromRaster(subset, bm, props);
209 }
reeda2217ef2016-07-20 06:04:34 -0700210 }
reedad7604b2016-07-20 16:13:32 -0700211 return nullptr;
robertphillipsb6c65e92016-02-04 10:52:42 -0800212}
213
214///////////////////////////////////////////////////////////////////////////////
robertphillipsb6c65e92016-02-04 10:52:42 -0800215
216class SkSpecialImage_Raster : public SkSpecialImage_Base {
217public:
robertphillips3e302272016-04-20 11:48:36 -0700218 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props)
219 : INHERITED(subset, bm.getGenerationID(), props)
reedbd2bd5c2016-07-25 14:26:02 -0700220 , fBitmap(bm)
221 {
222 SkASSERT(bm.pixelRef());
223
224 // We have to lock now, while bm is still in scope, since it may have come from our
225 // cache, which means we need to keep it locked until we (the special) are done, since
226 // we cannot re-generate the cache entry (if bm came from a generator).
227 fBitmap.lockPixels();
228 SkASSERT(fBitmap.getPixels());
robertphillipsb6c65e92016-02-04 10:52:42 -0800229 }
230
brianosman80e96082016-08-16 07:09:47 -0700231 SkAlphaType alphaType() const override { return fBitmap.alphaType(); }
robertphillips3b087f42016-02-18 08:48:03 -0800232
233 size_t getSize() const override { return fBitmap.getSize(); }
234
robertphillipse8c34972016-02-16 12:09:36 -0800235 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800236 SkRect dst = SkRect::MakeXYWH(x, y,
237 this->subset().width(), this->subset().height());
238
239 canvas->drawBitmapRect(fBitmap, this->subset(),
240 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
241 }
242
robertphillips64612512016-04-08 12:10:42 -0700243 bool onGetROPixels(SkBitmap* bm) const override {
244 *bm = fBitmap;
robertphillips3b087f42016-02-18 08:48:03 -0800245 return true;
246 }
247
brianosmanafbf71d2016-07-21 07:15:37 -0700248 SkColorSpace* onGetColorSpace() const override {
249 return fBitmap.colorSpace();
250 }
251
robertphillips64612512016-04-08 12:10:42 -0700252#if SK_SUPPORT_GPU
Robert Phillips696b2932017-02-08 12:49:00 +0000253 sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override {
254 if (context) {
255 return sk_ref_sp(GrRefCachedBitmapTexture(context, fBitmap,
256 GrSamplerParams::ClampNoFilter(), nullptr));
257 }
258
259 return nullptr;
260 }
261
262 sk_sp<GrTextureProxy> onAsTextureProxy(GrContext* context) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400263 if (context) {
Robert Phillips37430132016-11-09 06:50:43 -0500264 sk_sp<GrTexture> tex(sk_ref_sp(GrRefCachedBitmapTexture(
Robert Phillips67c18d62017-01-20 12:44:06 -0500265 context, fBitmap, GrSamplerParams::ClampNoFilter(), nullptr)));
Robert Phillips37430132016-11-09 06:50:43 -0500266 sk_sp<GrSurfaceProxy> sProxy = GrSurfaceProxy::MakeWrapped(std::move(tex));
Robert Phillips77b3f322017-01-31 18:24:12 -0500267 if (!sProxy) {
268 return nullptr;
269 }
270
Robert Phillips37430132016-11-09 06:50:43 -0500271 return sk_ref_sp(sProxy->asTextureProxy());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400272 }
273
274 return nullptr;
275 }
robertphillipsc91fd342016-04-25 12:32:54 -0700276#endif
robertphillips64612512016-04-08 12:10:42 -0700277
brianosmaneed6b0e2016-09-23 13:04:05 -0700278// TODO: The raster implementations of image filters all currently assume that the pixels are
279// legacy N32. Until they actually check the format and operate on sRGB or F16 data appropriately,
280// we can't enable this. (They will continue to produce incorrect results, but less-so).
281#define RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 0
282
283 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
284 const SkISize& size, SkAlphaType at) const override {
285#if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16
286 SkColorSpace* colorSpace = outProps.colorSpace();
287#else
288 SkColorSpace* colorSpace = nullptr;
289#endif
290 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
291 ? kRGBA_F16_SkColorType : kN32_SkColorType;
292 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
293 sk_ref_sp(colorSpace));
robertphillips3e302272016-04-20 11:48:36 -0700294 return SkSpecialSurface::MakeRaster(info, nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -0800295 }
296
robertphillips37bd7c32016-03-17 14:31:39 -0700297 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
robertphillipsc5035e72016-03-17 06:58:39 -0700298 SkBitmap subsetBM;
halcanary9d524f22016-03-29 09:03:52 -0700299
robertphillipsc5035e72016-03-17 06:58:39 -0700300 if (!fBitmap.extractSubset(&subsetBM, subset)) {
301 return nullptr;
302 }
303
robertphillips3e302272016-04-20 11:48:36 -0700304 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()),
brianosman898235c2016-04-06 07:38:23 -0700305 subsetBM,
306 &this->props());
robertphillipsc5035e72016-03-17 06:58:39 -0700307 }
308
robertphillipsb4bd11e2016-03-21 13:44:18 -0700309 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override {
310 SkBitmap subsetBM;
311
312 if (!fBitmap.extractSubset(&subsetBM, subset)) {
313 return nullptr;
314 }
315
316 return SkImage::MakeFromBitmap(subsetBM);
317 }
318
brianosmaneed6b0e2016-09-23 13:04:05 -0700319 sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
320 const SkISize& size, SkAlphaType at) const override {
321#if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16
322 SkColorSpace* colorSpace = outProps.colorSpace();
323#else
324 SkColorSpace* colorSpace = nullptr;
325#endif
326 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
327 ? kRGBA_F16_SkColorType : kN32_SkColorType;
328 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
329 sk_ref_sp(colorSpace));
reede8f30622016-03-23 18:59:25 -0700330 return SkSurface::MakeRaster(info);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700331 }
332
robertphillipsb6c65e92016-02-04 10:52:42 -0800333private:
334 SkBitmap fBitmap;
335
336 typedef SkSpecialImage_Base INHERITED;
337};
338
robertphillips3e302272016-04-20 11:48:36 -0700339sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -0700340 const SkBitmap& bm,
341 const SkSurfaceProps* props) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800342 SkASSERT(rect_fits(subset, bm.width(), bm.height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700343
reedbd2bd5c2016-07-25 14:26:02 -0700344 if (!bm.pixelRef()) {
345 return nullptr;
346 }
347
reeda2217ef2016-07-20 06:04:34 -0700348 const SkBitmap* srcBM = &bm;
349 SkBitmap tmpStorage;
350 // ImageFilters only handle N32 at the moment, so force our src to be that
351 if (!valid_for_imagefilters(bm.info())) {
352 if (!bm.copyTo(&tmpStorage, kN32_SkColorType)) {
353 return nullptr;
354 }
355 srcBM = &tmpStorage;
356 }
357 return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props);
robertphillipsb6c65e92016-02-04 10:52:42 -0800358}
359
360#if SK_SUPPORT_GPU
361///////////////////////////////////////////////////////////////////////////////
robertphillipsed086ca2016-04-26 15:02:25 -0700362#include "GrTexture.h"
robertphillipsb6c65e92016-02-04 10:52:42 -0800363
Robert Phillipse2f7d182016-12-15 09:23:05 -0500364static sk_sp<SkImage> wrap_proxy_in_image(GrContext* context, GrSurfaceProxy* proxy,
365 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
366 // TODO: add GrTextureProxy-backed SkImage_Gpus
367 GrSurface* surf = proxy->instantiate(context->textureProvider());
368 if (!surf) {
369 return nullptr;
370 }
371
372 return sk_make_sp<SkImage_Gpu>(proxy->width(), proxy->height(),
373 kNeedNewImageUniqueID, alphaType,
374 sk_ref_sp(surf->asTexture()),
375 std::move(colorSpace), SkBudgeted::kYes);
376}
377
robertphillipsed086ca2016-04-26 15:02:25 -0700378class SkSpecialImage_Gpu : public SkSpecialImage_Base {
379public:
380 SkSpecialImage_Gpu(const SkIRect& subset,
381 uint32_t uniqueID, sk_sp<GrTexture> tex, SkAlphaType at,
brianosmanafbf71d2016-07-21 07:15:37 -0700382 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props)
robertphillipsed086ca2016-04-26 15:02:25 -0700383 : INHERITED(subset, uniqueID, props)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400384 , fContext(tex->getContext())
385 , fAlphaType(at)
386 , fColorSpace(std::move(colorSpace))
387 , fAddedRasterVersionToCache(false) {
Robert Phillips37430132016-11-09 06:50:43 -0500388 fSurfaceProxy = GrSurfaceProxy::MakeWrapped(std::move(tex));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400389 }
390
391 SkSpecialImage_Gpu(GrContext* context, const SkIRect& subset,
Robert Phillips37430132016-11-09 06:50:43 -0500392 uint32_t uniqueID, sk_sp<GrSurfaceProxy> proxy, SkAlphaType at,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400393 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props)
394 : INHERITED(subset, uniqueID, props)
395 , fContext(context)
Robert Phillips37430132016-11-09 06:50:43 -0500396 , fSurfaceProxy(std::move(proxy))
robertphillipsed086ca2016-04-26 15:02:25 -0700397 , fAlphaType(at)
brianosmanafbf71d2016-07-21 07:15:37 -0700398 , fColorSpace(std::move(colorSpace))
robertphillipsed086ca2016-04-26 15:02:25 -0700399 , fAddedRasterVersionToCache(false) {
400 }
401
402 ~SkSpecialImage_Gpu() override {
403 if (fAddedRasterVersionToCache.load()) {
404 SkNotifyBitmapGenIDIsStale(this->uniqueID());
405 }
406 }
407
brianosman80e96082016-08-16 07:09:47 -0700408 SkAlphaType alphaType() const override { return fAlphaType; }
robertphillipsed086ca2016-04-26 15:02:25 -0700409
Robert Phillips37430132016-11-09 06:50:43 -0500410 size_t getSize() const override { return fSurfaceProxy->gpuMemorySize(); }
robertphillipsed086ca2016-04-26 15:02:25 -0700411
412 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
413 SkRect dst = SkRect::MakeXYWH(x, y,
414 this->subset().width(), this->subset().height());
415
Robert Phillips8bc06d02016-11-01 17:28:40 -0400416 // TODO: add GrTextureProxy-backed SkImage_Gpus
Robert Phillips37430132016-11-09 06:50:43 -0500417 GrSurface* surf = fSurfaceProxy->instantiate(fContext->textureProvider());
Robert Phillipse60ad622016-11-17 10:22:48 -0500418 if (!surf) {
419 return;
420 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400421
Robert Phillips7f6cd902016-11-10 17:03:43 -0500422 // TODO: In this instance we know we're going to draw a sub-portion of the backing
423 // texture into the canvas so it is okay to wrap it in an SkImage. This poses
424 // some problems for full deferral however in that when the deferred SkImage_Gpu
425 // instantiates itself it is going to have to either be okay with having a larger
426 // than expected backing texture (unlikely) or the 'fit' of the SurfaceProxy needs
427 // to be tightened (if it is deferred).
428 auto img = sk_sp<SkImage>(new SkImage_Gpu(surf->width(), surf->height(),
Robert Phillips37430132016-11-09 06:50:43 -0500429 this->uniqueID(), fAlphaType,
430 sk_ref_sp(surf->asTexture()),
brianosmanafbf71d2016-07-21 07:15:37 -0700431 fColorSpace, SkBudgeted::kNo));
robertphillipsed086ca2016-04-26 15:02:25 -0700432
reed77d6f7d2016-07-13 12:24:48 -0700433 canvas->drawImageRect(img, this->subset(),
Robert Phillips7f6cd902016-11-10 17:03:43 -0500434 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
robertphillipsed086ca2016-04-26 15:02:25 -0700435 }
436
Robert Phillips8bc06d02016-11-01 17:28:40 -0400437 GrContext* onGetContext() const override { return fContext; }
robertphillipsed086ca2016-04-26 15:02:25 -0700438
Robert Phillips696b2932017-02-08 12:49:00 +0000439 // This entry point should go away in favor of asTextureProxy
440 sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override {
441 GrSurface* surf = fSurfaceProxy->instantiate(context->textureProvider());
442 if (!surf) {
443 return nullptr;
444 }
445 return sk_ref_sp(surf->asTexture());
446 }
447
448 sk_sp<GrTextureProxy> onAsTextureProxy(GrContext*) const override {
Robert Phillips37430132016-11-09 06:50:43 -0500449 return sk_ref_sp(fSurfaceProxy->asTextureProxy());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400450 }
robertphillipsed086ca2016-04-26 15:02:25 -0700451
452 bool onGetROPixels(SkBitmap* dst) const override {
453 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
454 SkASSERT(dst->getGenerationID() == this->uniqueID());
455 SkASSERT(dst->isImmutable());
456 SkASSERT(dst->getPixels());
457 return true;
458 }
459
460 SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(),
brianosman80e96082016-08-16 07:09:47 -0700461 this->alphaType(), fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700462
463 if (!dst->tryAllocPixels(info)) {
464 return false;
465 }
466
Robert Phillips8bc06d02016-11-01 17:28:40 -0400467 // Reading back to an SkBitmap ends deferral
Robert Phillips37430132016-11-09 06:50:43 -0500468 GrSurface* surface = fSurfaceProxy->instantiate(fContext->textureProvider());
Robert Phillipse60ad622016-11-17 10:22:48 -0500469 if (!surface) {
470 return false;
471 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400472
Robert Phillips37430132016-11-09 06:50:43 -0500473 if (!surface->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400474 dst->getPixels(), dst->rowBytes())) {
robertphillipsed086ca2016-04-26 15:02:25 -0700475 return false;
476 }
477
478 dst->pixelRef()->setImmutableWithID(this->uniqueID());
479 SkBitmapCache::Add(this->uniqueID(), *dst);
480 fAddedRasterVersionToCache.store(true);
481 return true;
482 }
483
brianosmanafbf71d2016-07-21 07:15:37 -0700484 SkColorSpace* onGetColorSpace() const override {
485 return fColorSpace.get();
486 }
487
brianosmaneed6b0e2016-09-23 13:04:05 -0700488 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
489 const SkISize& size, SkAlphaType at) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400490 if (!fContext) {
robertphillipsed086ca2016-04-26 15:02:25 -0700491 return nullptr;
492 }
493
brianosmaneed6b0e2016-09-23 13:04:05 -0700494 SkColorSpace* colorSpace = outProps.colorSpace();
495 return SkSpecialSurface::MakeRenderTarget(
Robert Phillips8bc06d02016-11-01 17:28:40 -0400496 fContext, size.width(), size.height(),
brianosmaneed6b0e2016-09-23 13:04:05 -0700497 GrRenderableConfigForColorSpace(colorSpace), sk_ref_sp(colorSpace));
robertphillipsed086ca2016-04-26 15:02:25 -0700498 }
499
500 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400501 return SkSpecialImage::MakeDeferredFromGpu(fContext,
502 subset,
503 this->uniqueID(),
Robert Phillips37430132016-11-09 06:50:43 -0500504 fSurfaceProxy,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400505 fColorSpace,
506 &this->props(),
507 fAlphaType);
robertphillipsed086ca2016-04-26 15:02:25 -0700508 }
509
Robert Phillipse2f7d182016-12-15 09:23:05 -0500510 // TODO: move all the logic here into the subset-flavor GrSurfaceProxy::copy?
robertphillipsed086ca2016-04-26 15:02:25 -0700511 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500512 // TODO: this is problematic since the surfaceProxy could be loose
robertphillipsed086ca2016-04-26 15:02:25 -0700513 if (0 == subset.fLeft && 0 == subset.fTop &&
Robert Phillips37430132016-11-09 06:50:43 -0500514 fSurfaceProxy->width() == subset.width() &&
515 fSurfaceProxy->height() == subset.height()) {
robertphillipsed086ca2016-04-26 15:02:25 -0700516 // The existing GrTexture is already tight so reuse it in the SkImage
Robert Phillipse2f7d182016-12-15 09:23:05 -0500517 return wrap_proxy_in_image(fContext, fSurfaceProxy.get(),
518 fAlphaType, fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700519 }
520
Robert Phillipse2f7d182016-12-15 09:23:05 -0500521 sk_sp<GrSurfaceProxy> subsetProxy(GrSurfaceProxy::Copy(fContext, fSurfaceProxy.get(),
522 subset, SkBudgeted::kYes));
robertphillipsed086ca2016-04-26 15:02:25 -0700523
Robert Phillipse2f7d182016-12-15 09:23:05 -0500524 return wrap_proxy_in_image(fContext, subsetProxy.get(), fAlphaType, fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700525 }
526
brianosmaneed6b0e2016-09-23 13:04:05 -0700527 sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
528 const SkISize& size, SkAlphaType at) const override {
529 SkColorSpace* colorSpace = outProps.colorSpace();
530 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
531 ? kRGBA_F16_SkColorType : kRGBA_8888_SkColorType;
532 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
533 sk_ref_sp(colorSpace));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400534 return SkSurface::MakeRenderTarget(fContext, SkBudgeted::kYes, info);
robertphillipsed086ca2016-04-26 15:02:25 -0700535 }
536
537private:
Robert Phillips8bc06d02016-11-01 17:28:40 -0400538 GrContext* fContext;
Robert Phillips37430132016-11-09 06:50:43 -0500539 sk_sp<GrSurfaceProxy> fSurfaceProxy;
robertphillipsed086ca2016-04-26 15:02:25 -0700540 const SkAlphaType fAlphaType;
brianosmanafbf71d2016-07-21 07:15:37 -0700541 sk_sp<SkColorSpace> fColorSpace;
robertphillipsed086ca2016-04-26 15:02:25 -0700542 mutable SkAtomic<bool> fAddedRasterVersionToCache;
543
544 typedef SkSpecialImage_Base INHERITED;
545};
546
robertphillips3e302272016-04-20 11:48:36 -0700547sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset,
robertphillips37bd7c32016-03-17 14:31:39 -0700548 uint32_t uniqueID,
robertphillipsc91fd342016-04-25 12:32:54 -0700549 sk_sp<GrTexture> tex,
brianosmanafbf71d2016-07-21 07:15:37 -0700550 sk_sp<SkColorSpace> colorSpace,
brianosman898235c2016-04-06 07:38:23 -0700551 const SkSurfaceProps* props,
robertphillips37bd7c32016-03-17 14:31:39 -0700552 SkAlphaType at) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800553 SkASSERT(rect_fits(subset, tex->width(), tex->height()));
brianosmanafbf71d2016-07-21 07:15:37 -0700554 return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at,
555 std::move(colorSpace), props);
robertphillipsb6c65e92016-02-04 10:52:42 -0800556}
557
Robert Phillips8bc06d02016-11-01 17:28:40 -0400558sk_sp<SkSpecialImage> SkSpecialImage::MakeDeferredFromGpu(GrContext* context,
559 const SkIRect& subset,
560 uint32_t uniqueID,
Robert Phillips37430132016-11-09 06:50:43 -0500561 sk_sp<GrSurfaceProxy> proxy,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400562 sk_sp<SkColorSpace> colorSpace,
563 const SkSurfaceProps* props,
564 SkAlphaType at) {
565 SkASSERT(rect_fits(subset, proxy->width(), proxy->height()));
566 return sk_make_sp<SkSpecialImage_Gpu>(context, subset, uniqueID, std::move(proxy), at,
567 std::move(colorSpace), props);
568}
robertphillipsb6c65e92016-02-04 10:52:42 -0800569#endif