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