Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(766)

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

Issue 1396323007: API to support native scaling by image-generator (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: mark test image as opaque, so jpeg has a chance of encoding it Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkImageGenerator.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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...) Expand 10 before | Expand all | Expand 10 after
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 SkASSERT(scale > 0 && scale <= 1);
87 const int w = this->getInfo().width();
88 const int h = this->getInfo().height();
89 const int sw = SkScalarRoundToInt(scale * w);
90 const int sh = SkScalarRoundToInt(scale * h);
91 if (sw > 0 && sh > 0) {
92 sizes->fSizes[0].set(sw, sh);
93 sizes->fSizes[1].set(sw, sh);
94 return true;
95 }
96 return false;
97 }
98
99 bool SkPictureImageGenerator::onGenerateScaledPixels(const SkISize& scaledSize,
100 const SkIPoint& scaledOrigi n,
101 const SkPixmap& scaledPixel s) {
102 int w = scaledSize.width();
103 int h = scaledSize.height();
104
105 const SkScalar scaleX = SkIntToScalar(w) / this->getInfo().width();
106 const SkScalar scaleY = SkIntToScalar(h) / this->getInfo().height();
107 SkMatrix matrix = SkMatrix::MakeScale(scaleX, scaleY);
108 matrix.postTranslate(-SkIntToScalar(scaledOrigin.x()), -SkIntToScalar(scaled Origin.y()));
109
110 SkBitmap bitmap;
111 if (!bitmap.installPixels(scaledPixels.info(), scaledPixels.writable_addr(),
112 scaledPixels.rowBytes())) {
113 return false;
114 }
115
116 bitmap.eraseColor(SK_ColorTRANSPARENT);
117 SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
118 matrix.preConcat(fMatrix);
119 canvas.drawPicture(fPicture, &matrix, fPaint.getMaybeNull());
120 return true;
121 }
122
123 //////////////////////////////////////////////////////////////////////////////// ///////////////////
124
81 SkImageGenerator* SkImageGenerator::NewFromPicture(const SkISize& size, const Sk Picture* picture, 125 SkImageGenerator* SkImageGenerator::NewFromPicture(const SkISize& size, const Sk Picture* picture,
82 const SkMatrix* matrix, const SkPaint* paint) { 126 const SkMatrix* matrix, const SkPaint* paint) {
83 return SkPictureImageGenerator::Create(size, picture, matrix, paint); 127 return SkPictureImageGenerator::Create(size, picture, matrix, paint);
84 } 128 }
85 129
86 //////////////////////////////////////////////////////////////////////////////// /////////////////// 130 //////////////////////////////////////////////////////////////////////////////// ///////////////////
87 131
88 #if SK_SUPPORT_GPU 132 #if SK_SUPPORT_GPU
89 #include "GrTexture.h" 133 #include "GrTexture.h"
90 134
(...skipping 17 matching lines...) Expand all
108 } 152 }
109 surface->getCanvas()->clear(0); // does NewRenderTarget promise to do this f or us? 153 surface->getCanvas()->clear(0); // does NewRenderTarget promise to do this f or us?
110 surface->getCanvas()->drawPicture(fPicture, &matrix, fPaint.getMaybeNull()); 154 surface->getCanvas()->drawPicture(fPicture, &matrix, fPaint.getMaybeNull());
111 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); 155 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
112 if (!image.get()) { 156 if (!image.get()) {
113 return nullptr; 157 return nullptr;
114 } 158 }
115 return SkSafeRef(image->getTexture()); 159 return SkSafeRef(image->getTexture());
116 } 160 }
117 #endif 161 #endif
OLDNEW
« no previous file with comments | « src/core/SkImageGenerator.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698