blob: 6a42ece802f7eb29db01a70204c64055533502dc [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"
reed4af267b2014-11-21 08:46:37 -080012#include "SkSurface.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000013#include "GrContext.h"
14#include "GrTexture.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
reed3f10b9d2014-11-21 10:27:53 -080020 SkImage_Gpu(const SkBitmap&, int sampleCountForNewSurfaces);
reed@google.com5d4ba882012-07-31 15:45:27 +000021
reed4af267b2014-11-21 08:46:37 -080022 void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const SK_OVERRIDE;
23 void onDrawRect(SkCanvas*, const SkRect* src, const SkRect& dst,
24 const SkPaint*) const SK_OVERRIDE;
25 SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const SK_OVERRIDE;
26 GrTexture* onGetTexture() const SK_OVERRIDE;
27 bool getROPixels(SkBitmap*) const SK_OVERRIDE;
robertphillips@google.com97b6b072012-10-31 14:48:39 +000028
reed8572fc02014-08-11 13:03:55 -070029 GrTexture* getTexture() const { return fBitmap.getTexture(); }
reed@google.com5d4ba882012-07-31 15:45:27 +000030
reed4af267b2014-11-21 08:46:37 -080031 SkShader* onNewShader(SkShader::TileMode,
piotaixr76d5b472014-07-22 15:02:05 -070032 SkShader::TileMode,
33 const SkMatrix* localMatrix) const SK_OVERRIDE;
piotaixrd2a35222014-08-19 14:29:02 -070034
reed4af267b2014-11-21 08:46:37 -080035 bool isOpaque() const SK_OVERRIDE;
piotaixrd2a35222014-08-19 14:29:02 -070036
reed@google.com5d4ba882012-07-31 15:45:27 +000037private:
38 SkBitmap fBitmap;
reed3f10b9d2014-11-21 10:27:53 -080039 const int fSampleCountForNewSurfaces; // 0 if we don't know
reed@google.com5d4ba882012-07-31 15:45:27 +000040
41 typedef SkImage_Base INHERITED;
42};
43
44///////////////////////////////////////////////////////////////////////////////
45
reed3f10b9d2014-11-21 10:27:53 -080046SkImage_Gpu::SkImage_Gpu(const SkBitmap& bitmap, int sampleCountForNewSurfaces)
reed4af267b2014-11-21 08:46:37 -080047 : INHERITED(bitmap.width(), bitmap.height(), NULL)
48 , fBitmap(bitmap)
reed3f10b9d2014-11-21 10:27:53 -080049 , fSampleCountForNewSurfaces(sampleCountForNewSurfaces)
reed4af267b2014-11-21 08:46:37 -080050{
bsalomon49f085d2014-09-05 13:34:00 -070051 SkASSERT(fBitmap.getTexture());
reed@google.com5d4ba882012-07-31 15:45:27 +000052}
53
piotaixr76d5b472014-07-22 15:02:05 -070054SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX,
55 SkShader::TileMode tileY,
56 const SkMatrix* localMatrix) const
57{
58 return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
piotaixrcef04f82014-07-14 07:48:04 -070059}
60
reed8572fc02014-08-11 13:03:55 -070061void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
reed@google.com5d4ba882012-07-31 15:45:27 +000062 canvas->drawBitmap(fBitmap, x, y, paint);
63}
64
piotaixr5ceff912014-09-26 07:36:26 -070065void SkImage_Gpu::onDrawRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
reed8572fc02014-08-11 13:03:55 -070066 const SkPaint* paint) const {
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000067 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
68}
69
reed4af267b2014-11-21 08:46:37 -080070SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const {
71 GrContext* ctx = this->getTexture()->getContext();
reed3f10b9d2014-11-21 10:27:53 -080072 return SkSurface::NewRenderTarget(ctx, info, fSampleCountForNewSurfaces, &props);
reed4af267b2014-11-21 08:46:37 -080073}
74
reed8572fc02014-08-11 13:03:55 -070075GrTexture* SkImage_Gpu::onGetTexture() const {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000076 return fBitmap.getTexture();
reed@google.com5d4ba882012-07-31 15:45:27 +000077}
78
reed@google.comace13542014-02-06 22:00:58 +000079bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000080 return fBitmap.copyTo(dst, kN32_SkColorType);
reed@google.comace13542014-02-06 22:00:58 +000081}
82
piotaixrd2a35222014-08-19 14:29:02 -070083bool SkImage_Gpu::isOpaque() const {
84 return fBitmap.isOpaque();
85}
86
robertphillips@google.com97b6b072012-10-31 14:48:39 +000087///////////////////////////////////////////////////////////////////////////////
88
reed3f10b9d2014-11-21 10:27:53 -080089SkImage* SkNewImageFromBitmapTexture(const SkBitmap& bitmap, int sampleCountForNewSurfaces) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000090 if (NULL == bitmap.getTexture()) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +000091 return NULL;
92 }
reed3f10b9d2014-11-21 10:27:53 -080093 return SkNEW_ARGS(SkImage_Gpu, (bitmap, sampleCountForNewSurfaces));
reed4af267b2014-11-21 08:46:37 -080094}
robertphillips@google.com97b6b072012-10-31 14:48:39 +000095
robertphillips@google.com97b6b072012-10-31 14:48:39 +000096GrTexture* SkTextureImageGetTexture(SkImage* image) {
97 return ((SkImage_Gpu*)image)->getTexture();
98}
reed4af267b2014-11-21 08:46:37 -080099