Chromium Code Reviews

Side by Side Diff: src/core/SkPictureImageGenerator.cpp

Issue 1487683004: Create an SkCodecImageGenerator (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkImageGenerator.h" 8 #include "SkImageGenerator.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkMatrix.h" 10 #include "SkMatrix.h"
11 #include "SkPaint.h" 11 #include "SkPaint.h"
12 #include "SkPicture.h" 12 #include "SkPicture.h"
13 #include "SkSurface.h" 13 #include "SkSurface.h"
14 #include "SkTLazy.h" 14 #include "SkTLazy.h"
15 15
16 class SkPictureImageGenerator : SkImageGenerator { 16 class SkPictureImageGenerator : SkImageGenerator {
17 public: 17 public:
18 static SkImageGenerator* Create(const SkISize&, const SkPicture*, const SkMa trix*, 18 static SkImageGenerator* Create(const SkISize&, const SkPicture*, const SkMa trix*,
19 const SkPaint*); 19 const SkPaint*);
20 20
21 protected: 21 protected:
22 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkP MColor ctable[], 22 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkP MColor ctable[],
23 int* ctableCount) override; 23 int* ctableCount) override;
24 bool onComputeScaledDimensions(SkScalar scale, SupportedSizes*) override;
25 bool onGenerateScaledPixels(const SkISize&, const SkIPoint&, const SkPixmap& ) override;
26
24 #if SK_SUPPORT_GPU 27 #if SK_SUPPORT_GPU
25 GrTexture* onGenerateTexture(GrContext*, const SkIRect*) override; 28 GrTexture* onGenerateTexture(GrContext*, const SkIRect*) override;
26 #endif 29 #endif
27 30
28 private: 31 private:
29 SkPictureImageGenerator(const SkISize&, const SkPicture*, const SkMatrix*, c onst SkPaint*); 32 SkPictureImageGenerator(const SkISize&, const SkPicture*, const SkMatrix*, c onst SkPaint*);
30 33
31 SkAutoTUnref<const SkPicture> fPicture; 34 SkAutoTUnref<const SkPicture> fPicture;
32 SkMatrix fMatrix; 35 SkMatrix fMatrix;
33 SkTLazy<SkPaint> fPaint; 36 SkTLazy<SkPaint> fPaint;
(...skipping 37 matching lines...)
71 return false; 74 return false;
72 } 75 }
73 76
74 bitmap.eraseColor(SK_ColorTRANSPARENT); 77 bitmap.eraseColor(SK_ColorTRANSPARENT);
75 SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); 78 SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
76 canvas.drawPicture(fPicture, &fMatrix, fPaint.getMaybeNull()); 79 canvas.drawPicture(fPicture, &fMatrix, fPaint.getMaybeNull());
77 80
78 return true; 81 return true;
79 } 82 }
80 83
84 bool SkPictureImageGenerator::onComputeScaledDimensions(SkScalar scale,
85 SupportedSizes* sizes) {
86 const int w = this->getInfo().width();
87 const int h = this->getInfo().height();
88 const int sw = SkMin32(SkScalarRoundToInt(scale * w), w);
89 const int sh = SkMin32(SkScalarRoundToInt(scale * h), h);
90 if (sw > 0 && sh > 0) {
91 sizes->fSizes[0].set(sw, sh);
92 sizes->fSizes[1].set(sw, sh);
93 return true;
94 }
95 return false;
96 }
97
98 bool SkPictureImageGenerator::onGenerateScaledPixels(const SkISize& scaledSize,
99 const SkIPoint& scaledOrigi n,
100 const SkPixmap& scaledPixel s) {
101 int w = scaledSize.width();
102 int h = scaledSize.height();
103
104 const SkScalar scaleX = SkIntToScalar(w) / this->getInfo().width();
105 const SkScalar scaleY = SkIntToScalar(h) / this->getInfo().height();
106 SkMatrix matrix = SkMatrix::MakeScale(scaleX, scaleY);
107 matrix.postTranslate(-SkIntToScalar(scaledOrigin.x()), -SkIntToScalar(scaled Origin.y()));
108
109 SkBitmap bitmap;
110 if (!bitmap.installPixels(scaledPixels.info(), scaledPixels.writable_addr(),
111 scaledPixels.rowBytes())) {
112 return false;
113 }
114
115 bitmap.eraseColor(SK_ColorTRANSPARENT);
116 SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
117 matrix.preConcat(fMatrix);
118 canvas.drawPicture(fPicture, &matrix, fPaint.getMaybeNull());
119 return true;
120 }
121
122 //////////////////////////////////////////////////////////////////////////////// ///////////////////
123
81 SkImageGenerator* SkImageGenerator::NewFromPicture(const SkISize& size, const Sk Picture* picture, 124 SkImageGenerator* SkImageGenerator::NewFromPicture(const SkISize& size, const Sk Picture* picture,
82 const SkMatrix* matrix, const SkPaint* paint) { 125 const SkMatrix* matrix, const SkPaint* paint) {
83 return SkPictureImageGenerator::Create(size, picture, matrix, paint); 126 return SkPictureImageGenerator::Create(size, picture, matrix, paint);
84 } 127 }
85 128
86 //////////////////////////////////////////////////////////////////////////////// /////////////////// 129 //////////////////////////////////////////////////////////////////////////////// ///////////////////
87 130
88 #if SK_SUPPORT_GPU 131 #if SK_SUPPORT_GPU
89 #include "GrTexture.h" 132 #include "GrTexture.h"
90 133
(...skipping 17 matching lines...)
108 } 151 }
109 surface->getCanvas()->clear(0); // does NewRenderTarget promise to do this f or us? 152 surface->getCanvas()->clear(0); // does NewRenderTarget promise to do this f or us?
110 surface->getCanvas()->drawPicture(fPicture, &matrix, fPaint.getMaybeNull()); 153 surface->getCanvas()->drawPicture(fPicture, &matrix, fPaint.getMaybeNull());
111 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); 154 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
112 if (!image.get()) { 155 if (!image.get()) {
113 return nullptr; 156 return nullptr;
114 } 157 }
115 return SkSafeRef(image->getTexture()); 158 return SkSafeRef(image->getTexture());
116 } 159 }
117 #endif 160 #endif
OLDNEW

Powered by Google App Engine