| 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; |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 24 | virtual GrTexture* onGetTexture() SK_OVERRIDE; |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 25 | |
| 26 | GrTexture* getTexture() { return fTexture; } |
| 27 | |
| 28 | void setTexture(GrTexture* texture); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 29 | |
| 30 | private: |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 31 | GrTexture* fTexture; |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 32 | SkBitmap fBitmap; |
| 33 | |
| 34 | typedef SkImage_Base INHERITED; |
| 35 | }; |
| 36 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 37 | SK_DEFINE_INST_COUNT(SkImage_Gpu) |
| 38 | |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 41 | SkImage_Gpu::SkImage_Gpu(GrTexture* texture) |
| 42 | : INHERITED(texture->width(), texture->height()) |
| 43 | , fTexture(texture) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 44 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 45 | SkASSERT(NULL != fTexture); |
| 46 | fTexture->ref(); |
| 47 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fTexture->width(), fTexture->height()); |
| junov@chromium.org | c37589d | 2013-04-03 13:58:32 +0000 | [diff] [blame] | 48 | fBitmap.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (fTexture)))->unref(); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 51 | SkImage_Gpu::~SkImage_Gpu() { |
| 52 | SkSafeUnref(fTexture); |
| 53 | } |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 54 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 55 | void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 56 | const SkPaint* paint) { |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 57 | canvas->drawBitmap(fBitmap, x, y, paint); |
| 58 | } |
| 59 | |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 60 | GrTexture* SkImage_Gpu::onGetTexture() { |
| 61 | return fTexture; |
| 62 | } |
| 63 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 64 | void SkImage_Gpu::setTexture(GrTexture* texture) { |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 65 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 66 | if (texture == fTexture) { |
| 67 | return; |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 68 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 69 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 70 | SkRefCnt_SafeAssign(fTexture, texture); |
| 71 | fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref(); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | SkImage* SkImage::NewTexture(GrTexture* texture) { |
| 77 | if (NULL == texture) { |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | return SkNEW_ARGS(SkImage_Gpu, (texture)); |
| 82 | } |
| 83 | |
| 84 | GrTexture* SkTextureImageGetTexture(SkImage* image) { |
| 85 | return ((SkImage_Gpu*)image)->getTexture(); |
| 86 | } |
| 87 | |
| 88 | void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) { |
| 89 | ((SkImage_Gpu*)image)->setTexture(texture); |
| 90 | } |