OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "gm.h" | 8 #include "gm.h" |
9 #include "SkBlurMask.h" | 9 #include "SkBlurMask.h" |
10 #include "SkBlurMaskFilter.h" | 10 #include "SkBlurMaskFilter.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkGradientShader.h" | 12 #include "SkGradientShader.h" |
| 13 #include "SkImage.h" |
13 #include "SkShader.h" | 14 #include "SkShader.h" |
14 #include "SkImage.h" | 15 #include "SkSurface.h" |
| 16 |
| 17 #if SK_SUPPORT_GPU |
| 18 #include "SkGr.h" |
| 19 #endif |
15 | 20 |
16 static SkBitmap make_chessbm(int w, int h) { | 21 static SkBitmap make_chessbm(int w, int h) { |
17 SkBitmap bm; | 22 SkBitmap bm; |
18 bm.allocN32Pixels(w, h); | 23 bm.allocN32Pixels(w, h); |
19 | 24 |
20 for (int y = 0; y < bm.height(); y++) { | 25 for (int y = 0; y < bm.height(); y++) { |
21 uint32_t* p = bm.getAddr32(0, y); | 26 uint32_t* p = bm.getAddr32(0, y); |
22 for (int x = 0; x < bm.width(); x++) { | 27 for (int x = 0; x < bm.width(); x++) { |
23 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; | 28 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
24 } | 29 } |
25 } | 30 } |
26 bm.unlockPixels(); | 31 bm.unlockPixels(); |
27 return bm; | 32 return bm; |
28 } | 33 } |
29 | 34 |
30 static SkImage* image_from_bitmap(const SkBitmap& bm) { | 35 static SkImage* makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) { |
31 SkBitmap b(bm); | 36 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
32 b.lockPixels(); | 37 |
33 return SkImage::NewRasterCopy(b.info(), b.getPixels(), b.rowBytes()); | 38 SkAutoTUnref<SkSurface> surface(origCanvas->newSurface(info)); |
34 } | 39 if (nullptr == surface) { |
| 40 // picture canvas will return null, so fall-back to raster |
| 41 surface.reset(SkSurface::NewRaster(info)); |
| 42 } |
35 | 43 |
36 static SkImage* makebm(SkBitmap* bm, int w, int h) { | 44 SkCanvas* canvas = surface->getCanvas(); |
37 bm->allocN32Pixels(w, h); | |
38 bm->eraseColor(SK_ColorTRANSPARENT); | |
39 | 45 |
40 SkCanvas canvas(*bm); | 46 canvas->clear(SK_ColorTRANSPARENT); |
41 | 47 |
42 SkScalar wScalar = SkIntToScalar(w); | 48 SkScalar wScalar = SkIntToScalar(w); |
43 SkScalar hScalar = SkIntToScalar(h); | 49 SkScalar hScalar = SkIntToScalar(h); |
44 | 50 |
45 SkPoint pt = { wScalar / 2, hScalar / 2 }; | 51 SkPoint pt = { wScalar / 2, hScalar / 2 }; |
46 | 52 |
47 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); | 53 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); |
48 | 54 |
49 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, | 55 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, |
50 SK_ColorGREEN, SK_ColorMAGENTA, | 56 SK_ColorGREEN, SK_ColorMAGENTA, |
(...skipping 11 matching lines...) Expand all Loading... |
62 SkPaint paint; | 68 SkPaint paint; |
63 SkRect rect = SkRect::MakeWH(wScalar, hScalar); | 69 SkRect rect = SkRect::MakeWH(wScalar, hScalar); |
64 SkMatrix mat = SkMatrix::I(); | 70 SkMatrix mat = SkMatrix::I(); |
65 for (int i = 0; i < 4; ++i) { | 71 for (int i = 0; i < 4; ++i) { |
66 paint.setShader(SkGradientShader::CreateRadial( | 72 paint.setShader(SkGradientShader::CreateRadial( |
67 pt, radius, | 73 pt, radius, |
68 colors, pos, | 74 colors, pos, |
69 SK_ARRAY_COUNT(colors), | 75 SK_ARRAY_COUNT(colors), |
70 SkShader::kRepeat_TileMode, | 76 SkShader::kRepeat_TileMode, |
71 0, &mat))->unref(); | 77 0, &mat))->unref(); |
72 canvas.drawRect(rect, paint); | 78 canvas->drawRect(rect, paint); |
73 rect.inset(wScalar / 8, hScalar / 8); | 79 rect.inset(wScalar / 8, hScalar / 8); |
74 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); | 80 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); |
75 } | 81 } |
| 82 |
| 83 SkImage* image = surface->newImageSnapshot(); |
| 84 |
| 85 SkBitmap tempBM; |
| 86 |
| 87 #if SK_SUPPORT_GPU |
| 88 if (image->getTexture()) { |
| 89 GrWrapTextureInBitmap(image->getTexture(), |
| 90 image->width(), image->height(), image->isOpaque()
, &tempBM); |
| 91 } else |
| 92 #endif |
| 93 { |
| 94 image->asLegacyBitmap(&tempBM, SkImage::kRO_LegacyBitmapMode); |
| 95 } |
| 96 |
76 // Let backends know we won't change this, so they don't have to deep copy i
t defensively. | 97 // Let backends know we won't change this, so they don't have to deep copy i
t defensively. |
77 bm->setImmutable(); | 98 tempBM.setImmutable(); |
| 99 *resultBM = tempBM; |
78 | 100 |
79 return image_from_bitmap(*bm); | 101 return image; |
80 } | 102 } |
81 | 103 |
82 static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkI
Rect& srcR, | 104 static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkI
Rect& srcR, |
83 const SkRect& dstR) { | 105 const SkRect& dstR) { |
84 canvas->drawBitmapRect(bm, srcR, dstR, nullptr); | 106 canvas->drawBitmapRect(bm, srcR, dstR, nullptr); |
85 } | 107 } |
86 | 108 |
87 static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const S
kIRect& srcR, | 109 static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const S
kIRect& srcR, |
88 const SkRect& dstR) { | 110 const SkRect& dstR) { |
89 canvas->drawImageRect(image, srcR, dstR, nullptr); | 111 canvas->drawImageRect(image, srcR, dstR, nullptr); |
(...skipping 16 matching lines...) Expand all Loading... |
106 DrawRectRectProc* fProc; | 128 DrawRectRectProc* fProc; |
107 SkBitmap fLargeBitmap; | 129 SkBitmap fLargeBitmap; |
108 SkAutoTUnref<SkImage> fImage; | 130 SkAutoTUnref<SkImage> fImage; |
109 SkString fName; | 131 SkString fName; |
110 | 132 |
111 protected: | 133 protected: |
112 SkString onShortName() override { return fName; } | 134 SkString onShortName() override { return fName; } |
113 | 135 |
114 SkISize onISize() override { return SkISize::Make(gSize, gSize); } | 136 SkISize onISize() override { return SkISize::Make(gSize, gSize); } |
115 | 137 |
116 void onOnceBeforeDraw() override { | 138 void setupImage(SkCanvas* canvas) { |
117 fImage.reset(makebm(&fLargeBitmap, gBmpSize, gBmpSize)); | 139 fImage.reset(makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize)); |
118 } | 140 } |
119 | 141 |
120 void onDraw(SkCanvas* canvas) override { | 142 void onDraw(SkCanvas* canvas) override { |
| 143 if (!fImage) { |
| 144 this->setupImage(canvas); |
| 145 } |
| 146 |
121 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; | 147 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; |
122 static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2); | 148 static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2); |
123 | 149 |
124 static const int kPadX = 30; | 150 static const int kPadX = 30; |
125 static const int kPadY = 40; | 151 static const int kPadY = 40; |
126 SkPaint paint; | 152 SkPaint paint; |
127 paint.setAlpha(0x20); | 153 paint.setAlpha(0x20); |
128 canvas->drawBitmapRect(fLargeBitmap, SkRect::MakeIWH(gSize, gSize), &pai
nt); | 154 canvas->drawBitmapRect(fLargeBitmap, SkRect::MakeIWH(gSize, gSize), &pai
nt); |
129 canvas->translate(SK_Scalar1 * kPadX / 2, | 155 canvas->translate(SK_Scalar1 * kPadX / 2, |
130 SK_Scalar1 * kPadY / 2); | 156 SK_Scalar1 * kPadY / 2); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 canvas->drawBitmapRect(bm, srcRect, dstRect, &paint); | 220 canvas->drawBitmapRect(bm, srcRect, dstRect, &paint); |
195 } | 221 } |
196 } | 222 } |
197 | 223 |
198 private: | 224 private: |
199 typedef skiagm::GM INHERITED; | 225 typedef skiagm::GM INHERITED; |
200 }; | 226 }; |
201 | 227 |
202 DEF_GM( return new DrawBitmapRectGM(canvasproc, nullptr); ) | 228 DEF_GM( return new DrawBitmapRectGM(canvasproc, nullptr); ) |
203 DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); ) | 229 DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); ) |
OLD | NEW |