| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkImage_Base.h" |
| 9 | #include "SkImagePriv.h" |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
| 13 | #include "GrTexture.h" |
| 14 | #include "SkGrPixelRef.h" |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 15 | |
| 16 | class SkImage_Gpu : public SkImage_Base { |
| 17 | public: |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 18 | SK_DECLARE_INST_COUNT(SkImage_Gpu) |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 19 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 20 | SkImage_Gpu(GrTexture*); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 21 | virtual ~SkImage_Gpu(); |
| 22 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 23 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) SK_OVERRIDE; |
| commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 24 | virtual void onDrawRectToRect(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*) SK_OVERRIDE; |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 25 | virtual GrTexture* onGetTexture() SK_OVERRIDE; |
| reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 26 | virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE { |
| 27 | // TODO |
| 28 | return false; |
| 29 | } |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 30 | |
| 31 | GrTexture* getTexture() { return fTexture; } |
| 32 | |
| 33 | void setTexture(GrTexture* texture); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 34 | |
| 35 | private: |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 36 | GrTexture* fTexture; |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 37 | SkBitmap fBitmap; |
| 38 | |
| 39 | typedef SkImage_Base INHERITED; |
| 40 | }; |
| 41 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 42 | SK_DEFINE_INST_COUNT(SkImage_Gpu) |
| 43 | |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 46 | SkImage_Gpu::SkImage_Gpu(GrTexture* texture) |
| 47 | : INHERITED(texture->width(), texture->height()) |
| 48 | , fTexture(texture) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 49 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 50 | SkASSERT(NULL != fTexture); |
| 51 | fTexture->ref(); |
| 52 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fTexture->width(), fTexture->height()); |
| junov@chromium.org | c37589d | 2013-04-03 13:58:32 +0000 | [diff] [blame] | 53 | fBitmap.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (fTexture)))->unref(); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 56 | SkImage_Gpu::~SkImage_Gpu() { |
| 57 | SkSafeUnref(fTexture); |
| 58 | } |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 59 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 60 | void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 61 | const SkPaint* paint) { |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 62 | canvas->drawBitmap(fBitmap, x, y, paint); |
| 63 | } |
| 64 | |
| commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 65 | void SkImage_Gpu::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst, |
| 66 | const SkPaint* paint) { |
| 67 | canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); |
| 68 | } |
| 69 | |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 70 | GrTexture* SkImage_Gpu::onGetTexture() { |
| 71 | return fTexture; |
| 72 | } |
| 73 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 74 | void SkImage_Gpu::setTexture(GrTexture* texture) { |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 75 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 76 | if (texture == fTexture) { |
| 77 | return; |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 78 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 79 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 80 | SkRefCnt_SafeAssign(fTexture, texture); |
| 81 | fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref(); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | SkImage* SkImage::NewTexture(GrTexture* texture) { |
| 87 | if (NULL == texture) { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | return SkNEW_ARGS(SkImage_Gpu, (texture)); |
| 92 | } |
| 93 | |
| 94 | GrTexture* SkTextureImageGetTexture(SkImage* image) { |
| 95 | return ((SkImage_Gpu*)image)->getTexture(); |
| 96 | } |
| 97 | |
| 98 | void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) { |
| 99 | ((SkImage_Gpu*)image)->setTexture(texture); |
| 100 | } |