fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkImageGenerator.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkMatrix.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkPicture.h" |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 14 | |
| 15 | class SkPictureImageGenerator : SkImageGenerator { |
| 16 | public: |
| 17 | static SkImageGenerator* Create(const SkISize&, const SkPicture*, const SkMatrix*, |
| 18 | const SkPaint*); |
| 19 | |
| 20 | protected: |
| 21 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[], |
| 22 | int* ctableCount) override; |
| 23 | |
| 24 | private: |
| 25 | SkPictureImageGenerator(const SkISize&, const SkPicture*, const SkMatrix*, const SkPaint*); |
| 26 | |
| 27 | SkAutoTUnref<const SkPicture> fPicture; |
| 28 | SkMatrix fMatrix; |
| 29 | SkTLazy<SkPaint> fPaint; |
| 30 | |
| 31 | typedef SkImageGenerator INHERITED; |
| 32 | }; |
| 33 | |
| 34 | SkImageGenerator* SkPictureImageGenerator::Create(const SkISize& size, const SkPicture* picture, |
| 35 | const SkMatrix* matrix, const SkPaint* paint) { |
| 36 | if (!picture || size.isEmpty()) { |
| 37 | return nullptr; |
| 38 | } |
| 39 | |
| 40 | return SkNEW_ARGS(SkPictureImageGenerator, (size, picture, matrix, paint)); |
| 41 | } |
| 42 | |
| 43 | SkPictureImageGenerator::SkPictureImageGenerator(const SkISize& size, const SkPicture* picture, |
| 44 | const SkMatrix* matrix, const SkPaint* paint) |
| 45 | : INHERITED(SkImageInfo::MakeN32Premul(size)) |
| 46 | , fPicture(SkRef(picture)) { |
| 47 | |
| 48 | if (matrix) { |
| 49 | fMatrix = *matrix; |
| 50 | } else { |
| 51 | fMatrix.reset(); |
| 52 | } |
| 53 | |
| 54 | if (paint) { |
| 55 | fPaint.set(*paint); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | bool SkPictureImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 60 | SkPMColor ctable[], int* ctableCount) { |
| 61 | if (info != getInfo() || ctable || ctableCount) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | SkBitmap bitmap; |
| 66 | if (!bitmap.installPixels(info, pixels, rowBytes)) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | bitmap.eraseColor(SK_ColorTRANSPARENT); |
| 71 | SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
| 72 | canvas.drawPicture(fPicture, &fMatrix, fPaint.getMaybeNull()); |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | SkImageGenerator* SkImageGenerator::NewFromPicture(const SkISize& size, const SkPicture* picture, |
| 78 | const SkMatrix* matrix, const SkPaint* paint) { |
| 79 | return SkPictureImageGenerator::Create(size, picture, matrix, paint); |
| 80 | } |