blob: b6e4211d28fc422501ed3c79449a98ec393c5c82 [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;
junov@chromium.orgda904742013-05-01 22:38:16 +000024 virtual GrTexture* onGetTexture() SK_OVERRIDE;
reed@google.com4b0757b2013-05-20 16:33:41 +000025 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE {
26 // TODO
27 return false;
28 }
robertphillips@google.com97b6b072012-10-31 14:48:39 +000029
30 GrTexture* getTexture() { return fTexture; }
31
32 void setTexture(GrTexture* texture);
reed@google.com5d4ba882012-07-31 15:45:27 +000033
34private:
robertphillips@google.com97b6b072012-10-31 14:48:39 +000035 GrTexture* fTexture;
reed@google.com5d4ba882012-07-31 15:45:27 +000036 SkBitmap fBitmap;
37
38 typedef SkImage_Base INHERITED;
39};
40
robertphillips@google.com97b6b072012-10-31 14:48:39 +000041SK_DEFINE_INST_COUNT(SkImage_Gpu)
42
reed@google.com5d4ba882012-07-31 15:45:27 +000043///////////////////////////////////////////////////////////////////////////////
44
robertphillips@google.com97b6b072012-10-31 14:48:39 +000045SkImage_Gpu::SkImage_Gpu(GrTexture* texture)
46 : INHERITED(texture->width(), texture->height())
47 , fTexture(texture) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048
robertphillips@google.com97b6b072012-10-31 14:48:39 +000049 SkASSERT(NULL != fTexture);
50 fTexture->ref();
51 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fTexture->width(), fTexture->height());
junov@chromium.orgc37589d2013-04-03 13:58:32 +000052 fBitmap.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (fTexture)))->unref();
reed@google.com5d4ba882012-07-31 15:45:27 +000053}
54
robertphillips@google.com97b6b072012-10-31 14:48:39 +000055SkImage_Gpu::~SkImage_Gpu() {
56 SkSafeUnref(fTexture);
57}
reed@google.com5d4ba882012-07-31 15:45:27 +000058
robertphillips@google.com97b6b072012-10-31 14:48:39 +000059void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
60 const SkPaint* paint) {
reed@google.com5d4ba882012-07-31 15:45:27 +000061 canvas->drawBitmap(fBitmap, x, y, paint);
62}
63
junov@chromium.orgda904742013-05-01 22:38:16 +000064GrTexture* SkImage_Gpu::onGetTexture() {
65 return fTexture;
66}
67
robertphillips@google.com97b6b072012-10-31 14:48:39 +000068void SkImage_Gpu::setTexture(GrTexture* texture) {
reed@google.com5d4ba882012-07-31 15:45:27 +000069
robertphillips@google.com97b6b072012-10-31 14:48:39 +000070 if (texture == fTexture) {
71 return;
reed@google.com5d4ba882012-07-31 15:45:27 +000072 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
robertphillips@google.com97b6b072012-10-31 14:48:39 +000074 SkRefCnt_SafeAssign(fTexture, texture);
75 fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref();
reed@google.com5d4ba882012-07-31 15:45:27 +000076}
77
robertphillips@google.com97b6b072012-10-31 14:48:39 +000078///////////////////////////////////////////////////////////////////////////////
79
80SkImage* SkImage::NewTexture(GrTexture* texture) {
81 if (NULL == texture) {
82 return NULL;
83 }
84
85 return SkNEW_ARGS(SkImage_Gpu, (texture));
86}
87
88GrTexture* SkTextureImageGetTexture(SkImage* image) {
89 return ((SkImage_Gpu*)image)->getTexture();
90}
91
92void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) {
93 ((SkImage_Gpu*)image)->setTexture(texture);
94}