2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "DMSrcSinkAndroid.h"
11 #include "AnimationContext.h"
12 #include "DisplayListRenderer.h"
13 #include "IContextFactory.h"
14 #include "RenderNode.h"
15 #include "SkAndroidSDKCanvas.h"
17 #include "SkiaCanvasProxy.h"
19 #include "SkMaskFilter.h"
20 #include "SkPictureRecorder.h"
22 #include "android/rect.h"
23 #include "android/native_window.h"
24 #include "gui/BufferQueue.h"
25 #include "gui/CpuConsumer.h"
26 #include "gui/IGraphicBufferConsumer.h"
27 #include "gui/IGraphicBufferProducer.h"
28 #include "gui/Surface.h"
29 #include "renderthread/RenderProxy.h"
30 #include "renderthread/TimeLord.h"
32 /* These functions are only compiled in the Android Framework. */
37 * Helper class for setting up android::uirenderer::renderthread::RenderProxy.
39 class ContextFactory : public android::uirenderer::IContextFactory {
41 android::uirenderer::AnimationContext* createAnimationContext
42 (android::uirenderer::renderthread::TimeLord& clock) SK_OVERRIDE {
43 return new android::uirenderer::AnimationContext(clock);
51 Error HWUISink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
52 // Do all setup in this function because we don't know the size
53 // for the RenderNode and RenderProxy during the constructor.
54 // In practice this doesn't seem too expensive.
55 const SkISize size = src.size();
57 // Based on android::SurfaceTexture_init()
58 android::sp<android::IGraphicBufferProducer> producer;
59 android::sp<android::IGraphicBufferConsumer> consumer;
60 android::BufferQueue::createBufferQueue(&producer, &consumer);
64 android::sp<android::CpuConsumer> cpuConsumer =
65 new android::CpuConsumer(consumer, 1);
66 cpuConsumer->setName(android::String8("SkiaTestClient"));
67 cpuConsumer->setDefaultBufferSize(size.width(), size.height());
71 android::sp<android::Surface> surface = new android::Surface(producer);
72 native_window_set_buffers_dimensions(surface.get(), size.width(), size.height());
73 native_window_set_buffers_format(surface.get(), android::PIXEL_FORMAT_RGBA_8888);
74 native_window_set_usage(surface.get(), GRALLOC_USAGE_SW_READ_OFTEN |
75 GRALLOC_USAGE_SW_WRITE_NEVER |
76 GRALLOC_USAGE_HW_RENDER);
78 // RenderNode setup based on hwui/tests/main.cpp:TreeContentAnimation
79 SkAutoTDelete<android::uirenderer::RenderNode> rootNode
80 (new android::uirenderer::RenderNode());
81 rootNode->incStrong(nullptr);
83 // Values set here won't be applied until the framework has called
84 // RenderNode::pushStagingPropertiesChanges() during RenderProxy::syncAndDrawFrame().
85 rootNode->mutateStagingProperties().setLeftTopRightBottom(0, 0, size.width(), size.height());
86 rootNode->setPropertyFieldsDirty(android::uirenderer::RenderNode::X |
87 android::uirenderer::RenderNode::Y);
88 rootNode->mutateStagingProperties().setClipToBounds(false);
89 rootNode->setPropertyFieldsDirty(android::uirenderer::RenderNode::GENERIC);
91 // RenderProxy setup based on hwui/tests/main.cpp:TreeContentAnimation
92 ContextFactory factory;
93 SkAutoTDelete<android::uirenderer::renderthread::RenderProxy> proxy
94 (new android::uirenderer::renderthread::RenderProxy(false, rootNode, &factory));
95 proxy->loadSystemProperties();
97 proxy->initialize(surface.get());
99 float lightX = size.width() / 2.0f;
100 android::uirenderer::Vector3 lightVector { lightX, -200.0f, 800.0f };
101 proxy->setup(size.width(), size.height(), lightVector, 800.0f, 255 * 0.075f, 255 * 0.15f);
105 SkAutoTDelete<android::uirenderer::DisplayListRenderer> renderer
106 (new android::uirenderer::DisplayListRenderer());
107 renderer->setViewport(size.width(), size.height());
109 renderer->clipRect(0, 0, size.width(), size.height(), SkRegion::Op::kReplace_Op);
111 Error err = src.draw(renderer->asSkCanvas());
112 if (!err.isEmpty()) {
117 rootNode->setStagingDisplayList(renderer->finishRecording());
119 proxy->syncAndDrawFrame();
124 SkImageInfo destinationConfig =
125 SkImageInfo::Make(size.width(), size.height(),
126 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
127 dst->allocPixels(destinationConfig);
128 sk_memset32((uint32_t*) dst->getPixels(), SK_ColorRED, size.width() * size.height());
130 android::CpuConsumer::LockedBuffer nativeBuffer;
131 android::status_t retval = cpuConsumer->lockNextBuffer(&nativeBuffer);
132 if (retval == android::BAD_VALUE) {
133 SkDebugf("HWUISink::draw() got no buffer; returning transparent");
134 // No buffer ready to read - commonly triggered by dm sending us
135 // a no-op source, or calling code that doesn't do anything on this
137 dst->eraseColor(SK_ColorTRANSPARENT);
140 return SkStringPrintf("Failed to lock buffer to read pixels: %d.", retval);
143 // Move the pixels into the destination SkBitmap
145 SK_ALWAYSBREAK(nativeBuffer.format == android::PIXEL_FORMAT_RGBA_8888 &&
146 "Native buffer not RGBA!");
147 SkImageInfo nativeConfig =
148 SkImageInfo::Make(nativeBuffer.width, nativeBuffer.height,
149 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
151 // Android stride is in pixels, Skia stride is in bytes
152 SkBitmap nativeWrapper;
154 nativeWrapper.installPixels(nativeConfig, nativeBuffer.data, nativeBuffer.stride * 4);
156 return "Failed to wrap HWUI buffer in a SkBitmap";
159 SK_ALWAYSBREAK(dst->colorType() == kRGBA_8888_SkColorType &&
160 "Destination buffer not RGBA!");
162 nativeWrapper.readPixels(destinationConfig, dst->getPixels(), dst->rowBytes(), 0, 0);
164 return "Failed to extract pixels from HWUI buffer";
167 cpuConsumer->unlockBuffer(nativeBuffer);
171 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
173 ViaAndroidSDK::ViaAndroidSDK(Sink* sink) : fSink(sink) { }
175 Error ViaAndroidSDK::draw(const Src& src,
178 SkString* log) const {
179 struct ProxySrc : public Src {
181 ProxySrc(const Src& src)
184 Error draw(SkCanvas* canvas) const SK_OVERRIDE {
185 // Pass through HWUI's upper layers to get operational transforms
186 SkAutoTDelete<android::Canvas> ac (android::Canvas::create_canvas(canvas));
187 SkAutoTUnref<android::uirenderer::SkiaCanvasProxy> scProxy
188 (new android::uirenderer::SkiaCanvasProxy(ac));
190 // Pass through another proxy to get paint transforms
191 SkAndroidSDKCanvas fc;
198 SkISize size() const SK_OVERRIDE { return fSrc.size(); }
199 Name name() const SK_OVERRIDE { sk_throw(); return ""; }
202 return fSink->draw(proxy, bitmap, stream, log);