OLD | NEW |
(Empty) | |
| 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 "nanobenchAndroid.h" |
| 9 |
| 10 #include "AnimationContext.h" |
| 11 #include "IContextFactory.h" |
| 12 #include "SkiaCanvasProxy.h" |
| 13 #include "android/rect.h" |
| 14 #include "android/native_window.h" |
| 15 #include "renderthread/TimeLord.h" |
| 16 |
| 17 namespace { |
| 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 |
| 32 HWUITarget::HWUITarget(const Config& c, Benchmark* bench) : Target(c) { } |
| 33 |
| 34 void HWUITarget::setup() { |
| 35 this->proxy->fence(); |
| 36 } |
| 37 |
| 38 SkCanvas* HWUITarget::beginTiming(SkCanvas* canvas) { |
| 39 this->renderer->prepare(); |
| 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) { |
| 44 this->fc.reset(targetCanvas); |
| 45 canvas = &this->fc; |
| 46 // This might minimally distort timing, but canvas isn't valid outside t
he timer. |
| 47 canvas->clear(SK_ColorWHITE); |
| 48 } |
| 49 return canvas; |
| 50 } |
| 51 |
| 52 void HWUITarget::endTiming() { |
| 53 this->renderer->finish(); |
| 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 } |
| 59 |
| 60 void HWUITarget::fence() { |
| 61 this->proxy->fence(); |
| 62 } |
| 63 |
| 64 bool HWUITarget::needsFrameTiming() const { |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool HWUITarget::init(SkImageInfo info, Benchmark* bench) { |
| 69 // extracted from DMSrcSinkAndroid.cpp's HWUISink::draw() |
| 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; |
| 105 } |
| 106 |
| 107 bool HWUITarget::capturePixels(SkBitmap* bmp) { |
| 108 SkImageInfo destinationConfig = |
| 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 } |
| 159 |
| 160 |
OLD | NEW |