blob: d19d2100c3000b5c8dceef341fead9c3281df819 [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;
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000024 virtual void onDrawRectToRect(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*) SK_OVERRIDE;
junov@chromium.orgda904742013-05-01 22:38:16 +000025 virtual GrTexture* onGetTexture() SK_OVERRIDE;
reed@google.com4b0757b2013-05-20 16:33:41 +000026 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE {
27 // TODO
28 return false;
29 }
robertphillips@google.com97b6b072012-10-31 14:48:39 +000030
31 GrTexture* getTexture() { return fTexture; }
32
33 void setTexture(GrTexture* texture);
reed@google.com5d4ba882012-07-31 15:45:27 +000034
35private:
robertphillips@google.com97b6b072012-10-31 14:48:39 +000036 GrTexture* fTexture;
reed@google.com5d4ba882012-07-31 15:45:27 +000037 SkBitmap fBitmap;
38
39 typedef SkImage_Base INHERITED;
40};
41
robertphillips@google.com97b6b072012-10-31 14:48:39 +000042SK_DEFINE_INST_COUNT(SkImage_Gpu)
43
reed@google.com5d4ba882012-07-31 15:45:27 +000044///////////////////////////////////////////////////////////////////////////////
45
robertphillips@google.com97b6b072012-10-31 14:48:39 +000046SkImage_Gpu::SkImage_Gpu(GrTexture* texture)
47 : INHERITED(texture->width(), texture->height())
48 , fTexture(texture) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000049
robertphillips@google.com97b6b072012-10-31 14:48:39 +000050 SkASSERT(NULL != fTexture);
51 fTexture->ref();
52 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fTexture->width(), fTexture->height());
junov@chromium.orgc37589d2013-04-03 13:58:32 +000053 fBitmap.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (fTexture)))->unref();
reed@google.com5d4ba882012-07-31 15:45:27 +000054}
55
robertphillips@google.com97b6b072012-10-31 14:48:39 +000056SkImage_Gpu::~SkImage_Gpu() {
57 SkSafeUnref(fTexture);
58}
reed@google.com5d4ba882012-07-31 15:45:27 +000059
robertphillips@google.com97b6b072012-10-31 14:48:39 +000060void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
61 const SkPaint* paint) {
reed@google.com5d4ba882012-07-31 15:45:27 +000062 canvas->drawBitmap(fBitmap, x, y, paint);
63}
64
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000065void 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.orgda904742013-05-01 22:38:16 +000070GrTexture* SkImage_Gpu::onGetTexture() {
71 return fTexture;
72}
73
robertphillips@google.com97b6b072012-10-31 14:48:39 +000074void SkImage_Gpu::setTexture(GrTexture* texture) {
reed@google.com5d4ba882012-07-31 15:45:27 +000075
robertphillips@google.com97b6b072012-10-31 14:48:39 +000076 if (texture == fTexture) {
77 return;
reed@google.com5d4ba882012-07-31 15:45:27 +000078 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079
robertphillips@google.com97b6b072012-10-31 14:48:39 +000080 SkRefCnt_SafeAssign(fTexture, texture);
81 fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref();
reed@google.com5d4ba882012-07-31 15:45:27 +000082}
83
robertphillips@google.com97b6b072012-10-31 14:48:39 +000084///////////////////////////////////////////////////////////////////////////////
85
86SkImage* SkImage::NewTexture(GrTexture* texture) {
87 if (NULL == texture) {
88 return NULL;
89 }
90
91 return SkNEW_ARGS(SkImage_Gpu, (texture));
92}
93
94GrTexture* SkTextureImageGetTexture(SkImage* image) {
95 return ((SkImage_Gpu*)image)->getTexture();
96}
97
98void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) {
99 ((SkImage_Gpu*)image)->setTexture(texture);
100}