| 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 "SkHwuiRenderer.h" | |
| 9 | |
| 10 #include "AnimationContext.h" | |
| 11 #include "IContextFactory.h" | |
| 12 #include "SkBitmap.h" | |
| 13 #include "gui/BufferQueue.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 /** | |
| 18 * Helper class for setting up android::uirenderer::renderthread::RenderProxy. | |
| 19 */ | |
| 20 class ContextFactory : public android::uirenderer::IContextFactory { | |
| 21 public: | |
| 22 android::uirenderer::AnimationContext* createAnimationContext | |
| 23 (android::uirenderer::renderthread::TimeLord& clock) override { | |
| 24 return new android::uirenderer::AnimationContext(clock); | |
| 25 } | |
| 26 }; | |
| 27 | |
| 28 } | |
| 29 | |
| 30 void SkHwuiRenderer::initialize(SkISize size) { | |
| 31 this->size = size; | |
| 32 android::BufferQueue::createBufferQueue(&this->producer, &this->consumer); | |
| 33 this->cpuConsumer = new android::CpuConsumer(this->consumer, 1); | |
| 34 this->cpuConsumer->setName(android::String8("SkiaBenchmarkClient")); | |
| 35 this->cpuConsumer->setDefaultBufferSize(size.width(), size.height()); | |
| 36 this->androidSurface = new android::Surface(this->producer); | |
| 37 native_window_set_buffers_dimensions(this->androidSurface.get(), | |
| 38 size.width(), size.height()); | |
| 39 native_window_set_buffers_format(this->androidSurface.get(), | |
| 40 android::PIXEL_FORMAT_RGBA_8888); | |
| 41 native_window_set_usage(this->androidSurface.get(), GRALLOC_USAGE_SW_READ_OF
TEN | | |
| 42 GRALLOC_USAGE_SW_WRITE_NEVER | | |
| 43 GRALLOC_USAGE_HW_RENDER); | |
| 44 this->rootNode.reset(new android::uirenderer::RenderNode()); | |
| 45 this->rootNode->incStrong(nullptr); | |
| 46 this->rootNode->mutateStagingProperties().setLeftTopRightBottom | |
| 47 (0, 0, size.width(), size.height()); | |
| 48 this->rootNode->mutateStagingProperties().setClipToBounds(false); | |
| 49 this->rootNode->setPropertyFieldsDirty(android::uirenderer::RenderNode::GENE
RIC); | |
| 50 ContextFactory factory; | |
| 51 this->proxy.reset | |
| 52 (new android::uirenderer::renderthread::RenderProxy(false, this->rootNod
e, &factory)); | |
| 53 this->proxy->loadSystemProperties(); | |
| 54 this->proxy->initialize(this->androidSurface.get()); | |
| 55 float lightX = size.width() / 2.0f; | |
| 56 android::uirenderer::Vector3 lightVector { lightX, -200.0f, 800.0f }; | |
| 57 this->proxy->setup(size.width(), size.height(), 800.0f, | |
| 58 255 * 0.075f, 255 * 0.15f); | |
| 59 this->proxy->setLightCenter(lightVector); | |
| 60 this->canvas.reset(new android::uirenderer::DisplayListCanvas(size.width(),
size.height())); | |
| 61 } | |
| 62 | |
| 63 SkCanvas* SkHwuiRenderer::prepareToDraw() { | |
| 64 this->canvas->clipRect(0, 0, this->size.width(), this->size.height(), | |
| 65 SkRegion::Op::kReplace_Op); | |
| 66 return this->canvas->asSkCanvas(); | |
| 67 } | |
| 68 | |
| 69 void SkHwuiRenderer::finishDrawing() { | |
| 70 this->rootNode->setStagingDisplayList(this->canvas->finishRecording()); | |
| 71 this->proxy->syncAndDrawFrame(); | |
| 72 // Surprisingly, calling this->proxy->fence() here appears to make no differ
ence to | |
| 73 // the timings we record. | |
| 74 } | |
| 75 | |
| 76 bool SkHwuiRenderer::capturePixels(SkBitmap* bmp) { | |
| 77 SkImageInfo destinationConfig = | |
| 78 SkImageInfo::Make(this->size.width(), this->size.height(), | |
| 79 kRGBA_8888_SkColorType, kPremul_SkAlphaType); | |
| 80 bmp->allocPixels(destinationConfig); | |
| 81 sk_memset32((uint32_t*) bmp->getPixels(), SK_ColorRED, | |
| 82 this->size.width() * this->size.height()); | |
| 83 | |
| 84 android::CpuConsumer::LockedBuffer nativeBuffer; | |
| 85 android::status_t retval = this->cpuConsumer->lockNextBuffer(&nativeBuffer); | |
| 86 if (retval == android::BAD_VALUE) { | |
| 87 SkDebugf("write_canvas_png() got no buffer; returning transparent"); | |
| 88 // No buffer ready to read - commonly triggered by dm sending us | |
| 89 // a no-op source, or calling code that doesn't do anything on this | |
| 90 // backend. | |
| 91 bmp->eraseColor(SK_ColorTRANSPARENT); | |
| 92 return false; | |
| 93 } else if (retval) { | |
| 94 SkDebugf("Failed to lock buffer to read pixels: %d.", retval); | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 // Move the pixels into the destination SkBitmap | |
| 99 | |
| 100 SK_ALWAYSBREAK(nativeBuffer.format == android::PIXEL_FORMAT_RGBA_8888 && | |
| 101 "Native buffer not RGBA!"); | |
| 102 SkImageInfo nativeConfig = | |
| 103 SkImageInfo::Make(nativeBuffer.width, nativeBuffer.height, | |
| 104 kRGBA_8888_SkColorType, kPremul_SkAlphaType); | |
| 105 | |
| 106 // Android stride is in pixels, Skia stride is in bytes | |
| 107 SkBitmap nativeWrapper; | |
| 108 bool success = | |
| 109 nativeWrapper.installPixels(nativeConfig, nativeBuffer.data, nativeBuffe
r.stride * 4); | |
| 110 if (!success) { | |
| 111 SkDebugf("Failed to wrap HWUI buffer in a SkBitmap"); | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 SK_ALWAYSBREAK(bmp->colorType() == kRGBA_8888_SkColorType && | |
| 116 "Destination buffer not RGBA!"); | |
| 117 success = | |
| 118 nativeWrapper.readPixels(destinationConfig, bmp->getPixels(), bmp->rowBy
tes(), 0, 0); | |
| 119 if (!success) { | |
| 120 SkDebugf("Failed to extract pixels from HWUI buffer"); | |
| 121 return false; | |
| 122 } | |
| 123 | |
| 124 this->cpuConsumer->unlockBuffer(nativeBuffer); | |
| 125 | |
| 126 return true; | |
| 127 } | |
| 128 | |
| OLD | NEW |