OLD | NEW |
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 "nanobenchAndroid.h" | 8 #include "nanobenchAndroid.h" |
9 | 9 |
10 #include "AnimationContext.h" | 10 #include "AnimationContext.h" |
11 #include "IContextFactory.h" | 11 #include "IContextFactory.h" |
12 #include "SkiaCanvasProxy.h" | 12 #include "SkiaCanvasProxy.h" |
13 #include "android/rect.h" | 13 #include "android/rect.h" |
14 #include "android/native_window.h" | 14 #include "android/native_window.h" |
15 #include "renderthread/TimeLord.h" | 15 #include "renderthread/TimeLord.h" |
16 | 16 |
17 namespace { | 17 /* These functions are only compiled in the Android Framework. */ |
18 | |
19 /** | |
20 * Helper class for setting up android::uirenderer::renderthread::RenderProxy. | |
21 */ | |
22 class ContextFactory : public android::uirenderer::IContextFactory { | |
23 public: | |
24 android::uirenderer::AnimationContext* createAnimationContext | |
25 (android::uirenderer::renderthread::TimeLord& clock) override { | |
26 return new android::uirenderer::AnimationContext(clock); | |
27 } | |
28 }; | |
29 | |
30 } | |
31 | 18 |
32 HWUITarget::HWUITarget(const Config& c, Benchmark* bench) : Target(c) { } | 19 HWUITarget::HWUITarget(const Config& c, Benchmark* bench) : Target(c) { } |
33 | 20 |
34 void HWUITarget::setup() { | 21 void HWUITarget::setup() { |
35 this->proxy->fence(); | 22 this->renderer.proxy->fence(); |
36 } | 23 } |
37 | 24 |
38 SkCanvas* HWUITarget::beginTiming(SkCanvas* canvas) { | 25 SkCanvas* HWUITarget::beginTiming(SkCanvas* canvas) { |
39 this->renderer->prepare(); | 26 SkCanvas* targetCanvas = this->renderer.prepareToDraw(); |
40 this->renderer->clipRect(0, 0, this->size.width(), this->size.height(), | |
41 SkRegion::Op::kReplace_Op); | |
42 SkCanvas* targetCanvas = this->renderer->asSkCanvas(); | |
43 if (targetCanvas) { | 27 if (targetCanvas) { |
44 this->fc.reset(targetCanvas); | 28 this->fc.reset(targetCanvas); |
45 canvas = &this->fc; | 29 canvas = &this->fc; |
46 // This might minimally distort timing, but canvas isn't valid outside t
he timer. | 30 // This might minimally distort timing, but canvas isn't valid outside t
he timer. |
47 canvas->clear(SK_ColorWHITE); | 31 canvas->clear(SK_ColorWHITE); |
48 } | 32 } |
| 33 |
49 return canvas; | 34 return canvas; |
50 } | 35 } |
51 | 36 |
52 void HWUITarget::endTiming() { | 37 void HWUITarget::endTiming() { |
53 this->renderer->finish(); | 38 this->renderer.finishDrawing(); |
54 this->rootNode->setStagingDisplayList(this->renderer->finishRecording()); | |
55 this->proxy->syncAndDrawFrame(); | |
56 // Surprisingly, calling this->proxy->fence() here appears to make no differ
ence to | |
57 // the timings we record. | |
58 } | 39 } |
59 | 40 |
60 void HWUITarget::fence() { | 41 void HWUITarget::fence() { |
61 this->proxy->fence(); | 42 this->renderer.proxy->fence(); |
62 } | 43 } |
63 | 44 |
64 bool HWUITarget::needsFrameTiming() const { | 45 bool HWUITarget::needsFrameTiming() const { |
65 return true; | 46 return true; |
66 } | 47 } |
67 | 48 |
68 bool HWUITarget::init(SkImageInfo info, Benchmark* bench) { | 49 bool HWUITarget::init(SkImageInfo info, Benchmark* bench) { |
69 // extracted from DMSrcSinkAndroid.cpp's HWUISink::draw() | 50 this->renderer.initialize({bench->getSize().x(), bench->getSize().y()}); |
70 size.set(bench->getSize().x(), bench->getSize().y()); | |
71 android::BufferQueue::createBufferQueue(&this->producer, &this->consumer); | |
72 this->cpuConsumer = new android::CpuConsumer(this->consumer, 1); | |
73 this->cpuConsumer->setName(android::String8("SkiaBenchmarkClient")); | |
74 this->cpuConsumer->setDefaultBufferSize(size.width(), size.height()); | |
75 this->androidSurface = new android::Surface(this->producer); | |
76 native_window_set_buffers_dimensions(this->androidSurface.get(), | |
77 size.width(), size.height()); | |
78 native_window_set_buffers_format(this->androidSurface.get(), | |
79 android::PIXEL_FORMAT_RGBA_8888); | |
80 native_window_set_usage(this->androidSurface.get(), GRALLOC_USAGE_SW_READ_OF
TEN | | |
81 GRALLOC_USAGE_SW_WRITE_NEVER | | |
82 GRALLOC_USAGE_HW_RENDER); | |
83 this->rootNode.reset(new android::uirenderer::RenderNode()); | |
84 this->rootNode->incStrong(nullptr); | |
85 this->rootNode->mutateStagingProperties().setLeftTopRightBottom | |
86 (0, 0, size.width(), size.height()); | |
87 this->rootNode->mutateStagingProperties().setClipToBounds(false); | |
88 this->rootNode->setPropertyFieldsDirty(android::uirenderer::RenderNode::GENE
RIC); | |
89 ContextFactory factory; | |
90 this->proxy.reset | |
91 (new android::uirenderer::renderthread::RenderProxy(false, this->rootNod
e, &factory)); | |
92 this->proxy->loadSystemProperties(); | |
93 this->proxy->initialize(this->androidSurface.get()); | |
94 float lightX = size.width() / 2.0f; | |
95 android::uirenderer::Vector3 lightVector { lightX, -200.0f, 800.0f }; | |
96 this->proxy->setup(size.width(), size.height(), lightVector, 800.0f, | |
97 255 * 0.075f, 255 * 0.15f); | |
98 this->renderer.reset(new android::uirenderer::DisplayListRenderer()); | |
99 this->renderer->setViewport(size.width(), size.height()); | |
100 | |
101 // Since we have no SkSurface for HWUI, other parts of the code base have to | |
102 // explicitly work around the fact that it may be invalid / have no SkCanvas
. | |
103 | |
104 return true; | 51 return true; |
105 } | 52 } |
106 | 53 |
107 bool HWUITarget::capturePixels(SkBitmap* bmp) { | 54 bool HWUITarget::capturePixels(SkBitmap* bmp) { |
108 SkImageInfo destinationConfig = | 55 return this->renderer.capturePixels(bmp); |
109 SkImageInfo::Make(this->size.width(), this->size.height(), | |
110 kRGBA_8888_SkColorType, kPremul_SkAlphaType); | |
111 bmp->allocPixels(destinationConfig); | |
112 sk_memset32((uint32_t*) bmp->getPixels(), SK_ColorRED, | |
113 this->size.width() * this->size.height()); | |
114 | |
115 android::CpuConsumer::LockedBuffer nativeBuffer; | |
116 android::status_t retval = this->cpuConsumer->lockNextBuffer(&nativeBuffer); | |
117 if (retval == android::BAD_VALUE) { | |
118 SkDebugf("write_canvas_png() got no buffer; returning transparent"); | |
119 // No buffer ready to read - commonly triggered by dm sending us | |
120 // a no-op source, or calling code that doesn't do anything on this | |
121 // backend. | |
122 bmp->eraseColor(SK_ColorTRANSPARENT); | |
123 return false; | |
124 } else if (retval) { | |
125 SkDebugf("Failed to lock buffer to read pixels: %d.", retval); | |
126 return false; | |
127 } | |
128 | |
129 // Move the pixels into the destination SkBitmap | |
130 | |
131 SK_ALWAYSBREAK(nativeBuffer.format == android::PIXEL_FORMAT_RGBA_8888 && | |
132 "Native buffer not RGBA!"); | |
133 SkImageInfo nativeConfig = | |
134 SkImageInfo::Make(nativeBuffer.width, nativeBuffer.height, | |
135 kRGBA_8888_SkColorType, kPremul_SkAlphaType); | |
136 | |
137 // Android stride is in pixels, Skia stride is in bytes | |
138 SkBitmap nativeWrapper; | |
139 bool success = | |
140 nativeWrapper.installPixels(nativeConfig, nativeBuffer.data, nativeBuffe
r.stride * 4); | |
141 if (!success) { | |
142 SkDebugf("Failed to wrap HWUI buffer in a SkBitmap"); | |
143 return false; | |
144 } | |
145 | |
146 SK_ALWAYSBREAK(bmp->colorType() == kRGBA_8888_SkColorType && | |
147 "Destination buffer not RGBA!"); | |
148 success = | |
149 nativeWrapper.readPixels(destinationConfig, bmp->getPixels(), bmp->rowBy
tes(), 0, 0); | |
150 if (!success) { | |
151 SkDebugf("Failed to extract pixels from HWUI buffer"); | |
152 return false; | |
153 } | |
154 | |
155 this->cpuConsumer->unlockBuffer(nativeBuffer); | |
156 | |
157 return true; | |
158 } | 56 } |
159 | 57 |
160 | 58 |
OLD | NEW |