blob: c721077b629d0f753185374a997dab251220533f [file] [log] [blame]
reed@google.com5d4ba882012-07-31 15:45:27 +00001/*
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.com97b6b072012-10-31 14:48:39 +000012#include "GrContext.h"
13#include "GrTexture.h"
14#include "SkGrPixelRef.h"
reed@google.com5d4ba882012-07-31 15:45:27 +000015
16class SkImage_Gpu : public SkImage_Base {
17public:
robertphillips@google.com97b6b072012-10-31 14:48:39 +000018 SK_DECLARE_INST_COUNT(SkImage_Gpu)
reed@google.com5d4ba882012-07-31 15:45:27 +000019
robertphillips@google.com97b6b072012-10-31 14:48:39 +000020 SkImage_Gpu(GrTexture*);
reed@google.com5d4ba882012-07-31 15:45:27 +000021 virtual ~SkImage_Gpu();
22
robertphillips@google.com97b6b072012-10-31 14:48:39 +000023 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.com5d4ba882012-07-31 15:45:27 +000028
29private:
robertphillips@google.com97b6b072012-10-31 14:48:39 +000030 GrTexture* fTexture;
reed@google.com5d4ba882012-07-31 15:45:27 +000031 SkBitmap fBitmap;
32
33 typedef SkImage_Base INHERITED;
34};
35
robertphillips@google.com97b6b072012-10-31 14:48:39 +000036SK_DEFINE_INST_COUNT(SkImage_Gpu)
37
reed@google.com5d4ba882012-07-31 15:45:27 +000038///////////////////////////////////////////////////////////////////////////////
39
robertphillips@google.com97b6b072012-10-31 14:48:39 +000040SkImage_Gpu::SkImage_Gpu(GrTexture* texture)
41 : INHERITED(texture->width(), texture->height())
42 , fTexture(texture) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
robertphillips@google.com97b6b072012-10-31 14:48:39 +000044 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.com5d4ba882012-07-31 15:45:27 +000048}
49
robertphillips@google.com97b6b072012-10-31 14:48:39 +000050SkImage_Gpu::~SkImage_Gpu() {
51 SkSafeUnref(fTexture);
52}
reed@google.com5d4ba882012-07-31 15:45:27 +000053
robertphillips@google.com97b6b072012-10-31 14:48:39 +000054void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
55 const SkPaint* paint) {
reed@google.com5d4ba882012-07-31 15:45:27 +000056 canvas->drawBitmap(fBitmap, x, y, paint);
57}
58
robertphillips@google.com97b6b072012-10-31 14:48:39 +000059void SkImage_Gpu::setTexture(GrTexture* texture) {
reed@google.com5d4ba882012-07-31 15:45:27 +000060
robertphillips@google.com97b6b072012-10-31 14:48:39 +000061 if (texture == fTexture) {
62 return;
reed@google.com5d4ba882012-07-31 15:45:27 +000063 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064
robertphillips@google.com97b6b072012-10-31 14:48:39 +000065 SkRefCnt_SafeAssign(fTexture, texture);
66 fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref();
reed@google.com5d4ba882012-07-31 15:45:27 +000067}
68
robertphillips@google.com97b6b072012-10-31 14:48:39 +000069///////////////////////////////////////////////////////////////////////////////
70
71SkImage* SkImage::NewTexture(GrTexture* texture) {
72 if (NULL == texture) {
73 return NULL;
74 }
75
76 return SkNEW_ARGS(SkImage_Gpu, (texture));
77}
78
79GrTexture* SkTextureImageGetTexture(SkImage* image) {
80 return ((SkImage_Gpu*)image)->getTexture();
81}
82
83void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) {
84 ((SkImage_Gpu*)image)->setTexture(texture);
85}