blob: 03223a2694e8cd91ebafafdb8c5f968491c48246 [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 */
robertphillips83c17fa2016-03-18 08:14:27 -07007#include "SkSpecialImage.h"
8
9#if SK_SUPPORT_GPU
10#include "GrTexture.h"
11#include "GrTextureParams.h"
12#include "SkGr.h"
13#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080014
15#include "SkCanvas.h"
bsalomon84a4e5a2016-02-29 11:41:52 -080016#include "SkImage_Base.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080017#include "SkSpecialSurface.h"
18
19///////////////////////////////////////////////////////////////////////////////
20class SkSpecialImage_Base : public SkSpecialImage {
21public:
robertphillips3b087f42016-02-18 08:48:03 -080022 SkSpecialImage_Base(SkImageFilter::Proxy* proxy, const SkIRect& subset, uint32_t uniqueID)
23 : INHERITED(proxy, subset, uniqueID) {
24 }
robertphillipsb6c65e92016-02-04 10:52:42 -080025 virtual ~SkSpecialImage_Base() { }
26
robertphillipse8c34972016-02-16 12:09:36 -080027 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080028
robertphillipsc5035e72016-03-17 06:58:39 -070029 virtual bool onPeekPixels(SkPixmap*) const { return false; }
robertphillipsb6c65e92016-02-04 10:52:42 -080030
31 virtual GrTexture* onPeekTexture() const { return nullptr; }
32
robertphillips4418dba2016-03-07 12:45:14 -080033 virtual bool testingOnlyOnGetROPixels(SkBitmap*) const = 0;
34
robertphillips3b087f42016-02-18 08:48:03 -080035 // Delete this entry point ASAP (see skbug.com/4965)
robertphillipsab01ccd2016-03-08 10:45:32 -080036 virtual bool getBitmapDeprecated(SkBitmap* result) const = 0;
robertphillips3b087f42016-02-18 08:48:03 -080037
robertphillipsb4bd11e2016-03-21 13:44:18 -070038 virtual sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const = 0;
39
robertphillips37bd7c32016-03-17 14:31:39 -070040 virtual sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const = 0;
robertphillipsc5035e72016-03-17 06:58:39 -070041
robertphillipsb4bd11e2016-03-21 13:44:18 -070042 virtual sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const = 0;
43
44 virtual SkSurface* onMakeTightSurface(const SkImageInfo& info) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080045
46private:
47 typedef SkSpecialImage INHERITED;
48};
49
50///////////////////////////////////////////////////////////////////////////////
bsalomon84a4e5a2016-02-29 11:41:52 -080051static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) {
robertphillipsb6c65e92016-02-04 10:52:42 -080052 return static_cast<const SkSpecialImage_Base*>(image);
53}
54
robertphillips83c17fa2016-03-18 08:14:27 -070055sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(SkImageFilter::Proxy* proxy,
56 GrContext* context) {
57#if SK_SUPPORT_GPU
58 if (!context) {
59 return nullptr;
60 }
61 if (GrTexture* peek = as_SIB(this)->peekTexture()) {
62 return peek->getContext() == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr;
63 }
64
65 SkBitmap bmp;
66 if (!this->internal_getBM(&bmp)) {
67 return nullptr;
68 }
69
70 SkAutoTUnref<GrTexture> resultTex(
71 GrRefCachedBitmapTexture(context, bmp, GrTextureParams::ClampNoFilter()));
72 if (!resultTex) {
73 return nullptr;
74 }
75
76 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
77
78 return SkSpecialImage::MakeFromGpu(proxy,
79 SkIRect::MakeWH(resultTex->width(), resultTex->height()),
80 this->uniqueID(),
81 resultTex, at);
82#else
83 return nullptr;
84#endif
85}
86
robertphillipse8c34972016-02-16 12:09:36 -080087void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
bsalomon84a4e5a2016-02-29 11:41:52 -080088 return as_SIB(this)->onDraw(canvas, x, y, paint);
robertphillipsb6c65e92016-02-04 10:52:42 -080089}
90
robertphillipsc5035e72016-03-17 06:58:39 -070091bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const {
92 return as_SIB(this)->onPeekPixels(pixmap);
robertphillipsb6c65e92016-02-04 10:52:42 -080093}
94
95GrTexture* SkSpecialImage::peekTexture() const {
bsalomon84a4e5a2016-02-29 11:41:52 -080096 return as_SIB(this)->onPeekTexture();
robertphillipsb6c65e92016-02-04 10:52:42 -080097}
98
robertphillips4418dba2016-03-07 12:45:14 -080099bool SkSpecialImage::testingOnlyGetROPixels(SkBitmap* result) const {
100 return as_SIB(this)->testingOnlyOnGetROPixels(result);
101}
102
robertphillips37bd7c32016-03-17 14:31:39 -0700103sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageInfo& info) const {
104 return as_SIB(this)->onMakeSurface(info);
robertphillipsb6c65e92016-02-04 10:52:42 -0800105}
106
robertphillipsb4bd11e2016-03-21 13:44:18 -0700107sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageInfo& info) const {
108 sk_sp<SkSurface> tmp(SkRef(as_SIB(this)->onMakeTightSurface(info)));
109 return tmp;
110}
111
robertphillips37bd7c32016-03-17 14:31:39 -0700112sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const {
113 return as_SIB(this)->onMakeSubset(subset);
robertphillipsc5035e72016-03-17 06:58:39 -0700114}
115
robertphillipsb4bd11e2016-03-21 13:44:18 -0700116sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const {
117 return as_SIB(this)->onMakeTightSubset(subset);
118}
119
robertphillips3b087f42016-02-18 08:48:03 -0800120#if SK_SUPPORT_GPU
121#include "SkGr.h"
122#include "SkGrPixelRef.h"
123#endif
124
robertphillips37bd7c32016-03-17 14:31:39 -0700125sk_sp<SkSpecialImage> SkSpecialImage::internal_fromBM(SkImageFilter::Proxy* proxy,
126 const SkBitmap& src) {
robertphillips3b087f42016-02-18 08:48:03 -0800127 // Need to test offset case! (see skbug.com/4967)
128 if (src.getTexture()) {
robertphillips37bd7c32016-03-17 14:31:39 -0700129 return SkSpecialImage::MakeFromGpu(proxy,
130 src.bounds(),
131 src.getGenerationID(),
132 src.getTexture());
robertphillips3b087f42016-02-18 08:48:03 -0800133 }
134
robertphillips37bd7c32016-03-17 14:31:39 -0700135 return SkSpecialImage::MakeFromRaster(proxy, src.bounds(), src);
robertphillips3b087f42016-02-18 08:48:03 -0800136}
137
138bool SkSpecialImage::internal_getBM(SkBitmap* result) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800139 const SkSpecialImage_Base* ib = as_SIB(this);
robertphillips3b087f42016-02-18 08:48:03 -0800140
141 // TODO: need to test offset case! (see skbug.com/4967)
robertphillipsab01ccd2016-03-08 10:45:32 -0800142 return ib->getBitmapDeprecated(result);
robertphillips3b087f42016-02-18 08:48:03 -0800143}
144
robertphillipsc5035e72016-03-17 06:58:39 -0700145SkImageFilter::Proxy* SkSpecialImage::internal_getProxy() const {
robertphillips3b087f42016-02-18 08:48:03 -0800146 return fProxy;
147}
148
robertphillipsb6c65e92016-02-04 10:52:42 -0800149///////////////////////////////////////////////////////////////////////////////
150#include "SkImage.h"
151#if SK_SUPPORT_GPU
brianosmana6359362016-03-21 06:55:37 -0700152#include "GrContext.h"
robertphillipsb6c65e92016-02-04 10:52:42 -0800153#include "SkGrPriv.h"
154#endif
155
156class SkSpecialImage_Image : public SkSpecialImage_Base {
157public:
robertphillips37bd7c32016-03-17 14:31:39 -0700158 SkSpecialImage_Image(SkImageFilter::Proxy* proxy,
159 const SkIRect& subset,
160 sk_sp<SkImage> image)
robertphillips3b087f42016-02-18 08:48:03 -0800161 : INHERITED(proxy, subset, image->uniqueID())
robertphillips37bd7c32016-03-17 14:31:39 -0700162 , fImage(image) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800163 }
164
165 ~SkSpecialImage_Image() override { }
robertphillips37bd7c32016-03-17 14:31:39 -0700166
robertphillips3b087f42016-02-18 08:48:03 -0800167 bool isOpaque() const override { return fImage->isOpaque(); }
168
169 size_t getSize() const override {
170#if SK_SUPPORT_GPU
bsalomon84a4e5a2016-02-29 11:41:52 -0800171 if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) {
172 return texture->gpuMemorySize();
robertphillips3b087f42016-02-18 08:48:03 -0800173 } else
174#endif
175 {
reed6ceeebd2016-03-09 14:26:26 -0800176 SkPixmap pm;
177 if (fImage->peekPixels(&pm)) {
178 return pm.height() * pm.rowBytes();
robertphillips3b087f42016-02-18 08:48:03 -0800179 }
180 }
181 return 0;
182 }
183
robertphillipse8c34972016-02-16 12:09:36 -0800184 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800185 SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset().height());
186
robertphillips37bd7c32016-03-17 14:31:39 -0700187 canvas->drawImageRect(fImage.get(), this->subset(),
robertphillipsb6c65e92016-02-04 10:52:42 -0800188 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
189 }
190
robertphillipsc5035e72016-03-17 06:58:39 -0700191 bool onPeekPixels(SkPixmap* pixmap) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800192 return fImage->peekPixels(pixmap);
193 }
194
bsalomon84a4e5a2016-02-29 11:41:52 -0800195 GrTexture* onPeekTexture() const override { return as_IB(fImage.get())->peekTexture(); }
robertphillipsb6c65e92016-02-04 10:52:42 -0800196
robertphillipsab01ccd2016-03-08 10:45:32 -0800197 bool getBitmapDeprecated(SkBitmap* result) const override {
robertphillips6ac97b72016-03-09 05:17:10 -0800198#if SK_SUPPORT_GPU
199 if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) {
200 const SkImageInfo info = GrMakeInfoFromTexture(texture,
201 fImage->width(), fImage->height(),
202 fImage->isOpaque());
203 if (!result->setInfo(info)) {
204 return false;
205 }
206
207 result->setPixelRef(new SkGrPixelRef(info, texture))->unref();
208 return true;
209 }
210#endif
211
212 return as_IB(fImage.get())->asBitmapForImageFilters(result);
robertphillips3b087f42016-02-18 08:48:03 -0800213 }
214
robertphillips4418dba2016-03-07 12:45:14 -0800215 bool testingOnlyOnGetROPixels(SkBitmap* result) const override {
robertphillips6ac97b72016-03-09 05:17:10 -0800216 return fImage->asLegacyBitmap(result, SkImage::kRO_LegacyBitmapMode);
robertphillips4418dba2016-03-07 12:45:14 -0800217 }
218
robertphillips37bd7c32016-03-17 14:31:39 -0700219 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800220#if SK_SUPPORT_GPU
bsalomon84a4e5a2016-02-29 11:41:52 -0800221 GrTexture* texture = as_IB(fImage.get())->peekTexture();
robertphillipsb6c65e92016-02-04 10:52:42 -0800222 if (texture) {
brianosmana6359362016-03-21 06:55:37 -0700223 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info, *texture->getContext()->caps());
robertphillipsb6c65e92016-02-04 10:52:42 -0800224 desc.fFlags = kRenderTarget_GrSurfaceFlag;
225
robertphillips37bd7c32016-03-17 14:31:39 -0700226 return SkSpecialSurface::MakeRenderTarget(this->proxy(), texture->getContext(), desc);
robertphillipsb6c65e92016-02-04 10:52:42 -0800227 }
228#endif
robertphillips37bd7c32016-03-17 14:31:39 -0700229 return SkSpecialSurface::MakeRaster(this->proxy(), info, nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -0800230 }
231
robertphillips37bd7c32016-03-17 14:31:39 -0700232 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
233 sk_sp<SkImage> subsetImg(fImage->makeSubset(subset));
robertphillipsc5035e72016-03-17 06:58:39 -0700234 if (!subsetImg) {
235 return nullptr;
236 }
237
robertphillips37bd7c32016-03-17 14:31:39 -0700238 return SkSpecialImage::MakeFromImage(this->internal_getProxy(),
239 SkIRect::MakeWH(subset.width(), subset.height()),
240 subsetImg);
robertphillipsc5035e72016-03-17 06:58:39 -0700241 }
242
robertphillipsb4bd11e2016-03-21 13:44:18 -0700243 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override {
244 return fImage->makeSubset(subset);
245 }
246
247 SkSurface* onMakeTightSurface(const SkImageInfo& info) const override {
248#if SK_SUPPORT_GPU
249 GrTexture* texture = as_IB(fImage.get())->peekTexture();
250 if (texture) {
251 return SkSurface::NewRenderTarget(texture->getContext(), SkBudgeted::kYes, info, 0);
252 }
253#endif
254 return SkSurface::NewRaster(info, nullptr);
255 }
256
robertphillipsb6c65e92016-02-04 10:52:42 -0800257private:
robertphillips37bd7c32016-03-17 14:31:39 -0700258 sk_sp<SkImage> fImage;
robertphillipsb6c65e92016-02-04 10:52:42 -0800259
260 typedef SkSpecialImage_Base INHERITED;
261};
262
263#ifdef SK_DEBUG
264static bool rect_fits(const SkIRect& rect, int width, int height) {
robertphillips4418dba2016-03-07 12:45:14 -0800265 if (0 == width && 0 == height) {
266 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom);
267 return true;
268 }
269
robertphillipsb6c65e92016-02-04 10:52:42 -0800270 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight &&
271 rect.fRight >= 0 && rect.fRight <= width &&
272 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom &&
273 rect.fBottom >= 0 && rect.fBottom <= height;
274}
275#endif
276
robertphillips37bd7c32016-03-17 14:31:39 -0700277sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(SkImageFilter::Proxy* proxy,
278 const SkIRect& subset,
279 sk_sp<SkImage> image) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800280 SkASSERT(rect_fits(subset, image->width(), image->height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700281
282 return sk_make_sp<SkSpecialImage_Image>(proxy, subset, image);
robertphillipsb6c65e92016-02-04 10:52:42 -0800283}
284
285///////////////////////////////////////////////////////////////////////////////
286#include "SkBitmap.h"
287#include "SkImageInfo.h"
288#include "SkPixelRef.h"
289
290class SkSpecialImage_Raster : public SkSpecialImage_Base {
291public:
robertphillips3b087f42016-02-18 08:48:03 -0800292 SkSpecialImage_Raster(SkImageFilter::Proxy* proxy, const SkIRect& subset, const SkBitmap& bm)
293 : INHERITED(proxy, subset, bm.getGenerationID())
robertphillipsb6c65e92016-02-04 10:52:42 -0800294 , fBitmap(bm) {
robertphillips4418dba2016-03-07 12:45:14 -0800295 if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800296 // we only preemptively lock if there is no chance of triggering something expensive
297 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already.
298 fBitmap.lockPixels();
299 }
300 }
301
robertphillipsc5035e72016-03-17 06:58:39 -0700302 SkSpecialImage_Raster(SkImageFilter::Proxy* proxy,
303 const SkIRect& subset,
304 const SkPixmap& pixmap,
robertphillips37bd7c32016-03-17 14:31:39 -0700305 RasterReleaseProc releaseProc,
306 ReleaseContext context)
robertphillipsc5035e72016-03-17 06:58:39 -0700307 : INHERITED(proxy, subset, kNeedNewImageUniqueID_SpecialImage) {
308 fBitmap.installPixels(pixmap.info(), pixmap.writable_addr(),
309 pixmap.rowBytes(), pixmap.ctable(),
310 releaseProc, context);
311 }
312
robertphillipsb6c65e92016-02-04 10:52:42 -0800313 ~SkSpecialImage_Raster() override { }
314
robertphillips3b087f42016-02-18 08:48:03 -0800315 bool isOpaque() const override { return fBitmap.isOpaque(); }
316
317 size_t getSize() const override { return fBitmap.getSize(); }
318
robertphillipse8c34972016-02-16 12:09:36 -0800319 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800320 SkRect dst = SkRect::MakeXYWH(x, y,
321 this->subset().width(), this->subset().height());
322
323 canvas->drawBitmapRect(fBitmap, this->subset(),
324 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
325 }
326
robertphillipsc5035e72016-03-17 06:58:39 -0700327 bool onPeekPixels(SkPixmap* pixmap) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800328 const SkImageInfo info = fBitmap.info();
329 if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) {
330 return false;
331 }
robertphillipsc5035e72016-03-17 06:58:39 -0700332
333 return fBitmap.peekPixels(pixmap);
robertphillipsb6c65e92016-02-04 10:52:42 -0800334 }
335
robertphillipsab01ccd2016-03-08 10:45:32 -0800336 bool getBitmapDeprecated(SkBitmap* result) const override {
robertphillips3b087f42016-02-18 08:48:03 -0800337 *result = fBitmap;
338 return true;
339 }
340
robertphillips4418dba2016-03-07 12:45:14 -0800341 bool testingOnlyOnGetROPixels(SkBitmap* result) const override {
342 *result = fBitmap;
343 return true;
344 }
345
robertphillips37bd7c32016-03-17 14:31:39 -0700346 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const override {
347 return SkSpecialSurface::MakeRaster(this->proxy(), info, nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -0800348 }
349
robertphillips37bd7c32016-03-17 14:31:39 -0700350 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
robertphillipsc5035e72016-03-17 06:58:39 -0700351 SkBitmap subsetBM;
352
353 if (!fBitmap.extractSubset(&subsetBM, subset)) {
354 return nullptr;
355 }
356
robertphillips37bd7c32016-03-17 14:31:39 -0700357 return SkSpecialImage::MakeFromRaster(this->internal_getProxy(),
358 SkIRect::MakeWH(subset.width(), subset.height()),
359 subsetBM);
robertphillipsc5035e72016-03-17 06:58:39 -0700360 }
361
robertphillipsb4bd11e2016-03-21 13:44:18 -0700362 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override {
363 SkBitmap subsetBM;
364
365 if (!fBitmap.extractSubset(&subsetBM, subset)) {
366 return nullptr;
367 }
368
369 return SkImage::MakeFromBitmap(subsetBM);
370 }
371
372 SkSurface* onMakeTightSurface(const SkImageInfo& info) const override {
373 return SkSurface::NewRaster(info);
374 }
375
robertphillipsb6c65e92016-02-04 10:52:42 -0800376private:
377 SkBitmap fBitmap;
378
379 typedef SkSpecialImage_Base INHERITED;
380};
381
robertphillips37bd7c32016-03-17 14:31:39 -0700382sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(SkImageFilter::Proxy* proxy,
383 const SkIRect& subset,
384 const SkBitmap& bm) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800385 SkASSERT(nullptr == bm.getTexture());
386 SkASSERT(rect_fits(subset, bm.width(), bm.height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700387
388 return sk_make_sp<SkSpecialImage_Raster>(proxy, subset, bm);
robertphillipsb6c65e92016-02-04 10:52:42 -0800389}
390
robertphillips37bd7c32016-03-17 14:31:39 -0700391sk_sp<SkSpecialImage> SkSpecialImage::MakeFromPixmap(SkImageFilter::Proxy* proxy,
392 const SkIRect& subset,
393 const SkPixmap& src,
394 RasterReleaseProc releaseProc,
395 ReleaseContext context) {
396 if (!src.addr()) {
397 return nullptr;
398 }
399
400 return sk_make_sp<SkSpecialImage_Raster>(proxy, subset, src, releaseProc, context);
robertphillipsc5035e72016-03-17 06:58:39 -0700401}
402
403
robertphillipsb6c65e92016-02-04 10:52:42 -0800404#if SK_SUPPORT_GPU
405///////////////////////////////////////////////////////////////////////////////
406#include "GrTexture.h"
robertphillipsb4bd11e2016-03-21 13:44:18 -0700407#include "SkImage_Gpu.h"
robertphillipsb6c65e92016-02-04 10:52:42 -0800408
409class SkSpecialImage_Gpu : public SkSpecialImage_Base {
robertphillips3b087f42016-02-18 08:48:03 -0800410public:
411 SkSpecialImage_Gpu(SkImageFilter::Proxy* proxy, const SkIRect& subset,
412 uint32_t uniqueID, GrTexture* tex, SkAlphaType at)
413 : INHERITED(proxy, subset, uniqueID)
414 , fTexture(SkRef(tex))
415 , fAlphaType(at) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800416 }
417
418 ~SkSpecialImage_Gpu() override { }
419
robertphillips3b087f42016-02-18 08:48:03 -0800420 bool isOpaque() const override {
421 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType;
422 }
423
424 size_t getSize() const override { return fTexture->gpuMemorySize(); }
425
robertphillipse8c34972016-02-16 12:09:36 -0800426 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800427 SkRect dst = SkRect::MakeXYWH(x, y,
428 this->subset().width(), this->subset().height());
429
430 SkBitmap bm;
431
robertphillipsb6c65e92016-02-04 10:52:42 -0800432 GrWrapTextureInBitmap(fTexture,
robertphillips3b087f42016-02-18 08:48:03 -0800433 fTexture->width(), fTexture->height(), this->isOpaque(), &bm);
robertphillipsb6c65e92016-02-04 10:52:42 -0800434
435 canvas->drawBitmapRect(bm, this->subset(),
436 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
437 }
438
439 GrTexture* onPeekTexture() const override { return fTexture; }
440
robertphillipsab01ccd2016-03-08 10:45:32 -0800441 bool getBitmapDeprecated(SkBitmap* result) const override {
robertphillips3b087f42016-02-18 08:48:03 -0800442 const SkImageInfo info = GrMakeInfoFromTexture(fTexture,
443 this->width(), this->height(),
444 this->isOpaque());
445 if (!result->setInfo(info)) {
446 return false;
447 }
448
robertphillipsc5035e72016-03-17 06:58:39 -0700449 const SkImageInfo prInfo = info.makeWH(fTexture->width(), fTexture->height());
450
451 SkAutoTUnref<SkGrPixelRef> pixelRef(new SkGrPixelRef(prInfo, fTexture));
452 result->setPixelRef(pixelRef, this->subset().fLeft, this->subset().fTop);
robertphillips3b087f42016-02-18 08:48:03 -0800453 return true;
454 }
455
robertphillips4418dba2016-03-07 12:45:14 -0800456 bool testingOnlyOnGetROPixels(SkBitmap* result) const override {
457
458 const SkImageInfo info = SkImageInfo::MakeN32(this->width(),
459 this->height(),
460 this->isOpaque() ? kOpaque_SkAlphaType
461 : kPremul_SkAlphaType);
462 if (!result->tryAllocPixels(info)) {
463 return false;
464 }
465
466 if (!fTexture->readPixels(0, 0, result->width(), result->height(), kSkia8888_GrPixelConfig,
467 result->getPixels(), result->rowBytes())) {
468 return false;
469 }
470
471 result->pixelRef()->setImmutable();
472 return true;
473 }
474
robertphillips37bd7c32016-03-17 14:31:39 -0700475 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const override {
brianosmana6359362016-03-21 06:55:37 -0700476 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info, *fTexture->getContext()->caps());
robertphillipsb6c65e92016-02-04 10:52:42 -0800477 desc.fFlags = kRenderTarget_GrSurfaceFlag;
478
robertphillips37bd7c32016-03-17 14:31:39 -0700479 return SkSpecialSurface::MakeRenderTarget(this->proxy(), fTexture->getContext(), desc);
robertphillipsb6c65e92016-02-04 10:52:42 -0800480 }
481
robertphillips37bd7c32016-03-17 14:31:39 -0700482 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
483 return SkSpecialImage::MakeFromGpu(this->internal_getProxy(),
484 subset,
485 this->uniqueID(),
486 fTexture,
487 fAlphaType);
robertphillipsc5035e72016-03-17 06:58:39 -0700488 }
489
robertphillipsb4bd11e2016-03-21 13:44:18 -0700490 sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override {
491 if (0 == subset.fLeft && 0 == subset.fTop &&
492 fTexture->width() == subset.width() &&
493 fTexture->height() == subset.height()) {
494 // The existing GrTexture is already tight so reuse it in the SkImage
495 return sk_make_sp<SkImage_Gpu>(fTexture->width(), fTexture->height(),
496 kNeedNewImageUniqueID,
497 fAlphaType, fTexture, SkBudgeted::kYes);
498 }
499
500 GrContext* ctx = fTexture->getContext();
501 GrSurfaceDesc desc = fTexture->desc();
502 desc.fWidth = subset.width();
503 desc.fHeight = subset.height();
504
505 GrTexture* subTx = ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes);
506 if (!subTx) {
507 return nullptr;
508 }
509 ctx->copySurface(subTx, fTexture, subset, SkIPoint::Make(0, 0));
510 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
511 fAlphaType, subTx, SkBudgeted::kYes);
512 }
513
514 SkSurface* onMakeTightSurface(const SkImageInfo& info) const override {
515 return SkSurface::NewRenderTarget(fTexture->getContext(), SkBudgeted::kYes, info);
516 }
517
robertphillipsb6c65e92016-02-04 10:52:42 -0800518private:
519 SkAutoTUnref<GrTexture> fTexture;
robertphillips3b087f42016-02-18 08:48:03 -0800520 const SkAlphaType fAlphaType;
robertphillipsb6c65e92016-02-04 10:52:42 -0800521
522 typedef SkSpecialImage_Base INHERITED;
523};
524
robertphillips37bd7c32016-03-17 14:31:39 -0700525sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(SkImageFilter::Proxy* proxy,
526 const SkIRect& subset,
527 uint32_t uniqueID,
528 GrTexture* tex,
529 SkAlphaType at) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800530 SkASSERT(rect_fits(subset, tex->width(), tex->height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700531 return sk_make_sp<SkSpecialImage_Gpu>(proxy, subset, uniqueID, tex, at);
robertphillipsb6c65e92016-02-04 10:52:42 -0800532}
533
534#else
535
robertphillips37bd7c32016-03-17 14:31:39 -0700536sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(SkImageFilter::Proxy* proxy,
537 const SkIRect& subset,
538 uint32_t uniqueID,
539 GrTexture* tex,
540 SkAlphaType at) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800541 return nullptr;
542}
543
544#endif