tomhudson | eebc39a | 2015-02-23 12:18:05 -0800 | [diff] [blame] | 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 "DMSrcSink.h" |
| 9 | #include "DMSrcSinkAndroid.h" |
| 10 | |
| 11 | #include "AnimationContext.h" |
| 12 | #include "DisplayListRenderer.h" |
| 13 | #include "IContextFactory.h" |
| 14 | #include "RenderNode.h" |
| 15 | #include "android/rect.h" |
| 16 | #include "android/native_window.h" |
| 17 | #include "gui/BufferQueue.h" |
| 18 | #include "gui/CpuConsumer.h" |
| 19 | #include "gui/IGraphicBufferConsumer.h" |
| 20 | #include "gui/IGraphicBufferProducer.h" |
| 21 | #include "gui/Surface.h" |
| 22 | #include "renderthread/RenderProxy.h" |
| 23 | #include "renderthread/TimeLord.h" |
| 24 | |
| 25 | namespace DM { |
| 26 | |
| 27 | /* These functions are only compiled in the Android Framework. */ |
| 28 | |
| 29 | class ContextFactory : public android::uirenderer::IContextFactory { |
| 30 | public: |
| 31 | android::uirenderer::AnimationContext* createAnimationContext |
| 32 | (android::uirenderer::renderthread::TimeLord& clock) SK_OVERRIDE { |
| 33 | return new android::uirenderer::AnimationContext(clock); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | Error HWUISink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const { |
| 38 | // Do all setup in this function because we don't know the size |
| 39 | // for the RenderNode and RenderProxy during the constructor. |
| 40 | // In practice this doesn't seem too expensive. |
| 41 | const SkISize size = src.size(); |
| 42 | |
| 43 | // Based on android::SurfaceTexture_init() |
| 44 | android::sp<android::IGraphicBufferProducer> producer; |
| 45 | android::sp<android::IGraphicBufferConsumer> consumer; |
| 46 | android::BufferQueue::createBufferQueue(&producer, &consumer); |
| 47 | |
| 48 | // Consumer setup |
| 49 | |
| 50 | android::sp<android::CpuConsumer> cpuConsumer = |
| 51 | new android::CpuConsumer(consumer, 1); |
| 52 | cpuConsumer->setName(android::String8("SkiaTestClient")); |
| 53 | cpuConsumer->setDefaultBufferSize(size.width(), size.height()); |
| 54 | |
| 55 | // Producer setup |
| 56 | |
| 57 | android::sp<android::Surface> surface = new android::Surface(producer); |
| 58 | native_window_set_buffers_dimensions(surface.get(), size.width(), size.height()); |
| 59 | native_window_set_buffers_format(surface.get(), android::PIXEL_FORMAT_RGBA_8888); |
| 60 | native_window_set_usage(surface.get(), GRALLOC_USAGE_SW_READ_OFTEN | |
| 61 | GRALLOC_USAGE_SW_WRITE_NEVER | |
| 62 | GRALLOC_USAGE_HW_RENDER); |
| 63 | |
| 64 | // RenderNode setup based on hwui/tests/main.cpp:TreeContentAnimation |
| 65 | SkAutoTDelete<android::uirenderer::RenderNode> rootNode |
| 66 | (new android::uirenderer::RenderNode()); |
| 67 | rootNode->incStrong(nullptr); |
| 68 | |
| 69 | // Values set here won't be applied until the framework has called |
| 70 | // RenderNode::pushStagingPropertiesChanges() during RenderProxy::syncAndDrawFrame(). |
| 71 | rootNode->mutateStagingProperties().setLeftTopRightBottom(0, 0, size.width(), size.height()); |
| 72 | rootNode->setPropertyFieldsDirty(android::uirenderer::RenderNode::X | |
| 73 | android::uirenderer::RenderNode::Y); |
| 74 | rootNode->mutateStagingProperties().setClipToBounds(false); |
| 75 | rootNode->setPropertyFieldsDirty(android::uirenderer::RenderNode::GENERIC); |
| 76 | |
| 77 | // RenderProxy setup based on hwui/tests/main.cpp:TreeContentAnimation |
| 78 | ContextFactory factory; |
| 79 | SkAutoTDelete<android::uirenderer::renderthread::RenderProxy> proxy |
| 80 | (new android::uirenderer::renderthread::RenderProxy(false, rootNode, &factory)); |
| 81 | proxy->loadSystemProperties(); |
| 82 | |
| 83 | proxy->initialize(surface.get()); |
| 84 | |
| 85 | float lightX = size.width() / 2.0f; |
| 86 | android::uirenderer::Vector3 lightVector { lightX, dp(-200.0f), dp(800.0f) }; |
| 87 | proxy->setup(size.width(), size.height(), lightVector, dp(800.0f), 255 * 0.075f, 255 * 0.15f, |
| 88 | kDensity); |
| 89 | |
| 90 | // Do the draw |
| 91 | |
| 92 | SkAutoTDelete<android::uirenderer::DisplayListRenderer> renderer |
| 93 | (new android::uirenderer::DisplayListRenderer()); |
| 94 | renderer->setViewport(size.width(), size.height()); |
| 95 | renderer->prepare(); |
| 96 | renderer->clipRect(0, 0, size.width(), size.height(), SkRegion::Op::kReplace_Op); |
| 97 | |
| 98 | Error err = src.draw(renderer->asSkCanvas()); |
| 99 | if (!err.isEmpty()) { |
| 100 | return err; |
| 101 | } |
| 102 | |
| 103 | renderer->finish(); |
| 104 | rootNode->setStagingDisplayList(renderer->finishRecording()); |
| 105 | |
| 106 | proxy->syncAndDrawFrame(); |
| 107 | proxy->fence(); |
| 108 | |
| 109 | // Capture pixels |
| 110 | |
| 111 | SkImageInfo destinationConfig = |
| 112 | SkImageInfo::Make(size.width(), size.height(), |
| 113 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 114 | dst->allocPixels(destinationConfig); |
| 115 | sk_memset32((uint32_t*) dst->getPixels(), SK_ColorRED, size.width() * size.height()); |
| 116 | |
| 117 | android::CpuConsumer::LockedBuffer nativeBuffer; |
| 118 | android::status_t retval = cpuConsumer->lockNextBuffer(&nativeBuffer); |
| 119 | if (retval == android::BAD_VALUE) { |
| 120 | SkDebugf("HWUISink::draw() got no buffer; returning transparent"); |
| 121 | // No buffer ready to read - commonly triggered by dm sending us |
| 122 | // a no-op source, or calling code that doesn't do anything on this |
| 123 | // backend. |
| 124 | dst->eraseColor(SK_ColorTRANSPARENT); |
| 125 | return ""; |
| 126 | } else if (retval) { |
| 127 | return SkStringPrintf("Failed to lock buffer to read pixels: %d.", retval); |
| 128 | } |
| 129 | |
| 130 | // Move the pixels into the destination SkBitmap |
| 131 | |
| 132 | SK_ALWAYSBREAK(nativeBuffer.format == android::PIXEL_FORMAT_RGBA_8888 && |
| 133 | "Native buffer not RGBA!"); |
| 134 | SkImageInfo nativeConfig = |
| 135 | SkImageInfo::Make(nativeBuffer.width, nativeBuffer.height, |
| 136 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 137 | |
| 138 | // Android stride is in pixels, Skia stride is in bytes |
| 139 | SkBitmap nativeWrapper; |
| 140 | bool success = |
| 141 | nativeWrapper.installPixels(nativeConfig, nativeBuffer.data, nativeBuffer.stride * 4); |
| 142 | if (!success) { |
| 143 | return "Failed to wrap HWUI buffer in a SkBitmap"; |
| 144 | } |
| 145 | |
| 146 | SK_ALWAYSBREAK(dst->colorType() == kRGBA_8888_SkColorType && |
| 147 | "Destination buffer not RGBA!"); |
| 148 | success = |
| 149 | nativeWrapper.readPixels(destinationConfig, dst->getPixels(), dst->rowBytes(), 0, 0); |
| 150 | if (!success) { |
| 151 | return "Failed to extract pixels from HWUI buffer"; |
| 152 | } |
| 153 | |
| 154 | cpuConsumer->unlockBuffer(nativeBuffer); |
| 155 | return ""; |
| 156 | } |
| 157 | |
| 158 | } // namespace DM |