blob: 7a73c7453bc918ccf0ceb7f4e41ae02ea643e879 [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
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000020 explicit SkImage_Gpu(const SkBitmap&);
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.comace13542014-02-06 22:00:58 +000026 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE;
robertphillips@google.com97b6b072012-10-31 14:48:39 +000027
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000028 GrTexture* getTexture() { return fBitmap.getTexture(); }
reed@google.com5d4ba882012-07-31 15:45:27 +000029
piotaixrcef04f82014-07-14 07:48:04 -070030 virtual SkShader* onNewShader(SkShader::TileMode, SkShader::TileMode) const SK_OVERRIDE;
reed@google.com5d4ba882012-07-31 15:45:27 +000031private:
32 SkBitmap fBitmap;
33
34 typedef SkImage_Base INHERITED;
35};
36
37///////////////////////////////////////////////////////////////////////////////
38
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000039SkImage_Gpu::SkImage_Gpu(const SkBitmap& bitmap)
40 : INHERITED(bitmap.width(), bitmap.height())
41 , fBitmap(bitmap) {
42 SkASSERT(NULL != fBitmap.getTexture());
reed@google.com5d4ba882012-07-31 15:45:27 +000043}
44
robertphillips@google.com97b6b072012-10-31 14:48:39 +000045SkImage_Gpu::~SkImage_Gpu() {
robertphillips@google.com97b6b072012-10-31 14:48:39 +000046}
reed@google.com5d4ba882012-07-31 15:45:27 +000047
piotaixrcef04f82014-07-14 07:48:04 -070048SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY) const {
49 return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, NULL);
50}
51
robertphillips@google.com97b6b072012-10-31 14:48:39 +000052void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
53 const SkPaint* paint) {
reed@google.com5d4ba882012-07-31 15:45:27 +000054 canvas->drawBitmap(fBitmap, x, y, paint);
55}
56
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000057void SkImage_Gpu::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
58 const SkPaint* paint) {
59 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
60}
61
junov@chromium.orgda904742013-05-01 22:38:16 +000062GrTexture* SkImage_Gpu::onGetTexture() {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000063 return fBitmap.getTexture();
reed@google.com5d4ba882012-07-31 15:45:27 +000064}
65
reed@google.comace13542014-02-06 22:00:58 +000066bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000067 return fBitmap.copyTo(dst, kN32_SkColorType);
reed@google.comace13542014-02-06 22:00:58 +000068}
69
robertphillips@google.com97b6b072012-10-31 14:48:39 +000070///////////////////////////////////////////////////////////////////////////////
71
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000072SkImage* SkImage::NewTexture(const SkBitmap& bitmap) {
73 if (NULL == bitmap.getTexture()) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +000074 return NULL;
75 }
76
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000077 return SkNEW_ARGS(SkImage_Gpu, (bitmap));
robertphillips@google.com97b6b072012-10-31 14:48:39 +000078}
79
80GrTexture* SkTextureImageGetTexture(SkImage* image) {
81 return ((SkImage_Gpu*)image)->getTexture();
82}